In [40]: a, b = 5, 15
In [41]: (b - a) * np.random.rand(3) + a
Out[41]: array([ 6.40061821, 6.72343487, 10.49412407])
# 一般的,可以选择已有的库函数:
In [42]: np.random.uniform(5, 15, 3)
Out[42]: array([11.10830186, 7.35193797, 8.46971257])
randn 生成了 N(0,I)的标准正态分布:
In [43]: np.random.randn(3)
Out[43]: array([ 1.2642241 , -1.04640246, 0.05297258])
In [44]: np.random.randn(2, 2)
Out[44]:
array([[2.65755302, 0.12266858],
[0.29899713, 0.40504878]])
# 一元正态分布
In [45]: sigma, mu = 2.5, 3
In [46]: mu + np.random.randn(3) * sigma
Out[46]: array([6.46031228, 0.57297935, 5.2692226 ])
# 已有函数
In [47]: np.random.normal(3, 2.5, 3)
Out[47]: array([2.72546019, 7.42390272, 3.71079215])
In [53]: np.random.permutation(my_list)
Out[53]: array(['b', 'd', 'a', 'c'], dtype='<U1')
随机种子,它能够固定随机数的输出结果:
In [54]: np.random.seed(0)
In [55]: np.random.rand()
Out[55]: 0.5488135039273248
In [56]: np.random.seed(0)
In [57]: np.random.rand()
Out[57]: 0.5488135039273248
In [61]: try:
....: np.r_[np.array([0,0]),np.zeros((2,1))]
....: except Exception as e:
....: Err_Msg = e
....:
In [62]: Err_Msg
Out[62]: ValueError('all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 1 has 2 dimension(s)')
In [63]: np.r_[np.array([0,0]),np.zeros(2)]
Out[63]: array([0., 0., 0., 0.])
In [64]: np.c_[np.array([0,0]),np.zeros((2,3))]
Out[64]:
array([[0., 0., 0., 0.],
[0., 0., 0., 0.]])
【c】维度变换: reshape<br />reshape 能够帮助用户把原数组按照新的维度重新排列。在使用时有两种模式,分别为 C 模式和 F 模式,分别以逐行和逐列的顺序进行填充读取。
In [82]: a = np.array([-2,-5,0,1,3,-1])
In [83]: np.nonzero(a)
Out[83]: (array([0, 1, 3, 4, 5], dtype=int64),)
In [84]: a.argmax()
Out[84]: 4
In [85]: a.argmin()
Out[85]: 1
In [122]: a = np.array([1,2,3])
In [123]: b = np.array([1,3,5])
In [124]: a.dot(b)
Out[124]: 22
【b】向量范数和矩阵范数: np.linalg.norm<br /> 在矩阵范数的计算中,最重要的是 ord 参数
ord
norm for matrices
norm for vectors
None
Frobenius norm
2-norm
‘fro’
Frobenius norm
–
‘nuc’
nuclear norm
–
inf
max(sum(abs(x), axis=1))
max(abs(x))
-inf
min(sum(abs(x), axis=1))
min(abs(x))
0
–
sum(x != 0)
1
max(sum(abs(x), axis=0))
as below
-1
min(sum(abs(x), axis=0))
as below
2
2-norm (largest sing. value)
as below
-2
smallest singular value
as below
other
–
sum(abs(x)ord) (1./ord)
In [125]: matrix_target = np.arange(4).reshape(-1,2)
In [126]: matrix_target
Out[126]:
array([[0, 1],
[2, 3]])
In [127]: np.linalg.norm(matrix_target, 'fro')
Out[127]: 3.7416573867739413
In [128]: np.linalg.norm(matrix_target, np.inf)
Out[128]: 5.0
In [129]: np.linalg.norm(matrix_target, 2)
Out[129]: 3.702459173643833
In [130]: vector_target = np.arange(4)
In [131]: vector_target
Out[131]: array([0, 1, 2, 3])
In [132]: np.linalg.norm(vector_target, np.inf)
Out[132]: 3.0
In [133]: np.linalg.norm(vector_target, 2)
Out[133]: 3.7416573867739413
In [134]: np.linalg.norm(vector_target, 3)
Out[134]: 3.3019272488946263
【c】矩阵乘法: @
In [135]: a = np.arange(4).reshape(-1,2)
In [136]: a
Out[136]:
array([[0, 1],
[2, 3]])
In [137]: b = np.arange(-4,0).reshape(-1,2)
In [138]: b
Out[138]:
array([[-4, -3],
[-2, -1]])
In [139]: a@b
Out[139]:
array([[ -2, -1],
[-14, -9]])
Numpy中的ascontiguousarray
在使用Numpy的时候,有时候会遇到下面的错误:
AttributeError: incompatible shape for a non-contiguous array