python - 为什么使用冒号与范围进行 numpy 索引会导致奇怪的行为?
问题描述
有人可以解释以下行为吗?
test = np.random.rand(1000, 10)
test.shape
(1000, 10)
idxs = np.random.randint(0, 10, 1000)
idxs.shape
(1000,)
test[:, idxs].shape
(1000, 1000)
test[range(test.shape[0]), idxs].shape
(1000,)
我希望这些是等效的。我错过了什么?
解决方案
由于还没有人明确表示:您缺少参考手册的高级索引部分。
Advanced indexing is triggered when the selection object, obj, is a non-tuple sequence object, an ndarray (of data type integer or bool), or a tuple with at least one sequence object or ndarray (of data type integer or bool). 高级索引有两种类型:整数和布尔。
(我的粗体字)
推荐阅读
- django - Django 更改表单字段检测
- c# - WPF TreeView 中的用户选择
- python - 从数组末尾取消填充全零行
- php - 重力形式:日期验证失败
- android - 如何从 Android 中的 Vimeo URL 中提取视频 ID
- python - pytest 可以抑制第 3 方代码中的回溯吗?
- javascript - 在 Redux 中,我们为什么要设置 initialState?
- android - 如何使用 react-native-firebase 从 Firebase 动态链接接收查询参数?
- javascript - JavaScript 函数未被识别为使用提示/警报的函数
- nativescript-angular - 类型“订阅”缺少“类型订阅”类型中的以下属性:原型、空