练习篇(Part 2)
11. Create a 3x3 identity matrix (★☆☆)
1 arr = np.eye(3)
2 print(arr)
运行结果:[[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]]
12. Create a 3x3x3 array with random values (★☆☆)
1 arr = np.random.random((3,3,3))
2 print(arr)
运行结果:略
13. Create a 10x10 array with random values and find the minimum and maximum values (★☆☆)
1 arr = np.random.random((10,10))
2 print('max:'+str(arr.max()))
3 print('min:'+str(arr.min()))
运行结果:
max:0.9966220981691146
min:0.0034603079973672957
14. Create a random vector of size 30 and find the mean value (★☆☆)
1 arr = np.random.random(30)
2 print(arr.mean())
运行结果:0.49710820465862965
15. Create a 2d array with 1 on the border and 0 inside (★☆☆)
1 arr = np.ones((10,10))
2 arr[1:9,1:9] = 0
3 print(arr)
运行结果:
[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]]
16. How to add a border (filled with 0's) around an existing array? (★☆☆)
1 arr = np.ones((10,10))
2 arr = np.pad(arr, pad_width=1, mode='constant', constant_values=0)
3 print(arr)
运行结果:
[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0.]
[0. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
17. What is the result of the following expression? (★☆☆)
1 0 * np.nan
2 np.nan == np.nan
3 np.inf > np.nan
4 np.nan - np.nan
5 np.nan in set([np.nan])
6 0.3 == 3 * 0.1
1 print(0 * np.nan)
2 print(np.nan == np.nan)
3 print(np.inf > np.nan)
4 print(np.nan - np.nan)
5 print(np.nan in set([np.nan]))
6 print(0.3 == 3 * 0.1)
运行结果:nan False False nan True False
18. Create a 5x5 matrix with values 1,2,3,4 just below the diagonal (★☆☆)
1 arr = np.diag(1+np.arange(4),k=-1)
2 print(arr)
运行结果:
[[0 0 0 0 0]
[1 0 0 0 0]
[0 2 0 0 0]
[0 0 3 0 0]
[0 0 0 4 0]]
19. Create a 8x8 matrix and fill it with a checkerboard pattern (★☆☆)
1 arr = np.zeros((8,8))
2 arr[0::2,0::2]=1
3 arr[1::2,1::2]=1
4 print(arr)
运行结果:
[[1. 0. 1. 0. 1. 0. 1. 0.]
[0. 1. 0. 1. 0. 1. 0. 1.]
[1. 0. 1. 0. 1. 0. 1. 0.]
[0. 1. 0. 1. 0. 1. 0. 1.]
[1. 0. 1. 0. 1. 0. 1. 0.]
[0. 1. 0. 1. 0. 1. 0. 1.]
[1. 0. 1. 0. 1. 0. 1. 0.]
[0. 1. 0. 1. 0. 1. 0. 1.]]
20. Consider a (6,7,8) shape array, what is the index (x,y,z) of the 100th element?
1 print(np.unravel_index(99,(6,7,8)))
运行结果:(1, 5, 3)
21. Create a checkerboard 8x8 matrix using the tile function (★☆☆)
1 arr = np.tile(np.array([[0,1],[1,0]]),(4,4))
2 print(arr)
运行结果:
[[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]]
22. Normalize a 5x5 random matrix (★☆☆)
1 arr = np.random.random((5,5))
2 arr = (arr - np.min(arr))/(np.max(arr) - np.min(arr))
3 print(arr)
运行结果:
[[0.25182827 1. 0. 0.08239415 0.28511849]
[0.10300901 0.52930264 0.95743154 0.84571053 0.50581171]
[0.82070737 0.5720979 0.91581986 0.16283325 0.27075288]
[0.91480517 0.40637193 0.7032704 0.15695137 0.79951099]
[0.18035443 0.50388197 0.48436665 0.9828424 0.02296698]]
23. Create a custom dtype that describes a color as four unsigned bytes (RGBA) (★☆☆)
1 color = np.dtype([
2 ("r", np.ubyte, 1),
3 ("g", np.ubyte, 1),
4 ("b", np.ubyte, 1),
5 ("a", np.ubyte, 1)])
6 print(color)
运行结果:[('r', 'u1'), ('g', 'u1'), ('b', 'u1'), ('a', 'u1')]
24. Multiply a 5x3 matrix by a 3x2 matrix (real matrix product) (★☆☆)
1 arr1 = np.random.random((5,3))
2 arr2 = np.random.random((3,2))
3 arr3 = np.dot(arr1,arr2)
4 print(arr1)
5 print(arr2)
6 print(arr3)
运行结果:
[[0.51143733 0.22531681 0.71917393]
[0.81667839 0.1703277 0.1519645 ]
[0.15206992 0.370845 0.62943418]
[0.33517061 0.6224698 0.46285361]
[0.09370654 0.04592576 0.52768143]]
[[0.0785262 0.25563589]
[0.04088907 0.04818778]
[0.82035168 0.40391289]]
[[0.63934976 0.43208287]
[0.19575952 0.27836044]
[0.54346236 0.3109813 ]
[0.43147462 0.30262961]
[0.44212063 0.23930515]]
25. Given a 1D array, negate all elements which are between 3 and 8, in place. (★☆☆)
1 arr = np.random.randint(1,10,10)
2 print(arr)
3 arr[(arr >=3) & (arr <= 8)] = 0
4 print(arr)
运行结果:
1 print(sum(range(5),-1))
2 from numpy import *
3 print(sum(range(5),-1))
1 print(sum(range(5),-1)) #-1表示起始
2 from numpy import *
3 print(sum(range(5),-1)) #-1表示轴向
运行结果:9 10
27. Consider an integer vector Z, which of these expressions are legal? (★☆☆)
1 Z**Z
2 2 << Z >> 2
3 Z <- Z
4 1j*Z
5 Z/1/1
6 Z<Z>Z
1 z = np.random.randint(1,4,(5,5))
2 print(z)
3 print(z**z)
4 print(2<<z>>2)
5 print(z < -z)
6 print(1j*z) #1j表示复数的i
7 print(z/1/1)
8 print(z<z>z)
运行结果:略(只有最后一个会出错)
28. What are the result of the following expressions?
1 np.array(0) / np.array(0)
2 np.array(0) // np.array(0)
3 np.array([np.nan]).astype(int).astype(float)
运行结果:nan 0 [-2.14748365e+09]
29. How to round away from zero a float array ? (★☆☆)
1 arr = np.random.random(5)*100
2 arr1 = np.trunc(arr+0.5)
3 print(arr)
4 print(arr1)
运行结果:
[69.37662327 99.0777984 93.6006897 45.79127547 51.93021804]
[69. 99. 94. 46. 52.]
30. How to find common values between two arrays? (★☆☆)
1 arr1 = np.random.randint(1,5,10)
2 arr2 = np.random.randint(3,10,10)
3 print(arr1)
4 print(arr2)
5 print(np.intersect1d(arr1,arr2))
运行结果:
[1 2 4 3 4 4 2 4 1 1]
[7 5 4 3 9 9 8 8 8 9]
[3 4]