NumPy 是 Python 语言的一个扩充程序库。支持高级大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库,也是学习 python 必学的一个库。python
numpy.genfromtxt() 用于读取 txt 文件,其中传入的参数依次为:数组
help(numpy.genfromtxt)用于查看帮助文档:
若是不想看 API 能够启动一个程序用 help 查看指令的详细用法dom
import numpy world_alcohol = numpy.genfromtxt("world_alcohol.txt", delimiter=",",dtype=str) print(type(world_alcohol)) print(world_alcohol) print(help(numpy.genfromtxt))
numpy.array()中传入数组参数,能够是一维的也能够是二维三维的。numpy 会将其转变成 ndarray 的结构。函数
vector = numpy.array([1,2,3,4]) matrix = numpy.array([[1,2,3],[4,5,6]])
传入的参数必须是同一结构,不是同一结构将发生转换。学习
vector = numpy.array([1,2,3,4]) array([1, 2, 3, 4])
均为 int 类型spa
vector = numpy.array([1,2,3,4.0]) array([ 1., 2., 3., 4.])
转为浮点数类型debug
vector = numpy.array([1,2,'3',4]) array(['1', '2', '3', '4'],dtype='<U21')
转为字符类型code
可以了解 array 的结构,debug 时经过查看结构可以更好地了解程序运行的过程。文档
print(vector.shape)
print(matrix.shape)
(4,) (2, 3)
vector = numpy.array([1,2,3,4]) vector.dtype dtype('int64')
一维数学
vector = numpy.array([1,2,3,4]) vector.ndim 1
二维
matrix = numpy.array([[1,2,3], [4,5,6], [7,8,9]]) matrix.ndim 2
matrix.size
9
matrix = numpy.array([[1,2,3], [4,5,6], [7,8,9]])
numpy 可以依次比较 vector 和元素之间是否相同
vector = numpy.array([5, 10, 15, 20]) vector == 10 array([False, True, False, False], dtype=bool)
根据返回值获取元素
vector = numpy.array([5, 10, 15, 20]) equal_to_ten = (vector == 10) print(equal_to_ten) print(vector[equal_to_ten]) [False True False False] [10]
进行运算以后获取
vector = numpy.array([5, 10, 15, 20]) equal_to_ten_and_five = (vector == 10) & (vector == 5)
vector = numpy.array([5, 10, 15, 20]) equal_to_ten_or_five = (vector == 10) | (vector == 5)
将总体类型进行转换
vector = numpy.array([5, 10, 15, 20]) print(vector.dtype) vector = vector.astype(str) print(vector.dtype) int64 <U21
sum() 可以对 ndarray 进行各类求和操做,好比分别按行按列进行求和
matrix = numpy.array([[1,2,3], [4,5,6], [7,8,9]]) print(matrix.sum()) print(matrix.sum(1)) print(matrix.sum(0)) 45 [ 6 15 24] [12 15 18]
sum(1) 是 sum(axis=1)) 的缩写,1表示按照 x轴方向求和,0表示按照y轴方向求和
生成从 0-14 的 15 个数字,使用 reshape(3,5) 将其构形成一个三行五列的 array。
import numpy as np arr = np.arange(15).reshape(3, 5) arr array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14]])
生成指定结构的默认为 0. 的 array
np.zeros ((3,4)) array([[ 0., 0., 0., 0.], [ 0., 0., 0., 0.], [ 0., 0., 0., 0.]])
生成一个三维的 array,经过 dtype 指定类型
np.ones( (2,3,4), 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]]])
指定范围和数值间的间隔生成 array,注意范围包左不包右
np.arange(0,10,2) array([0, 2, 4, 6, 8])
生成指定结构的随机数,能够用于生成随机权重
np.random.random((2,3)) array([[ 0.86166627, 0.37756207, 0.94265883], [ 0.9768257 , 0.96915312, 0.33495431]])
元素之间依次相减相减
a = np.array([10,20,30,40]) b = np.array(4) a - b array([ 6, 16, 26, 36])
乘方
a**2 array([ 100, 400, 900, 1600])
开根号
np.sqrt(B)
array([[ 1.41421356, 0. ], [ 1.73205081, 2. ]])
e 求方
np.exp(B)
array([[ 7.3890561 , 1. ], [ 20.08553692, 54.59815003]])
向下取整
a = np.floor(10*np.random.random((2,2))) a array([[ 0., 0.], [ 3., 6.]])
行列变换
a.T
array([[ 0., 3.], [ 0., 6.]])
变换结构
a.resize(1,4) a array([[ 0., 0., 3., 6.]])
矩阵之间的运算
A = np.array( [[1,1], [0,1]] ) B = np.array( [[2,0], [3,4]] )
对应位置一次相乘
A*B
array([[2, 0], [0, 4]])
矩阵乘法
print (A.dot(B)) print(np.dot(A,B)) [[5 4] [3 4]]
横向相加
a = np.floor(10*np.random.random((2,2))) b = np.floor(10*np.random.random((2,2))) print(a) print(b) print(np.hstack((a,b))) [[ 2. 3.] [ 9. 3.]] [[ 8. 1.] [ 0. 0.]] [[ 2. 3. 8. 1.] [ 9. 3. 0. 0.]]
纵向相加
print(np.vstack((a,b)))
[[ 2. 3.] [ 9. 3.] [ 8. 1.] [ 0. 0.]]
矩阵分割
#横向分割 print( np.hsplit(a,3)) #纵向风格 print(np.vsplit(a,3))
经过 b = a 复制 a 的值,b 与 a 指向同一地址,改变 b 同时也改变 a。
a = np.arange(12) b = a print(a is b) print(a.shape) print(b.shape) b.shape = (3,4) print(a.shape) print(b.shape) True (12,) (12,) (3, 4) (3, 4)
经过 a.view() 仅复制值,当对 c 值进行改变会改变 a 的对应的值,而改变 c 的 shape 不改变 a 的 shape
a = np.arange(12) c = a.view() print(c is a) c.shape = 2,6 c[0,0] = 9999 print(a) print(c) False [9999 1 2 3 4 5 6 7 8 9 10 11] [[9999 1 2 3 4 5] [ 6 7 8 9 10 11]]
a.copy() 进行的完整的拷贝,产生一份彻底相同的独立的复制
a = np.arange(12) c = a.copy() print(c is a) c.shape = 2,6 c[0,0] = 9999 print(a) print(c) False [ 0 1 2 3 4 5 6 7 8 9 10 11] [[9999 1 2 3 4 5] [ 6 7 8 9 10 11]]