numpy中是否有多维版本的范围/ linspace?

时间:2021-05-21 21:25:31

I would like a list of 2d numpy arrays (x,y) , where each x is in {-5, -4.5, -4, -3.5, ..., 3.5, 4, 4.5, 5} and the same for y.

我想要一个2d numpy数组(x,y)的列表,其中每个x在{-5,-4.5,-4,-3.5,...,3.5,4,4.5,5}中,并且y相同。

I could do

我可以

x = np.arange(-5, 5.1, 0.5)
y = np.arange(-5, 5.1, 0.5)

and then iterate through all possible pairs, but I'm sure there's a nicer way...

然后迭代所有可能的对,但我确信有一个更好的方式......

I would like something back that looks like:

我想回复一下,看起来像:

[[-5, -5],
 [-5, -4.5],
 [-5, -4],
 ...
 [5, 5]]

but the order does not matter.

但顺序并不重要。

5 个解决方案

#1


30  

You can use np.mgrid for this, it's often more convenient than np.meshgrid because it creates the arrays in one step:

你可以使用np.mgrid,它通常比np.meshgrid更方便,因为它只需一步创建数组:

import numpy as np
X,Y = np.mgrid[-5:5.1:0.5, -5:5.1:0.5]

For linspace-like functionality, replace the step (i.e. 0.5) with a complex number whose magnitude specifies the number of points you want in the series. Using this syntax, the same arrays as above are specified as:

对于类似linspace的功能,将步数(即0.5)替换为复数,其大小指定系列中所需的点数。使用此语法,将上述相同的数组指定为:

X, Y = np.mgrid[-5:5:21j, -5:5:21j]

You can then create your pairs as:

然后,您可以创建对:

xy = np.vstack((X.flatten(), Y.flatten())).T

As @ali_m suggested, this can all be done in one line:

正如@ali_m建议的那样,这一切都可以在一行中完成:

xy = np.mgrid[-5:5.1:0.5, -5:5.1:0.5].reshape(2,-1).T

Best of luck!

祝你好运!

#2


5  

I think you want np.meshgrid:

我想你想要np.meshgrid:

Return coordinate matrices from coordinate vectors.

从坐标向量返回坐标矩阵。

Make N-D coordinate arrays for vectorized evaluations of N-D scalar/vector fields over N-D grids, given one-dimensional coordinate arrays x1, x2,..., xn.

在给定一维坐标阵列x1,x2,...,xn的情况下,为N-D网格上的N-D标量/矢量场的矢量化评估制作N-D坐标数组。

import numpy as np
x = np.arange(-5, 5.1, 0.5)
y = np.arange(-5, 5.1, 0.5)
X,Y = np.meshgrid(x,y)

you can convert that to your desired output with

您可以将其转换为所需的输出

XY=np.array([X.flatten(),Y.flatten()]).T

print XY
array([[-5. , -5. ],
       [-4.5, -5. ],
       [-4. , -5. ],
       [-3.5, -5. ],
       [-3. , -5. ],
       [-2.5, -5. ],
       ....
       [ 3. ,  5. ],
       [ 3.5,  5. ],
       [ 4. ,  5. ],
       [ 4.5,  5. ],
       [ 5. ,  5. ]])

#3


2  

If you just want to iterate through pairs (and not do calculations on the whole set of points at once), you may be best served by itertools.product to iterate through all possible pairs:

如果你只想迭代对(而不是一次对整个点集进行计算),itertools.product可能最好服务于迭代所有可能的对:

import itertools

for (xi, yi) in itertools.product(x, y):
    print(xi, yi)

This avoids generating large matrices via meshgrid.

这避免了通过meshgrid生成大型矩阵。

#4


0  

Not sure if I understand the question - to make a list of 2-element NumPy arrays, this works:

不确定我是否理解这个问题 - 要制作一个2元素NumPy数组的列表,这可行:

import numpy as np
x = np.arange(-5, 5.1, 0.5)
X, Y = np.meshgrid(x, x)
Liszt = [np.array(thing) for thing in zip(X.flatten(), Y.flatten())] # for python 2.7

