我有一个48x365的numpy数组,其中每个元素都是包含3个整数的列表。我想将它转换为一个1x17520的数组,并保留所有列表作为元素。可以使用以下代码:
np.reshape(-1)
似乎将元素分成三个独立的整数,并创建一个1x52560的数组。因此,我需要一种重新排列原始数组的新方法,或者一种将新的np.reshape数组中的元素(仍按顺序)分组回3个列表的方法。
感谢您的帮助。
我有一个48x365的numpy数组,其中每个元素都是包含3个整数的列表。我想将它转换为一个1x17520的数组,并保留所有列表作为元素。可以使用以下代码:
np.reshape(-1)
你不能明确地做这件事吗?例如:
>>> a = numpy.arange(17520 * 3).reshape(48, 365, 3)
>>> a.reshape((17520,3))
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
...,
[52551, 52552, 52553],
[52554, 52555, 52556],
[52557, 52558, 52559]])
您也可以使用-1
进行操作,只需要与适当大小的另一个参数配对即可。
>>> a.reshape((17520,-1))
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
...,
[52551, 52552, 52553],
[52554, 52555, 52556],
[52557, 52558, 52559]])
或者
>>> a.reshape((-1,3))
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
...,
[52551, 52552, 52553],
[52554, 52555, 52556],
[52557, 52558, 52559]])
后来我想到,你也可以创建一个记录数组 -- 在某些情况下可能更合适:
a = numpy.recarray((17520,), dtype=[('x', int), ('y', int), ('z', int)])
reshape(-1)
。然而,正如larsmans的评论所说,将你的数据视为3D数组是最简单的方法。