Numpy: How to create a grid-like array?

I want to create an array representing the XY panel (-50, 50). That is: [[-50, -50], [-49,-50],[-48,-50]....[50,50]]whose length is 101 * 101.

Clearly, I can generate through a double loop from (-50,50). I am wondering how to do this prefered?

+4
source share
6 answers

numpy.meshgridis obviously the clearest way for me (as @benbo mentioned), you need one more step for ravelor an flattenarray of 2D mesh:

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

Or, as @John said, shorten your code with np.c_to skip transposing:

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

For comparison:

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
+3
source

How about this:

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)

Result:

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

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.]])
+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]])
+1

, :

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

If it is not important that the first coordinate changes faster, the reordering achieved with [::-1]can be removed:

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

Use ndimprovides a free feature; it allows you to create a similar array in higher dimensions:

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]])
0
source

Source: https://habr.com/ru/post/1608251/


All Articles