简体   繁体   English

访问 numpy ndarray 中的各个列

[英]Accessing individual columns in a numpy ndarray

I have an nd-numpy array of shape (m, 1,100,4) for which I would like to access the individual columns of the inner array (shape: (1,100,4) ).我有一个形状为(m, 1,100,4)的 nd-numpy 数组,我想为其访问内部数组的各个列(形状: (1,100,4) )。

MWE: As example, say I have this: MWE:例如,假设我有这个:

import numpy as np
X = np.random.randn(2, 1, 5, 4)
    
X
array([[[[-0.40867508,  0.09331783,  1.26134307, -1.18900601],
         [-0.79177772,  0.96738931, -0.33332772,  0.53130287],
         [ 3.67290383,  0.30954936,  0.63221306, -0.64003826],
         [-1.20878773,  1.21499506,  1.84995811,  0.15663168],
         [-0.60648072, -0.30464852, -0.44044224, -4.46482868]]],


       [[[-1.90531392, -0.47108517,  1.21177166,  0.09561669],
         [ 3.21803694,  0.30611821,  1.71334417,  0.73383279],
         [-1.12869017, -0.1497266 , -0.54913676,  0.36704922],
         [ 0.5652546 , -0.75012341, -0.72496611,  1.12428097],
         [-1.19727408, -0.13813127,  2.63948821, -0.37661527]]]])

where nested arrays are shaped (1,5,4) .其中嵌套的 arrays 的形状为(1,5,4) Then accessing the first columns of each nested array returns the entire array instead:然后访问每个嵌套数组的第一列会返回整个数组:

X[ :, 0]
array([[[-0.40867508,  0.09331783,  1.26134307, -1.18900601],
        [-0.79177772,  0.96738931, -0.33332772,  0.53130287],
        [ 3.67290383,  0.30954936,  0.63221306, -0.64003826],
        [-1.20878773,  1.21499506,  1.84995811,  0.15663168],
        [-0.60648072, -0.30464852, -0.44044224, -4.46482868]],

       [[-1.90531392, -0.47108517,  1.21177166,  0.09561669],
        [ 3.21803694,  0.30611821,  1.71334417,  0.73383279],
        [-1.12869017, -0.1497266 , -0.54913676,  0.36704922],
        [ 0.5652546 , -0.75012341, -0.72496611,  1.12428097],
        [-1.19727408, -0.13813127,  2.63948821, -0.37661527]]])

My intention is to get a tuple, such that:我的意图是得到一个元组,这样:

s,t,u,v = X[first_columns], X[second_columns], X[third_columns], X[fouth_columns]

such that:这样:

s =[-0.40867508, -0.79177772, 3.67290383, -1.20878773, -0.60648072,
   -1.90531392, 3.21803694, -1.12869017, 0.5652546, -1.19727408]

What you are looking for is你正在寻找的是

X[:,0,:,0].ravel()

Note that with this shape of X , we cannot directly get the desired elements as an array but as a 2d matrix.请注意,使用X的这种形状,我们不能直接将所需的元素作为一个数组,而是作为一个 2d 矩阵。 Therefore we need to reshape to array form.因此我们需要reshape为数组形式。

The other correspond to:另一个对应:

t = X[:,0,:,1].ravel()
u = X[:,0,:,2].ravel()
v = X[:,0,:,3].ravel()

If you reshape the array into the the appropriate way, you can directly unpack its inner arrays into s,t,u,v .如果将数组重新整形为适当的方式,则可以直接将其内部 arrays 解包为s,t,u,v In this case we can transpose and swapaxes to bring the columns to the front, then squeeze to remove that additional single axis:在这种情况下,我们可以转置和swapaxes以将列放在前面,然后squeeze以移除额外的单轴:

s,t,u,v = X.T.swapaxes(1,3).squeeze()

print(s)
array([[-0.40867508, -0.79177772,  3.67290383, -1.20878773, -0.60648072],
       [-1.90531392,  3.21803694, -1.12869017,  0.5652546 , -1.19727408]])

print(t)
array([[ 0.09331783,  0.96738931,  0.30954936,  1.21499506, -0.30464852],
       [-0.47108517,  0.30611821, -0.1497266 , -0.75012341, -0.13813127]])

I would just access the array like this: X[0,0,:].T , or using a loop to cover the entire first dimension:我会像这样访问数组: X[0,0,:].T ,或者使用循环来覆盖整个第一维:

for i in range(X.shape[0]):
    a,b,c,d = X[i,0,:].T

Explanation: by setting the 1st and 2nd dimensions (1st to i , 2nd to 0 due to its size 1), you get a 2D array.说明:通过设置第 1 维和第 2 维(第 1 维为i ,第 2 维为0 ,因为它的大小为 1),您将得到一个 2D 数组。 To extract the columns, just apply transpose or .T and get each column separately.要提取列,只需应用转置或.T并分别获取每一列。

Eg:例如:

X = np.random.randn(2, 1, 5, 4)
X
array([[[[-0.5654864 ,  0.83400636, -0.43981782,  0.79797726],
         [-0.53889591,  0.20837148, -0.14120152,  1.4920727 ],
         [ 0.45982834,  0.18474384, -1.47445088, -1.10874298],
         [-1.45259119, -1.72788464, -0.19379806, -0.42558103],
         [-2.16298358,  0.10093486, -0.00730153,  0.00871548]]],

       [[[ 0.60556812, -0.55684663, -0.63648966, -0.34645153],
         [ 0.39557121,  1.50672188, -1.06762611, -1.38979522],
         [-0.06393524, -0.84720836,  0.27615171, -0.31991015],
         [-0.9626267 ,  0.26539901, -1.08703265, -0.97718657],
         [-0.39556868, -0.81102407,  0.33091579, -0.652497  ]]]])

a,b,c,d = X[0,0,:].T
a,b,c,d
(array([-0.5654864 , -0.53889591,  0.45982834, -1.45259119, -2.16298358]),
 array([ 0.83400636,  0.20837148,  0.18474384, -1.72788464,  0.10093486]),
 array([-0.43981782, -0.14120152, -1.47445088, -0.19379806, -0.00730153]),
 array([ 0.79797726,  1.4920727 , -1.10874298, -0.42558103,  0.00871548]))

If you want to get all columns at once, just reshape your data from [a,1,b,c] to [1,a*b,c] and transpose:如果您想一次获取所有列,只需将数据从[a,1,b,c]重塑为[1,a*b,c]并转置:

a,b,c,d = X.reshape([1,10,4]).T

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM