Skip to content
Snippets Groups Projects
Commit a6ec72ed authored by Bouyahya Zied's avatar Bouyahya Zied
Browse files

Upload New File

parent 73934b00
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:d46e26d4 tags:
# 100 numpy exercises
Source https://github.com/rougier/numpy-100/blob/master/100_Numpy_exercises.ipynb
%% Cell type:markdown id:dcc6b1ac tags:
#### 1. Import the numpy package under the name `np` (★☆☆)
%% Cell type:code id:6c52078e tags:
```
```
%% Cell type:markdown id:118ded29 tags:
#### 2. Print the numpy version and the configuration (★☆☆)
%% Cell type:code id:023a2c52 tags:
```
```
%% Cell type:markdown id:172b913a tags:
#### 3. Create a null vector of size 10 (★☆☆)
%% Cell type:code id:df54025a tags:
```
```
%% Cell type:markdown id:95ee9e07 tags:
#### 4. How to find the memory size of any array (★☆☆)
%% Cell type:code id:5c5b0950 tags:
```
```
%% Cell type:markdown id:45fca154 tags:
#### 5. How to get the documentation of the numpy add function from the command line? (★☆☆)
%% Cell type:code id:fcb1248a tags:
```
```
%% Cell type:markdown id:674e8019 tags:
#### 6. Create a null vector of size 10 but the fifth value which is 1 (★☆☆)
%% Cell type:code id:452df2a7 tags:
```
```
%% Cell type:markdown id:7685e337 tags:
#### 7. Create a vector with values ranging from 10 to 49 (★☆☆)
%% Cell type:code id:10847d11 tags:
```
```
%% Cell type:markdown id:54c913ba tags:
#### 8. Reverse a vector (first element becomes last) (★☆☆)
%% Cell type:code id:34722e83 tags:
```
```
%% Cell type:markdown id:8e72fd08 tags:
#### 9. Create a 3x3 matrix with values ranging from 0 to 8 (★☆☆)
%% Cell type:code id:e18eef56 tags:
```
```
%% Cell type:markdown id:088424fc tags:
#### 10. Find indices of non-zero elements from [1,2,0,0,4,0] (★☆☆)
%% Cell type:code id:9a402d06 tags:
```
```
%% Cell type:markdown id:3f347de9 tags:
#### 11. Create a 3x3 identity matrix (★☆☆)
%% Cell type:code id:e38459c2 tags:
```
```
%% Cell type:markdown id:24e98f25 tags:
#### 12. Create a 3x3x3 array with random values (★☆☆)
%% Cell type:code id:94752bb3 tags:
```
```
%% Cell type:markdown id:7cbca2cc tags:
#### 13. Create a 10x10 array with random values and find the minimum and maximum values (★☆☆)
%% Cell type:code id:c0643b1f tags:
```
```
%% Cell type:markdown id:6cab4cbb tags:
#### 14. Create a random vector of size 30 and find the mean value (★☆☆)
%% Cell type:code id:e922a524 tags:
```
```
%% Cell type:markdown id:72053be8 tags:
#### 15. Create a 2d array with 1 on the border and 0 inside (★☆☆)
%% Cell type:code id:6bc84dfe tags:
```
```
%% Cell type:markdown id:86b0206b tags:
#### 16. How to add a border (filled with 0's) around an existing array? (★☆☆)
%% Cell type:code id:41e66e3e tags:
```
```
%% Cell type:markdown id:98a45574 tags:
#### 17. What is the result of the following expression? (★☆☆)
```python
0 * np.nan
np.nan == np.nan
np.inf > np.nan
np.nan - np.nan
np.nan in set([np.nan])
0.3 == 3 * 0.1
```
%% Cell type:code id:7eb533d5 tags:
```
```
%% Cell type:markdown id:1486619a tags:
#### 18. Create a 5x5 matrix with values 1,2,3,4 just below the diagonal (★☆☆)
%% Cell type:code id:2b9fb59c tags:
```
```
%% Cell type:markdown id:319287fd tags:
#### 19. Create a 8x8 matrix and fill it with a checkerboard pattern (★☆☆)
%% Cell type:code id:299f5e60 tags:
```
```
%% Cell type:markdown id:fb1686ed tags:
#### 20. Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element? (★☆☆)
%% Cell type:code id:22888cc4 tags:
```
```
%% Cell type:markdown id:137ea278 tags:
#### 21. Create a checkerboard 8x8 matrix using the tile function (★☆☆)
%% Cell type:code id:9cb81fe1 tags:
```
```
%% Cell type:markdown id:9319f5a1 tags:
#### 22. Normalize a 5x5 random matrix (★☆☆)
%% Cell type:code id:fcdc0459 tags:
```
```
%% Cell type:markdown id:f1902fef tags:
#### 23. Create a custom dtype that describes a color as four unsigned bytes (RGBA) (★☆☆)
%% Cell type:code id:7445cb2d tags:
```
```
%% Cell type:markdown id:3f34bd2a tags:
#### 24. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) (★☆☆)
%% Cell type:code id:ec8bd6da tags:
```
```
%% Cell type:markdown id:b1919ba3 tags:
#### 25. Given a 1D array, negate all elements which are between 3 and 8, in place. (★☆☆)
%% Cell type:code id:5b63b63b tags:
```
```
%% Cell type:markdown id:fb1daa27 tags:
#### 26. What is the output of the following script? (★☆☆)
```python
# Author: Jake VanderPlas
print(sum(range(5),-1))
from numpy import *
print(sum(range(5),-1))
```
%% Cell type:code id:52a34d3d tags:
```
```
%% Cell type:markdown id:89b7e7a7 tags:
#### 27. Consider an integer vector Z, which of these expressions are legal? (★☆☆)
```python
Z**Z
2 << Z >> 2
Z <- Z
1j*Z
Z/1/1
Z<Z>Z
```
%% Cell type:code id:451181cf tags:
```
```
%% Cell type:markdown id:8c7601c6 tags:
#### 28. What are the result of the following expressions? (★☆☆)
```python
np.array(0) / np.array(0)
np.array(0) // np.array(0)
np.array([np.nan]).astype(int).astype(float)
```
%% Cell type:code id:52983a0b tags:
```
```
%% Cell type:markdown id:06c9164d tags:
#### 29. How to round away from zero a float array ? (★☆☆)
%% Cell type:code id:4cce0af8 tags:
```
```
%% Cell type:markdown id:52235994 tags:
#### 30. How to find common values between two arrays? (★☆☆)
%% Cell type:code id:27eff6b1 tags:
```
```
%% Cell type:markdown id:7e6f7d78 tags:
#### 31. How to ignore all numpy warnings (not recommended)? (★☆☆)
%% Cell type:code id:1bdde6e1 tags:
```
```
%% Cell type:markdown id:b001277e tags:
#### 32. Is the following expressions true? (★☆☆)
```python
np.sqrt(-1) == np.emath.sqrt(-1)
```
%% Cell type:code id:d8673bdf tags:
```
```
%% Cell type:markdown id:b9a5ff84 tags:
#### 33. How to get the dates of yesterday, today and tomorrow? (★☆☆)
%% Cell type:code id:f33f9cc2 tags:
```
```
%% Cell type:markdown id:f8e96e58 tags:
#### 34. How to get all the dates corresponding to the month of July 2016? (★★☆)
%% Cell type:code id:3927a8e8 tags:
```
```
%% Cell type:markdown id:557c163e tags:
#### 35. How to compute ((A+B)*(-A/2)) in place (without copy)? (★★☆)
%% Cell type:code id:3c2f59ac tags:
```
```
%% Cell type:markdown id:14c05852 tags:
#### 36. Extract the integer part of a random array of positive numbers using 4 different methods (★★☆)
%% Cell type:code id:8b7d2937 tags:
```
```
%% Cell type:markdown id:003b8721 tags:
#### 37. Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆)
%% Cell type:code id:63c75db0 tags:
```
```
%% Cell type:markdown id:8e71dc21 tags:
#### 38. Consider a generator function that generates 10 integers and use it to build an array (★☆☆)
%% Cell type:code id:f6aefd94 tags:
```
```
%% Cell type:markdown id:b7f03449 tags:
#### 39. Create a vector of size 10 with values ranging from 0 to 1, both excluded (★★☆)
%% Cell type:code id:a27cd904 tags:
```
```
%% Cell type:markdown id:043156c5 tags:
#### 40. Create a random vector of size 10 and sort it (★★☆)
%% Cell type:code id:6ae5eb58 tags:
```
```
%% Cell type:markdown id:4f7e8f3f tags:
#### 41. How to sum a small array faster than np.sum? (★★☆)
%% Cell type:code id:88790af3 tags:
```
```
%% Cell type:markdown id:13d1ca52 tags:
#### 42. Consider two random array A and B, check if they are equal (★★☆)
%% Cell type:code id:ca6dd579 tags:
```
```
%% Cell type:markdown id:cf7cca6d tags:
#### 43. Make an array immutable (read-only) (★★☆)
%% Cell type:code id:cbbb8917 tags:
```
```
%% Cell type:markdown id:37b0828f tags:
#### 44. Consider a random 10x2 matrix representing cartesian coordinates, convert them to polar coordinates (★★☆)
%% Cell type:code id:a89969af tags:
```
```
%% Cell type:markdown id:b7d7dd80 tags:
#### 45. Create random vector of size 10 and replace the maximum value by 0 (★★☆)
%% Cell type:code id:8403a440 tags:
```
```
%% Cell type:markdown id:b47e5837 tags:
#### 46. Create a structured array with `x` and `y` coordinates covering the [0,1]x[0,1] area (★★☆)
%% Cell type:code id:42c72e94 tags:
```
```
%% Cell type:markdown id:8c7946ee tags:
#### 47. Given two arrays, X and Y, construct the Cauchy matrix C (Cij =1/(xi - yj)) (★★☆)
%% Cell type:code id:26b93f06 tags:
```
```
%% Cell type:markdown id:3e898e5f tags:
#### 48. Print the minimum and maximum representable value for each numpy scalar type (★★☆)
%% Cell type:code id:afd1ae18 tags:
```
```
%% Cell type:markdown id:6fe6133b tags:
#### 49. How to print all the values of an array? (★★☆)
%% Cell type:code id:5764ec92 tags:
```
```
%% Cell type:markdown id:7ee2dacc tags:
#### 50. How to find the closest value (to a given scalar) in a vector? (★★☆)
%% Cell type:code id:742518ef tags:
```
```
%% Cell type:markdown id:cfeda6ba tags:
#### 51. Create a structured array representing a position (x,y) and a color (r,g,b) (★★☆)
%% Cell type:code id:9fcbd031 tags:
```
```
%% Cell type:markdown id:b97e75b5 tags:
#### 52. Consider a random vector with shape (100,2) representing coordinates, find point by point distances (★★☆)
%% Cell type:code id:c58a45b6 tags:
```
```
%% Cell type:markdown id:291e0bb9 tags:
#### 53. How to convert a float (32 bits) array into an integer (32 bits) in place?
%% Cell type:code id:2f3e6c00 tags:
```
```
%% Cell type:markdown id:1e305e92 tags:
#### 54. How to read the following file? (★★☆)
```
1, 2, 3, 4, 5
6, , , 7, 8
, , 9,10,11
```
%% Cell type:code id:753024cb tags:
```
```
%% Cell type:markdown id:94a8ad69 tags:
#### 55. What is the equivalent of enumerate for numpy arrays? (★★☆)
%% Cell type:code id:ae45249f tags:
```
```
%% Cell type:markdown id:72b340c9 tags:
#### 56. Generate a generic 2D Gaussian-like array (★★☆)
%% Cell type:code id:a80e5c9b tags:
```
```
%% Cell type:markdown id:86550d61 tags:
#### 57. How to randomly place p elements in a 2D array? (★★☆)
%% Cell type:code id:db9f2582 tags:
```
```
%% Cell type:markdown id:153f8cfa tags:
#### 58. Subtract the mean of each row of a matrix (★★☆)
%% Cell type:code id:8301cb12 tags:
```
```
%% Cell type:markdown id:f4038090 tags:
#### 59. How to sort an array by the nth column? (★★☆)
%% Cell type:code id:eb5745df tags:
```
```
%% Cell type:markdown id:20d2a462 tags:
#### 60. How to tell if a given 2D array has null columns? (★★☆)
%% Cell type:code id:b92b7316 tags:
```
```
%% Cell type:markdown id:bb2aa49b tags:
#### 61. Find the nearest value from a given value in an array (★★☆)
%% Cell type:code id:8b62e59c tags:
```
```
%% Cell type:markdown id:1a950e1a tags:
#### 62. Considering two arrays with shape (1,3) and (3,1), how to compute their sum using an iterator? (★★☆)
%% Cell type:code id:cad1d01b tags:
```
```
%% Cell type:markdown id:165b3a20 tags:
#### 63. Create an array class that has a name attribute (★★☆)
%% Cell type:code id:666bf983 tags:
```
```
%% Cell type:markdown id:87f85b12 tags:
#### 64. Consider a given vector, how to add 1 to each element indexed by a second vector (be careful with repeated indices)? (★★★)
%% Cell type:code id:03ee4c88 tags:
```
```
%% Cell type:markdown id:1cda27c4 tags:
#### 65. How to accumulate elements of a vector (X) to an array (F) based on an index list (I)? (★★★)
%% Cell type:code id:aee2c782 tags:
```
```
%% Cell type:markdown id:1db205db tags:
#### 66. Considering a (w,h,3) image of (dtype=ubyte), compute the number of unique colors (★★☆)
%% Cell type:code id:43204f35 tags:
```
```
%% Cell type:markdown id:ebaf1222 tags:
#### 67. Considering a four dimensions array, how to get sum over the last two axis at once? (★★★)
%% Cell type:code id:aabbbab9 tags:
```
```
%% Cell type:markdown id:2dfb4033 tags:
#### 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? (★★★)
%% Cell type:code id:7a9c2287 tags:
```
```
%% Cell type:markdown id:7035c626 tags:
#### 69. How to get the diagonal of a dot product? (★★★)
%% Cell type:code id:f6af16c1 tags:
```
```
%% Cell type:markdown id:8f73f0e5 tags:
#### 70. Consider the vector [1, 2, 3, 4, 5], how to build a new vector with 3 consecutive zeros interleaved between each value? (★★★)
%% Cell type:code id:b0cabc11 tags:
```
```
%% Cell type:markdown id:bafaa79e tags:
#### 71. Consider an array of dimension (5,5,3), how to mulitply it by an array with dimensions (5,5)? (★★★)
%% Cell type:code id:d441e30b tags:
```
```
%% Cell type:markdown id:80a9ac82 tags:
#### 72. How to swap two rows of an array? (★★★)
%% Cell type:code id:1aee2791 tags:
```
```
%% Cell type:markdown id:5e53d68c tags:
#### 73. Consider a set of 10 triplets describing 10 triangles (with shared vertices), find the set of unique line segments composing all the triangles (★★★)
%% Cell type:code id:2f975322 tags:
```
```
%% Cell type:markdown id:f85da603 tags:
#### 74. Given a sorted array C that corresponds to a bincount, how to produce an array A such that np.bincount(A) == C? (★★★)
%% Cell type:code id:8c476a73 tags:
```
```
%% Cell type:markdown id:a2330e6c tags:
#### 75. How to compute averages using a sliding window over an array? (★★★)
%% Cell type:code id:35e5e9f5 tags:
```
```
%% Cell type:markdown id:87698de8 tags:
#### 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]) (★★★)
%% Cell type:code id:753763ed tags:
```
```
%% Cell type:markdown id:b8417b15 tags:
#### 77. How to negate a boolean, or to change the sign of a float inplace? (★★★)
%% Cell type:code id:3cad31b2 tags:
```
```
%% Cell type:markdown id:37a49831 tags:
#### 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])? (★★★)
%% Cell type:code id:df95dce8 tags:
```
```
%% Cell type:markdown id:f5ae032f tags:
#### 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])? (★★★)
%% Cell type:code id:f5ae2e9b tags:
```
```
%% Cell type:markdown id:6bc35ba2 tags:
#### 80. Consider an arbitrary array, write a function that extract a subpart with a fixed shape and centered on a given element (pad with a `fill` value when necessary) (★★★)
%% Cell type:code id:2ad8db75 tags:
```
```
%% Cell type:markdown id:d17818f7 tags:
#### 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]]? (★★★)
%% Cell type:code id:97deb72f tags:
```
```
%% Cell type:markdown id:67f526e6 tags:
#### 82. Compute a matrix rank (★★★)
%% Cell type:code id:d70bed5b tags:
```
```
%% Cell type:markdown id:0a3fb55f tags:
#### 83. How to find the most frequent value in an array?
%% Cell type:code id:b0647d07 tags:
```
```
%% Cell type:markdown id:cb0ac341 tags:
#### 84. Extract all the contiguous 3x3 blocks from a random 10x10 matrix (★★★)
%% Cell type:code id:91798923 tags:
```
```
%% Cell type:markdown id:c2a9da0d tags:
#### 85. Create a 2D array subclass such that Z[i,j] == Z[j,i] (★★★)
%% Cell type:code id:f9b027ff tags:
```
```
%% Cell type:markdown id:718ac868 tags:
#### 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)) (★★★)
%% Cell type:code id:4de05217 tags:
```
```
%% Cell type:markdown id:a6eda6f8 tags:
#### 87. Consider a 16x16 array, how to get the block-sum (block size is 4x4)? (★★★)
%% Cell type:code id:286b8db5 tags:
```
```
%% Cell type:markdown id:625aa611 tags:
#### 88. How to implement the Game of Life using numpy arrays? (★★★)
%% Cell type:code id:a01c1ff2 tags:
```
```
%% Cell type:markdown id:ca3dde4a tags:
#### 89. How to get the n largest values of an array (★★★)
%% Cell type:code id:5d14148c tags:
```
```
%% Cell type:markdown id:e233805e tags:
#### 90. Given an arbitrary number of vectors, build the cartesian product (every combinations of every item) (★★★)
%% Cell type:code id:17638e13 tags:
```
```
%% Cell type:markdown id:c163f115 tags:
#### 91. How to create a record array from a regular array? (★★★)
%% Cell type:code id:da402514 tags:
```
```
%% Cell type:markdown id:049c006e tags:
#### 92. Consider a large vector Z, compute Z to the power of 3 using 3 different methods (★★★)
%% Cell type:code id:96ce278a tags:
```
```
%% Cell type:markdown id:6e652635 tags:
#### 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? (★★★)
%% Cell type:code id:f429d6d4 tags:
```
```
%% Cell type:markdown id:33c4755d tags:
#### 94. Considering a 10x3 matrix, extract rows with unequal values (e.g. [2,2,3]) (★★★)
%% Cell type:code id:29507586 tags:
```
```
%% Cell type:markdown id:c9fe158d tags:
#### 95. Convert a vector of ints into a matrix binary representation (★★★)
%% Cell type:code id:58209fb7 tags:
```
```
%% Cell type:markdown id:f60086c9 tags:
#### 96. Given a two dimensional array, how to extract unique rows? (★★★)
%% Cell type:code id:787b6e4c tags:
```
```
%% Cell type:markdown id:0f5e1299 tags:
#### 97. Considering 2 vectors A & B, write the einsum equivalent of inner, outer, sum, and mul function (★★★)
%% Cell type:code id:cd2a8214 tags:
```
```
%% Cell type:markdown id:a04dd898 tags:
#### 98. Considering a path described by two vectors (X,Y), how to sample it using equidistant samples (★★★)?
%% Cell type:code id:12d9c30f tags:
```
```
%% Cell type:markdown id:b45bf2ae tags:
#### 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. (★★★)
%% Cell type:code id:f4179f4a tags:
```
```
%% Cell type:markdown id:d92b6592 tags:
#### 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). (★★★)
%% Cell type:code id:256ebe30 tags:
```
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment