np.zeros_like奇怪的bug
import numpy as np
aa = np.array([[1,2,3],[2,3,3]])
cc = np.random.randn(2,3)
print(aa)
print(cc)
bb = np.zeros_like(aa)
print(bb)
for i in range(bb.shape[0]):
for j in range(bb.shape[1]):
bb[i,j] = cc[i,j]
print(bb)
结果如下
这里发现这个bb的结果是没有赋值的
正确做法如下
import numpy as np
aa = np.array([[1,2,3],[2,3,3]])
cc = np.random.randn(2,3)
print(aa)
print(cc)
bb = np.zeros_like(aa,dtype=float)
print(bb)
for i in range(bb.shape[0]):
for j in range(bb.shape[1]):
bb[i,j] = cc[i,j]
print(bb)