Issue
What is the difference between these three:
np.zeros(img)
, np.zeros_like(img)
, and np.copy(img)*0
?
When should I use each of them?
Solution
zeros
is the basic operation, creating an array with a given shape and dtype:
In [313]: arr = np.zeros((3,4),int)
In [314]: arr
Out[314]:
array([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]])
zeros_like
is an alternative input to zeros
, where you provide an array, and it copies its shape and dtype (but not values):
In [315]: bar = np.zeros_like(arr)
In [316]: bar
Out[316]:
array([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]])
copy*0
also works, but generally I wouldn't recommend it. I did use N2 *=0
recently in while testing some code to reset an array.
In [317]: car = arr.copy()*0
In [318]: car
Out[318]:
array([[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]])
Answered By - hpaulj
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.