1.np.ones(shape, dtype=None, order=‘C’)
np.ones(shape=(3, 4, 3), dtype=np.int32)
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]],
[[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
[1, 1, 1]]])
2.np.zeros(shape, dtype=float, order=‘C’)
np.zeros(shape=(3, 4, 3), dtype=np.int32)
array([[[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]],
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]],
[[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]])
3.np.full(shape, fill_value, dtype=None, order=‘C’)
np.full(shape=(3, 4, 3), fill_value=8.0)
array([[[8., 8., 8.],
[8., 8., 8.],
[8., 8., 8.],
[8., 8., 8.]],
[[8., 8., 8.],
[8., 8., 8.],
[8., 8., 8.],
[8., 8., 8.]],
[[8., 8., 8.],
[8., 8., 8.],
[8., 8., 8.],
[8., 8., 8.]]])
4.np.eye(N, M=None, k=0, dtype=float)
# 生成2维, 对角线上全是1, 其他位置全是0的矩阵.
# k=0表示主对角线.
# 行列数相同的矩阵,, 叫做方阵
# 主对角线上全是1, 其他位置全是0的方阵, 叫做单位矩阵.
# 如果一个矩阵可以变成单位矩阵, 那么我们把这个矩阵叫做满秩矩阵.
np.eye(6, k=-1)
array([[0., 0., 0., 0., 0., 0.],
[1., 0., 0., 0., 0., 0.],
[0., 1., 0., 0., 0., 0.],
[0., 0., 1., 0., 0., 0.],
[0., 0., 0., 1., 0., 0.],
[0., 0., 0., 0., 1., 0.]])
5.np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
# 线段等分
np.linspace(0, 10, num=10, endpoint=False, retstep=True)
(array([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.]), 1.0)
6.np.arange([start, ]stop, [step, ]dtype=None)
range(10)
range(0, 10)
# start=0, end, step
# 在使用np.arange的时候,步长step尽量是整数, 如果是小数的话, 那么使用np.linspace会好一点.
np.arange(0, 10, 0.3333)
array([0. , 0.3333, 0.6666, 0.9999, 1.3332, 1.6665, 1.9998, 2.3331,
2.6664, 2.9997, 3.333 , 3.6663, 3.9996, 4.3329, 4.6662, 4.9995,
5.3328, 5.6661, 5.9994, 6.3327, 6.666 , 6.9993, 7.3326, 7.6659,
7.9992, 8.3325, 8.6658, 8.9991, 9.3324, 9.6657, 9.999 ])
7.np.random.randint(low, high=None, size=None, dtype=‘l’)
import random
random.randint()
# 对应python中random.randint几乎一样, 都表示随机整数, 但是python的random.randint是全闭.
# numpy中random.randint是左闭右开的.
np.random.randint(0, 10, size=(4, 5))
array([[0, 8, 5, 4, 8],
[3, 6, 9, 9, 1],
[5, 3, 1, 7, 9],
[9, 0, 4, 3, 7]])
np.random.randn(d0, d1, …, dn) 标准正太分布
# 均值是0, 标准偏差是1的正态分布叫做标准正态分布.
np.random.randn(100)
9)np.random.normal(loc=0.0, scale=1.0, size=None)
# 正态分布.也叫高斯分布
n = np.random.normal(loc=10, scale=3, size=(1000, 1000))
n.std()
3.001498641855265
10.np.random.random(size=None) 生成0到1的随机数,左闭右开
np.random.random(size=(4, 5))
array([[0.67843562, 0.3869334 , 0.39633632, 0.02041326, 0.22930584],
[0.00226294, 0.65044501, 0.9686908 , 0.39624771, 0.42997183],
[0.42402243, 0.75483434, 0.48130763, 0.78706159, 0.04279338],
[0.0304375 , 0.46255901, 0.1095761 , 0.91346565, 0.63222052]])
np.random.rand(d0, d1, d2, d3…,dn)
# 和np.random.random一样
np.random.rand(4, 5)
array([[0.40357817, 0.64473777, 0.57900838, 0.27885215, 0.99461885],
[0.18433061, 0.71325003, 0.17032778, 0.92897935, 0.72633564],
[0.92248825, 0.64570196, 0.85607951, 0.30209461, 0.04010778],
[0.52723821, 0.32526319, 0.00127211, 0.23917366, 0.71829923]])