Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用numpy将一维数组转换为二维数组?_Python_Arrays_Numpy - Fatal编程技术网

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)