l有一个名为(96,18,3)的名为 my_data 的3D数组。给定axis = 1的索引,我只想获取一些值。轴= 1的长度为18
为此,我尝试了以下操作:
from operator import itemgetter
index_1= [3,4,6,7,9,10,12,13]
getter=itemgetter(3,4,6,7,9,10,12,13)
getter(my_data)
但是,它在第一个轴上应用吸气剂,从而将96减少到8。我想要在第二个轴上应用吸气剂,以便获得形状为(96,8,3)的 my_data 。
我也尝试过:
my_data[:,getter,:]
我收到以下错误:
*** IndexError:只有整数,切片(:
),省略号(...
),numpy.newaxis(None
)和整数或布尔数组都是有效索引
答案 0 :(得分:0)
为什么不使用itemgetter
并仅将列表作为索引器提供
index_1= [3,4,6,7,9,10,12,13]
my_data[:,index_1,:]
在诸如您的数据集上:
>>> my_data.shape
(96, 18, 3)
>>> my_data[:,index_1,:].shape
(96, 8, 3)
最小示例:
采用1
数组的维度(5,5,3)
的第二个元素和第四个元素:
>>> my_data = np.random.random((5,5,3))
>>> my_data
array([[[ 0.48913302, 0.65967146, 0.16984338],
[ 0.65309136, 0.61112866, 0.48317725],
[ 0.75979879, 0.1788647 , 0.84855963],
[ 0.95604821, 0.91686885, 0.04629087],
[ 0.32021119, 0.17582171, 0.44410709]],
[[ 0.40507398, 0.33866034, 0.57994344],
[ 0.35664701, 0.82952864, 0.48164719],
[ 0.53201074, 0.74598244, 0.025587 ],
[ 0.29090129, 0.85763979, 0.12372515],
[ 0.66274253, 0.45789019, 0.5960151 ]],
[[ 0.98733701, 0.21920232, 0.25870197],
[ 0.83877241, 0.74712859, 0.12972104],
[ 0.99349399, 0.58134955, 0.90017913],
[ 0.21030304, 0.68324321, 0.36743921],
[ 0.26277439, 0.30750822, 0.10385251]],
[[ 0.73189247, 0.49511839, 0.04785461],
[ 0.62492651, 0.92238879, 0.04875051],
[ 0.6779602 , 0.7643024 , 0.17460262],
[ 0.57072683, 0.87087793, 0.5888601 ],
[ 0.37419042, 0.39583678, 0.30297916]],
[[ 0.51922434, 0.21364451, 0.32678503],
[ 0.66437971, 0.6550304 , 0.61334286],
[ 0.16475464, 0.84246673, 0.32644154],
[ 0.56004586, 0.02378533, 0.61112593],
[ 0.59555996, 0.88077068, 0.44648423]]])
>>> index_1 = [1,3]
>>> my_data[:, index_1, :]
array([[[ 0.65309136, 0.61112866, 0.48317725],
[ 0.95604821, 0.91686885, 0.04629087]],
[[ 0.35664701, 0.82952864, 0.48164719],
[ 0.29090129, 0.85763979, 0.12372515]],
[[ 0.83877241, 0.74712859, 0.12972104],
[ 0.21030304, 0.68324321, 0.36743921]],
[[ 0.62492651, 0.92238879, 0.04875051],
[ 0.57072683, 0.87087793, 0.5888601 ]],
[[ 0.66437971, 0.6550304 , 0.61334286],
[ 0.56004586, 0.02378533, 0.61112593]]])