0x01 生成01数组
import numpy as np
# 生成4行6列0数组
ones = np.ones([4, 6])
"""
查看下zeros Out[3]:
array([[0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0.]])
"""
# 生成3行7列1数组
zeros = np.zeros([3, 7])
"""
查看下ones Out[4]:
array([[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1.]])
"""
0x02 从现有数组生成
浅拷贝np.asarray与深拷贝np.arrary的区别
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
a1 = np.array(a) # 深拷贝
a2 = np.asarray(a) # 浅拷贝
a[0][0] = 999 # 给a[0][0] 赋值成999
"""
# 查看a1[0][0]还是为1,因为使用了深拷贝
a1 Out[4]: array([[1, 2, 3], [4, 5, 6]])
# 查看a2[0][0]也变成了999,因为使用了浅拷贝
a2 Out[5]: array([[999, 2, 3], [4, 5, 6]])
"""
0x03 生成固定范围的数组
import numpy as np
# 创建等差数组,指定数量。np.linspace(start,stop,num,endpoint)
a = np.linspace(0, 100, 11) # Out[3]: array([ 0., 10., 20., 30., 40., 50., 60., 70., 80., 90., 100.])
# 创建等差数组,指定步长。np.arange(start,stop,step,dtype)
b = np.arange(10, 50, 2) # Out[3]: array([10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42,44, 46, 48])
# 创建等比数列,生成10^x(从10的0次方,到10的2次方,生成3次)。np.logspace(start,stop,num)
c = np.logspace(0, 2, 3) # Out[3]: array([ 1., 10., 100.])