Python numpy 中是否有 arange/linspace 的多维版本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32208359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:12:56  来源:igfitidea点击:

Is there a multi-dimensional version of arange/linspace in numpy?

pythonnumpycartesian-product

提问by Hilemonstoer

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.

但顺序无关紧要。

采纳答案by farenorth

You can use np.mgridfor this, it's often more convenient than np.meshgridbecause 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 numberwhose magnitude specifies the number of points you want in the series. Using this syntax, the same arrays as above are specified as:

对于类似 linspace 的功能,将步骤 (ie 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!

祝你好运!

回答by tmdavison

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.

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

为 ND 网格上的 ND 标量/矢量场的矢量化评估制作 ND 坐标数组,给定一维坐标数组 x1、x2、...、xn。

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

回答by uhoh

Not sure if I understand the question - to make a list of 2-elementNumPy 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

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

zip给你一个元组列表,列表理解完成剩下的工作。

回答by chthonicdaemon

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.productto 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

回答by Pranav Joshi

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

回答by Martin

Based on this example, you can make any dim you want

基于这个例子,你可以做任何你想要的暗淡

def linspace3D(point1,point2,length):
    v1 = np.linspace(point1[0],point2[0],length)
    v2 = np.linspace(point1[1],point2[1],length)
    v3 = np.linspace(point1[2],point2[2],length)
    line = np.zeros(shape=[length,3])
    line[:,0]=v1
    line[:,1]=v2
    line[:,2]=v3
    return line

回答by F?rat Korkmaz

This is just what you are looking for:

这正是您要寻找的:

matr = np.linspace((1,2),(10,20),10)

This means:

这意味着:

For the first column; from 1 of (1,2) to 10 of (10,20), put the increasing 10 numbers.

对于第一列;从 (1,2) 的 1 到 (10,20) 的 10,放入递增的 10 个数字。

For the second column; from 2 of (1,2) to 20 of (10,20), put the incresing 10 numbers.

对于第二列;从 (1,2) 的 2 到 (10,20) 的 20,输入递增的 10 个数字。

And the result will be:

结果将是:

[[ 1.  2.]
 [ 2.  4.]
 [ 3.  6.]
 [ 4.  8.]
 [ 5. 10.]
 [ 6. 12.]
 [ 7. 14.]
 [ 8. 16.]
 [ 9. 18.]
 [10. 20.]]

You may also keep only one column's values increasing, for example, if you say that:

你也可以只保持一列的值增加,例如,如果你说:

matr = np.linspace((1,2),(1,20),10)

The first column will be from 1 of (1,2) to 1 of (1,20) for 10 times which means that it will stay as 1 and the result will be:

第一列将从 (1,2) 的 1 到 (1,20) 的 1 进行 10 次,这意味着它将保持为 1,结果将是:

[[ 1.  2.]
 [ 1.  4.]
 [ 1.  6.]
 [ 1.  8.]
 [ 1. 10.]
 [ 1. 12.]
 [ 1. 14.]
 [ 1. 16.]
 [ 1. 18.]
 [ 1. 20.]]