Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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
Arrays 从每行的特定列中获取值_Arrays_Python 3.x_Numpy - Fatal编程技术网

Arrays 从每行的特定列中获取值

Arrays 从每行的特定列中获取值,arrays,python-3.x,numpy,Arrays,Python 3.x,Numpy,我有一个带有一些值的数组X [[0.3,0.4,0.5], [0.1,0.7,0.9], . . . [0.3,0.6,0.9]] 索引为I=[0,2,1,2,0,…]的I have数组 我想根据数组I中的索引从数组X中获取每一行的值,就像,在数组I中,第一个值是0,所以从I中的第一行将从列0中获取值,即0.3,依此类推。 有没有可能在没有循环的情况下做到这一点 我的想法是: Y=X[:,I]没有意义 您就快到了,您需要的是在顶部添加一些奇特的索引: Y = X[np.arange(len(

我有一个带有一些值的数组
X

[[0.3,0.4,0.5],
 [0.1,0.7,0.9],
.
.
.
[0.3,0.6,0.9]]
索引为
I=[0,2,1,2,0,…]
的I have数组

我想根据数组
I
中的索引从数组
X
中获取每一行的值,就像,在数组
I
中,第一个值是
0
,所以从
I
中的第一行将从列
0
中获取值,即
0.3
,依此类推。 有没有可能在没有循环的情况下做到这一点

我的想法是:
Y=X[:,I]
没有意义

您就快到了,您需要的是在顶部添加一些奇特的索引:

Y = X[np.arange(len(I)),I]

这种索引告诉numpy选择
(i,i(i))
中的
X

perfect:)我自己也不知道。非常感谢。如果你从未面对过,你就不会知道:)