import numpy as np
a = np.array([2,3,4])#单维矩阵
b = np.array([[1,2,3],[2,3,4]])#多维矩阵
1.2创建全零、全一、和什么都没有的矩阵
import numpy as np
a = np.zeros((3,4))#全零矩阵 (几行/几列)
b = np.ones((3,4))#全1矩阵(几行/几列)
c = np.empty((3,5))#什么都没有的矩阵(几行/几列)
1.3创建有规律的矩阵(几到几 零到几 线段)
import numpy as np
a = np.arange(10,20,2)#创建规律矩阵(几行/几列/步长)
b =np.arange(12).reshape((3,4))#定义0-12的矩阵并重新定义矩阵行列
c = np.linspace(1,20,10)#生成一个线段矩阵(从几开始/到几结束/分成多少段)
1.4随机生成矩阵、正态分布
import numpy as np
a = np.random.random((2,4))#生成0-1的随机矩阵(几行,几列)
Y = np.random.normal(0,0.05,(2000,))#正态分布的均值,正态分布标准差,(几行,几列)
1.5生成矩阵指定类型
import numpy as np
b = np.array([2,3,4],dtype=np.int32)#单维矩阵并定义int32类型'''
numpy类型有
int8,16,32,64 / float16,32,64,128
complex64,128,256 / bool / object / Unicode_
'''
numpy.array信息输出
2.1输出矩阵和类型
import numpy as np
a = np.array([[1,2,3],[2,3,4]])print(a,a.dtype)#输出及打印类型
2.2输出array维度
import numpy as np
a = np.array([[1,2,3],[2,3,4]])print('number of dim:',a.ndim)#输出维度
2.3输出array形状
import numpy as np
a = np.array([[1,2,3],[2,3,4]])print('shape:',a.shape)#输出形状
2.4输出array元素个数
import numpy as np
a = np.array([[1,2,3],[2,3,4]])print('size:',a.size)#输出元素个数
numpy基本运算1
3.1矩阵相加、相减
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
b = np.array([[11,12,13],[14,15,16]])
c = a+b#矩阵相加
d =b-a#矩阵相减
3.2次方、按序相乘、矩阵相乘
import numpy as np
a = np.array([[1,2],[3,4]])
b = np.array([[5,6],[7,8]])
c = a**2#矩阵次方
d = a*b#逐个相乘
e = np.dot(a,b)#矩阵相乘
f = a.dot(b)#矩阵相乘的另一种形式
3.3矩阵求三角函数
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
b = np.sin(a)#对a的每个元素求sin(弧度制)
c = np.cos(a)#对a的每个元素求cos(弧度制)
d = np.tan(a)#对a的每个元素求tan(弧度制)
3.4矩阵求max、min、sum
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
d = np.max(a)#对a求元素最大值
c = np.min(a)#对a求元素最小值
b = np.sum(a)#对a求元素和
3.5按行或按列运算矩阵最大、最小、求和
import numpy as np
a = np.array([[1,2,3],[4,5,6]])
b = np.max(a,axis =1)#每一行进行max操作
c = np.max(a,axis =0)#每一列进行max操作
d = np.min(a,axis =1)#每一行进行min操作
e = np.min(a,axis =0)#每一列进行min操作
f = np.sum(a,axis =1)#每一行进行sum操作
g = np.sum(a,axis =0)#每一列进行sum操作
numpy基本运算2
4.1打印最大值、最小值的索引
import numpy as np
A = np.arange(2,14).reshape((3,4))print(np.argmin(A))#输出最小值的索引print(np.argmin(A,axis=0))#按列输出最小值索引print(np.argmin(A,axis=1))#按行输出最小值索引print(np.argmax(A))#输出最大值的索引print(np.argmax(A,axis=0))#按列输出最大值索引print(np.argmax(A,axis=1))#按行输出最大值索引
4.2打印平均数、中位数
import numpy as np
A = np.arange(2,14).reshape((3,4))print(np.mean(A))#输出矩阵平均值print(np.mean(A,axis=0))#按列输出矩阵平均值print(np.mean(A,axis=1))#按行输出矩阵平均值print(np.median(A))#输出矩阵中位数print(np.median(A,axis=0))#按列输出矩阵中位数print(np.median(A,axis=1))#按行输出矩阵中位数
4.3打印矩阵累加、差值
import numpy as np
A = np.arange(2,14).reshape((3,4))print(np.cumsum(A))#输出矩阵累加值print(np.cumsum(A,axis=0))#按列输出矩阵累加值print(np.cumsum(A,axis=1))#按行输出矩阵累加值print(np.diff(A))#输出矩阵前后数差值print(np.diff(A,axis=0))#按列输出矩阵前后数差值print(np.diff(A,axis=1))#按行输出矩阵前后数差值
import numpy as np
A = np.arange(14,2,-1).reshape((3,4))print(np.sort(A))#按行排序print(np.transpose(A))#矩阵的行和列互换print(np.clip(A,5,9))#矩阵所有小于最小值的换成最小值,大于最大值的换成最大值(哪个矩阵,最小值,最大值)print(A.flatten())#把矩阵变成一行print(A[:,np.newaxis])#横向单行矩阵变成纵向单列矩
numpy索引
4.1索引固定行、列、元素、范围内元素
import numpy as np
A = np.arange(3,15).reshape((3,4))print(A[1])#索引行(哪一行+1)print(A.T[1])#索引列(哪一列+1)print(A[1][2])#索引哪一个元素(哪一行+1/哪一列+1)print(A[1][2:3])#索引哪一行从几到几(哪一行/(从n+1个元素开始‘’包括/到n+1个元素结束‘不包括’))
4.2迭代array行、列、元素
import numpy as np
A = np.arange(3,15).reshape((3,4))#迭代行for row in A:print(row)#迭代列for column in A.T:print(column)#迭代每个元素for item in A.flat:print(item)
numpy合并和分割
5.1两个numpy进行合并
import numpy as np
A = np.array([1,1])
B = np.array([2,2])print(np.vstack((A,B)))#上下合并print(np.hstack((A,B)))#左右和并
5.2多个numpy进行合并
import numpy as np
A = np.array([1,1,1])[:,np.newaxis]
B = np.array([2,2,2])[:,np.newaxis]print( np.concatenate((A,B,B,A),axis =0))#多个array横向合并print(np.concatenate((A,B,B,A),axis =1))#多个array纵向合并
5.3array分割
import numpy as np
A = np.arange(12).reshape((3,4))print(np.split(A,2,axis=1))#array分割(对谁分割/分割成几块/横向还是纵向)print(np.array_split(A,2,axis=1))#array不等分割(对谁分割/分割成几块/横向还是纵向)print(np.vsplit(A,3))#纵向(对谁分割,分成几份)print(np.hsplit(A,2))#横向(对谁分割,分成几份)
深copy
6.1深copy
import numpy as np
A = np.arange(12).reshape((3,4))
B = a.copy