NumPy的安装
1 2
| pip install numpy conda list numpy
|
1 2
| import numpy as np print(np.__version__)
|
NumPy属性
1 2 3 4 5 6
| Array = np.array([[1,2,3], [2,3,4]]) print(Array) print('number of dim: ', Array.ndim) print('shape: ', Array.shape) print('size: ', Array.size)
|
1 2 3 4 5 6
| [[1 2 3] [2 3 4]] number of dim: 2 shape: (2, 3) size: 6
|
NumPy创建array
NumPy中的数据类型
np.int_
、np.int8
、np.int16
、np.int32
、np.int64
np.float16
、np.float32
、np.float64
- 如果不指定
dtype
, NumPy默认类型为float64
1 2
| a = np.array([2, 23, 4], dtype=np.int_)
|
定义一个元素全为0的矩阵
定义一个元素全为1的矩阵
定义一个空矩阵
1 2 3
| [[6.23042070e-307 4.67296746e-307 1.69121096e-306] [7.56596412e-307 7.56595733e-307 5.18768928e-322]]
|
用arange生成一个数列
用linspace生成一个数列
1
| a = np.linspace(0, 1, 5)
|
将数列reshape成矩阵
reshape后的矩阵元素个数必须和原数列的元素个数相对应,否则会报错
1
| a = np.arange(12).reshape((3,4))
|
1 2 3 4
| [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]]
|
可以reshape元组列表等非NumPy的数据类型,返回NumPy数据类型
1 2 3 4
| a = (1,2,3,4,5,6) print(a,type(a),np.shape(a)) b = np.reshape(a,[2,3]) print(b,type(b),b.shape)
|
1 2 3 4
| (1, 2, 3, 4, 5, 6) <class 'tuple'> (6,) [[1 2 3] [4 5 6]] <class 'numpy.ndarray'> (2, 3)
|
将矩阵flatten成数列
NumPy的基础运算
NumPy的四则运算和乘方
+
、-
、*
、\
、**
- NumPy参与四则运算的两个向量必须是一样形状的,否则会报错
1
| ValueError: operands could not be broadcast together with shapes (5,) (4,)
|
NumPy的三角函数
np.sin()
、np.cos()
、np.tan()
矩阵的乘法
1 2 3
| A*B np.dot(A,B) A.dot(B)
|
矩阵的转置
最大值、最小值、求和
1 2 3
| np.sum(a) np.min(a, axis=0) np.max(a, axis=1)
|
最大值最小值的索引
1 2
| np.argmax(a, axis=1) np.argmin(a, axis=0)
|
均值
1 2 3
| np.mean(a) np.mean(a, axis=0) np.mean(a, axis=1)
|
加权平均
1
| np.average([a,b,c], weights = [aw,bw,cw])
|
这个式子在求加权平均,其计算式如下
a⋅aw+bw+cwaw+b⋅aw+bw+cwbw+c⋅aw+bw+cwcw
中位数
累加
上面的式子会输出如下结果
[a1,a1+a2,a1+a2+a3]
累差
上面的式子会输出如下结果
[a2−a1,a3−a2,a4−a3]
输出非零元素的坐标
1
| print(np.nonzero(np.array([[1,0,1],[0,1,0]])))
|
1 2
| (array([0, 0, 1], dtype=int64), array([0, 2, 1], dtype=int64))
|
排序
1
| np.sort(np.array([[1,7,2],[9,2,6]]),axis=None)
|
1 2 3 4 5 6 7 8
| [[1 7 2] [9 2 6]]
[[1 2 7] [2 6 9]]
[1 2 2 6 7 9]
|
clip函数
1
| numpy.clip(a, a_min, a_max, out=None)
|
将a中元素小于a_min的变成a_min, 大于a_max的变成a_max
Numpy的随机数
生成均匀分布的随机数
- 生成[low,high)上均匀分布的随机数, 默认分布区间是[0,1.0)
np.random.uniform()
不可指定dtype
, 其生成的随机数的类型是float
1
| np.random.uniform(low=0.0, high=1.0, size=None)
|
1 2 3
| >>> a = np.random.uniform() >>> print(a, type(a)) 0.5751878527508482 <class 'float'>
|
1 2 3
| >>> b = np.random.uniform(size=1) >>> print(b, type(b)) [0.56519959] <class 'numpy.ndarray'>
|
1 2 3 4
| >>> c = np.random.uniform(low=-1, high=1, size=[2,3]) >>> print(c, type(c)) [[ 0.86854768 -0.24873633 0.26262462] [-0.91575207 -0.26320594 -0.04508716]] <class 'numpy.ndarray'>
|
生成均匀分布的随机整数
生成正太分布的随机数
1
| np.random.normal(loc=0.0, scale=1.0, size=None)
|
1 2 3 4 5
| >>> mu, sigma = 0, 0.1 >>> s = np.random.normal(mu, sigma, size=[2,3]) >>> print(s, type(s)) [[ 0.01428685 -0.019985 -0.04419828] [-0.02343732 -0.05882594 0.02499089]] <class 'numpy.ndarray'>
|
NumPy的索引
矩阵的索引
1 2 3 4 5 6 7 8 9 10 11
| >>> A = np.arange(0,9,1).reshape(3,3) >>> print(A) [[0 1 2] [3 4 5] [6 7 8]]
>>> print(A[2,1]) 7
>>> print(A[0,0:3:2]) [0 2]
|
矩阵的迭代
1 2 3 4 5 6 7 8
| for i in A: print(i)
[0 1 2] [3 4 5] [6 7 8]
|
1 2 3 4 5 6 7 8
| for i in A.T: print(i)
[0 3 6] [1 4 7] [2 5 8]
|
1 2 3 4 5 6 7 8 9
| for i in A.flat: print(i)
0 1 ... 8
|
NumPy的array的合并
矩阵的合并
1 2 3 4 5 6 7 8 9 10 11 12 13
| >>> A = np.array([1,1,1]) >>> B = np.array([2,2,2])
>>> C = np.vstack((A,B)) >>> print(C) [[1 1 1] [2 2 2]]
>>> D = np.hstack((A,B)) >>> print(D) [1 1 1 2 2 2]
|
1 2 3 4 5
| >>> A = np.array([1,1,1]) >>> B = np.array([2,2,2]) >>> C = np.array([3,3,3]) >>> print(np.concatenate((A,B,C),axis=0)) [1 1 1 2 2 2 3 3 3]
|
向量的转置
[:]
是切片操作, np.newaxis
就是None
, 增加一个空的维度
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| >>> A = np.array([1,1,1]) >>> print(A[:],(A[:].shape)) [1 1 1] (3,)
>>> print(A[:,np.newaxis],(A[:,np.newaxis].shape)) [[1] [1] [1]] (3, 1)
>>> print(A[:,None],(A[:,None].shape)) [[1] [1] [1]] (3, 1)
>>> print(A.reshape(3,1),A.reshape(3,1).shape) [[1] [1] [1]] (3, 1)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| >>> A = np.array([1,1,1])[:,np.newaxis] >>> B = np.array([2,2,2])[:,np.newaxis] >>> C = np.array([3,3,3])[:,np.newaxis] >>> print(np.concatenate((A,B,C),axis=1)) [[1 2 3] [1 2 3] [1 2 3]] >>> print(np.concatenate((A,B,C),axis=0)) [[1] [1] [1] [2] [2] [2] [3] [3] [3]]
|
NumPy的array的分割
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| >>> A = np.arange(12).reshape((3,4)) >>> print(A) [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]]
>>> print(np.split(A,3,axis=0)) [array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])] >>> print(np.split(A,4,axis=1)) [array([[0], [4], [8]]), array([[1], [5], [9]]), array([[ 2], [ 6], [10]]), array([[ 3], [ 7], [11]])]
|
1 2 3 4 5 6 7 8 9 10 11
| >>> print(np.split(A,[2,3],axis=1)) [array([[0, 1], [4, 5], [8, 9]]), array([[ 2], [ 6], [10]]), array([[ 3], [ 7], [11]])]
|
copy和deep copy
1 2
| a = np.arange(3) b = a.copy()
|
savetxt和loadtxt
1 2 3 4 5
| data = np.arange(10, dtype=np.float64)
np.savetxt('./data.txt',data)
data1 = np.loadtxt('./data.txt', dtype=np.float64)
|