zip gives you a list of tuples, and the list comprehension does the rest.

zip为您提供元组列表,其余的列表理解。

#5


0  

We can use arrange function as:

我们可以使用安排功能:

z1 = np.array([np.array(np.arange(1,5)),np.array(np.arange(1,5))])
print(z1)
o/p=> [[1 2 3 4]
       [1 2 3 4]]

#1


30  

You can use np.mgrid for this, it's often more convenient than np.meshgrid because it creates the arrays in one step:

你可以使用np.mgrid,它通常比np.meshgrid更方便,因为它只需一步创建数组:

import numpy as np
X,Y = np.mgrid[-5:5.1:0.5, -5:5.1:0.5]

For linspace-like functionality, replace the step (i.e. 0.5) with a complex number whose magnitude specifies the number of points you want in the series. Using this syntax, the same arrays as above are specified as:

对于类似linspace的功能,将步数(即0.5)替换为复数,其大小指定系列中所需的点数。使用此语法,将上述相同的数组指定为:

X, Y = np.mgrid[-5:5:21j, -5:5:21j]

You can then create your pairs as:

然后,您可以创建对:

xy = np.vstack((X.flatten(), Y.flatten())).T

As @ali_m suggested, this can all be done in one line:

正如@ali_m建议的那样,这一切都可以在一行中完成:

xy = np.mgrid[-5:5.1:0.5, -5:5.1:0.5].reshape(2,-1).T

Best of luck!

祝你好运!

#2


5  

I think you want np.meshgrid:

我想你想要np.meshgrid:

Return coordinate matrices from coordinate vectors.

从坐标向量返回坐标矩阵。

Make N-D coordinate arrays for vectorized evaluations of N-D scalar/vector fields over N-D grids, given one-dimensional coordinate arrays x1, x2,..., xn.

在给定一维坐标阵列x1,x2,...,xn的情况下,为N-D网格上的N-D标量/矢量场的矢量化评估制作N-D坐标数组。

import numpy as np
x = np.arange(-5, 5.1, 0.5)
y = np.arange(-5, 5.1, 0.5)
X,Y = np.meshgrid(x,y)

you can convert that to your desired output with

您可以将其转换为所需的输出

XY=np.array([X.flatten(),Y.flatten()]).T

print XY
array([[-5. , -5. ],
       [-4.5, -5. ],
       [-4. , -5. ],
       [-3.5, -5. ],
       [-3. , -5. ],
       [-2.5, -5. ],
       ....
       [ 3. ,  5. ],
       [ 3.5,  5. ],
       [ 4. ,  5. ],
       [ 4.5,  5. ],
       [ 5. ,  5. ]])

#3


2  

If you just want to iterate through pairs (and not do calculations on the whole set of points at once), you may be best served by itertools.product to iterate through all possible pairs:

如果你只想迭代对(而不是一次对整个点集进行计算),itertools.product可能最好服务于迭代所有可能的对:

import itertools

for (xi, yi) in itertools.product(x, y):
    print(xi, yi)

This avoids generating large matrices via meshgrid.

这避免了通过meshgrid生成大型矩阵。

#4


0  

Not sure if I understand the question - to make a list of 2-element NumPy arrays, this works:

不确定我是否理解这个问题 - 要制作一个2元素NumPy数组的列表,这可行:

import numpy as np
x = np.arange(-5, 5.1, 0.5)
X, Y = np.meshgrid(x, x)
Liszt = [np.array(thing) for thing in zip(X.flatten(), Y.flatten())] # for python 2.7

zip gives you a list of tuples, and the list comprehension does the rest.

zip为您提供元组列表,其余的列表理解。

#5


0  

We can use arrange function as:

我们可以使用安排功能:

z1 = np.array([np.array(np.arange(1,5)),np.array(np.arange(1,5))])
print(z1)
o/p=> [[1 2 3 4]
       [1 2 3 4]]