我有一个包含112行和40列的数组。
我需要转换的格式是40组56个点,每个点有x,y。
因此,第一行具有每组中第一个点的x坐标。第二行具有集合中第二个点的x ...直到第56行。在那之后,我有了y。
1st line : 40 x's
2nd line: 40 x's
...
56th line: 40 x's
57th line: 40 y's
...
112th line: 40 y's
最初我考虑过做data.reshape(40, 56, 2)
,但这不起作用,因为x的值在y的值之前。相反,如果我有一行x和另一行y,那将是有用的。
编辑:
for i in xrange(len(data)/2):
points.append(data[i])
points.append(data[i+len(data)/2])
points = np.array(points).T.reshape(len(data[0]), len(data)/2, 2)
return points
答案 0 :(得分:4)
只有一个想法:
[[(data[i,j], data[i+56,j]) for i in range(56)] for j in range(40)]
返回元组列表的列表。
编辑:您的编辑会澄清您想要的内容。如果你想要纯粹的Numpy,那么这有用吗?
data.reshape(2, 56, 40).swapaxes(0,2)
答案 1 :(得分:2)
我将使用较小的数组(8 x 5),以便我们可以轻松查看返回的值。
import numpy as NP
# just create a smaller array to work with:
A = NP.random.randint(0, 10, 40).reshape(8, 5)
# split A in half, to separate x and y
p, q = NP.vsplit(A, 2)
# create a 'template' array of the correct dimension
xy = NP.zeros(2, 4, 5)
# now just map the x and y values onto the template
xy[0:,:] = p
xy[1:,:] = q
# the transformed matrix:
array([[[ 8., 5., 2., 5., 7.],
[ 2., 6., 0., 7., 2.],
[ 4., 4., 7., 5., 5.],
[ 8., 5., 2., 0., 5.]],
[[ 4., 8., 6., 9., 2.],
[ 2., 6., 5., 8., 1.],
[ 3., 2., 6., 2., 2.],
[ 1., 8., 0., 7., 3.]]])