问题描述
我正在尝试获取坐标数组矩阵.这与numpy.meshgrid不同.例如,对于2x2的尺寸,我想要2x2x2的输出
I'm trying to get a matrix of coordinate-arrays. This is different from numpy.meshgrid. For example, for a 2x2 size I'd want the 2x2x2 output
[[[0,0],[0,1]],
[[1,0],[1,1]]]
作为一个numpy数组.这可能看起来更清晰地读取了一个2x2元组矩阵:
as a numpy array. This probably looks and reads cleaner a 2x2 matrix of tuples:
[[(0,0),(0,1)],
[(1,0),(1,1)]]
(除了我不认为您可以在numpy数组中包含元组,而这不是重点)
(except I don't think you can have tuples in a numpy array, and it's not the point here)
这个简单的示例可以通过切换numpy-meshgrid的输出轴(具体来说,将第一个轴移动到最后一个轴)来完成:
This simple example can be done by switching the axes of numpy-meshgrid's output (specifically, moving the first axis to be last):
np.array(np.meshgrid([0,1],[0,1])).transpose([1,2,0])
这可以很容易地推广到任意尺寸,除了meshgrid的行为不像我期望的多于2个输入.具体来说,返回的矩阵的坐标值沿轴以奇数顺序变化:
This could be easily generalized to arbitrary dimensions, except that meshgrid doesn't behave as I would expect for more than 2 inputs. Specifically, the returned matrices have coordinate values that vary along axes in an odd order:
In [627]: np.meshgrid([0,1],[0,1],[0,1])
Out[627]:
[array([[[0, 0],
[1, 1]],
[[0, 0],
[1, 1]]]),
array([[[0, 0],
[0, 0]],
[[1, 1],
[1, 1]]]),
array([[[0, 1],
[0, 1]],
[[0, 1],
[0, 1]]])]
请注意,此输出的元素分别沿轴1、0和2变化.这将建立不正确的坐标矩阵;我需要输出沿轴0、1和2以此顺序变化.所以我可以做
Notice that the elements of this output vary along axes 1, 0, and 2, respectively. This will build an incorrect coordinate matrix; I would need the output to vary along axes 0, 1, and 2, in that order. So I could do
In [642]: np.array(np.meshgrid([0,1],[0,1],[0,1])).swapaxes(1,2)
Out[642]:
array([[[[0, 0],
[0, 0]],
[[1, 1],
[1, 1]]],
[[[0, 0],
[1, 1]],
[[0, 0],
[1, 1]]],
[[[0, 1],
[0, 1]],
[[0, 1],
[0, 1]]]])
但是,这真的开始变得困难了,我不知道我是否可以在高维网状网格输出中依靠这个顺序. numpy.mgrid给出正确的顺序,但似乎不允许使用我需要的任意值.因此,这归结为两个问题:
But this is starting to get really hacky and I don't know if I can count on this order in higher-dimension meshgrid outputs. numpy.mgrid gives the right order, but doesn't seem to allow arbitrary values, which I will need. So this boils down to two questions:
1)是否有一种更简洁的方法,也许会缺少numpy中的某些函数,该函数会生成所描述的坐标矢量矩阵?2)这种奇怪的排序真的是我们对Meshgrid的期望吗?关于这一点,我是否可以依靠某个规范?
1) Is there a cleaner way, maybe some function in numpy I'm missing, that will generate a matrix of coordinate-vectors as described?2) Is this odd ordering really what we expect from meshgrid? Is there a spec to this point that I can count on?
跟着Jaime的解决方案,以下是一个更通用的函数,可以对感兴趣的任何人进行更明确的构建:
Following up on Jaime's solution, here's a more generalized function to build it a little more explicitly for anyone interested:
def build_coords(*vecs):
coords = numpy.empty(map(len,vecs)+[len(vecs)])
for ii in xrange(len(vecs)):
s = np.hstack((len(vecs[ii]), np.ones(len(vecs)-ii-1)))
v = vecs[ii].reshape(s)
coords[...,ii] = v
return coords
推荐答案
尝试np.meshgrid([0, 1], [0, 1], [0, 1], indexing="ij")
. meshgrid
文档实际上非常清楚地说明了默认的indexing="xy"
与非默认的indexing="ij"
相比如何产生有趣的轴顺序,因此您可以检查更多细节. (他们对为什么如此工作还不太清楚,a ...)
Try np.meshgrid([0, 1], [0, 1], [0, 1], indexing="ij")
. The meshgrid
docs are actually pretty explicit about how the default indexing="xy"
produces a funny axis ordering as compared to the non-default indexing="ij"
, so you can check that for more details. (They're not as clear on why it works this way, alas...)
这篇关于坐标的数字矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!