0

While studying the NumPy package of Python, I tried the following code segment

import numpy as np
x = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
v = np.array([1,0,1])
y = np.empty_like(x)
print(y)
for i in range(4):
   y[i,:] = x[i,:]+v
print "......."
print(y)

However, the first print(y) gives the output such as following, instead of all zero array. On the other side, the second print(y)generates the correct result as expected. I would like to know why.

[[        72          0          0]
 [         0       2676 1346720256]
 [1599357253 1950699087         10]
 [1346524499 1163154497  242503250]]
 .......
[[ 2  2  4]
 [ 5  5  7]
 [ 8  8 10]
 [11 11 13]]

1 Answer 1

3

You want zeros_like. empty_like gives an array filled with who-knows-what, so it doesn't have to spend time filling it with zeros.

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.