np.ogrid:
address:https://docs.scipy.org/doc/numpy/reference/generated/numpy.ogrid.html
returns an open (i.e. not fleshed out) mesh-grid when indexed, only one dimension of each returned array is greater than 1.
The dimension and number of the output arrays are equal to the number of indexing dimensions.
If the step length is not a complex number, then the stop is not inclusive.
if the step length is a complex number (e.g. 5j), then the integer part of its magnitude is interpreted as specifying the number of points to create between the start and stop values, where the stop value is inclusive.
上面几条翻译过来就是:
返回数组的维度只有一个大于1.
返回数组的个数和维度等于输入时索引维度的个数.
若步长不是复数,就不包含stop.
若步长是复数,其整数部分表示在start和stop之间创建的点数(start和stop也算),包含stop.
下面示例解释前2条:
1 a, b, c = np.ogrid[0:2, 0:2, 0:2] 2 print(a.shape, b.shape, c.shape) 3 print(a) 4 print(b) 5 print(c) 6 7 a, b, c, d = np.ogrid[0:2, 0:2, 0:2, 0:2] 8 print(a.shape, b.shape, c.shape, d.shape) 9 10 result: 11 (2, 1, 1) (1, 2, 1) (1, 1, 2) 12 [[[0]] 13 14 [[1]]] 15 [[[0] 16 [1]]] 17 [[[0 1]]] 18 (2, 1, 1, 1) (1, 2, 1, 1) (1, 1, 2, 1) (1, 1, 1, 2)
解释第3条:
1 a, b, c = np.ogrid[0:4:2, 0:5:3, 0:5:1] 2 print(a.shape, b.shape, c.shape) 3 print(a) 4 print(b) 5 print(c) 6 7 result: 8 (2, 1, 1) (1, 2, 1) (1, 1, 5) 9 [[[0]] 10 11 [[2]]] # 不包含stop 12 [[[0] 13 [3]]] 14 [[[0 1 2 3 4]]] # 不包含stop
解释第4条:
1 a, b, c = np.ogrid[0:4:3j, 0:5:3j, 0:5:4j] 2 print(a.shape, b.shape, c.shape) 3 print(a) 4 print(b) 5 print(c) 6 7 result: 8 (3, 1, 1) (1, 3, 1) (1, 1, 4) 9 [[[0.]] 10 11 [[2.]] 12 13 [[4.]]] # 包含stop 14 [[[0. ] 15 [2.5] 16 [5. ]]] 17 [[[0. 1.66666667 3.33333333 5. ]]] # 包含stop
np.mgrid:
address: https://docs.scipy.org/doc/numpy/reference/generated/numpy.mgrid.html
除第1条不同外,其他3条完全一样:
returns an dense (or fleshed out) mesh-grid when indexed, each returned argument has the same shape.
1 N = 100 2 X, Y = np.mgrid[-3:3:complex(0, N), -2:2:complex(0, N)] 3 print(X.shape, Y.shape) # (100, 100) (100, 100) 4 # X是每行都相等,每列递增;Y是每行都相等,每列递增 5 print(X) 6 # [[-3. -3. -3. ... -3. -3. 7 # -3. ] 8 # [-2.93939394 -2.93939394 -2.93939394 ... -2.93939394 -2.93939394 9 # -2.93939394] 10 # [-2.87878788 -2.87878788 -2.87878788 ... -2.87878788 -2.87878788 11 # -2.87878788] 12 # ... 13 # [ 2.87878788 2.87878788 2.87878788 ... 2.87878788 2.87878788 14 # 2.87878788] 15 # [ 2.93939394 2.93939394 2.93939394 ... 2.93939394 2.93939394 16 # 2.93939394] 17 # [ 3. 3. 3. ... 3. 3. 18 # 3. ]] 19 print(Y) 20 # [[-2. -1.95959596 -1.91919192 ... 1.91919192 1.95959596 21 # 2. ] 22 # ... 23 # [-2. -1.95959596 -1.91919192 ... 1.91919192 1.95959596 24 # 2. ]]
原文地址:https://www.cnblogs.com/yangxiaoling/p/10223875.html