Index a 2D Numpy array with 2 lists of indices(使用 2 个索引列表对 2D Numpy 数组进行索引)
问题描述
我遇到了一个奇怪的情况.
I've got a strange situation.
我有一个 2D Numpy 数组,x:
I have a 2D Numpy array, x:
x = np.random.random_integers(0,5,(20,8))
我有 2 个索引器——一个带有行索引,一个带有列索引.为了索引 X,我必须执行以下操作:
And I have 2 indexers--one with indices for the rows, and one with indices for the column. In order to index X, I am having to do the following:
row_indices = [4,2,18,16,7,19,4]
col_indices = [1,2]
x_rows = x[row_indices,:]
x_indexed = x_rows[:,column_indices]
不仅仅是:
x_new = x[row_indices,column_indices]
(失败并出现:错误,不能用(2,)广播(20,))
(which fails with: error, cannot broadcast (20,) with (2,))
我希望能够使用广播在一行中进行索引,因为这样可以使代码保持清洁和可读性......而且,我对 python 的底层知识不太了解,但是据我了解,在一行中完成它应该更快(我将使用相当大的数组).
I'd like to be able to do the indexing in one line using the broadcasting, since that would keep the code clean and readable...also, I don't know all that much about python under the hood, but as I understand it, it should be faster to do it in one line (and I'll be working with pretty big arrays).
测试用例:
x = np.random.random_integers(0,5,(20,8))
row_indices = [4,2,18,16,7,19,4]
col_indices = [1,2]
x_rows = x[row_indices,:]
x_indexed = x_rows[:,col_indices]
x_doesnt_work = x[row_indices,col_indices]
推荐答案
如果你用 np.newaxis 编写你的第一次尝试会成功
Your first try would work if you write it with np.newaxis
x_new = x[row_indices[:, np.newaxis],column_indices]
这篇关于使用 2 个索引列表对 2D Numpy 数组进行索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 2 个索引列表对 2D Numpy 数组进行索引


基础教程推荐
- Kivy 使用 opencv.调整图像大小 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01