机器学习预备-numpy

版权声明: https://blog.csdn.net/leida_wt/article/details/83079232

文档

https://docs.scipy.org/doc/numpy/reference/

先导

引入
import numpy as np
numpy 数据结构-ndarray
numpy 使用的数组类是 ndarray
在这里插入图片描述
一些重要属性如下:
ndarray.ndim 维数
ndarray.shape 返回(n, m),n行 m列
ndarray.dtype 类型
numpy 数据结构-mat
mat是ndarray的派生,进行矩阵运算比ndarray方便
a=np.mat(‘4 3; 2 1’)
a=np.mat(np.array([[1,2],[3,4]]))
mat 的*重载为矩阵乘法
求逆a.I

numpy常量

numpy.inf
numpy.nan
numpy.e
numpy.pi

创建数组(矩阵)

a = np.array([2,3,4])
a = np.array([[1, 1], [1, 1]])
#指定类型
np.array( [ [1,2], [3,4] ], dtype=complex )
np.zeros( (3,4) )
np.ones( (2,3,4), dtype=np.int16 )
np.empty( (2,3) )  #值不初始化,为内存乱值
#创建数字序列
np.arange( 10, 30, 5 ) #array([10, 15, 20, 25])
np.arange(15).reshape(3, 5)
#array([[ 0,  1,  2,  3,  4],
#       [ 5,  6,  7,  8,  9],
#       [10, 11, 12, 13, 14]])

附:
全部类型
在这里插入图片描述

操作数组(矩阵)

常见操作符均已重载,其中注意:*分配成了逐一乘(matlab中.*),矩阵乘法采用np.dot(A, B)
拷贝:d = a.copy()
在不同数组类型之间的操作,结果数组的类型趋于更普通或者更精确的一种
array的索引,切片和迭代与python[]同
切片
在这里插入图片描述
上下拼接
np.vstack((a,b))
左右拼接
np.hstack((a,b))

通用数学函数

https://docs.scipy.org/doc/numpy/reference/ufuncs.html

广播规则*

当向量和矩阵结构不匹配响应运算时,会启用广播规则处理,可认为是一种自动补全机制
在这里插入图片描述

索引

在这里插入图片描述

axis

axis=0,对每列操作
axis=1,对每行操作
如对
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/leida_wt/article/details/83079232