【发布时间】:2021-09-20 11:10:03
【问题描述】:
我有什么:
import numpy as np
np.random.seed(42)
dlen = 250000
data = np.random.rand(dlen, 3, 3)
mask = np.random.choice([0, 1, 2], dlen)
我想得到什么:
[[0.37454012 0.95071431 0.73199394],
[0.83244264 0.21233911 0.18182497],
[0.13949386 0.29214465 0.36636184],
[0.94888554 0.96563203 0.80839735],
[0.44015249 0.12203823 0.49517691],
....
(250000, 3)
我尝试使用什么:
data[:,mask,:]
{MemoryError}Unable to allocate 1.36 TiB for an array with shape (250000, 250000, 3) and data type float64
什么给出了正确的结果但看起来很奇怪:
data[np.arange(data.shape[0]), mask, :]
那么这个面具的正确使用方法是什么?
更新: 掩码应选择具有指定索引的列。形状为 [2,3,3] 的数组示例:
array = [[[5 6 7], [7 8 9], [2 3 4]],
[[2 1 0], [7 6 5], [7 6 5]]]
mask = [1 0]
result = [[7 8 9],
[2 1 0]]
【问题讨论】:
-
你能用文字解释一下你的面具应该完成什么吗?
-
试图在问题中解释。
-
您给定的示例数组只有 2 个轴而不是 3 个
-
..gives the correct result but looks strange- 之所以有效,是因为您使用 Index array -
这些是现在例子中的三个轴。
标签: python arrays numpy slice mask