定义
np.array(a)和np.asarry(a)
用法
array和asarray都可以将结构数据转化为ndarray,但是主要区别就是当数据源是ndarray时,array仍然会copy出一个副本,占用新的内存,但asarray不会。
示例
import numpy as np
a = np.random.random((3,4))
print(a)
b1 = np.array(a)
b2 = np.asarray(a)
a[1] = 1
print(a)
print(b1)
print(b2)
>>>[[0.41086444 0.51666194 0.72782757 0.19080557]
[0.08287474 0.61791302 0.04831193 0.06255579]
[0.23890174 0.97828276 0.51140157 0.73964937]]
[[0.41086444 0.51666194 0.72782757 0.19080557]
[1. 1. 1. 1. ]
[0.23890174 0.97828276 0.51140157 0.73964937]]
[[0.41086444 0.51666194 0.72782757 0.19080557]
[0.08287474 0.61791302 0.04831193 0.06255579]
[0.23890174 0.97828276 0.51140157 0.73964937]]
[[0.41086444 0.51666194 0.72782757 0.19080557]
[1. 1. 1. 1. ]
[0.23890174 0.97828276 0.51140157 0.73964937]]