0

I'm trying to apply a boolean mask array over a slice of my numpy matrix. 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? a.k.a, without the cost of copying.

#(data is a numpy 2d matrix)

tmp = data[i,:]
tmp[tmp==0] = mean
data[i,:] = tmp
2
  • Can you show a sample input/output. What is mean? Commented Jul 7, 2018 at 3:08
  • 1
    Take out the last line. tmp is a view, not a copy Commented Jul 7, 2018 at 5:42

1 Answer 1

1

What about this?

mask = data[i,:] == 0
data[i,mask] = mean
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.