简单记录一下笔记,方便以后查阅。
numpy是科学计算基础库,提供大量科学计算相关功能,比如数据统计,随机数生成等,其提供最核心类型为多为数组类型(ndarray),支持大量的维度数组与矩阵运算,Numpy支持向量处理ndarray对象,提高程序运算速度。
- 使用array数组的创建
使用array创建一维,二维,三维数组
import numpy as np
a=np.array([1,2,3,4])
b=np.array([[1,2,3,4],[2,3,4,5],[3,4,5,6]])
print(b)
print(type(b))
c=np.array([[[1,2,3,4],[2,3,4,5],[3,4,5,6]]])
print(c)
print(type(c))
[1 2 3 4]
[[1 2 3 4]
[2 3 4 5]
[3 4 5 6]]
<class ‘numpy.ndarray’>
[[[1 2 3 4]
[2 3 4 5]
[3 4 5 6]]]
<class ‘numpy.ndarray’>
array中dtype的使用:指定数组元素的类型
e=np.array([4,5,6],dtype=float,ndmin=3)
print(e)
print(e.dtype)
print(type(e))
print(e.shape)
[[[4. 5. 6.]]]
float64
<class ‘numpy.ndarray’>
(1, 1, 3)
- 使用arange创建数组 arange(start,stop,step,dtype)
a=np.arange(1,11,2,dtype=float)
print(a)
[1. 3. 5. 7. 9.]
使用random创建一维数组
a=np.random.random(size=5)
print(a)
print(type(a)
[0.83157395 0.15654983 0.72036078 0.12892552 0.73584286]
<class ‘numpy.ndarray’>
创建二维数组
b=np.random.random(size=(3,4))
print(b)
print(type(b))
[[0.72683455 0.04138861 0.96844183 0.81635114]
[0.1148116 0.49709996 0.16536158 0.3390249 ]
[0.45599801 0.01669408 0.74893079 0.40825785]]
<class ‘numpy.ndarray’>
创建三维数组
c=np.random.random(size=(3,4,5))
print(c)
print(type(c))
[[[0.86951203 0.37225917 0.95072502 0.77646651 0.09126167]
[0.17640583 0.48280342 0.63477072 0.30941115 0.10661427]
[0.25226838 0.64835903 0.6751478 0.23164397 0.2889746 ]
[0.1068626 0.76334846 0.97034864 0.98640148 0.5862251 ]]
[[0.8481732 0.27699148 0.5609061 0.44365779 0.78927503]
[0.77668409 0.4374574 0.92308199 0.60017668 0.44435186]
[0.70087703 0.02030743 0.03453481 0.51283466 0.03600702]
[0.08820035 0.9249886 0.83922645 0.43200715 0.2258239 ]]
[[0.15412024 0.75053583 0.54203033 0.42663348 0.2422215 ]
[0.70351357 0.06770975 0.56732562 0.41645515 0.03343333]
[0.65870401 0.85575317 0.08218955 0.08870605 0.18369361]
[0.3682271 0.1563156 0.21181074 0.46791335 0.62592415]]]
<class ‘numpy.ndarray’>
创建随机整数
d=np.random.randint(6,size=(2,3,4))
#小于6的随机整数
print(d)
e=np.random.randint(6,20,size=(2,3,4),dtype=np.int64)
#大于6小于20的随机整数
print(e)
[[[4 2 4 4]
[2 1 5 5]
[4 2 4 3]]
[[5 3 0 1]
[0 5 0 1]
[3 0 4 1]]]
[[[ 8 11 6 19]
[18 6 12 7]
[ 6 7 14 17]]
[[ 6 11 7 8]
[12 11 8 13]
[12 13 6 7]]]
创建标准正态分布,期望为0,方差为1,np.random.randn(size)
f=np.random.randn(2,3)
print(f)
[[-0.55266597 -1.51040979 -0.07656049]
[-1.08654262 0.81641191 -0.29925448]]
创建指定期望和方差的正态分布
np.random.normal(loc=‘期望’,scale=‘方差’,size=‘形状’)
g=np.random.normal(1,5,(5,2,2))
print(g)
[[[ 4.51064903 10.19667838]
[ 6.23389028 2.32001787]]
[[ 2.67466407 -2.50436868]
[-3.94762999 -9.2484413 ]]
[[-0.43546464 1.63408799]
[ 4.3532039 7.3476309 ]]
[[-6.77755129 2.56439565]
[-9.66189077 2.68719703]]
[[ 0.97060542 8.9539097 ]
[ 0.23180607 6.71725528]]]
- ndarray对象的属性
ndarray.ndim 秩,即轴的数量或维度的数量
.shape 数组的维度,对于矩阵,n行m列
.size 数组元素的总个数,相当于.shape 中n*m的值
.dtype ndarray 对象的元素类型
.itemsize ndarray对象中每个元素的大小,以字节为单位
.flags ndarray对象的内存信息
.real ndarray元素的实部
.imag ndarray元素的虚部
.data 包含实际数组元素的缓冲区,由于一般通过数组的索引获取元素,所以通常不需要这个属性
a=np.array([[1],[2],[3],[4]])
print(a)
b=np.arange(4,10)
print(b)
c=np.random.randn(2,3,4)
print(c)
print("ndim",a.ndim,b.ndim,c.ndim)
print("shape",a.shape,b.shape,c.shape)
print('dtype',a.dtype,b.dtype,c.dtype)
print('size',a.size,b.size,c.size)
print('itemsize',a.itemsize,b.itemsize,c.itemsize)
print('flags','\na',a.flags,'\nb',b.flags,'\nc',c.flags)
print('real','\na',a.real,'\nb',b.real,'\nc',c.real)
print('imag','\na',a.imag,'\nb',b.imag,'\nc',c.imag)
print('data',a.data,b.data,c.data)
[[1]
[2]
[3]
[4]]
[4 5 6 7 8 9]
[[[ 0.55099658 0.55213084 -0.3758489 -1.02504932]
[-0.46194989 1.77226355 -0.19308664 -2.27385215]
[ 0.31933194 0.87385735 -0.90201876 -0.50254732]]
[[-0.94266218 0.7199578 1.14558003 -0.82896663]
[ 0.20023188 -0.92978272 -0.96087977 -0.2561845 ]
[ 1.98703256 -1.89898035 -1.81898465 0.50882493]]]
ndim 2 1 3
shape (4, 1) (6,) (2, 3, 4)
dtype int32 int32 float64
size 4 6 24
itemsize 4 4 8
flags
a C_CONTIGUOUS : True
F_CONTIGUOUS : True
OWNDATA : True
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False
b C_CONTIGUOUS : True
F_CONTIGUOUS : True
OWNDATA : True
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False
c C_CONTIGUOUS : True
F_CONTIGUOUS : False
OWNDATA : True
WRITEABLE : True
ALIGNED : True
WRITEBACKIFCOPY : False
UPDATEIFCOPY : False
real
a [[1]
[2]
[3]
[4]]
b [4 5 6 7 8 9]
c [[[ 0.55099658 0.55213084 -0.3758489 -1.02504932]
[-0.46194989 1.77226355 -0.19308664 -2.27385215]
[ 0.31933194 0.87385735 -0.90201876 -0.50254732]]
[[-0.94266218 0.7199578 1.14558003 -0.82896663]
[ 0.20023188 -0.92978272 -0.96087977 -0.2561845 ]
[ 1.98703256 -1.89898035 -1.81898465 0.50882493]]]
imag
a [[0]
[0]
[0]
[0]]
b [0 0 0 0 0 0]
c [[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]]
data <memory at 0x00000166A59792B0> <memory at 0x00000166A5AA2940> <memory at 0x00000166A59D1E50>
- 其他方式创建数组
ndarray也可以通过其他方式创建
zeros创建指定大小的数组,数组以元素0来填充
numpy.zeros(shape,dtype=float,order=‘C’)
x=np.zeros((5,3),dtype=int)
print(x)
[[0 0 0]
[0 0 0]
[0 0 0]
[0 0 0]
[0 0 0]]
numpy.ones(shape,dtype=None,order=‘C’)
a=np.ones((3,5),dtype=float)
print(a)
[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]
numpy.empty(shape,dtype=float,order=‘C’)
b=np.empty((3,6))
print(b)
[[6.23042070e-307 4.67296746e-307 1.69121096e-306 9.34609111e-307
1.42413555e-306 1.78019082e-306]
[1.37959740e-306 6.23057349e-307 1.02360935e-306 1.69120416e-306
1.78022342e-306 6.23058028e-307]
[1.06811422e-306 1.33508761e-307 8.01097889e-307 1.20161526e-306
1.42410974e-306 5.97819431e-322]]
linspace用于创建一个一维数组,数组是一个等差数列构成,格式:numpy.linspace(start,stop,num=50,endpoint=True,retstep=Flase,dtype=None)
c=np.linspace(1,10,10)
d=np.linspace(1,10,10,False)
print(c)
print(d)
[ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10.]
[1. 1.9 2.8 3.7 4.6 5.5 6.4 7.3 8.2 9.1]
等比数列np.logspace(start,stop,num=50,endpoint=True,base=Flase,dtype=None) base是对数log的底数
e=np.logspace(0,9,10,base=2)
print(e)
[ 1. 2. 4. 8. 16. 32. 64. 128. 256. 512.]
- 切片和索引 ndarray对象的内容可以用过索引或切片来访问或修改,与Python中list的切片操作一样。
ndarray数组可以基于0-n的下标进行索引,并设置start,stop,及step参数进行,从原数组中切割出一个新数组。
a=np.arange(10)
print(a)
#索引访问,索引从0开始,长度为1
print('素引0的元素',a[0])
print('索引5的元素',a[5])
#负索引访问,倒数第一个的索引为-1
print('访问最后一个元素',a[-1])
print('访问倒数第三元素',a[-3])
#切片操作[start:stop:step]
print(a[:])
print(a[3:5])
#切片中负索引获取
print(a[::-1])
print(a[-1:-5:-1])
print(a[-1:-5:-2])
[0 1 2 3 4 5 6 7 8 9]
素引0的元素 0
索引5的元素 5
访问最后一个元素 9
访问倒数第三元素 7
[0 1 2 3 4 5 6 7 8 9]
[3 4]
[9 8 7 6 5 4 3 2 1 0]
[9 8 7 6]
[9 7]
二维数组中切片和索引的使用
a=np.arange(1,13)
print(a)
a=a.reshape((4,3))
print(a)
print(a[2][1])
#二维数组切片的使用 [行进行切片,列进行切片] [start:stop:step,start:stop:step]
#获取所有行所有列
print(a[:,:])
print('奇数行,1,2列','\n',a[::2,1:3])
#坐标获取
#获取第2行第3列的元素
print('第三行第零列,第四行第二列','\n',a[(2,3),(0,2)],'\n',np.array((a[2,0],a[3,2])))
#二维数组中负索引的使用
print('最后一行','\n',a[-1])
print('行倒序','\n',a[::-1])
print('列倒序','\n',a[:,::-1])
print('行列倒序','\n',a[::-1,::-1])
[ 1 2 3 4 5 6 7 8 9 10 11 12]
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
8
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
奇数行,1,2列
[[2 3]
[8 9]]
第三行第零列,第四行第二列
[ 7 12]
[ 7 12]
最后一行
[10 11 12]
行倒序
[[10 11 12]
[ 7 8 9]
[ 4 5 6]
[ 1 2 3]]
列倒序
[[ 3 2 1]
[ 6 5 4]
[ 9 8 7]
[12 11 10]]
行列倒序
[[12 11 10]
[ 9 8 7]
[ 6 5 4]
[ 3 2 1]]
- 数组的复制
a=np.arange(1,13).reshape(4,3)
print(a)
#对a数组进行切片处理,获取第一,二行,第一,二列,并赋值给一个变量
_a=a[:2,:2]
print(_a)
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
[[1 2]
[4 5]]
#对_a中第一行第一列的值进行修改,通过切片可以获得新数组,即使赋值给新的变量,但还是原来数组的视图,如果对切片数组中元素的值进行修改会影响到原来的数组
_a[0][0]=100
print(_a)
print(a)
[[100 2]
[ 4 5]]
[[100 2 3]
[ 4 5 6]
[ 7 8 9]
[ 10 11 12]]
#可以使用numpy中的copy方法实现
_aa=np.copy(a[:2,:2])
_aa[0][0]=200
print(_aa)
print(a)
[[200 2]
[ 4 5]]
[[100 2 3]
[ 4 5 6]
[ 7 8 9]
[ 10 11 12]]
- 修改数组的维度(提高数组的维度,降低数组的维度和数组的转置)
通过reshape方法将一维数组修改为二维三维,reshape(self,shape,order=‘C’)
b=np.arange(1,25).reshape((4,6))
print(b)
c=b.reshape((2,3,4))
print(c)
[[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]
[13 14 15 16 17 18]
[19 20 21 22 23 24]]
[[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
[[13 14 15 16]
[17 18 19 20]
[21 22 23 24]]]
通过np.reshape来进行修改 np.reshape(array,newshape,order=‘C’)
d=np.reshape(np.arange(1,25),(4,3,2))
print(d)
[[[ 1 2]
[ 3 4]
[ 5 6]]
[[ 7 8]
[ 9 10]
[11 12]]
[[13 14]
[15 16]
[17 18]]
[[19 20]
[21 22]
[23 24]]]
将多维数组修改为一维数组
a=d.reshape(24)#计算出元素个数
print(a)
_aa=d.reshape(-1)#无需计算出元素个数
print(_aa)
#使用ravel,flatten函数将多维数组转化为一维数组
a=d.ravel()
print(a)
aa=d.flatten()
print(aa)
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24]
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24]
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24]
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24]
- 数组的拼接:水平方向和竖直方向
水平方向,通过hstack函数可以将两个或多个数组水平组合起来形成一个数组
a=np.array([[1,2,3],[4,5,6]])
b=np.array([[11,12,13],[14,15,16]])
print(a,'\n',b)
c=np.hstack([a,b])
print(c)
[[1 2 3]
[4 5 6]]
[[11 12 13]
[14 15 16]]
[[ 1 2 3 11 12 13]
[ 4 5 6 14 15 16]]
竖直方向,通过vstack函数将两个数组垂直组合
d=np.vstack((a,b))
print(d)
[[ 1 2 3]
[ 4 5 6]
[11 12 13]
[14 15 16]]
使用concatenate 连接现有轴的数组序列 np.concatenate 函数用于沿指定轴连接相同形状的两个或多个数组,格式np.concatenate((a1,a2,…),axis=0)
其中a1,a2等是相同类型的数组
e=np.concatenate((a,b),axis=1)
print(e)
[[ 1 2 3 11 12 13]
[ 4 5 6 14 15 16]]
三维数组有三个轴,axis=0或1或2
a=np.arange(1,13).reshape(1,2,6)
b=np.arange(101,113).reshape(1,2,6)
c=np.arange(201,213).reshape(1,2,6)
print(a,'\n',b,'\n',c)
[[[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]]]
[[[101 102 103 104 105 106]
[107 108 109 110 111 112]]]
[[[201 202 203 204 205 206]
[207 208 209 210 211 212]]]
print('三维,sxis=0','\n',np.concatenate((a,b,c),axis=0),'\n',np.concatenate((a,b,c),axis=0).shape)
三维,sxis=0
[[[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]]
[[101 102 103 104 105 106]
[107 108 109 110 111 112]]
[[201 202 203 204 205 206]
[207 208 209 210 211 212]]]
(3, 2, 6)
print('三维,axis=1','\n',np.concatenate((a,b,c),axis=1),'\n',np.concatenate((a,b,c),axis=1).shape)
三维,axis=1
[[[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]
[101 102 103 104 105 106]
[107 108 109 110 111 112]
[201 202 203 204 205 206]
[207 208 209 210 211 212]]]
(1, 6, 6)
print('三维,axis=2','\n',np.concatenate((a,b,c),axis=2),'\n',np.concatenate((a,b,c),axis=0).shape)
三维,axis=2
[[[ 1 2 3 4 5 6 101 102 103 104 105 106 201 202 203 204 205
206]
[ 7 8 9 10 11 12 107 108 109 110 111 112 207 208 209 210 211
212]]]
(3, 2, 6)
- 数组的分隔 split分隔 np.split函数沿特定的轴将数组分割为子数组,格式 np.split(ary,indices_or_sections,axis)
ary:被分割的数组 indices_or_sections:如果是一个整数,就用该数平均切分,如果是一个数组,为沿轴切分的位置。
axis:沿着哪个维度进行切分,默认为0,横向切分,为1时,纵向切分。
hsplit,按水平方向进行分割
vsplit 按垂直方向进行分割,
hsplit,vsplit参数与split一致(但是少了axis参数)
x=np.arange(1,9)
print(x)
a=np.split(x,4)
print(a)
print(a[0])
print(a[2])
b=np.split(x,[3,5])
print(b)
#二维
aa=np.arange(1,17).reshape(4,4)
print(aa)
print('axis=0 垂直方向(按行),平均分割')
r,w=np.split(aa,2,axis=0)
print(r,'\n',w)
print('axis=0 垂直方向(按行),位置分割')
r,w,k=np.split(aa,[2,3],axis=0)
print(r,'\n',w,'\n',k)
[1 2 3 4 5 6 7 8]
[array([1, 2]), array([3, 4]), array([5, 6]), array([7, 8])]
[1 2]
[5 6]
[array([1, 2, 3]), array([4, 5]), array([6, 7, 8])]
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]
[13 14 15 16]]
axis=0 垂直方向(按行),平均分割
[[1 2 3 4]
[5 6 7 8]]
[[ 9 10 11 12]
[13 14 15 16]]
axis=0 垂直方向(按行),位置分割
[[1 2 3 4]
[5 6 7 8]]
[[ 9 10 11 12]]
[[13 14 15 16]]
- 数组的转置
使用transpose函数进行转置
a=np.arange(1,25).reshape(8,3)
print(a)
print('transpose函数对数组进行转置')
b=np.transpose(a)
# b=a.transpose()
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]
[13 14 15]
[16 17 18]
[19 20 21]
[22 23 24]]
transpose函数对数组进行转置
[[ 1 4 7 10 13 16 19 22]
[ 2 5 8 11 14 17 20 23]
[ 3 6 9 12 15 18 21 24]]
# 可以使用.T
print(a.T)
[[ 1 4 7 10 13 16 19 22]
[ 2 5 8 11 14 17 20 23]
[ 3 6 9 12 15 18 21 24]]
多维数组进行转置
a=a.reshape(2,3,4)
print(a,'\n',a.shape)
print('对于三维a[i][j][k]进行转置,默认将 i和k进行交换')
b=np.transpose(a)
print(b,'\n',b.shape)
[[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
[[13 14 15 16]
[17 18 19 20]
[21 22 23 24]]]
(2, 3, 4)
对于三维a[i][j][k]进行转置,默认将 i和k进行交换
[[[ 1 13]
[ 5 17]
[ 9 21]]
[[ 2 14]
[ 6 18]
[10 22]]
[[ 3 15]
[ 7 19]
[11 23]]
[[ 4 16]
[ 8 20]
[12 24]]]
(4, 3, 2)
shape(0,1,2),0维,1维,2维在transpose函数第二个参数中调换位置进行转置
c=np.transpose(a,(1,2,0))
print(c,'\n',c.shape)
[[[ 1 13]
[ 2 14]
[ 3 15]
[ 4 16]]
[[ 5 17]
[ 6 18]
[ 7 19]
[ 8 20]]
[[ 9 21]
[10 22]
[11 23]
[12 24]]]
(3, 4, 2)
- 算术函数
如果参与运算的两个对象都是ndarray,并且形状相同,那么会对位彼此之间进行(±*/)运算。
Numpy算术函数包含简单的加减乘除:add(),subtract().multiply(),divide()
a=np.arange(9).reshape(3,3)
b=np.array([10,20,30])
c=np.array([[40],[50],[60]])
print(a)
print(b)
print(c)
print('加法')
print(np.add(a,b))
print(a+b)
print(a+c)
print('减法')
print(np.subtract(b,a))
print(b-a)
print(c-a)
print('乘法')
print(a*b)
print(a*c)
print('除法')
print(a/b)
print(a/c)
[[0 1 2]
[3 4 5]
[6 7 8]]
[10 20 30]
[[40]
[50]
[60]]
加法
[[10 21 32]
[13 24 35]
[16 27 38]]
[[10 21 32]
[13 24 35]
[16 27 38]]
[[40 41 42]
[53 54 55]
[66 67 68]]
减法
[[10 19 28]
[ 7 16 25]
[ 4 13 22]]
[[10 19 28]
[ 7 16 25]
[ 4 13 22]]
[[40 39 38]
[47 46 45]
[54 53 52]]
乘法
[[ 0 20 60]
[ 30 80 150]
[ 60 140 240]]
[[ 0 40 80]
[150 200 250]
[360 420 480]]
除法
[[0. 0.05 0.06666667]
[0.3 0.2 0.16666667]
[0.6 0.35 0.26666667]]
[[0. 0.025 0.05 ]
[0.06 0.08 0.1 ]
[0.1 0.11666667 0.13333333]]
out函数的使用
y=np.empty((3,3),dtype='int')
np.multiply(a,10,out=y)
print(y)
[[ 0 10 20]
[30 40 50]
[60 70 80]]
三角函数的使用
(应该是 a*np.pi/180求出a中个元素对应的弧长,然后再求的。未验证,只是当作笔记,以后用到再验证)
a=np.array([0,30,60,90])
print(np.sin(a))
[ 0. -0.98803162 -0.30481062 0.89399666]
around函数:四舍五入 ceil函数:向上取整 floor函数:向下取整
a=np.array([1.0,4.55,123,0.567,25.532])
print('原值',a)
print('around',np.around(a))
print('ceil',np.ceil(a))
print('floor',np.floor(a))
原值 [ 1. 4.55 123. 0.567 25.532]
around [ 1. 5. 123. 1. 26.]
ceil [ 1. 5. 123. 1. 26.]
floor [ 1. 4. 123. 0. 25.]
- 统计函数
numpy.power()函数将第一个输入数组中的元素作为底数,计算它与第二个输入数组中相应元素的幂。
a=np.arange(12).reshape(3,4)
print('原来的数组')
print(a)
print('指数运算:以a中元素为底数')
print(np.power(a,2))
print('幂运算:以2为底数')
print(np.power(2,a))
原来的数组
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
指数运算:以a中元素为底数
[[ 0 1 4 9]
[ 16 25 36 49]
[ 64 81 100 121]]
幂运算:以2为底数
[[ 1 2 4 8]
[ 16 32 64 128]
[ 256 512 1024 2048]]
power中out的使用
b=np.power(a,2)
print(b)
np.power(2,a,out=b)
print(b)
[[ 0 1 4 9]
[ 16 25 36 49]
[ 64 81 100 121]]
[[ 1 2 4 8]
[ 16 32 64 128]
[ 256 512 1024 2048]]
median() 求中位数
#一维数组的中位数
a=np.array([4,3,2,1,5,2,1,3])
#对数组排序,再求中位数
print(np.median(a))
2.5
#二维数组 要通过axis指定轴
aa=np.arange(1,13).reshape(3,4)
print(aa)
print('垂直方向','\n',np.median(aa,axis=0))
print('水平方向','\n',np.median(aa,axis=1))
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]]
垂直方向
[5. 6. 7. 8.]
水平方向
[ 2.5 6.5 10.5]
mean()求平均值
print('一维:',np.mean(a))
print('二维,垂直方向','\n',np.mean(aa,axis=0))
print('二维,水平方向','\n',np.mean(aa,axis=1))
一维: 2.625
二维,垂直方向
[5. 6. 7. 8.]
二维,水平方向
[ 2.5 6.5 10.5]
sum() max() min()
print('max:',np.max(a))
print('sum:',np.sum(a))
print('min:',np.min(a))
max: 5
sum: 21
min: 1
argmax argmin 返回最大最小值的下标
print('argmax:',np.argmax(a))
print('argmin:',np.argmin(a))
argmax: 4
argmin: 3