Numpy:如何创建类似网格的数组?

时间:2015-09-21 02:23:33

标签: python numpy

我想创建一个代表X-Y面板的数组(-50,50)。那是: [[-50, -50], [-49,-50],[-48,-50]....[50,50]],长度为101 * 101。

显然,我可以通过double loop from (-50,50)生成。我想知道prefered这样做的方法吗?

6 个答案:

答案 0 :(得分:3)

numpy.meshgrid显然是对我来说最清晰的方式(正如@benbo所提到的),你需要再向ravelflatten 2D网格数组迈出一步:

In [131]: import numpy as np
     ...: x=np.linspace(-2, 2, 5)
     ...: y=np.linspace(-2, 2, 5)
     ...: xx,yy=np.meshgrid(x,y)
     ...: coords=np.array((xx.ravel(), yy.ravel())).T

In [132]: coords
Out[132]: 
array([[-2., -2.],
       [-1., -2.],
       [ 0., -2.],
       [ 1., -2.],
       [ 2., -2.],
       [-2., -1.],
       ......
       [ 1.,  2.],
       [ 2.,  2.]])

In [133]:

或者@John提到,用np.c_缩短你的代码以跳过转置:

coords=np.c_[xx.ravel(), yy.ravel()]

基准:

In [156]: %timeit coords=np.array((xx.ravel(), yy.ravel())).T
100000 loops, best of 3: 14.6 µs per loop

In [157]: %timeit coords=np.c_[xx.ravel(), yy.ravel()] #not as efficient as ↑
10000 loops, best of 3: 47.6 µs per loop

答案 1 :(得分:2)

这个怎么样:

In [15]: import numpy as np

In [16]: a = np.arange(-3,4)

In [17]: a1 = np.tile(a, (7,1))

In [18]: np.dstack((a1, a1.T)).reshape(-1, 2)

结果:

array([[-3, -3],
       [-2, -3],
       [-1, -3],
       [ 0, -3],
       [ 1, -3],
        ....
       [-1,  3],
       [ 0,  3],
       [ 1,  3],
       [ 2,  3],
       [ 3,  3]])

答案 2 :(得分:1)

我不是100%确定你希望你的输出看起来像什么。这对你有用吗?

import numpy as np
a=np.linspace(-2, 2, 5)
b=np.linspace(-2, 2, 5)
c,d=np.meshgrid(a,b)
c+d
>>> array([[-4., -3., -2., -1.,  0.],
   [-3., -2., -1.,  0.,  1.],
   [-2., -1.,  0.,  1.,  2.],
   [-1.,  0.,  1.,  2.,  3.],
   [ 0.,  1.,  2.,  3.,  4.]])

答案 3 :(得分:1)

>>> import numpy as np
>>> np.array( zip(range(-50,51), [-50] * 50 + [50] * 51) )
array([[-50, -50],
       [-49, -50],
       [-48, -50],
       .
       .
       .
       [ 48,  50],
       [ 49,  50],
       [ 50,  50]])

答案 4 :(得分:0)

它可能看起来像这样:

In [1]: coords = np.array([[_v, _v] for _v in range(-50, 51)])

In [2]: coords
Out[22]: 
array([[-50, -50],
       [-49, -49],
       [-48, -48],
       ...
       ...
       ...
       [ 48,  48],
       [ 49,  49],
       [ 50,  50]])

答案 5 :(得分:0)

很多答案!而另一个是基于numpy.indices

In [1458]: low = -50

In [1459]: high = 51

In [1460]: ndim = 2

In [1461]: coords = (np.indices((high-low,)*ndim) + low)[::-1].reshape(ndim, -1).T

In [1462]: coords
Out[1462]: 
array([[-50, -50],
       [-49, -50],
       [-48, -50],
       ..., 
       [ 48,  50],
       [ 49,  50],
       [ 50,  50]])

如果第一个坐标变化最快并不重要,可以删除[::-1]实现的重新排序:

In [1463]: coords = (np.indices((high-low,)*ndim) + low).reshape(ndim, -1).T

In [1464]: coords
Out[1464]: 
array([[-50, -50],
       [-50, -49],
       [-50, -48],
       ..., 
       [ 50,  48],
       [ 50,  49],
       [ 50,  50]])

使用ndim提供了一个无偿的功能;它允许以更高的维度生成类似的数组:

In [1465]: ndim = 3

In [1466]: coords = (np.indices((high-low,)*ndim) + low)[::-1].reshape(ndim, -1).T

In [1467]: coords
Out[1467]: 
array([[-50, -50, -50],
       [-49, -50, -50],
       [-48, -50, -50],
       ..., 
       [ 48,  50,  50],
       [ 49,  50,  50],
       [ 50,  50,  50]])