简体   繁体   English

numpy:如何便宜地在numpy矩阵切片上应用布尔索引数组

[英]numpy: how to apply boolean index array on slice of numpy matrix cheaply

I'm trying to apply a boolean mask array over a slice of my numpy matrix. 我正在尝试在我的numpy矩阵的一部分上应用布尔掩码数组。 I only figure out the following to implement this, by first copy this slice out to a single array, then apply boolean mask over this temporary array and copy it back to the matrix. 我只是想出以下方法来实现此目的,首先将此切片复制到单个数组中,然后在此临时数组上应用布尔掩码,然后将其复制回到矩阵中。

I'm wondering if there exists (sure it exists! but I can't find it) a cheaper way to implement it? 我想知道是否存在(肯定存在!但是我找不到它)一种更便宜的实现方法? aka, without the cost of copying. 又名,无需复制成本。

#(data is a numpy 2d matrix)

tmp = data[i,:]
tmp[tmp==0] = mean
data[i,:] = tmp

What about this? 那这个呢?

mask = data[i,:] == 0
data[i,mask] = mean

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM