diff --git a/100_Numpy_exercises.ipynb b/100_Numpy_exercises.ipynb index 5c00ef6..3722882 100644 --- a/100_Numpy_exercises.ipynb +++ b/100_Numpy_exercises.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "markdown", - "id": "86d7d90d", + "id": "3780a226", "metadata": {}, "source": [ "# 100 numpy exercises\n", @@ -18,7 +18,7 @@ }, { "cell_type": "markdown", - "id": "23c3de32", + "id": "63ded581", "metadata": {}, "source": [ "File automatically generated. See the documentation to update questions/answers/hints programmatically." @@ -26,7 +26,7 @@ }, { "cell_type": "markdown", - "id": "71eb9a4d", + "id": "456258e7", "metadata": {}, "source": [ "Run the `initialize.py` module, then for each question you can query the\n", @@ -36,7 +36,7 @@ { "cell_type": "code", "execution_count": null, - "id": "f752b0f7", + "id": "ad8f85b1", "metadata": {}, "outputs": [], "source": [ @@ -45,7 +45,7 @@ }, { "cell_type": "markdown", - "id": "4f870b6b", + "id": "40518c2e", "metadata": {}, "source": [ "#### 1. Import the numpy package under the name `np` (★☆☆)" @@ -54,14 +54,14 @@ { "cell_type": "code", "execution_count": null, - "id": "62928f13", + "id": "08ee02d9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "edadb91f", + "id": "47c48198", "metadata": {}, "source": [ "#### 2. Print the numpy version and the configuration (★☆☆)" @@ -70,14 +70,14 @@ { "cell_type": "code", "execution_count": null, - "id": "81886061", + "id": "8a94ff73", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "68fc6d4e", + "id": "4f405544", "metadata": {}, "source": [ "#### 3. Create a null vector of size 10 (★☆☆)" @@ -86,14 +86,14 @@ { "cell_type": "code", "execution_count": null, - "id": "49109360", + "id": "4a0d86fc", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "0b1d0ac2", + "id": "c8754c7a", "metadata": {}, "source": [ "#### 4. How to find the memory size of any array (★☆☆)" @@ -102,14 +102,14 @@ { "cell_type": "code", "execution_count": null, - "id": "73b370a4", + "id": "4fa4c3e1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "56876d69", + "id": "0381891d", "metadata": {}, "source": [ "#### 5. How to get the documentation of the numpy add function from the command line? (★☆☆)" @@ -118,14 +118,14 @@ { "cell_type": "code", "execution_count": null, - "id": "95294b19", + "id": "79c56ec6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "ee618a7b", + "id": "fb0edf87", "metadata": {}, "source": [ "#### 6. Create a null vector of size 10 but the fifth value which is 1 (★☆☆)" @@ -134,14 +134,14 @@ { "cell_type": "code", "execution_count": null, - "id": "ce78fafd", + "id": "5b8c3d98", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "fd391587", + "id": "6aedaa04", "metadata": {}, "source": [ "#### 7. Create a vector with values ranging from 10 to 49 (★☆☆)" @@ -150,14 +150,14 @@ { "cell_type": "code", "execution_count": null, - "id": "327d872f", + "id": "399d0f91", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "74e245ac", + "id": "c05183de", "metadata": {}, "source": [ "#### 8. Reverse a vector (first element becomes last) (★☆☆)" @@ -166,14 +166,14 @@ { "cell_type": "code", "execution_count": null, - "id": "d1dd2da1", + "id": "beb3ed87", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "330064b1", + "id": "49ffe24b", "metadata": {}, "source": [ "#### 9. Create a 3x3 matrix with values ranging from 0 to 8 (★☆☆)" @@ -182,14 +182,14 @@ { "cell_type": "code", "execution_count": null, - "id": "ad5a2a6e", + "id": "6bcbb69c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "51a32c95", + "id": "d74c5259", "metadata": {}, "source": [ "#### 10. Find indices of non-zero elements from [1,2,0,0,4,0] (★☆☆)" @@ -198,14 +198,14 @@ { "cell_type": "code", "execution_count": null, - "id": "9d820628", + "id": "5a70a7b4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "bcc28d4e", + "id": "8a986270", "metadata": {}, "source": [ "#### 11. Create a 3x3 identity matrix (★☆☆)" @@ -214,14 +214,14 @@ { "cell_type": "code", "execution_count": null, - "id": "eb504454", + "id": "c681b2ae", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3adc02f3", + "id": "b024a4bf", "metadata": {}, "source": [ "#### 12. Create a 3x3x3 array with random values (★☆☆)" @@ -230,14 +230,14 @@ { "cell_type": "code", "execution_count": null, - "id": "8b91d489", + "id": "e7b54796", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a7cd9e02", + "id": "629c0620", "metadata": {}, "source": [ "#### 13. Create a 10x10 array with random values and find the minimum and maximum values (★☆☆)" @@ -246,14 +246,14 @@ { "cell_type": "code", "execution_count": null, - "id": "a9871150", + "id": "0f12525b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9c007df3", + "id": "25f9f091", "metadata": {}, "source": [ "#### 14. Create a random vector of size 30 and find the mean value (★☆☆)" @@ -262,14 +262,14 @@ { "cell_type": "code", "execution_count": null, - "id": "2ee43ca7", + "id": "03dce2ff", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "290aab6f", + "id": "9c4ac621", "metadata": {}, "source": [ "#### 15. Create a 2d array with 1 on the border and 0 inside (★☆☆)" @@ -278,14 +278,14 @@ { "cell_type": "code", "execution_count": null, - "id": "def020fc", + "id": "0a811a3b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d5949ccd", + "id": "c0110caf", "metadata": {}, "source": [ "#### 16. How to add a border (filled with 0's) around an existing array? (★☆☆)" @@ -294,14 +294,14 @@ { "cell_type": "code", "execution_count": null, - "id": "47aa6e9b", + "id": "5e461b43", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "da6ea764", + "id": "c097faee", "metadata": {}, "source": [ "#### 17. What is the result of the following expression? (★☆☆)\n", @@ -318,14 +318,14 @@ { "cell_type": "code", "execution_count": null, - "id": "047bf4c4", + "id": "de307b6d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "e0b25bf8", + "id": "7539be84", "metadata": {}, "source": [ "#### 18. Create a 5x5 matrix with values 1,2,3,4 just below the diagonal (★☆☆)" @@ -334,14 +334,14 @@ { "cell_type": "code", "execution_count": null, - "id": "9392a2cc", + "id": "897eeebd", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "ffc7e714", + "id": "69207418", "metadata": {}, "source": [ "#### 19. Create a 8x8 matrix and fill it with a checkerboard pattern (★☆☆)" @@ -350,14 +350,14 @@ { "cell_type": "code", "execution_count": null, - "id": "1892d23a", + "id": "da9fc94e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "42412361", + "id": "aeb836d2", "metadata": {}, "source": [ "#### 20. Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element? (★☆☆)" @@ -366,14 +366,14 @@ { "cell_type": "code", "execution_count": null, - "id": "c4060dee", + "id": "c026c791", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "fec7cacf", + "id": "9aef01d6", "metadata": {}, "source": [ "#### 21. Create a checkerboard 8x8 matrix using the tile function (★☆☆)" @@ -382,14 +382,14 @@ { "cell_type": "code", "execution_count": null, - "id": "b94338bc", + "id": "1cdf747e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "e3bb989c", + "id": "931830f8", "metadata": {}, "source": [ "#### 22. Normalize a 5x5 random matrix (★☆☆)" @@ -398,14 +398,14 @@ { "cell_type": "code", "execution_count": null, - "id": "95b73cf2", + "id": "1d081cd3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9bf6610b", + "id": "5f3b5379", "metadata": {}, "source": [ "#### 23. Create a custom dtype that describes a color as four unsigned bytes (RGBA) (★☆☆)" @@ -414,14 +414,14 @@ { "cell_type": "code", "execution_count": null, - "id": "0f45e6b5", + "id": "56847b08", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "0c4834ae", + "id": "27294dd6", "metadata": {}, "source": [ "#### 24. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) (★☆☆)" @@ -430,14 +430,14 @@ { "cell_type": "code", "execution_count": null, - "id": "7b4bbf2b", + "id": "5b219d77", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c7b74399", + "id": "57a463f5", "metadata": {}, "source": [ "#### 25. Given a 1D array, negate all elements which are between 3 and 8, in place. (★☆☆)" @@ -446,14 +446,14 @@ { "cell_type": "code", "execution_count": null, - "id": "30f35110", + "id": "1137097c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "da225b07", + "id": "4ec37bca", "metadata": {}, "source": [ "#### 26. What is the output of the following script? (★☆☆)\n", @@ -469,14 +469,14 @@ { "cell_type": "code", "execution_count": null, - "id": "1c55040f", + "id": "ae0f3d30", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "60e2d14c", + "id": "fb875416", "metadata": {}, "source": [ "#### 27. Consider an integer vector Z, which of these expressions are legal? (★☆☆)\n", @@ -493,14 +493,14 @@ { "cell_type": "code", "execution_count": null, - "id": "cb463c42", + "id": "0b516d7c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "aa45d8b5", + "id": "3322bcc4", "metadata": {}, "source": [ "#### 28. What are the result of the following expressions? (★☆☆)\n", @@ -514,14 +514,14 @@ { "cell_type": "code", "execution_count": null, - "id": "ff3500c8", + "id": "68a40ed3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "0b6acccc", + "id": "e85dae57", "metadata": {}, "source": [ "#### 29. How to round away from zero a float array ? (★☆☆)" @@ -530,14 +530,14 @@ { "cell_type": "code", "execution_count": null, - "id": "f0d27652", + "id": "8981d8e2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "87fb3166", + "id": "3d78851a", "metadata": {}, "source": [ "#### 30. How to find common values between two arrays? (★☆☆)" @@ -546,14 +546,14 @@ { "cell_type": "code", "execution_count": null, - "id": "e76b28fc", + "id": "ca4b0788", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "7182fca7", + "id": "535563a3", "metadata": {}, "source": [ "#### 31. How to ignore all numpy warnings (not recommended)? (★☆☆)" @@ -562,14 +562,14 @@ { "cell_type": "code", "execution_count": null, - "id": "652def1c", + "id": "b1884a9f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "115e0e2a", + "id": "9ff18333", "metadata": {}, "source": [ "#### 32. Is the following expressions true? (★☆☆)\n", @@ -581,14 +581,14 @@ { "cell_type": "code", "execution_count": null, - "id": "d0573d89", + "id": "b47e3ad5", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "96c9777e", + "id": "94dfbdcb", "metadata": {}, "source": [ "#### 33. How to get the dates of yesterday, today and tomorrow? (★☆☆)" @@ -597,14 +597,14 @@ { "cell_type": "code", "execution_count": null, - "id": "fc0f2ec9", + "id": "3f686831", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "85237768", + "id": "eb1816a2", "metadata": {}, "source": [ "#### 34. How to get all the dates corresponding to the month of July 2016? (★★☆)" @@ -613,14 +613,14 @@ { "cell_type": "code", "execution_count": null, - "id": "baf7883b", + "id": "963239fb", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "7873c566", + "id": "9174632a", "metadata": {}, "source": [ "#### 35. How to compute ((A+B)*(-A/2)) in place (without copy)? (★★☆)" @@ -629,14 +629,14 @@ { "cell_type": "code", "execution_count": null, - "id": "a454d2c4", + "id": "660f5868", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f4f87f16", + "id": "b6b4504d", "metadata": {}, "source": [ "#### 36. Extract the integer part of a random array of positive numbers using 4 different methods (★★☆)" @@ -645,14 +645,14 @@ { "cell_type": "code", "execution_count": null, - "id": "11fd9724", + "id": "a125254d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c8c3ac99", + "id": "dcd769b0", "metadata": {}, "source": [ "#### 37. Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆)" @@ -661,14 +661,14 @@ { "cell_type": "code", "execution_count": null, - "id": "be466890", + "id": "6e5f39c8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f8a91990", + "id": "83642768", "metadata": {}, "source": [ "#### 38. Consider a generator function that generates 10 integers and use it to build an array (★☆☆)" @@ -677,14 +677,14 @@ { "cell_type": "code", "execution_count": null, - "id": "58568088", + "id": "a24bdb56", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a4a6c98c", + "id": "8ab76708", "metadata": {}, "source": [ "#### 39. Create a vector of size 10 with values ranging from 0 to 1, both excluded (★★☆)" @@ -693,14 +693,14 @@ { "cell_type": "code", "execution_count": null, - "id": "4faf8ebb", + "id": "2b66dc48", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "76b7ccb5", + "id": "a77ffeff", "metadata": {}, "source": [ "#### 40. Create a random vector of size 10 and sort it (★★☆)" @@ -709,14 +709,14 @@ { "cell_type": "code", "execution_count": null, - "id": "03f7e0a0", + "id": "4bd4613d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f1d3571d", + "id": "0f131622", "metadata": {}, "source": [ "#### 41. How to sum a small array faster than np.sum? (★★☆)" @@ -725,14 +725,14 @@ { "cell_type": "code", "execution_count": null, - "id": "6a9e7a0f", + "id": "50006b4f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5f2e7f1d", + "id": "0205ef5d", "metadata": {}, "source": [ "#### 42. Consider two random arrays A and B, check if they are equal (★★☆)" @@ -741,14 +741,14 @@ { "cell_type": "code", "execution_count": null, - "id": "fea071d5", + "id": "33df1c36", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "25089569", + "id": "be053e5d", "metadata": {}, "source": [ "#### 43. Make an array immutable (read-only) (★★☆)" @@ -757,14 +757,14 @@ { "cell_type": "code", "execution_count": null, - "id": "b39c64fd", + "id": "060a0518", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "8e0a92a1", + "id": "92732bec", "metadata": {}, "source": [ "#### 44. Consider a random 10x2 matrix representing cartesian coordinates, convert them to polar coordinates (★★☆)" @@ -773,14 +773,14 @@ { "cell_type": "code", "execution_count": null, - "id": "5dbb04dc", + "id": "d4f46c96", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "be171fc3", + "id": "1f75774a", "metadata": {}, "source": [ "#### 45. Create random vector of size 10 and replace the maximum value by 0 (★★☆)" @@ -789,14 +789,14 @@ { "cell_type": "code", "execution_count": null, - "id": "29050d57", + "id": "812354bf", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "ec8169df", + "id": "ed8431ad", "metadata": {}, "source": [ "#### 46. Create a structured array with `x` and `y` coordinates covering the [0,1]x[0,1] area (★★☆)" @@ -805,14 +805,14 @@ { "cell_type": "code", "execution_count": null, - "id": "e1d5811b", + "id": "f0966336", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "6edcee5a", + "id": "34250c0f", "metadata": {}, "source": [ "#### 47. Given two arrays, X and Y, construct the Cauchy matrix C (Cij =1/(xi - yj)) (★★☆)" @@ -821,14 +821,14 @@ { "cell_type": "code", "execution_count": null, - "id": "72ce5000", + "id": "e8a7ac1a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "e792c9d8", + "id": "5418176b", "metadata": {}, "source": [ "#### 48. Print the minimum and maximum representable values for each numpy scalar type (★★☆)" @@ -837,14 +837,14 @@ { "cell_type": "code", "execution_count": null, - "id": "57f92e12", + "id": "ad633154", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "eca7651c", + "id": "1d868ac4", "metadata": {}, "source": [ "#### 49. How to print all the values of an array? (★★☆)" @@ -853,14 +853,14 @@ { "cell_type": "code", "execution_count": null, - "id": "9fea21ff", + "id": "d12c9111", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b2527d57", + "id": "c5f1c082", "metadata": {}, "source": [ "#### 50. How to find the closest value (to a given scalar) in a vector? (★★☆)" @@ -869,14 +869,14 @@ { "cell_type": "code", "execution_count": null, - "id": "1f94b185", + "id": "4374f950", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "42580cd7", + "id": "91d0c841", "metadata": {}, "source": [ "#### 51. Create a structured array representing a position (x,y) and a color (r,g,b) (★★☆)" @@ -885,14 +885,14 @@ { "cell_type": "code", "execution_count": null, - "id": "c280e178", + "id": "11b8fc88", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "7e23ffda", + "id": "076d36d7", "metadata": {}, "source": [ "#### 52. Consider a random vector with shape (100,2) representing coordinates, find point by point distances (★★☆)" @@ -901,14 +901,14 @@ { "cell_type": "code", "execution_count": null, - "id": "9d07ad1d", + "id": "14c1ff07", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "8a2a9157", + "id": "3581ca30", "metadata": {}, "source": [ "#### 53. How to convert a float (32 bits) array into an integer (32 bits) array in place?" @@ -917,14 +917,14 @@ { "cell_type": "code", "execution_count": null, - "id": "4ffd9601", + "id": "72732a66", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "10b38393", + "id": "3f440ff2", "metadata": {}, "source": [ "#### 54. How to read the following file? (★★☆)\n", @@ -938,14 +938,14 @@ { "cell_type": "code", "execution_count": null, - "id": "29f152f2", + "id": "9d03d55d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "72ac7927", + "id": "8ef1aff5", "metadata": {}, "source": [ "#### 55. What is the equivalent of enumerate for numpy arrays? (★★☆)" @@ -954,14 +954,14 @@ { "cell_type": "code", "execution_count": null, - "id": "3de613a5", + "id": "bc1cfb8e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "db66e31f", + "id": "b8737694", "metadata": {}, "source": [ "#### 56. Generate a generic 2D Gaussian-like array (★★☆)" @@ -970,14 +970,14 @@ { "cell_type": "code", "execution_count": null, - "id": "60673f97", + "id": "e38c976d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "6997c220", + "id": "1f7a093e", "metadata": {}, "source": [ "#### 57. How to randomly place p elements in a 2D array? (★★☆)" @@ -986,14 +986,14 @@ { "cell_type": "code", "execution_count": null, - "id": "81ac5c03", + "id": "aa14eab2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c168fd87", + "id": "4b1f4144", "metadata": {}, "source": [ "#### 58. Subtract the mean of each row of a matrix (★★☆)" @@ -1002,14 +1002,14 @@ { "cell_type": "code", "execution_count": null, - "id": "4158b357", + "id": "39be0ed4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f74204b8", + "id": "36c47776", "metadata": {}, "source": [ "#### 59. How to sort an array by the nth column? (★★☆)" @@ -1018,14 +1018,14 @@ { "cell_type": "code", "execution_count": null, - "id": "665d7fb2", + "id": "43867c6a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "26b46ba2", + "id": "d798a57f", "metadata": {}, "source": [ "#### 60. How to tell if a given 2D array has null columns? (★★☆)" @@ -1034,14 +1034,14 @@ { "cell_type": "code", "execution_count": null, - "id": "0420e879", + "id": "d2059b5c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a7c73eaf", + "id": "eee145c4", "metadata": {}, "source": [ "#### 61. Find the nearest value from a given value in an array (★★☆)" @@ -1050,14 +1050,14 @@ { "cell_type": "code", "execution_count": null, - "id": "e00ee9c8", + "id": "1b1b5829", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "016f9f8f", + "id": "745bdd9a", "metadata": {}, "source": [ "#### 62. Considering two arrays with shape (1,3) and (3,1), how to compute their sum using an iterator? (★★☆)" @@ -1066,14 +1066,14 @@ { "cell_type": "code", "execution_count": null, - "id": "5d73e1b3", + "id": "3a9859f9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5d04eec4", + "id": "652b80f0", "metadata": {}, "source": [ "#### 63. Create an array class that has a name attribute (★★☆)" @@ -1082,14 +1082,14 @@ { "cell_type": "code", "execution_count": null, - "id": "ddb2a215", + "id": "cfe2e2a7", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9efcd2a7", + "id": "58349a3a", "metadata": {}, "source": [ "#### 64. Consider a given vector, how to add 1 to each element indexed by a second vector (be careful with repeated indices)? (★★★)" @@ -1098,14 +1098,14 @@ { "cell_type": "code", "execution_count": null, - "id": "4ed538af", + "id": "7d5f94a6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9215bb59", + "id": "13247c07", "metadata": {}, "source": [ "#### 65. How to accumulate elements of a vector (X) to an array (F) based on an index list (I)? (★★★)" @@ -1114,14 +1114,14 @@ { "cell_type": "code", "execution_count": null, - "id": "74f5d129", + "id": "074b5598", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a0ea705d", + "id": "8996b4be", "metadata": {}, "source": [ "#### 66. Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique colors (★★☆)" @@ -1130,14 +1130,14 @@ { "cell_type": "code", "execution_count": null, - "id": "6a800287", + "id": "4541bada", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "39363678", + "id": "1de1a251", "metadata": {}, "source": [ "#### 67. Considering a four dimensions array, how to get sum over the last two axis at once? (★★★)" @@ -1146,14 +1146,14 @@ { "cell_type": "code", "execution_count": null, - "id": "f4102915", + "id": "855d4734", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "8d0566f6", + "id": "2c3a49fa", "metadata": {}, "source": [ "#### 68. Considering a one-dimensional vector D, how to compute means of subsets of D using a vector S of same size describing subset indices? (★★★)" @@ -1162,14 +1162,14 @@ { "cell_type": "code", "execution_count": null, - "id": "b698d7c6", + "id": "9785a70e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c5f95a28", + "id": "d97e5ce6", "metadata": {}, "source": [ "#### 69. How to get the diagonal of a dot product? (★★★)" @@ -1178,14 +1178,14 @@ { "cell_type": "code", "execution_count": null, - "id": "5ff048b9", + "id": "b8adf4c7", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9926d135", + "id": "59f04026", "metadata": {}, "source": [ "#### 70. Consider the vector [1, 2, 3, 4, 5], how to build a new vector with 3 consecutive zeros interleaved between each value? (★★★)" @@ -1194,14 +1194,14 @@ { "cell_type": "code", "execution_count": null, - "id": "b3309bfc", + "id": "9bf72549", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "77faa124", + "id": "5915be32", "metadata": {}, "source": [ "#### 71. Consider an array of dimension (5,5,3), how to multiply it by an array with dimensions (5,5)? (★★★)" @@ -1210,14 +1210,14 @@ { "cell_type": "code", "execution_count": null, - "id": "f632f202", + "id": "615685a1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "56aa0c3d", + "id": "88afd68d", "metadata": {}, "source": [ "#### 72. How to swap two rows of an array? (★★★)" @@ -1226,14 +1226,14 @@ { "cell_type": "code", "execution_count": null, - "id": "efe16b12", + "id": "ab382690", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "1068d7cd", + "id": "103b64e3", "metadata": {}, "source": [ "#### 73. Consider a set of 10 triplets describing 10 triangles (with shared vertices), find the set of unique line segments composing all the triangles (★★★)" @@ -1242,14 +1242,14 @@ { "cell_type": "code", "execution_count": null, - "id": "e391b049", + "id": "ea64e9b8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "bcc34f27", + "id": "69e0c2c7", "metadata": {}, "source": [ "#### 74. Given a sorted array C that corresponds to a bincount, how to produce an array A such that np.bincount(A) == C? (★★★)" @@ -1258,14 +1258,14 @@ { "cell_type": "code", "execution_count": null, - "id": "beb8e3e8", + "id": "7935210d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b365cc6c", + "id": "a217bfc5", "metadata": {}, "source": [ "#### 75. How to compute averages using a sliding window over an array? (★★★)" @@ -1274,14 +1274,14 @@ { "cell_type": "code", "execution_count": null, - "id": "e0831ba1", + "id": "16cd9bde", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "91c854ca", + "id": "1da33793", "metadata": {}, "source": [ "#### 76. Consider a one-dimensional array Z, build a two-dimensional array whose first row is (Z[0],Z[1],Z[2]) and each subsequent row is shifted by 1 (last row should be (Z[-3],Z[-2],Z[-1]) (★★★)" @@ -1290,14 +1290,14 @@ { "cell_type": "code", "execution_count": null, - "id": "1ac36aad", + "id": "8059a53b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "cbd50313", + "id": "f6c798af", "metadata": {}, "source": [ "#### 77. How to negate a boolean, or to change the sign of a float inplace? (★★★)" @@ -1306,14 +1306,14 @@ { "cell_type": "code", "execution_count": null, - "id": "7c3b7ab3", + "id": "95d52799", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "94a6cb93", + "id": "ed6b13c8", "metadata": {}, "source": [ "#### 78. Consider 2 sets of points P0,P1 describing lines (2d) and a point p, how to compute distance from p to each line i (P0[i],P1[i])? (★★★)" @@ -1322,14 +1322,14 @@ { "cell_type": "code", "execution_count": null, - "id": "705f9fa8", + "id": "2870e6b4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "7a7b065f", + "id": "7f9a94ca", "metadata": {}, "source": [ "#### 79. Consider 2 sets of points P0,P1 describing lines (2d) and a set of points P, how to compute distance from each point j (P[j]) to each line i (P0[i],P1[i])? (★★★)" @@ -1338,14 +1338,14 @@ { "cell_type": "code", "execution_count": null, - "id": "a76a1e2a", + "id": "de27d0c4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a4a22eac", + "id": "61709616", "metadata": {}, "source": [ "#### 80. Consider an arbitrary array, write a function that extracts a subpart with a fixed shape and centered on a given element (pad with a `fill` value when necessary) (★★★)" @@ -1354,14 +1354,14 @@ { "cell_type": "code", "execution_count": null, - "id": "3d0a25fa", + "id": "952b826a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3e056709", + "id": "585a0296", "metadata": {}, "source": [ "#### 81. Consider an array Z = [1,2,3,4,5,6,7,8,9,10,11,12,13,14], how to generate an array R = [[1,2,3,4], [2,3,4,5], [3,4,5,6], ..., [11,12,13,14]]? (★★★)" @@ -1370,14 +1370,14 @@ { "cell_type": "code", "execution_count": null, - "id": "16e69f8c", + "id": "66729fa0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "df30b27a", + "id": "f6ea03b3", "metadata": {}, "source": [ "#### 82. Compute a matrix rank (★★★)" @@ -1386,14 +1386,14 @@ { "cell_type": "code", "execution_count": null, - "id": "a1d0d2cb", + "id": "80b6d0f4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f03b141d", + "id": "1ef1692e", "metadata": {}, "source": [ "#### 83. How to find the most frequent value in an array?" @@ -1402,14 +1402,14 @@ { "cell_type": "code", "execution_count": null, - "id": "4ba555c9", + "id": "de8f66cc", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "eccb13af", + "id": "310aa04e", "metadata": {}, "source": [ "#### 84. Extract all the contiguous 3x3 blocks from a random 10x10 matrix (★★★)" @@ -1418,14 +1418,14 @@ { "cell_type": "code", "execution_count": null, - "id": "c0fa657e", + "id": "f2639e17", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "1e955525", + "id": "cdf46958", "metadata": {}, "source": [ "#### 85. Create a 2D array subclass such that Z[i,j] == Z[j,i] (★★★)" @@ -1434,14 +1434,14 @@ { "cell_type": "code", "execution_count": null, - "id": "4b640a43", + "id": "7f3cb2c9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "24891de6", + "id": "9ab6b1ca", "metadata": {}, "source": [ "#### 86. Consider a set of p matrices with shape (n,n) and a set of p vectors with shape (n,1). How to compute the sum of of the p matrix products at once? (result has shape (n,1)) (★★★)" @@ -1450,14 +1450,14 @@ { "cell_type": "code", "execution_count": null, - "id": "3be310a0", + "id": "a6f93a7b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c250f782", + "id": "2c8cd60e", "metadata": {}, "source": [ "#### 87. Consider a 16x16 array, how to get the block-sum (block size is 4x4)? (★★★)" @@ -1466,14 +1466,14 @@ { "cell_type": "code", "execution_count": null, - "id": "090b56b5", + "id": "1f8d7f5f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "48064219", + "id": "cbd5321e", "metadata": {}, "source": [ "#### 88. How to implement the Game of Life using numpy arrays? (★★★)" @@ -1482,14 +1482,14 @@ { "cell_type": "code", "execution_count": null, - "id": "4c9bd070", + "id": "a8bd84fd", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3ad7c2e9", + "id": "f9c4b416", "metadata": {}, "source": [ "#### 89. How to get the n largest values of an array (★★★)" @@ -1498,14 +1498,14 @@ { "cell_type": "code", "execution_count": null, - "id": "8d1dd580", + "id": "c4123ace", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3a64b152", + "id": "495897e7", "metadata": {}, "source": [ "#### 90. Given an arbitrary number of vectors, build the cartesian product (every combination of every item) (★★★)" @@ -1514,14 +1514,14 @@ { "cell_type": "code", "execution_count": null, - "id": "e1bd5177", + "id": "9a9ed4a7", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2d4144aa", + "id": "c26bde0b", "metadata": {}, "source": [ "#### 91. How to create a record array from a regular array? (★★★)" @@ -1530,14 +1530,14 @@ { "cell_type": "code", "execution_count": null, - "id": "d66e2085", + "id": "4958f3a6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3712ad1d", + "id": "d6c5435c", "metadata": {}, "source": [ "#### 92. Consider a large vector Z, compute Z to the power of 3 using 3 different methods (★★★)" @@ -1546,14 +1546,14 @@ { "cell_type": "code", "execution_count": null, - "id": "5627fa7c", + "id": "5269d1ec", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "7aadddea", + "id": "9f7efc22", "metadata": {}, "source": [ "#### 93. Consider two arrays A and B of shape (8,3) and (2,2). How to find rows of A that contain elements of each row of B regardless of the order of the elements in B? (★★★)" @@ -1562,14 +1562,14 @@ { "cell_type": "code", "execution_count": null, - "id": "2d2bcb22", + "id": "1d9166aa", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "ce2e0ef6", + "id": "2bfe9e3c", "metadata": {}, "source": [ "#### 94. Considering a 10x3 matrix, extract rows with unequal values (e.g. [2,2,3]) (★★★)" @@ -1578,14 +1578,14 @@ { "cell_type": "code", "execution_count": null, - "id": "94b79a35", + "id": "c00f8f84", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "fac5263a", + "id": "1bebee22", "metadata": {}, "source": [ "#### 95. Convert a vector of ints into a matrix binary representation (★★★)" @@ -1594,14 +1594,14 @@ { "cell_type": "code", "execution_count": null, - "id": "62291895", + "id": "4bf1b73f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "85bcb763", + "id": "37acca61", "metadata": {}, "source": [ "#### 96. Given a two dimensional array, how to extract unique rows? (★★★)" @@ -1610,14 +1610,14 @@ { "cell_type": "code", "execution_count": null, - "id": "c348ae06", + "id": "7051b1d7", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5d35d3f8", + "id": "6c8a313c", "metadata": {}, "source": [ "#### 97. Considering 2 vectors A & B, write the einsum equivalent of inner, outer, sum, and mul function (★★★)" @@ -1626,14 +1626,14 @@ { "cell_type": "code", "execution_count": null, - "id": "7f04a281", + "id": "7bb32dd1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "fd4cd346", + "id": "dffed116", "metadata": {}, "source": [ "#### 98. Considering a path described by two vectors (X,Y), how to sample it using equidistant samples (★★★)?" @@ -1642,14 +1642,14 @@ { "cell_type": "code", "execution_count": null, - "id": "65790a2b", + "id": "c8a0a05f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5f0572af", + "id": "e2208f4e", "metadata": {}, "source": [ "#### 99. Given an integer n and a 2D array X, select from X the rows which can be interpreted as draws from a multinomial distribution with n degrees, i.e., the rows which only contain integers and which sum to n. (★★★)" @@ -1658,14 +1658,14 @@ { "cell_type": "code", "execution_count": null, - "id": "fcea30b6", + "id": "5a240dba", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "0c86dfd0", + "id": "21157036", "metadata": {}, "source": [ "#### 100. Compute bootstrapped 95% confidence intervals for the mean of a 1D array X (i.e., resample the elements of an array with replacement N times, compute the mean of each sample, and then compute percentiles over the means). (★★★)" @@ -1674,7 +1674,7 @@ { "cell_type": "code", "execution_count": null, - "id": "05e1d7f9", + "id": "66c8a5db", "metadata": {}, "outputs": [], "source": [] diff --git a/100_Numpy_exercises_with_hints_with_solutions.md b/100_Numpy_exercises_with_hints_with_solutions.md index 6e60dd2..409c00f 100644 --- a/100_Numpy_exercises_with_hints_with_solutions.md +++ b/100_Numpy_exercises_with_hints_with_solutions.md @@ -870,7 +870,27 @@ np.negative(Z, out=Z) `No hints provided...` ```python -def distance(P0, P1, p): +P0 = np.random.uniform(-10,10,(10,2)) +P1 = np.random.uniform(-10,10,(10,2)) +p = np.random.uniform(-10,10,( 1,2)) + +def distance_faster(P0,P1,p): + #Author: Hemanth Pasupuleti + #Reference: https://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html + + v = P1- P0 + v[:,[0,1]] = v[:,[1,0]] + v[:,1]*=-1 + norm = np.linalg.norm(v,axis=1) + r = P0 - p + d = np.abs(np.einsum("ij,ij->i",r,v)) / norm + + return d + +print(distance_faster(P0, P1, p)) + +##--------------- OR ---------------## +def distance_slower(P0, P1, p): T = P1 - P0 L = (T**2).sum(axis=1) U = -((P0[:,0]-p[...,0])*T[:,0] + (P0[:,1]-p[...,1])*T[:,1]) / L @@ -878,10 +898,7 @@ def distance(P0, P1, p): D = P0 + U*T - p return np.sqrt((D**2).sum(axis=1)) -P0 = np.random.uniform(-10,10,(10,2)) -P1 = np.random.uniform(-10,10,(10,2)) -p = np.random.uniform(-10,10,( 1,2)) -print(distance(P0, P1, p)) +print(distance_slower(P0, P1, p)) ``` #### 79. Consider 2 sets of points P0,P1 describing lines (2d) and a set of points P, how to compute distance from each point j (P[j]) to each line i (P0[i],P1[i])? (★★★) `No hints provided...` diff --git a/100_Numpy_exercises_with_solutions.md b/100_Numpy_exercises_with_solutions.md index 307e658..c7285d2 100644 --- a/100_Numpy_exercises_with_solutions.md +++ b/100_Numpy_exercises_with_solutions.md @@ -870,7 +870,27 @@ np.negative(Z, out=Z) ```python -def distance(P0, P1, p): +P0 = np.random.uniform(-10,10,(10,2)) +P1 = np.random.uniform(-10,10,(10,2)) +p = np.random.uniform(-10,10,( 1,2)) + +def distance_faster(P0,P1,p): + #Author: Hemanth Pasupuleti + #Reference: https://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html + + v = P1- P0 + v[:,[0,1]] = v[:,[1,0]] + v[:,1]*=-1 + norm = np.linalg.norm(v,axis=1) + r = P0 - p + d = np.abs(np.einsum("ij,ij->i",r,v)) / norm + + return d + +print(distance_faster(P0, P1, p)) + +##--------------- OR ---------------## +def distance_slower(P0, P1, p): T = P1 - P0 L = (T**2).sum(axis=1) U = -((P0[:,0]-p[...,0])*T[:,0] + (P0[:,1]-p[...,1])*T[:,1]) / L @@ -878,10 +898,7 @@ def distance(P0, P1, p): D = P0 + U*T - p return np.sqrt((D**2).sum(axis=1)) -P0 = np.random.uniform(-10,10,(10,2)) -P1 = np.random.uniform(-10,10,(10,2)) -p = np.random.uniform(-10,10,( 1,2)) -print(distance(P0, P1, p)) +print(distance_slower(P0, P1, p)) ``` #### 79. Consider 2 sets of points P0,P1 describing lines (2d) and a set of points P, how to compute distance from each point j (P[j]) to each line i (P0[i],P1[i])? (★★★) diff --git a/100_Numpy_random.ipynb b/100_Numpy_random.ipynb index 2766fc5..880138a 100644 --- a/100_Numpy_random.ipynb +++ b/100_Numpy_random.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "markdown", - "id": "c1a8ea63", + "id": "738eba3f", "metadata": {}, "source": [ "# 100 numpy exercises\n", @@ -18,7 +18,7 @@ }, { "cell_type": "markdown", - "id": "8481f467", + "id": "f65f901e", "metadata": {}, "source": [ "File automatically generated. See the documentation to update questions/answers/hints programmatically." @@ -26,7 +26,7 @@ }, { "cell_type": "markdown", - "id": "7c1a3673", + "id": "15045647", "metadata": {}, "source": [ "Run the `initialize.py` module, then call a random question with `pick()` an hint towards its solution with\n", @@ -36,7 +36,7 @@ { "cell_type": "code", "execution_count": null, - "id": "6db52927", + "id": "0d23aa5b", "metadata": {}, "outputs": [], "source": [ @@ -46,7 +46,7 @@ { "cell_type": "code", "execution_count": null, - "id": "2c9f898f", + "id": "4a6a613b", "metadata": {}, "outputs": [], "source": [