Numpy基础
目录
数据结构
基础操作
矩阵属性
矩阵操作
矩阵函数
数据结构 |
# -*- coding: utf-8 -*- import numpy as np #从文本构建ndarray a = np.genfromtxt("a.txt", delimiter="\t", dtype=int, skip_header=0) print(type(a)) print(a) ''' <class 'numpy.ndarray'> [[1 2 3] [4 5 6]] ''' #直接构造ndarray a = np.array([[1,2],[3,4]]) #元素类型最好设置成一致 print(type(a)) print(a.dtype) print(a) a = np.array([[1,2],[3,4.0]]) print(type(a)) print(a.dtype) print(a) ''' <class 'numpy.ndarray'> int32 [[1 2] [3 4]] <class 'numpy.ndarray'> float64 [[ 1. 2.] [ 3. 4.]] ''' #矩阵的shape a = np.array([[1,2,3],[4,5,6]]) print(a.shape) # (2, 3) #取某个值 print(a[1,2]) #第2行第3列 # 6 #切片 a = np.array([1,2,3,4,5,6]) print(a[:3]) #打印前三个值 #[1 2 3] a = np.array([[1,2,3],[4,5,6],[7,8,9]]) print(a[:,:2]) #行全取,列只取前两列 ''' [[1 2] [4 5] [7 8]] '''
基础操作 |
# -*- coding: utf-8 -*- import numpy as np # 转bool类型 a = np.array([[1,2],[3,4]]) print(a == 2) ''' [[False True] [False False]] ''' # 与或运算 a = np.array([[1,2],[3,4]]) b = np.array([[1,2],[0,4]]) print(a&b) print(a|b) ''' [[1 2] [0 4]] [[1 2] [3 4]] ''' # 类型转换 a = np.array(["1","2","3","4"]) print(a.dtype) a = a.astype(float) print(a.dtype) print(a) ''' <U1 float64 [ 1. 2. 3. 4.] ''' # 获取最值 a = np.array([[1,4],[3,2],[8,6]]) ''' [[1 4] [3 2] [8 6]] ''' print(a.min(axis=0)) #取0轴上的最小值 # [1 2] print(a.min(axis=1)) #取1轴上的最小值 # [1 3 6] print(a.min()) #取最小值 # 1 # 求和 a = np.array([[1,2],[3,4],[5,6]]) print(a.sum(axis=0)) print(a.sum(axis=1)) print(a.sum()) ''' [ 9 12] [ 3 7 11] 21 ''' # 求平均数 a = np.array([[1,2],[3,4],[5,6]]) print(a.mean(axis=0)) print(a.mean(axis=1)) print(a.mean()) ''' [ 3. 4.] [ 1.5 3.5 5.5] 3.5 '''
矩阵属性 |
# -*- coding: utf-8 -*- import numpy as np a = np.arange(12) print(a) # [ 0 1 2 3 4 5 6 7 8 9 10 11] a = a.reshape(3,4) print(a) ''' [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] ''' print(a.shape) #形状 # (3, 4) print(a.ndim) #维度 # 2 print(a.size) #大小 # 12
矩阵操作 |
# -*- coding: utf-8 -*- import numpy as np a = np.zeros((2,3)) #0矩阵 print(a) # [[ 0. 0. 0.] # [ 0. 0. 0.]] a = np.ones((2,3), dtype = np.int32) #1矩阵 print(a) # [[1 1 1] # [1 1 1]] a = np.arange(100,110,2) #自定义步长矩阵 print(a) # [100 102 104 106 108] a = np.random.random((2,3)) #随机矩阵 print(a) # [[ 0.51411639 0.77741782 0.5720869 ] # [ 0.8042447 0.36104249 0.62305819]] a = np.linspace(1, 2, 5) #在1和2之间 平均取5个点 print(a) # [ 1. 1.25 1.5 1.75 2. ] # 四则运算 a = np.array([2,4,6,8]) b = np.array([1,3,5,10]) print(a-b) # [ 1 1 1 -2] print(a-1) # [1 3 5 7] print(a**2) # [ 4 16 36 64] print(a>5) # [False False True True] #矩阵乘法 a = np.array([[1,1], [0,1]]) b = np.array([[2,0], [3,4]]) print(a*b) #对应位置相乘 # [[2 0] # [0 4]] print(a.dot(b)) #矩阵相乘 # [[5 4] # [3 4]]
矩阵函数 |
# -*- coding: utf-8 -*- import numpy as np a = np.array([[1,2], [3,4]]) print(np.exp(a)) # [[ 2.71828183 7.3890561 ] # [ 20.08553692 54.59815003]] print(np.sqrt(a)) # [[ 1. 1.41421356] # [ 1.73205081 2. ]] a = np.random.random((2,3)) * 10 print(a) # [[ 1.61719344 2.678753 1.26624097] # [ 8.54779284 2.81985938 9.78669941]] print(np.floor(a)) #向下取整 # [[ 1. 2. 1.] # [ 8. 2. 9.]] a = np.array([[1,2], [3,4]]) print(a.ravel()) #转化成向量 # [1 2 3 4] print(a.T) #转置 # [[1 3] # [2 4]] #矩阵拼接 a = np.array([[1,2], [3,4]]) b = np.array([[5,6], [7,8]]) print(np.vstack((a,b))) # [[1 2] # [3 4] # [5 6] # [7 8]] print(np.hstack((a,b))) # [[1 2 5 6] # [3 4 7 8]] #矩阵切分 a = np.arange(24).reshape(4,6) print(a) # [[ 0 1 2 3 4 5] # [ 6 7 8 9 10 11] # [12 13 14 15 16 17] # [18 19 20 21 22 23]] print(np.hsplit(a,2)) #竖着平均切2份 # [array([[ 0, 1, 2], # [ 6, 7, 8], # [12, 13, 14], # [18, 19, 20]]), # array([[ 3, 4, 5], # [ 9, 10, 11], # [15, 16, 17], # [21, 22, 23]])] print(np.vsplit(a,2)) #横着平均切2份 # [array([[ 0, 1, 2, 3, 4, 5], # [ 6, 7, 8, 9, 10, 11]]), # array([[12, 13, 14, 15, 16, 17], # [18, 19, 20, 21, 22, 23]]) print(np.hsplit(a,(2,3))) #竖着在索引2,索引3的位置各切一刀 # [array([[ 0, 1], # [ 6, 7], # [12, 13], # [18, 19]]), # array([[ 2], # [ 8], # [14], # [20]]), # array([[ 3, 4, 5], # [ 9, 10, 11], # [15, 16, 17], # [21, 22, 23]])] print(np.vsplit(a,(2,3))) #横着在索引2,索引3的位置各切一刀 # [array([[ 0, 1, 2, 3, 4, 5], # [ 6, 7, 8, 9, 10, 11]]), # array([[12, 13, 14, 15, 16, 17]]), # array([[18, 19, 20, 21, 22, 23]])] #复制矩阵 a = np.array([[1,2], [3,4]]) b = a #b和a指向同一个内存,改变b的值a也会发生改变 print(id(a)==id(b)) # True b = a.copy() #b和a的内容一样,但各自有各自的存储单元,改变b的值a不会发生改变 print(id(a)==id(b)) # False #寻找index值 a = np.arange(20).reshape(4,5) # [[ 0 1 2 3 4] # [ 5 6 7 8 9] # [10 11 12 13 14] # [15 16 17 18 19]] print(a.argmax(axis=0)) # [3 3 3 3 3] print(a.argmax(axis=1)) # [4 4 4 4] # 扩展矩阵 a = np.array([[1,2], [3,4]]) a = np.tile(a,(2,3)) print(a) # [[1 2 1 2 1 2] # [3 4 3 4 3 4] # [1 2 1 2 1 2] # [3 4 3 4 3 4]] #排序 a = np.array([[1,4], [6,3]]) b = np.sort(a,axis=0) #列排序 print(b) # [[1 3] # [6 4]] b = np.sort(a,axis=1)#行排序 print(b) # [[1 4] # [3 6]] a = np.array([1,4,3,2]) print(np.argsort(a)) #打印排序索引 # [0 3 2 1]