【问题标题】:Accessing a column of a NumPy array of structured arrays访问结构化数组的 NumPy 数组的列
【发布时间】:2017-07-28 09:15:59
【问题描述】:

我得到了一个多维 numpy 数组,x,看起来像这样:

array([ array([  398.24475098,  -196.1497345 ,  -110.79341125, ..., -1937.22399902,
       -6158.89355469,  1742.84399414], dtype=float32),
       array([   32.27750397,  -171.73371887,  -342.6328125 , ..., -4727.4296875 ,
       -4727.4296875 , -2545.10375977], dtype=float32),
       array([  785.83660889,  -234.88890076,   140.49914551, ..., -7982.19482422,
       -2127.640625  , -1434.77160645], dtype=float32),
       ...,
       array([   181.93313599,   -146.41413879,   -416.02978516, ...,
        -4517.796875  ,  10491.84570312,  -6604.39550781], dtype=float32),
       array([ -1.37602341e+02,   1.71733719e+02,   7.13068867e+00, ...,
         8.60104688e+03,   1.39115127e+04,   3.31622314e+03], dtype=float32),
       array([   453.17272949,    152.49285889,    260.41452026, ...,
        19061.60742188,  11232.8046875 ,   7312.13964844], dtype=float32)], dtype=object)

我正在尝试访问每一列(特别是我正在尝试获取每一列的标准差)。我找到了this answer,我试过了,

>>> x[:,0]

但这返回了一个错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: too many indices for array

是否可以将此结构化数组转换为简单的 2D numpy 数组以访问列?或者有没有直接访问这些列的好方法?

谢谢!

编辑

关于这个数组的更多信息:

>>> x.shape
(8685,)
>>> x[0].shape  # Same for x[1], x[2], ...
(3524,)

如果有任何帮助,我使用了root_numpy 包中的tree2array 函数来生成这个数组。

【问题讨论】:

  • 你的阵列是什么形状的?你试过np.ascontiguousarray(x)吗?
  • 所有子数组的长度都相同吗?如果不是,这可能是个问题
  • 您是否将内部数组解释为矩阵的行或列?
  • 如果将外部数组的dtype 更改为arr.astype(np.float32) 会发生什么?
  • @obachtos 每个内部数组都是矩阵中的一行。做 x.astype(np.float32) 给ValueError: setting an array element with a sequence.

标签: python arrays numpy multidimensional-array


【解决方案1】:

在这个答案的帮助下,我能够让事情顺利进行:

How do I convert an array of arrays into a multi-dimensional array in Python?.

>>> y = np.stack(x)
>>> y
array([[  3.98244751e+02,  -1.96149734e+02,  -1.10793411e+02, ...,
         -1.93722400e+03,  -6.15889355e+03,   1.74284399e+03],
       [  3.22775040e+01,  -1.71733719e+02,  -3.42632812e+02, ...,
         -4.72742969e+03,  -4.72742969e+03,  -2.54510376e+03],
       [  7.85836609e+02,  -2.34888901e+02,   1.40499146e+02, ...,
         -7.98219482e+03,  -2.12764062e+03,  -1.43477161e+03],
       ...,
       [  1.81933136e+02,  -1.46414139e+02,  -4.16029785e+02, ...,
         -4.51779688e+03,   1.04918457e+04,  -6.60439551e+03],
       [ -1.37602341e+02,   1.71733719e+02,   7.13068867e+00, ...,
          8.60104688e+03,   1.39115127e+04,   3.31622314e+03],
       [  4.53172729e+02,   1.52492859e+02,   2.60414520e+02, ...,
          1.90616074e+04,   1.12328047e+04,   7.31213965e+03]], dtype=float32)
>>> y[:,0]
array([ 398.24475098,   32.27750397,  785.83660889, ...,  181.93313599,
       -137.6023407 ,  453.17272949], dtype=float32)

【讨论】:

    【解决方案2】:

    不知道你是怎么做的,但它似乎对我有用 直接从提示符

    a=np.zeros((2,6),dtype=np.float32)
    >>> a
    array([[ 0.,  0.,  0.,  0.,  0.,  0.],
           [ 0.,  0.,  0.,  0.,  0.,  0.]], dtype=float32)
    >>> a[:,2]=1
    >>> a
    array([[ 0.,  0.,  1.,  0.,  0.,  0.],
           [ 0.,  0.,  1.,  0.,  0.,  0.]], dtype=float32)
    

    【讨论】:

    • 但是他没有二维数组而是嵌套数组,所以 id 不起作用。
    • 它也适用于嵌套数组,只要所有子数组的长度相同
    • 显然不是。否则 numpy 会自动将它们解释为二维数组。
    • 查看我在stackoverflow.com/q/45361548 中的回答,了解构造一维数组的方法。
    【解决方案3】:

    “或者有没有直接访问这些列的好方法?” - 是的,有!

    假设您想获取此数组的第 i 列而不先将其转换为二维数组(但是,这是更简洁的方法)。

    >>> col = np.array([row[i] for row in x])
    

    但是,我建议先将其转换为二维数组,如下所示:

    columns = x[0].shape[0] # Note: Number of elements in each array should be the same
    rows = len(x)
    x_flat = x.flatten()
    x_2d = x_flat.reshape((rows, columns))
    col = x_2d[:, i]
    

    【讨论】:

    • 谢谢!您的第一种方法有效,尽管它在我的机器上有点慢(处理完整的 8685 x 3524 阵列大约需要 20 秒)。我尝试重塑数组,但出现此错误:ValueError: cannot reshape array of size 8685 into shape (8685,3524)
    • 添加 x_flat 后能否请您检查一下现在是否正常工作?
    • 不,x.flatten() 不会展平数组(即它返回原始数组数组)
    【解决方案4】:

    一种解决方案可能是将结构化数组转换为列表,然后从中创建一个标准数组:

    np.array(x.tolist())
    

    参考this question

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-08
      相关资源
      最近更新 更多