diff --git a/100_Numpy_exercises.ipynb b/100_Numpy_exercises.ipynb index 39b8d97..a1590c3 100644 --- a/100_Numpy_exercises.ipynb +++ b/100_Numpy_exercises.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "markdown", - "id": "24963653", + "id": "9a9aa121", "metadata": {}, "source": [ "# 100 numpy exercises\n", @@ -18,7 +18,7 @@ }, { "cell_type": "markdown", - "id": "59ddae7e", + "id": "8befc7ce", "metadata": {}, "source": [ "File automatically generated. See the documentation to update questions/answers/hints programmatically." @@ -26,7 +26,7 @@ }, { "cell_type": "markdown", - "id": "4a02b98c", + "id": "6f825f1c", "metadata": {}, "source": [ "Run the `initialise.py` module, then for each question you can query the\n", @@ -36,7 +36,7 @@ { "cell_type": "code", "execution_count": null, - "id": "c2161124", + "id": "703184e0", "metadata": {}, "outputs": [], "source": [ @@ -45,7 +45,7 @@ }, { "cell_type": "markdown", - "id": "96fe6659", + "id": "d1f04fa5", "metadata": {}, "source": [ "#### 1. Import the numpy package under the name `np` (★☆☆)" @@ -54,14 +54,14 @@ { "cell_type": "code", "execution_count": null, - "id": "9ffea9a1", + "id": "bc171159", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "126932fd", + "id": "4fb231dc", "metadata": {}, "source": [ "#### 2. Print the numpy version and the configuration (★☆☆)" @@ -70,14 +70,14 @@ { "cell_type": "code", "execution_count": null, - "id": "fdd0d7a8", + "id": "b2ed5c7c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "43c84f29", + "id": "04caf5c7", "metadata": {}, "source": [ "#### 3. Create a null vector of size 10 (★☆☆)" @@ -86,14 +86,14 @@ { "cell_type": "code", "execution_count": null, - "id": "e007a9d1", + "id": "42e74a30", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "22dc97de", + "id": "751cf42e", "metadata": {}, "source": [ "#### 4. How to find the memory size of any array (★☆☆)" @@ -102,14 +102,14 @@ { "cell_type": "code", "execution_count": null, - "id": "d27dba65", + "id": "97c5fbab", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "4e5bae8d", + "id": "88a97f0c", "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": "1dbbbed8", + "id": "92abb854", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "fcfdbff1", + "id": "a7671961", "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": "0387a362", + "id": "6c38f791", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "bdf2ba90", + "id": "8e5fdd72", "metadata": {}, "source": [ "#### 7. Create a vector with values ranging from 10 to 49 (★☆☆)" @@ -150,14 +150,14 @@ { "cell_type": "code", "execution_count": null, - "id": "81f0a925", + "id": "0c911e4b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5d0c6cfe", + "id": "de9853d4", "metadata": {}, "source": [ "#### 8. Reverse a vector (first element becomes last) (★☆☆)" @@ -166,14 +166,14 @@ { "cell_type": "code", "execution_count": null, - "id": "8e7754ae", + "id": "63ec0dea", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3f5bef92", + "id": "a35c1207", "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": "e0e277fc", + "id": "bde1723f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "14b44c2e", + "id": "9ad0f11f", "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": "1ad14e21", + "id": "959823d4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "67c0f072", + "id": "d3c8eac1", "metadata": {}, "source": [ "#### 11. Create a 3x3 identity matrix (★☆☆)" @@ -214,14 +214,14 @@ { "cell_type": "code", "execution_count": null, - "id": "ca2bc573", + "id": "306a48be", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "097460e8", + "id": "a905079b", "metadata": {}, "source": [ "#### 12. Create a 3x3x3 array with random values (★☆☆)" @@ -230,14 +230,14 @@ { "cell_type": "code", "execution_count": null, - "id": "4bd63c1f", + "id": "3bd26f5d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f7a72443", + "id": "cbf5cf4a", "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": "86d85900", + "id": "a0686c44", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2100731f", + "id": "b322aab7", "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": "63e2bbf2", + "id": "97dce1ea", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "dfb30185", + "id": "101e9f4f", "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": "480afa6b", + "id": "55b6af0c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "36e4de87", + "id": "b75a05e9", "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": "5ac52150", + "id": "771b07f2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "6f43f816", + "id": "d773a68e", "metadata": {}, "source": [ "#### 17. What is the result of the following expression? (★☆☆)\n", @@ -318,14 +318,14 @@ { "cell_type": "code", "execution_count": null, - "id": "f2395fda", + "id": "5283493c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "446f87e0", + "id": "569089c1", "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": "58d0e495", + "id": "acdd4875", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b6366cdb", + "id": "c757729e", "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": "54d459ce", + "id": "a96e0ca1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "35077361", + "id": "63d93126", "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": "d9cbcf85", + "id": "22800ac9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "ef02a06b", + "id": "237998f6", "metadata": {}, "source": [ "#### 21. Create a checkerboard 8x8 matrix using the tile function (★☆☆)" @@ -382,14 +382,14 @@ { "cell_type": "code", "execution_count": null, - "id": "a29c4af1", + "id": "22a86261", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "4fc8f909", + "id": "578e1eca", "metadata": {}, "source": [ "#### 22. Normalize a 5x5 random matrix (★☆☆)" @@ -398,14 +398,14 @@ { "cell_type": "code", "execution_count": null, - "id": "ddb836b9", + "id": "b07707b1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b2083fc7", + "id": "57d1fff2", "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": "28bd9e0b", + "id": "1ea1a438", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "8bfb605c", + "id": "08189760", "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": "0e4525f6", + "id": "2b120295", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d945b917", + "id": "3fa7d315", "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": "13ea9d0c", + "id": "52e10a95", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "80e3937d", + "id": "6100c656", "metadata": {}, "source": [ "#### 26. What is the output of the following script? (★☆☆)\n", @@ -469,14 +469,14 @@ { "cell_type": "code", "execution_count": null, - "id": "3268939a", + "id": "099d3b5f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "4e586977", + "id": "21cadfdb", "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": "1f36c9d4", + "id": "eefdc61f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "4db5a6d5", + "id": "edb05d8c", "metadata": {}, "source": [ "#### 28. What are the result of the following expressions? (★☆☆)\n", @@ -514,14 +514,14 @@ { "cell_type": "code", "execution_count": null, - "id": "a38701d1", + "id": "98cf2ccd", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b4d3080a", + "id": "e125e143", "metadata": {}, "source": [ "#### 29. How to round away from zero a float array ? (★☆☆)" @@ -530,14 +530,14 @@ { "cell_type": "code", "execution_count": null, - "id": "84c7b39e", + "id": "ac635a3c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "4f89952c", + "id": "6fbcfde4", "metadata": {}, "source": [ "#### 30. How to find common values between two arrays? (★☆☆)" @@ -546,14 +546,14 @@ { "cell_type": "code", "execution_count": null, - "id": "ec4561c9", + "id": "cef72c3d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "26bf6974", + "id": "448290f7", "metadata": {}, "source": [ "#### 31. How to ignore all numpy warnings (not recommended)? (★☆☆)" @@ -562,14 +562,14 @@ { "cell_type": "code", "execution_count": null, - "id": "950d597f", + "id": "570ffcdb", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "261fd76b", + "id": "a9634713", "metadata": {}, "source": [ "#### 32. Is the following expressions true? (★☆☆)\n", @@ -581,14 +581,14 @@ { "cell_type": "code", "execution_count": null, - "id": "96eb8b24", + "id": "ea730787", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "4a82256f", + "id": "ca2c7bd0", "metadata": {}, "source": [ "#### 33. How to get the dates of yesterday, today and tomorrow? (★☆☆)" @@ -597,14 +597,14 @@ { "cell_type": "code", "execution_count": null, - "id": "4cff80dd", + "id": "e771971c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d90599b6", + "id": "2ad51e8b", "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": "16337844", + "id": "265753fd", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "aa35839c", + "id": "a58b5c61", "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": "54b8178b", + "id": "2a29953f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "13bc061a", + "id": "76f22ae3", "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": "c8fee789", + "id": "0990421d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f01b9650", + "id": "994a8b81", "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": "ab1101be", + "id": "3cbaa01b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "35976e4b", + "id": "c7d368b0", "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": "7ca66207", + "id": "92c49778", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "edb9a160", + "id": "6adeb79d", "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": "67aaf30a", + "id": "21e51b0f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "69a3604b", + "id": "1cdc3d67", "metadata": {}, "source": [ "#### 40. Create a random vector of size 10 and sort it (★★☆)" @@ -709,14 +709,14 @@ { "cell_type": "code", "execution_count": null, - "id": "278e79e3", + "id": "24d950b4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "05ec5273", + "id": "a2da25e2", "metadata": {}, "source": [ "#### 41. How to sum a small array faster than np.sum? (★★☆)" @@ -725,14 +725,14 @@ { "cell_type": "code", "execution_count": null, - "id": "94eea81d", + "id": "d7ca546d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d310ff7e", + "id": "19cc3ede", "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": "754e9de5", + "id": "5c055834", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "dab5009d", + "id": "7df700bc", "metadata": {}, "source": [ "#### 43. Make an array immutable (read-only) (★★☆)" @@ -757,14 +757,14 @@ { "cell_type": "code", "execution_count": null, - "id": "496ac553", + "id": "a5f059a0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "71643a79", + "id": "982e2f7e", "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": "60d87877", + "id": "d9423986", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "8ed31762", + "id": "b5863c52", "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": "6097ec5e", + "id": "3b65f66f", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "1a0d5a6b", + "id": "2a12f2ae", "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": "749e196f", + "id": "65763e44", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "ebc0da7d", + "id": "9be0eb72", "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": "eb21da87", + "id": "d999c9c6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "8f3327cb", + "id": "864fda69", "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": "3f197c5a", + "id": "03e5dc9e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "207e37c6", + "id": "e6437089", "metadata": {}, "source": [ "#### 49. How to print all the values of an array? (★★☆)" @@ -853,14 +853,14 @@ { "cell_type": "code", "execution_count": null, - "id": "b9165f3d", + "id": "d0b8972b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "1bda9a9a", + "id": "59b3f402", "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": "86771556", + "id": "9c3702bb", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "0f057548", + "id": "5161a0e8", "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": "b71873c2", + "id": "fee3d794", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "47118bd1", + "id": "f0c0a11e", "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": "567883c7", + "id": "6fdb2124", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "21d6ebad", + "id": "13007015", "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": "c27bac6f", + "id": "6b691b3b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "0c71ff26", + "id": "3acba415", "metadata": {}, "source": [ "#### 54. How to read the following file? (★★☆)\n", @@ -938,14 +938,14 @@ { "cell_type": "code", "execution_count": null, - "id": "efdf3972", + "id": "8505835e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "5953f719", + "id": "0c46d792", "metadata": {}, "source": [ "#### 55. What is the equivalent of enumerate for numpy arrays? (★★☆)" @@ -954,14 +954,14 @@ { "cell_type": "code", "execution_count": null, - "id": "d7e34455", + "id": "85fdeb4a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "87ded306", + "id": "9e276331", "metadata": {}, "source": [ "#### 56. Generate a generic 2D Gaussian-like array (★★☆)" @@ -970,14 +970,14 @@ { "cell_type": "code", "execution_count": null, - "id": "171a6cbc", + "id": "5f1d784a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "fcfd2f70", + "id": "da594a47", "metadata": {}, "source": [ "#### 57. How to randomly place p elements in a 2D array? (★★☆)" @@ -986,14 +986,14 @@ { "cell_type": "code", "execution_count": null, - "id": "1804e4ab", + "id": "b4f5aedd", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f0b9b18b", + "id": "5a8e20eb", "metadata": {}, "source": [ "#### 58. Subtract the mean of each row of a matrix (★★☆)" @@ -1002,14 +1002,14 @@ { "cell_type": "code", "execution_count": null, - "id": "eac4c892", + "id": "0850bec6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "61e40ad7", + "id": "529d3c17", "metadata": {}, "source": [ "#### 59. How to sort an array by the nth column? (★★☆)" @@ -1018,14 +1018,14 @@ { "cell_type": "code", "execution_count": null, - "id": "c687d9f1", + "id": "0d401082", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "9a4f7fb1", + "id": "0f6680ae", "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": "021aa204", + "id": "fe60a4d0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "bff8d09f", + "id": "f33c3a84", "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": "a340d3dc", + "id": "d45b9371", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "033f7e4e", + "id": "c2f889ed", "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": "f7bc41aa", + "id": "57ea3125", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "8035a7b7", + "id": "dcb09dcd", "metadata": {}, "source": [ "#### 63. Create an array class that has a name attribute (★★☆)" @@ -1082,14 +1082,14 @@ { "cell_type": "code", "execution_count": null, - "id": "0f19321f", + "id": "d7bd3904", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "fa4dbf31", + "id": "b0e91553", "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": "85893b93", + "id": "3affc760", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d55ea9ef", + "id": "e0385252", "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": "7241b03f", + "id": "c9e111a0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3d5ad91f", + "id": "30ceeeb5", "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": "2f8d8c18", + "id": "a07bac48", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "e4c0ca46", + "id": "f2c4f7fe", "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": "cb30cb50", + "id": "377e2aa2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "68781ae8", + "id": "0748b433", "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": "a1a09f76", + "id": "458ead5a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "42ec30c3", + "id": "fca120bf", "metadata": {}, "source": [ "#### 69. How to get the diagonal of a dot product? (★★★)" @@ -1178,14 +1178,14 @@ { "cell_type": "code", "execution_count": null, - "id": "77008bcb", + "id": "aabe682a", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "72cd1881", + "id": "a53781d5", "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": "d237d266", + "id": "f596a9f0", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "00e60dbf", + "id": "8d4c4bcc", "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": "55f0e1d4", + "id": "7eb126d1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "0ba31fcd", + "id": "870c6b3e", "metadata": {}, "source": [ "#### 72. How to swap two rows of an array? (★★★)" @@ -1226,14 +1226,14 @@ { "cell_type": "code", "execution_count": null, - "id": "9fd8d80e", + "id": "018a1fce", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "84f66062", + "id": "f807b95a", "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": "3d92aac2", + "id": "182d7ab8", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "18c7d944", + "id": "127d6162", "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": "1f82e01e", + "id": "58bfa175", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "de0fbc43", + "id": "bea3a91d", "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": "7d6d173f", + "id": "3da93cb6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3caf673b", + "id": "71481034", "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": "1b51c8bc", + "id": "b1419349", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "55ae7213", + "id": "29041930", "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": "f09474c7", + "id": "0f8c2129", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "453aed20", + "id": "75fd8ebf", "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": "288111ad", + "id": "e08558dc", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "a2242b1e", + "id": "1248ffc8", "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": "1238f08b", + "id": "53f1708e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "07c63697", + "id": "76875c5a", "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": "01e418e9", + "id": "165687fa", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "8f46a3e6", + "id": "cdaf9927", "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": "645a43d1", + "id": "f2312a8b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "339d1830", + "id": "f60ed7e8", "metadata": {}, "source": [ "#### 82. Compute a matrix rank (★★★)" @@ -1386,14 +1386,14 @@ { "cell_type": "code", "execution_count": null, - "id": "55909d61", + "id": "53d7964b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "94599b6d", + "id": "9df16ebf", "metadata": {}, "source": [ "#### 83. How to find the most frequent value in an array?" @@ -1402,14 +1402,14 @@ { "cell_type": "code", "execution_count": null, - "id": "cb701ed6", + "id": "b4ee330e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3f2ff8f2", + "id": "730664dc", "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": "93e6f167", + "id": "a828d6f9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "f70e04be", + "id": "8734d0ef", "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": "795e513d", + "id": "35f465a1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "08da7b7a", + "id": "bb4493e6", "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": "9890ed56", + "id": "5f3d2af1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "7853fe1e", + "id": "c4da8597", "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": "146e3ada", + "id": "f6ea8bcd", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2421dc6d", + "id": "c0fab898", "metadata": {}, "source": [ "#### 88. How to implement the Game of Life using numpy arrays? (★★★)" @@ -1482,14 +1482,14 @@ { "cell_type": "code", "execution_count": null, - "id": "06ade5e0", + "id": "7265ae17", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "57aee892", + "id": "376e5084", "metadata": {}, "source": [ "#### 89. How to get the n largest values of an array (★★★)" @@ -1498,14 +1498,14 @@ { "cell_type": "code", "execution_count": null, - "id": "29d72cd2", + "id": "4141f5ab", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "0e2da2b5", + "id": "391538a7", "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": "54874b32", + "id": "2d969dbe", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2ed5c735", + "id": "53187ef3", "metadata": {}, "source": [ "#### 91. How to create a record array from a regular array? (★★★)" @@ -1530,14 +1530,14 @@ { "cell_type": "code", "execution_count": null, - "id": "4e826b8c", + "id": "44ae3756", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d91cfe01", + "id": "6702714f", "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": "b610d5b1", + "id": "5fd2ce21", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "29645461", + "id": "4a093a17", "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": "9b8ceb78", + "id": "c51bb071", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "687c2370", + "id": "78e0b17f", "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": "8455c06b", + "id": "aa3b5e56", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "3726eeed", + "id": "531cf623", "metadata": {}, "source": [ "#### 95. Convert a vector of ints into a matrix binary representation (★★★)" @@ -1594,14 +1594,14 @@ { "cell_type": "code", "execution_count": null, - "id": "cc0652d4", + "id": "140abfed", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d31a4ffd", + "id": "c7e0f77f", "metadata": {}, "source": [ "#### 96. Given a two dimensional array, how to extract unique rows? (★★★)" @@ -1610,14 +1610,14 @@ { "cell_type": "code", "execution_count": null, - "id": "7a31971b", + "id": "b3add5bb", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "8b798880", + "id": "f85e3d17", "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": "c8d2fc50", + "id": "5d8603d7", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "07655168", + "id": "f1bae5a9", "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": "75b400ec", + "id": "5e40ac5c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "d9f463ce", + "id": "ef87eba2", "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": "229d9f12", + "id": "e8de02d6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "b019af39", + "id": "e2b693f7", "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": "b86a6fce", + "id": "cd36404a", "metadata": {}, "outputs": [], "source": [] diff --git a/100_Numpy_exercises_with_hints_with_solutions.md b/100_Numpy_exercises_with_hints_with_solutions.md index b028ed2..15c40cb 100644 --- a/100_Numpy_exercises_with_hints_with_solutions.md +++ b/100_Numpy_exercises_with_hints_with_solutions.md @@ -565,10 +565,18 @@ for index in np.ndindex(Z.shape): `hint: np.meshgrid, np.exp` ```python -X, Y = np.meshgrid(np.linspace(-1,1,10), np.linspace(-1,1,10)) -D = np.sqrt(X*X+Y*Y) +X, Y = np.meshgrid(np.linspace(-1, 1, 10), np.linspace(-1, 1, 10)) sigma, mu = 1.0, 0.0 -G = np.exp(-( (D-mu)**2 / ( 2.0 * sigma**2 ) ) ) +G = np.exp(-((X - mu) ** 2 + (Y - mu) ** 2) / (2.0 * sigma**2)) +print(G) + +# Another solution using the fact that a 2D Gaussian is separable +# Author: Nin17 +x = np.linspace(-1, 1, 10) +y = np.linspace(-1, 1, 10) +gx = np.exp(-((x - mu) ** 2 / (2.0 * sigma**2))) +gy = np.exp(-((y - mu) ** 2 / (2.0 * sigma**2))) +G = gy[:, None] * gx[None, :] print(G) ``` #### 57. How to randomly place p elements in a 2D array? (★★☆) diff --git a/100_Numpy_exercises_with_solutions.md b/100_Numpy_exercises_with_solutions.md index 13ed8dd..1032820 100644 --- a/100_Numpy_exercises_with_solutions.md +++ b/100_Numpy_exercises_with_solutions.md @@ -565,10 +565,18 @@ for index in np.ndindex(Z.shape): ```python -X, Y = np.meshgrid(np.linspace(-1,1,10), np.linspace(-1,1,10)) -D = np.sqrt(X*X+Y*Y) +X, Y = np.meshgrid(np.linspace(-1, 1, 10), np.linspace(-1, 1, 10)) sigma, mu = 1.0, 0.0 -G = np.exp(-( (D-mu)**2 / ( 2.0 * sigma**2 ) ) ) +G = np.exp(-((X - mu) ** 2 + (Y - mu) ** 2) / (2.0 * sigma**2)) +print(G) + +# Another solution using the fact that a 2D Gaussian is separable +# Author: Nin17 +x = np.linspace(-1, 1, 10) +y = np.linspace(-1, 1, 10) +gx = np.exp(-((x - mu) ** 2 / (2.0 * sigma**2))) +gy = np.exp(-((y - mu) ** 2 / (2.0 * sigma**2))) +G = gy[:, None] * gx[None, :] print(G) ``` #### 57. How to randomly place p elements in a 2D array? (★★☆) diff --git a/100_Numpy_random.ipynb b/100_Numpy_random.ipynb index dd8253d..f80bcf4 100644 --- a/100_Numpy_random.ipynb +++ b/100_Numpy_random.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "markdown", - "id": "a3747e86", + "id": "94b10be0", "metadata": {}, "source": [ "# 100 numpy exercises\n", @@ -18,7 +18,7 @@ }, { "cell_type": "markdown", - "id": "8b9ef9a4", + "id": "1d0b9900", "metadata": {}, "source": [ "File automatically generated. See the documentation to update questions/answers/hints programmatically." @@ -26,7 +26,7 @@ }, { "cell_type": "markdown", - "id": "792c1217", + "id": "21313ae9", "metadata": {}, "source": [ "Run the `initialise.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": "541f746c", + "id": "c4ad3a14", "metadata": {}, "outputs": [], "source": [ @@ -46,7 +46,7 @@ { "cell_type": "code", "execution_count": null, - "id": "d1743adb", + "id": "13bdf6d0", "metadata": {}, "outputs": [], "source": [