访问NumPy结构化数组的列

时间:2021-11-19 13:41:58

I've been given a multidimensional numpy array, x that looks like this:

我得到了一个多维的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)

I'm trying to access each column (specifically I'm trying to take the standard deviation of each column). I found this answer, and I tried,

我正在尝试访问每一列(特别是我试图获取每列的标准偏差)。我找到了这个答案,我试过了,

>>> x[:,0]

But this returned an error:

但是这返回了一个错误:

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

Is it possible to convert this structured array into a simple 2D numpy array to access the columns? Or is there a good way to access these columns directly?

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

Thanks!

谢谢!

Edit

编辑

Some more information on this array:

有关此阵列的更多信息:

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

If it's any help, I used the tree2array function in the root_numpy package to produce this array.

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

4 个解决方案

#1


0  

not sure how you are doing it,but it seems to work for me straight from the prompt

不确定你是怎么做的,但它似乎直接从提示中起作用

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)

#2


0  

"Or is there a good way to access these columns directly?" - Yes, there is!

“或者有一种直接访问这些列的好方法吗?” - 就在这里!

Suppose you want to get the ith column of this array without converting it to a 2D array first(which, however, is the cleaner approach).

假设您想要获取此数组的第i列而不首先将其转换为2D数组(但这是更清洁的方法)。

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

However, I would suggest to first convert it to a 2D array like so:

但是,我建议首先将其转换为2D数组,如下所示:

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]

#3


0  

A solution might be to transform the structured array to a list and then create a standard array from that:

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

np.array(x.tolist())

See this question for reference.

请参阅此问题以供参考。

#4


0  

I was able to get things to work with the help of this answer:

在这个答案的帮助下,我能够把事情搞定:

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

如何在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)

#1


0  

not sure how you are doing it,but it seems to work for me straight from the prompt

不确定你是怎么做的,但它似乎直接从提示中起作用

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)

#2


0  

"Or is there a good way to access these columns directly?" - Yes, there is!

“或者有一种直接访问这些列的好方法吗?” - 就在这里!

Suppose you want to get the ith column of this array without converting it to a 2D array first(which, however, is the cleaner approach).

假设您想要获取此数组的第i列而不首先将其转换为2D数组(但这是更清洁的方法)。

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

However, I would suggest to first convert it to a 2D array like so:

但是,我建议首先将其转换为2D数组,如下所示:

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]

#3


0  

A solution might be to transform the structured array to a list and then create a standard array from that:

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

np.array(x.tolist())

See this question for reference.

请参阅此问题以供参考。

#4


0  

I was able to get things to work with the help of this answer:

在这个答案的帮助下,我能够把事情搞定:

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

如何在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)