Python 如何使用numpy将一维数组转换为二维数组?
我有如下数据:Python 如何使用numpy将一维数组转换为二维数组?,python,arrays,numpy,Python,Arrays,Numpy,我有如下数据: >>>npfilled[:5] array([('!', 0, 0, 3, 10, 0, 2, 4, 4), ('!"', 0, 0, 0, 5, 0, 0, 0, 0), ('"', 23, 13, 20, 32, 0, 0, 22, 9), ("'", 21, 8, 23, 12, 5, 10, 0, 7), ('(', 3, 2, 2, 3, 0, 0, 0, 0)], dtype=[('token', '<
>>>npfilled[:5]
array([('!', 0, 0, 3, 10, 0, 2, 4, 4), ('!"', 0, 0, 0, 5, 0, 0, 0, 0),
('"', 23, 13, 20, 32, 0, 0, 22, 9),
("'", 21, 8, 23, 12, 5, 10, 0, 7), ('(', 3, 2, 2, 3, 0, 0, 0, 0)],
dtype=[('token', '<U64'), ('mel_freq1', '<i2'), ('mel_freq0', '<i2'), ('mel_freq2', '<i2'), ('mel_freq3', '<i2'), ('aus_freq0', '<i2'), ('aus_freq1', '<i2'), ('aus_freq2', '<i2'), ('aus_freq3', '<i2')])
>>>npfilled.shape
(301,)
npfilled[:5]
数组([(“!”,0,0,3,10,0,2,4,4),(“!”,0,0,0,5,0,0,0,0),
('"', 23, 13, 20, 32, 0, 0, 22, 9),
("'", 21, 8, 23, 12, 5, 10, 0, 7), ('(', 3, 2, 2, 3, 0, 0, 0, 0)],
dtype=[('token','用numpy术语来说,您正在询问如何将结构化数组转换为“普通”二维数组,其中结构中的每个项都沿一个新轴 简单地说,对于这样的异构数据,
pandas
可能更符合您的要求
话虽如此,这里有一个简单的解释:
首先,要从当前结构化数组中切片列,您可以执行以下操作:
import numpy as np
# Your example data...
data = np.array([('!', 0, 0, 3, 10, 0, 2, 4, 4),
('!"', 0, 0, 0, 5, 0, 0, 0, 0),
('"', 23, 13, 20, 32, 0, 0, 22, 9),
("'", 21, 8, 23, 12, 5, 10, 0, 7),
('(', 3, 2, 2, 3, 0, 0, 0, 0)],
dtype=[('token', '<U64'), ('mel_freq1', '<i2'),
('mel_freq0', '<i2'), ('mel_freq2', '<i2'),
('mel_freq3', '<i2'), ('aus_freq0', '<i2'),
('aus_freq1', '<i2'), ('aus_freq2', '<i2'),
('aus_freq3', '<i2')])
# Print out two arbitrary columns.
print data[['token', 'aus_freq1']]
如果您有异构数据,您可能并不真正希望切换到“普通”数组
但是,如果您确实想切换,您可能会想到以下几点:
array([[u'!', 0, 0, 3, 10, 0, 2, 4, 4],
[u'!"', 0, 0, 0, 5, 0, 0, 0, 0],
[u'"', 23, 13, 20, 32, 0, 0, 22, 9],
[u"'", 21, 8, 23, 12, 5, 10, 0, 7],
[u'(', 3, 2, 2, 3, 0, 0, 0, 0]], dtype=object)
简单的回答是:
如果您不太关心内存使用,您可以:
np.array(data.tolist(), dtype=object)
较长的答案是: 上面这一行只需要很少的麻烦就能得到你想要的东西。然而,这种方法有两个轻微的缺点
# Only the first column is text...
numeric_cols = list(data.dtype.names[1:])
# View the non-text parts as an array with the same dtype as the numeric cols:
data2d = data[numeric_cols].view('<i2')
# And reshape it into a 2D array:
data2d = data2d.reshape(-1, len(numeric_cols))
这种方法更为冗长,但如果您有一个非常大的数组,则速度会快得多
# Only the first column is text...
numeric_cols = list(data.dtype.names[1:])
# View the non-text parts as an array with the same dtype as the numeric cols:
data2d = data[numeric_cols].view('<i2')
# And reshape it into a 2D array:
data2d = data2d.reshape(-1, len(numeric_cols))
In [10]: data2d
Out[10]:
array([[ 0, 0, 3, 10, 0, 2, 4, 4],
[ 0, 0, 0, 5, 0, 0, 0, 0],
[23, 13, 20, 32, 0, 0, 22, 9],
[21, 8, 23, 12, 5, 10, 0, 7],
[ 3, 2, 2, 3, 0, 0, 0, 0]], dtype=int16)