zoukankan      html  css  js  c++  java
  • np.mgird np.ogrid

    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.        ]]

    np.meshgrid: 

    numpy.meshgrid(x, y)

    Return coordinate matrices from two coordinate vectors.

    参数:  x, y: Two 1-D arrays representing the x and y coordinates of a grid.

    返回:  X, Y: For vectors x, y with lengths Nx=len(x) and Ny=len(y), return X, Y where X and Y are (Ny, Nx) shaped arrays with the elements of x and y repeated to fill the matrix along the first dimension for x, the second for y.  # 返回两个shape为 (Ny, Nx) 的数组,其中第1个数组用x填充,第2个数组用y填充。

    meshgrid is very useful to evaluate functions on a grid.

     1 >>> X, Y = np.meshgrid([1,2,3], [4,5,6,7])
     2 >>> X
     3 array([[1, 2, 3],
     4        [1, 2, 3],
     5        [1, 2, 3],
     6        [1, 2, 3]])
     7 >>> Y
     8 array([[4, 4, 4],
     9        [5, 5, 5],
    10        [6, 6, 6],
    11        [7, 7, 7]])
  • 相关阅读:
    集合的整体
    StringBuffer类中的东西
    ChickHouse安装介绍
    Flink集群搭建
    hadoop-MapReduce总结
    hadoop-hdfs
    linux命令总结
    linux
    shall 2-13
    String 类的其他功能
  • 原文地址:https://www.cnblogs.com/yangxiaoling/p/10223875.html
Copyright © 2011-2022 走看看