练习篇(Part 4)
41. How to sum a small array faster than np.sum? (★★☆)
1 arr = np.arange(10)
2 print(np.add.reduce(arr))
运行结果:45
42. Consider two random array A and B, check if they are equal (★★☆)
1 arr1 = np.random.randint(0,2,4).reshape(2,2)
2 arr2 = np.random.randint(0,2,4).reshape(2,2)
3 print(arr1)
4 print(arr2)
5 print(np.allclose(arr1,arr2))
6 print(np.array_equal(arr1,arr2))
运行结果:
[[0 1]
[1 1]]
[[1 0]
[1 1]]
False
False
43. Make an array immutable (read-only) (★★☆)
1 arr = np.random.randint(1,2,(3,3))
2 arr.flags.writeable = False
3 arr[0][0] = 1
运行结果:
44. Consider a random 10x2 matrix representing cartesian coordinates, convert them to polar coordinates (★★☆)
1 arr = np.random.randint(1,10,(10,2))
2 x = arr[:,0]
3 y = arr[:,1]
4 R = np.sqrt(x**2+y**2)
5 T = np.arctan2(y,x)
6 print(R)
7 print(T)
运行结果:
[ 8.94427191 9.21954446 9.89949494 10.63014581 7.07106781 9.21954446
8.94427191 9.05538514 8.60232527 11.3137085 ]
[0.46364761 0.21866895 0.78539816 0.71883 0.78539816 0.86217005
1.10714872 0.11065722 0.62024949 0.78539816]
45. Create random vector of size 10 and replace the maximum value by 0 (★★☆)
1 arr = np.random.randint(1,10,10)
2 print(arr)
3 arr[arr.argmax()] = 0
4 print(arr)
运行结果:
[3 4 7 9 4 2 4 4 2 8]
[3 4 7 0 4 2 4 4 2 8]
46. Create a structured array with x
and y
coordinates covering the [0,1]x[0,1] area (★★☆)
1 arr = np.zeros((5,5),[('x',float),('y',float)])
2 arr['x'],arr['y'] = np.meshgrid(np.linspace(0,1,5),np.linspace(0,1,5))
3 print(arr)
运行结果:
[[(0. , 0. ) (0.25, 0. ) (0.5 , 0. ) (0.75, 0. ) (1. , 0. )]
[(0. , 0.25) (0.25, 0.25) (0.5 , 0.25) (0.75, 0.25) (1. , 0.25)]
[(0. , 0.5 ) (0.25, 0.5 ) (0.5 , 0.5 ) (0.75, 0.5 ) (1. , 0.5 )]
[(0. , 0.75) (0.25, 0.75) (0.5 , 0.75) (0.75, 0.75) (1. , 0.75)]
[(0. , 1. ) (0.25, 1. ) (0.5 , 1. ) (0.75, 1. ) (1. , 1. )]]
47. Given two arrays, X and Y, construct the Cauchy matrix C (Cij =1/(xi - yj))
1 arr1 = np.random.randint(5,10,5)
2 arr2 = np.random.randint(1,5,5)
3 print(arr1)
4 print(arr2)
5 arr3 = 1.0/np.subtract.outer(arr1,arr2)
6 print(arr3)
运行结果:
[9 9 5 6 7]
[2 3 1 4 1]
[[0.14285714 0.16666667 0.125 0.2 0.125 ]
[0.14285714 0.16666667 0.125 0.2 0.125 ]
[0.33333333 0.5 0.25 1. 0.25 ]
[0.25 0.33333333 0.2 0.5 0.2 ]
[0.2 0.25 0.16666667 0.33333333 0.16666667]]
48. Print the minimum and maximum representable value for each numpy scalar type (★★☆)
1 for dtype in [np.int8, np.int32, np.int64]:
2 print(np.iinfo(dtype).min)
3 print(np.iinfo(dtype).max)
4 for dtype in [np.float32, np.float64]:
5 print(np.finfo(dtype).min)
6 print(np.finfo(dtype).max)
运行结果:
-128
127
-2147483648
2147483647
-9223372036854775808
9223372036854775807
-3.4028235e+38
3.4028235e+38
-1.7976931348623157e+308
1.7976931348623157e+308
49. How to print all the values of an array? (★★☆)
1 arr = np.random.randint(1,10,9).reshape(3,3)
2 print(arr)
运行结果:
[[5 3 4]
[9 2 9]
[6 6 4]]
50. How to find the closest value (to a given scalar) in a vector? (★★☆)
1 arr1 = np.arange(100)
2 arr2 = np.random.uniform(0,100)
3 index = (np.abs(arr1-arr2)).argmin()
4 print(arr1)
5 print(arr2)
6 print(arr1[index])
运行结果:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
96 97 98 99]
46.27162981393338
46