np.mgrid的含义及reshape(2,-1)等
np.mgrid
介于网上很多教程只是单纯说用法,而对其意思解释不清楚,因此写了该释义。
np.mgrid中含有grid(网格),顾名思义,功效与网格相关。先看官方解释(废话一堆 ):
help(np.mgrid)
Help on nd_grid in module numpy.lib.index_tricks object:
class nd_grid(builtins.object)
| Construct a multi-dimensional "meshgrid".
|
| ``grid = nd_grid()`` creates an instance which will return a mesh-grid
| when indexed. 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.
|
| However, 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**.
|
| If instantiated with an argument of ``sparse=True``, the mesh-grid is
| open (or not fleshed out) so that only one-dimension of each returned
| argument is greater than 1.
|
| Parameters
| ----------
| sparse : bool, optional
| Whether the grid is sparse or not. Default is False.
|
| Notes
| -----
| Two instances of `nd_grid` are made available in the NumPy namespace,
| `mgrid` and `ogrid`::
|
| mgrid = nd_grid(sparse=False)
| ogrid = nd_grid(sparse=True)
|
| Users should use these pre-defined instances instead of using `nd_grid`
| directly.
|
| Examples
| --------
| >>> mgrid = np.lib.index_tricks.nd_grid()
| >>> mgrid[0:5,0:5]
| array([[[0, 0, 0, 0, 0],
| [1, 1, 1, 1, 1],
| [2, 2, 2, 2, 2],
| [3, 3, 3, 3, 3],
| [4, 4, 4, 4, 4]],
| [[0, 1, 2, 3, 4],
| [0, 1, 2, 3, 4],
| [0, 1, 2, 3, 4],
| [0, 1, 2, 3, 4],
| [0, 1, 2, 3, 4]]])
| >>> mgrid[-1:1:5j]
| array([-1. , -0.5, 0. , 0.5, 1. ])
|
| >>> ogrid = np.lib.index_tricks.nd_grid(sparse=True)
| >>> ogrid[0:5,0:5]
| [array([[0],
| [1],
| [2],
| [3],
| [4]]), array([[0, 1, 2, 3, 4]])]
|
| Methods defined here:
|
| __getitem__(self, key)
|
| __init__(self, sparse=False)
| Initialize self. See help(type(self)) for accurate signature.
|
| __len__(self)
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
常用的格式是
b, a = np.mgrid[start1:end1:step1, start2:end2:step2]
个人理解创造两个二维数组,每个数组的大小相同(行数由start1:end1:step1决定,列数由start2:end2:step2决定)。其中第一个二维数组b是按列存储,即从start1到end1,然后第二列start1到end1,第三列…;第二个二维数组a是按行存储,第一行从start2到end2,第二行重复…
演示结果:
In[3]: c = np.mgrid[0:5,0:6]
In[4]: c
Out[4]:
array([[[0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1],
[2, 2, 2, 2, 2, 2],
[3, 3, 3, 3, 3, 3],
[4, 4, 4, 4, 4, 4]],
[[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5],
[0, 1, 2, 3, 4, 5]]])
In[5]: b,a = c
reshape( )可以塑形,常见到-1,其作用是当不知道某一维有多少元素时,可先用-1指定,最后由其他维自动计算出来。
注意:-1不代表切片中的倒序
加到上面的b,a中,可以产生一个常用效果:
b,a = c.reshape(2,-1)#第一个位置对应行数2,列数由自动计算。reshape(-1,2,2)变为?行2列3页的数据。
输出b,a
b: array([0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4])
a: array([0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5])
此时对于一个5行6列(参考c数组),根据b,a值可以按顺序取完5行6列数组(假设为matrix)的所有数据。如matrix第16个元素的横坐标为b的第16个元素的值,纵坐标为a的第16个元素的值。
In[16]: matrix = np.random.randint(low=0,high=100,size=[5,6])
In[17]: matrix
Out[17]:
array([[94, 16, 32, 50, 99, 92],
[23, 52, 99, 0, 37, 54],
[76, 10, 99, 40, 14, 94],
[71, 3, 85, 78, 35, 94],
[70, 14, 65, 82, 85, 2]])
In[21]: matrix[b[16]][a[16]]
Out[21]: 14