#!/usr/bin/env python # coding: utf-8 # ## Adapted from : # # ### 100 numpy exercises # # This is a collection of exercises that have been collected in the numpy mailing list, on stack overflow and in the numpy documentation. The goal of this collection is to offer a quick reference for both old and new users but also to provide a set of exercises for those who teach. # # # If you find an error or think you've a better way to solve some of them, feel free to open an issue at # #### 1. Import the numpy package under the name `np` (★☆☆) # In[ ]: # #### 2. Print the numpy version and the configuration (★☆☆) # In[ ]: # #### 3. Create a null vector of size 10 (★☆☆) # In[ ]: # #### 6. Create a null vector of size 10 but the fifth value which is 1 (★☆☆) # In[ ]: # #### 7. Create a vector with values ranging from 10 to 49 (★☆☆) # In[ ]: # #### 8. Reverse a vector (first element becomes last) (★☆☆) # In[ ]: # #### 9. Create a 3x3 matrix with values ranging from 0 to 8 (★☆☆) # In[ ]: # #### 10. Find indices of non-zero elements from \[1,2,0,0,4,0\] (★☆☆) # In[ ]: # #### 11. Create a 3x3 identity matrix (★☆☆) # In[ ]: # #### 12. Create a 3x3x3 array with random values (★☆☆) # In[ ]: # #### 13. Create a 10x10 array with random values and find the minimum and maximum values (★☆☆) # In[ ]: # #### 14. Create a random vector of size 30 and find the mean value (★☆☆) # In[ ]: # #### 15. Create a 2d array with 1 on the border and 0 inside (★☆☆) # In[ ]: # #### 16. How to add a border (filled with 0's) around an existing array? (★☆☆) # In[ ]: # #### 18. Create a 5x5 matrix with values 1,2,3,4 just below the diagonal (★☆☆) # In[ ]: # #### 19. Create a 8x8 matrix and fill it with a checkerboard pattern (★☆☆) # In[ ]: # #### 20. Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element? # In[ ]: # #### 22. Normalize a 5x5 random matrix (★☆☆) # In[ ]: # #### 24. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) (★☆☆) # In[ ]: # #### 25. Given a 1D array, negate all elements which are between 3 and 8, in place. (★☆☆) # In[ ]: # #### 26. What is the output of the following script? (★☆☆) # In[ ]: # Author: Jake VanderPlas print(sum(range(5),-1)) from numpy import * print(sum(range(5),-1)) # #### 27. Consider an integer vector Z, which of these expressions are legal? (★☆☆) # In[ ]: Z**Z 2 << Z >> 2 Z <- Z 1j*Z Z/1/1 ZZ # #### 28. What are the result of the following expressions? # In[ ]: print(np.array(0) / np.array(0)) print(np.array(0) // np.array(0)) print(np.array([np.nan]).astype(int).astype(float)) # #### 29. How to round away from zero a float array ? (★☆☆) # In[ ]: # #### 30. How to find common values between two arrays? (★☆☆) # In[ ]: # #### 36. Extract the integer part of a random array using 5 different methods (★★☆) # In[ ]: # #### 37. Create a 5x5 matrix with row values ranging from 0 to 4 (★★☆) # In[ ]: # #### 39. Create a vector of size 10 with values ranging from 0 to 1, both excluded (★★☆) # In[ ]: # #### 40. Create a random vector of size 10 and sort it (★★☆) # In[ ]: # #### 42. Consider two random array A and B, check if they are equal (★★☆) # In[ ]: # #### 45. Create random vector of size 10 and replace the maximum value by 0 (★★☆) # In[ ]: # #### 46. Create a structured array with `x` and `y` coordinates covering the \[0,1\]x\[0,1\] area (★★☆) # In[ ]: # #### 48. Print the minimum and maximum representable value for each numpy scalar type (★★☆) # In[ ]: # #### 50. How to find the closest value (to a given scalar) in a vector? (★★☆) # In[ ]: # #### 58. Subtract the mean of each row of a matrix (★★☆) # In[ ]: # #### 61. Find the nearest value from a given value in an array (★★☆) # In[ ]: