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]])
  • 相关阅读:
    cef加载flash的办法
    一个高性能的对象属性复制类,支持不同类型对象间复制,支持Nullable<T>类型属性
    php检测php.ini是否配制正确
    openwrt的路由器重置root密码
    windows 7 + vs2010 sp1编译 x64位版qt4
    解决SourceGrid在某些系统上无法用鼠标滚轮滚动的问题
    判断一个点是否在多边形内部,射线法思路,C#实现
    [转载]使用HttpWebRequest进行请求时发生错误:基础连接已关闭,发送时发生错误处理
    让Dapper+SqlCE支持ntext数据类型和超过4000字符的存储
    通过WMI
  • 原文地址:https://www.cnblogs.com/yangxiaoling/p/10223875.html
Copyright © 2011-2022 走看看