0

I am working with a 2-d numpy array which looks like this:

array([[[0, 0, 0, 0],
    [0, 0, 0, 0],
    [0, 0, 0, 0],
    ..., 
    [0, 0, 0, 0],
    [0, 0, 0, 0],
    [0, 0, 0, 0]],

   [[0, 0, 0, 0],
    [0, 0, 0, 0],
    [0, 0, 0, 0],
    ..., 
    [0, 0, 0, 0],
    [0, 0, 0, 0],
    [0, 0, 0, 0]],

   [[0, 0, 0, 0],
    [0, 0, 0, 0],
    [0, 0, 0, 0],
    ..., 
    [0, 0, 0, 0],
    [0, 0, 0, 0],
    [0, 0, 0, 0]]])

So I have numpy inside numpy which has a list of four values (pixels in RGBA to be specific). I want to set all values to 0. What is the most pythonic way to do it? Thanks in advance!

2
  • That would be a 3D array. For example you obtain that from np.zeros((3,100,4), dtype=np.int8) Commented Apr 24, 2018 at 22:03
  • Hmm, you are correct! So here I am copying my numpy array from another numpy array and setting all values to zero. What is the most efficient way to do that? Can you suggest? Commented Apr 24, 2018 at 22:08

2 Answers 2

1
m[:] = 0

would set all values of your array m to zero.

If you need an array of zeros with the same shape and datatype of m, use:

np.zeros_like(m)
Sign up to request clarification or add additional context in comments.

Comments

0
import numpy as np

a = np.random.randn(10, 10)
b = np.zeros_like(a)

b will be an ndarray of exactly the same shape as the original, filled with 0.

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.