import numpy as np
np.__version__
运行结果:
‘1.20.3’
1、Numpy.array
1.1 利用Numpy创建一个数组
创建一个从0~10的数组
nparr=np.array([i for i in range(10)])
nparr
运行结果:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
1.2 查看存储类型
dtype
nparr.dtype #查看存储类型
运行结果:
dtype(‘int32’)
1.3 创建指定大小的数组,数组元素以 0 来填充
zeros()
np.zeros(10) #10个为float类型的0
运行结果:
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
np.zeros(10,dtype=int) #10个为int类型的0
运行结果:
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
np.zeros(shape=(3,5),dtype=int) #三行五列全为0的矩阵
运行结果:
array([[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]])
1.4 创建指定形状的数组,数组元素以 1 来填充:
ones()
np.ones(10)
运行结果:
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
np.ones((3,5)) #三行五列全为1的矩阵
运行结果:
array([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])
2、存入指定值的矩阵
2.1 以指定数值填充指定形状的数组
full(shape, fill_value, dtype=None, order=‘C’)
np.full(shape=(3,5),fill_value=666) #生成一个三行五列矩阵 并且值为666
运行结果:
array([[666, 666, 666, 666, 666],
[666, 666, 666, 666, 666],
[666, 666, 666, 666, 666]])
2.2 生成数组
arange(start, stop, step, dtype = None)
np.arange(0,20) #生成一个数组,存储的值为0~20 不包含尾
运行结果:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19])
np.arange(0,20,2) #步长为2
运行结果:
array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18])
2.3 创建一个一维数组,数组是一个等差数列构成的
linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
参数:
start:序列的起始值
stop:序列的终止值,如果endpoint为true,该值包含于数列中
step:要生成的等步长的样本数量,默认为50
np.linspace(0,20,10,dtype=int) #20包含在数组中的 10代表要生成10个样本数量 包括起始点和终止点的
运行结果:
array([ 0, 2, 4, 6, 8, 11, 13, 15, 17, 20])
3、Random
3.1 生成一个随机数
random.randint()
np.random.randint(0,10) #生成0,10之间的随机数
运行结果:
4
np.random.randint(6,10,size=10) #加入第三个参数 代表要生成一个数组 数字代表生成的数组中元素的个数
运行结果:
array([6, 7, 7, 6, 9, 6, 6, 8, 8, 8])
np.random.randint(6,10,size=(3,5)) #从6~10生成随机数 生成一个三行五列的矩阵
运行结果:
array([[9, 8, 6, 7, 7],
[6, 9, 9, 7, 8],
[6, 7, 9, 7, 7]])
3.2 生成符合正态分布的随机数 并且在0~1之间
random.normal()
np.random.normal() #符合正态分布的随机数 0~1之间
运行结果:
-0.30172177191410243
np.random.normal(10,100) #指定均值为10 方差为100
运行结果:
-167.68898338073987
np.random.normal(0,1,size=(3,5)) #指定均值为0 方差为1 三行五列的随机矩阵
运行结果:
array([[ 0.75087514, -0.74956741, -0.38480188, 1.20089131, -0.87826449],
[-0.46797944, 0.90680574, 0.91860824, 0.40180184, 0.53770265],
[-0.03535741, 0.07666546, -0.41779575, 0.96714372, -0.21326813]])
3.3 函数可以在不改变数据的条件下修改形状
reshape(arr, newshape, order=‘C’)
X=np.arange(15).reshape(3,5) #将一个一维数组 转换成3*5的二维数组
X
运行结果:
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
x:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
x.reshape(10,-1) #变成十行的矩阵
运行结果:
array([[0],
[1],
[2],
[3],
[4],
[5],
[6],
[7],
[8],
[9]])
3.4 查看秩,即轴的数量或维度的数量
ndim
X.ndim
运行结果:
2
3.5 查看数组的维度,对于矩阵,n 行 m 列
shape
X.shape
运行结果:
(3, 5)
3.6 查看数组的大小
size
X.size
运行结果:
15
4、Numpy.array的数据访问
4.1 访问多维数组中某个元素的方法
arr[indx,indx]
X:([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
X[2,2] #在numpy中访问多维数组中某个元素的方法
运行结果:
12
4.2 切片
访问从下标为A到下标为B 但不包含小标为B的元素
x:array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
x[0:5] #访问从下标为0到下标为5 但不包含小标为5的元素
运行结果:
array([0, 1, 2, 3, 4])
x[:5]#切片 访问从下标为0到下标为5 但不包含小标为5的元素
运行结果:
array([0, 1, 2, 3, 4])
x[5:] #访问从下标为5到最后下标的元素
运行结果:
array([5, 6, 7, 8, 9])
x[::2]#从头访问到尾 步长为2
运行结果:
array([0, 2, 4, 6, 8])
二维数组中的切片:
X:array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
X[:2,:3] #访问X二维数组的前两行和前三列
运行结果:
array([[0, 1, 2],
[5, 6, 7]])
X[:2,::2]#访问前两行 但间隔为2的元素
运行结果:
array([[0, 2, 4],
[5, 7, 9]])
X[::-1,::-1] #对矩阵做了反转
运行结果:
array([[14, 13, 12, 11, 10],
[ 9, 8, 7, 6, 5],
[ 4, 3, 2, 1, 0]])
X[0,:]#只取第一行
运行结果:
array([0, 1, 2, 3, 4])
X[:,0]#只取第一列
运行结果:
array([ 0, 5, 10])
5.Numpy的合并操作
5.1 用于沿指定轴连接相同形状的两个或多个数组
concatenate((a1, a2, …), axis)
a1, a2, …:相同类型的数组
axis:沿着它连接数组的轴,默认为 0
x=np.array([1,2,3])
y=np.array([3,2,1])
#x:[1, 2, 3]
#y:[3, 2, 1]
#将x y合并成 1*6的向量
np.concatenate([x,y])
运行结果:
[1, 2, 3, 3, 2, 1]
A=np.array([[1,2,3],
[4,5,6]
])
# A:[ [1, 2, 3],
# [4, 5, 6]]
np.concatenate([A,A],axis=0)#axis=0 (2,3)+(2,3)=>(4,3)
运行结果:
[[1, 2, 3],
[4, 5, 6],
[1, 2, 3],
[4, 5, 6]]
A=np.array([[1,2,3],
[4,5,6]
])
# A:[ [1, 2, 3],
# [4, 5, 6]]
np.concatenate([A,A],axis=1)#axis=1 (2,3)+(2,3)=>(2,6)
运行结果:
[[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6]]
5.2 竖直堆叠序列中的数组(行方向)
vstack
#A:[[1 2 3]
# [4 5 6]]
#Z:[666 666 666]
np.vstack([A,z])
运行结果:
[[ 1, 2, 3],
[ 4, 5, 6],
[666, 666, 666]]
5.3 水平堆叠序列中的数组(列方向)
hstack
#A:[[1, 2, 3],
# [4, 5, 6]]
#B:[[100, 100],
# [100, 100]]
np.hstack([A,B])
运行结果:
[[ 1, 2, 3, 100, 100],
[ 4, 5, 6, 100, 100]]
6.分割操作
6.1 将一个数组分割为多个子数组
split(ary, indices_or_sections, axis)
ary:被分割的数组
indices_or_sections:如果是一个整数,就用该数平均切分,如果是一个数组,为沿轴切分的位置(左开右闭)
axis:设置沿着哪个方向进行切分,默认为 0,横向切分,即水平方向。为 1 时,纵向切分,即竖直方向。
# x:[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
np.split(x,[3,7])#第一个参数要分割的数组 第二个参数分割点(ind1,ind2,....)
运行结果:
[array([0, 1, 2]), array([3, 4, 5, 6]), array([ 7, 8, 9, 10, 11])]
''' A:[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]] '''
A1,A2=np.split(A,[2],axis=0) #按行分割
A1
A2
运行结果:
A1:
[[0, 1, 2, 3],
[4, 5, 6, 7]]
A2:
[[ 8, 9, 10, 11],
[12, 13, 14, 15]]
A1,A2=np.split(A,[2],axis=1) #按列分割
A1
A2
运行结果:
A1:
[[ 0, 1],
[ 4, 5],
[ 8, 9],
[12, 13]]
A2:
[[ 2, 3],
[ 6, 7],
[10, 11],
[14, 15]]
6.2 将一个数组垂直分割为多个子数组(按行)
vsplit
''' A:[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]] '''
upper,lower=np.vsplit(A,[2]) #按行分割
upper
lower
运行结果:
upper:
[[0, 1, 2, 3],
[4, 5, 6, 7]]
lower:
[[ 8, 9, 10, 11],
[12, 13, 14, 15]]
6.3 将一个数组水平分割为多个子数组(按列)
hsplit
''' A:[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]] '''
left,right=np.hsplit(A,[2]) #按列分割
left
right
运行结果:
left:
[[ 0, 1],
[ 4, 5],
[ 8, 9],
[12, 13]]
right:
[[ 2, 3],
[ 6, 7],
[10, 11],
[14, 15]]
7.Numpy中的矩阵运算
7.1 矩阵加法
A+B
'''A:[[0, 1],
[2, 3]]'''
'''B:[[10, 10],
[10, 10]]
'''
A+B
运行结果:
[[10, 11],
[12, 13]]
7.2 矩阵减法
A-B
'''A:[[0, 1],
[2, 3]]'''
'''B:[[10, 10],
[10, 10]]
'''
A-B
运行结果:
[[-10, -9],
[ -8, -7]]
7.3 矩阵乘法
A.dot(B)
'''A:[[0, 1],
[2, 3]]'''
'''B:[[10, 10],
[10, 10]]
'''
A.dot(B)
运行结果:
[[10, 10],
[50, 50]]
7.4 矩阵的转置
A.T
'''A:[[0, 1],
[2, 3]]'''
A.T
运行结果:
[[0, 2],
[1, 3]]
7.5 计算矩阵的乘法逆矩阵
linalg.inv()
'''A:[[0, 1],
[2, 3]]'''
invA=np.linalg.inv(A) #得到A的逆矩阵
invA
运行结果:
[[-1.5, 0.5],
[ 1. , 0. ]]
8.聚合操作
8.1 计算数组中元素之和
sum()
''' L:[[0.02804115, 0.89374515, 0.10564531, 0.67513696, 0.41822928,0.18239652, 0.03782683, 0.03502166, 0.75831584, 0.96686519]] '''
np.sum(L)
运行结果:
4.101223889019293
'''X:[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]]'''
np.sum(X,axis=0) #每一列的和
运行结果:
[24, 28, 32, 36]
'''X:[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]]'''
np.sum(X,axis=1) #每一行的和
运行结果:
[ 6, 22, 38, 54]
8.2 求数组中最小元素
min()
''' L:[[0.02804115, 0.89374515, 0.10564531, 0.67513696, 0.41822928,0.18239652, 0.03782683, 0.03502166, 0.75831584, 0.96686519]] '''
np.min(L)
运行结果:
0.02804115262184914
8.3 求数组中最大元素
max()
''' L:[[0.02804115, 0.89374515, 0.10564531, 0.67513696, 0.41822928,0.18239652, 0.03782683, 0.03502166, 0.75831584, 0.96686519]] '''
np.max(L)
运行结果:
0.9668651857442104
8.4 计算矩阵中所有元素乘积
prod
'''X:[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]]'''
np.prod(X)#矩阵中所有元素的乘积
运算结果:
0
8.5 计算数组中元素的算术平均值
mean()
'''X:[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]]'''
np.mean(X) #求平均值
运行结果:
7.5
8.6 计算数组 中元素的中位数(中值)
median()
'''X:[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]]'''
np.median(X) #求中位数
运行结果:
7.5
8.7 百分位数是统计中使用的度量,表示小于这个值的观察值的百分比
percentile(a, q, axis)
a: 输入数组
q: 要计算的百分位数,在 0 ~ 100 之间
axis: 沿着它计算百分位数的轴
'''X:[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]]'''
np.percentile(X,q=25)
运行结果:
3.75
8.8 求标准差
std()
'''X:[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]]'''
np.std(X)#求标准差
运行结果:
4.6097722286464435
9. 索引
9.1 返回最小元素的索引
argmin()
# x:[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
np.argmin(x) #x中最小值的索引位置
运行结果:
0
9.2 返回最大元素的索引
argmax()
# x:[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
np.argmax(x)#x中最大值的索引位置
运行结果:
15
9.3 将序列的所有元素随机排序
shuffle()
permutation()
如果传给permutation一个矩阵,它会返回一个洗牌后的矩阵副本;而shuffle只是对一个矩阵进行洗牌,无返回值。
如果传入一个整数,它会返回一个洗牌后的arange
#x:[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
np.random.shuffle(x)
运行结果:
[15, 10, 1, 3, 12, 11, 6, 0, 13, 2, 8, 14, 4, 5, 7, 9]
#x:[ 0, 12, 22, 35, 42, 51, 62, 73, 83, 94, 105, 112, 124, 135, 146, 157]
copyx=np.random.permutation(x)
copyx
运行结果:
[ 22, 12, 73, 112, 135, 51, 105, 146, 157, 42, 124, 62, 83,
94, 35, 0]
#x:[ 0, 12, 22, 35, 42, 51, 62, 73, 83, 94, 105, 112, 124, 135, 146, 157]
shuffindex=np.random.permutation(len(x))
shuffindex
运行结果:
[ 8, 3, 4, 5, 9, 6, 15, 2, 0, 14, 10, 11, 12, 1, 7, 13]
9.4 对数组进行排序
sort()
# x:[15, 10, 1, 3, 12, 11, 6, 0, 13, 2, 8, 14, 4, 5, 7, 9]
x.sort() #对x本身进行排序
x
运行结果:
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
''' [[6, 6, 8, 7],
[5, 9, 4, 2],
[1, 0, 5, 6],
[8, 6, 8, 2]]'''
X.sort()#对X每一行进行排序
X
运行结果:
[[6, 6, 7, 8],
[2, 4, 5, 9],
[0, 1, 5, 6],
[2, 6, 8, 8]]
''' [[6, 6, 8, 7],
[5, 9, 4, 2],
[1, 0, 5, 6],
[8, 6, 8, 2]]'''
np.sort(X,axis=0) #每一列进行排序
运行结果:
[[0, 1, 5, 6],
[2, 4, 5, 8],
[2, 6, 7, 8],
[6, 6, 8, 9]]
''' [[6, 6, 8, 7],
[5, 9, 4, 2],
[1, 0, 5, 6],
[8, 6, 8, 2]]'''
np.sort(X,axis=1) #每一行进行排序
运行结果:
[[6, 6, 7, 8],
[2, 4, 5, 9],
[0, 1, 5, 6],
[2, 6, 8, 8]]