标签:value red 运行 choice pos iter mes atl create
练习篇(Part 5)
51. Create a structured array representing a position (x,y) and a color (r,g,b) (★★☆)
1 arr = np.zeros(10,[(‘position‘,[(‘x‘,float,1),(‘y‘,float,1)]),
2 (‘color‘,[(‘r‘,float,1),(‘g‘,float,1),(‘b‘,float,1)])])
3 print(arr)
运行结果:
[((0., 0.), (0., 0., 0.)) ((0., 0.), (0., 0., 0.))
((0., 0.), (0., 0., 0.)) ((0., 0.), (0., 0., 0.))
((0., 0.), (0., 0., 0.)) ((0., 0.), (0., 0., 0.))
((0., 0.), (0., 0., 0.)) ((0., 0.), (0., 0., 0.))
((0., 0.), (0., 0., 0.)) ((0., 0.), (0., 0., 0.))]
52. Consider a random vector with shape (100,2) representing coordinates, find point by point distances (★★☆)
1 arr = np.random.randint(1,4,(100,2))
2 x,y = np.atleast_2d(arr[:,0],arr[:,1])
3 print(arr)
4 dist = np.sqrt((x-x.T)**2+(y-y.T)**2)
5 print(dist)
运行结果:(太长)略
53. How to convert a float (32 bits) array into an integer (32 bits) in place?
1 arr = np.arange(10,dtype = np.float)
2 arr = arr.astype(np.int32)
3 print(arr)
运行结果:[0 1 2 3 4 5 6 7 8 9]
54. How to read the following file? (★★☆)
1,2,3,4,5
6, , ,7,8
, ,9,10,11
1 from io import StringIO
2 s = StringIO("""1, 2, 3, 4, 5\n
3 6, , , 7, 8\n
4 , , 9,10,11\n""")
5 arr = np.genfromtxt(s,delimiter=",",dtype=np.int)
6 print(arr)
运行结果:
[[ 1 2 3 4 5]
[ 6 -1 -1 7 8]
[-1 -1 9 10 11]]
55. What is the equivalent of enumerate for numpy arrays? (★★☆)
1 arr = np.arange(9).reshape(3,3)
2 for index, value in np.ndenumerate(arr):
3 print(index, value)
4 for index in np.ndindex(arr.shape):
5 print(index, arr[index])
运行结果:
(0, 0) 0
(0, 1) 1
(0, 2) 2
(1, 0) 3
(1, 1) 4
(1, 2) 5
(2, 0) 6
(2, 1) 7
(2, 2) 8
(0, 0) 0
(0, 1) 1
(0, 2) 2
(1, 0) 3
(1, 1) 4
(1, 2) 5
(2, 0) 6
(2, 1) 7
(2, 2) 8
56. Generate a generic 2D Gaussian-like array (★★☆)
1 x,y = np.meshgrid(np.linspace(-1,1,10),np.linspace(-1,1,10))
2 d = np.sqrt(x*x+y*y)
3 sigma,mu = 1.0,0.0
4 g = np.exp(-(d-mu)**2/(2.0*sigma**2))
5 print(g)
运行结果:(太长)略
57. How to randomly place p elements in a 2D array? (★★☆)
1 arr = np.zeros((10,10))
2 np.put(arr,np.random.choice(range(10*10),3,replace=False),25)
3 print(arr)
运行结果:
[[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 25. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 25. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 25. 0. 0. 0. 0. 0. 0. 0. 0.]]
58. Subtract the mean of each row of a matrix (★★☆)
1 arr = np.random.randint(1,5,(5,5))
2 arr2 = arr - arr.mean(axis=1,keepdims=True)
3 print(arr)
4 print(arr2)
运行结果:
[[3 1 4 2 2]
[1 1 1 4 4]
[1 3 2 4 2]
[4 4 1 4 3]
[3 1 3 2 1]]
[[ 0.6 -1.4 1.6 -0.4 -0.4]
[-1.2 -1.2 -1.2 1.8 1.8]
[-1.4 0.6 -0.4 1.6 -0.4]
[ 0.8 0.8 -2.2 0.8 -0.2]
[ 1. -1. 1. 0. -1. ]]
59. How to sort an array by the nth column? (★★☆)
1 arr = np.random.randint(1,10,(3,3))
2 print(arr)
3 arr = arr[arr[:,0].argsort()]
4 print(arr)
运行结果:
[[3 6 4]
[8 6 1]
[9 1 9]]
[[3 6 4]
[8 6 1]
[9 1 9]]
60. How to tell if a given 2D array has null columns? (★★☆)
1 arr = np.random.randint(0,6,(3,3))
2 print(arr)
3 print((~arr.any(axis=1)).any())
运行结果:
[[5 4 1]
[2 4 0]
[2 4 3]]
False
标签:value red 运行 choice pos iter mes atl create
原文地址:https://www.cnblogs.com/orangecyh/p/11614312.html