numpy
Posted baiyifengyun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了numpy相关的知识,希望对你有一定的参考价值。
import numpy as np
np.arange(10)
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
# 对列表中的元素开平方
b = [3, 4, 9]
np.sqrt(b)
array([1.73205081, 2. , 3. ])
# 使用array函数创建一维数组
a = np.array([1,2,3,4])
a
array([1, 2, 3, 4])
# 使用array创建二维数组
b = np.array([[1,2,3],[3,4,6]])
b
array([[1, 2, 3],
[3, 4, 6]])
## 使用dtype参数来设置数组的类型
c = np.array([3,4,5], dtype=float)
c
array([3., 4., 5.])
## 使用ndim参数来设置数组的维度
d = np.array([3,4,6], dtype=float,ndmin=3)
d
array([[[3., 4., 6.]]])
使用arange创建数组
e = np.arange(1,11)
e
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
## 设置步长
np.arange(1,11,2)
array([1, 3, 5, 7, 9])
## 设置dtype
np.arange(1,11,2,dtype=float)
array([1., 3., 5., 7., 9.])
随机数
## random函数创建0.0-1.0之间的随机数数组,注意不包括号1.0
np.random.random(size=5) ### 一维数组
array([0.14109248, 0.57757689, 0.31587811, 0.90959059, 0.25585209])
## 创建二维数组
np.random.random(size=(3,4)) ### 3,4 表示3行吧列
array([[0.27963513, 0.28511459, 0.63027425, 0.99946311],
[0.14654823, 0.62364526, 0.81366614, 0.88192038],
[0.91244454, 0.4028762 , 0.11921253, 0.54991022]])
### 创建三维数组
np.random.random(size=(2,3,4)) ### 2,3,4表示创建2个3行4列
array([[[0.27409587, 0.11804355, 0.34964509, 0.93155881],
[0.32850319, 0.42947898, 0.21363423, 0.94016219],
[0.66905712, 0.72171425, 0.23520955, 0.4893854 ]],
[[0.17257756, 0.99171624, 0.31052962, 0.61989267],
[0.84792176, 0.79669383, 0.92678657, 0.90099817],
[0.35242589, 0.95967321, 0.00670096, 0.91882932]]])
随机整数
np.random.randint(6,size=10) # 生成0到5之间的10个随机整数
array([0, 0, 1, 4, 1, 4, 2, 5, 3, 3])
## 生成5-10之间的随机整数的二维数组
np.random.randint(5,11,size=(4,3))
array([[ 9, 7, 10],
[10, 7, 5],
[ 8, 8, 7],
[ 7, 7, 10]])
## 生成三维数组
np.random.randint(5,11,size=(2,4,3))
array([[[ 7, 8, 5],
[ 7, 5, 6],
[ 8, 8, 8],
[ 8, 5, 10]],
[[ 7, 10, 6],
[ 6, 8, 8],
[ 9, 6, 6],
[ 5, 8, 6]]])
np.random.randint(5,11,size=(1,3,4))
array([[[ 7, 10, 5, 10],
[ 9, 10, 5, 10],
[ 6, 9, 10, 9]]])
randint中参数dtype的使用
d = np.random.randint(10,size=5)
d.dtype
dtype(‘int32‘)
f = np.random.randint(10,size=5,dtype=np.int64)
f.dtype
dtype(‘int64‘)
正态分布
## 一维
np.random.randn(4)
array([-0.65738946, -1.36270863, -0.5901353 , 0.63707697])
### 二维
np.random.randn(2,3)
array([[-0.86151919, -1.35400762, 0.60027059],
[-0.44439982, -0.69328396, 1.15670548]])
### 三维
np.random.randn(2,3,4)
array([[[ 0.21530965, 1.419034 , 1.2442361 , -1.47677229],
[ 0.41533172, -0.8700582 , 1.28418702, 0.77605165],
[-1.63712476, -0.24687974, 1.38743355, -0.13334918]],
[[-0.70745225, -0.73391537, -1.29003062, -0.96750294],
[-0.48278997, -0.3381754 , 2.30733888, -1.00871624],
[-0.88437762, 0.68254093, 0.90741469, -0.70638608]]])
以上是标准的正态分布:期望为0,方差为1
创建指定期望和方差值的正态分布
np.random.normal(size=5) ### 默认的期望是loc=0.0,方差是scale=1.0
array([ 0.37960069, 0.77483781, 0.23537764, 0.97151935, -0.08167272])
### 指定loc和scale
np.random.normal(loc=2,scale=3,size=5)
array([ 5.49844885, 6.50644569, -2.05466087, 2.53766272, 2.40695409])
np.random.normal(loc=2,scale=3,size=(3,4))
array([[ 6.95415439, 2.37018031, -2.9699421 , -1.12177319],
[ 2.69676034, 0.30211155, 4.04696743, -1.63856255],
[ 2.90005922, -3.83970821, 3.07125062, 3.78305087]])
ndarray对象属性
### 分别创建一维,二维,三维数组
a1 = np.array([1,2,3,4,5])
b1 = np.random.randint(4,10,size=(3,4))
c1 = np.random.randn(2,3,4)
a1
array([1, 2, 3, 4, 5])
b1
array([[6, 9, 7, 6],
[7, 5, 7, 5],
[9, 8, 7, 7]])
c1
array([[[-1.70317407, 0.57392171, -0.00487497, -0.32478167],
[ 0.69572239, -0.34328439, -1.31930338, -0.26807493],
[ 1.22140779, -0.49691301, 0.69210554, 0.14662866]],
[[ 0.083173 , -1.14836617, 1.5909447 , 0.55987063],
[-0.59362023, -0.73479667, 0.78516186, -0.46653616],
[-0.02626701, -1.49199613, 1.2136789 , 2.01738911]]])
ndim属性
print(a1.ndim,b1.ndim,c1.ndim)
1 2 3
shape属性
print(a1.shape,b1.shape,c1.shape)
(5,) (3, 4) (2, 3, 4)
dtype属性
print(a1.dtype,b1.dtype,c1.dtype)
int32 int32 float64
size属性,元素的总个数
print(a1.size,b1.size,c1.size)
5 12 24
itemsize 每个元素所占的字节
print(a1.itemsize,b1.itemsize,c1.itemsize)
4 4 8
zeros创建数组
np.zeros(5)
array([0., 0., 0., 0., 0.])
np.zeros(5,dtype=int)
array([0, 0, 0, 0, 0])
np.zeros((3,4))
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
ones创建数组
np.ones(10)
array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
np.ones((2,5),dtype=int)
array([[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]])
empty
np.empty(8)
array([0.0000000e+000, 0.0000000e+000, 0.0000000e+000, 0.0000000e+000,
0.0000000e+000, 7.3516968e-321, 1.3796137e-306, 0.0000000e+000])
np.empty((3,4))
array([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
linspace 创建等差数列
np.linspace(1,10,10)
array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
np.linspace(5,20,5)
array([ 5. , 8.75, 12.5 , 16.25, 20. ])
np.linspace(5,20,5,endpoint=False) #### endpoint默认为True
array([ 5., 8., 11., 14., 17.])
logspace创建等比数列
np.logspace(0,9,10,base=2) ### base默认为空0。0
array([ 1., 2., 4., 8., 16., 32., 64., 128., 256., 512.])
numpy 索引与切片
d1 = np.arange(10)
d1
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
d1[0]
0
d1[-1]
9
### 切片[start:stop:step]
d1[:]
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
d1[3:]
array([3, 4, 5, 6, 7, 8, 9])
d1[3:5]
array([3, 4])
d1[1:7:2]
array([1, 3, 5])
d1[::-1] # -1表示反向获取
array([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])
d1[-5:-2]
array([5, 6, 7])
d1[-7:-2:2]
array([3, 5, 7])
二维数组的切片
x = np.arange(1,13)
x
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
### 对一维数组修改形状
x1 = x.reshape(4,3)
x1
array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]])
x1[1]
array([4, 5, 6])
x1[1][2]
6
### 二维数组切片【行进行切片,列进行切片】
x1[:,:]
array([[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]])
x1[:,1]
array([ 2, 5, 8, 11])
x1[:,0:2] ### 所有行第一列第二列
array([[ 1, 2],
[ 4, 5],
[ 7, 8],
[10, 11]])
x1[::2,:] #### 奇数行所有列
array([[1, 2, 3],
[7, 8, 9]])
x1[::2,0:2] ## 奇数行,第一,二列
array([[1, 2],
[7, 8]])
坐标获取
x1[1,2]
6
x1[(1,2),(2,0)]
array([6, 7])
x1[-1]
array([10, 11, 12])
x1[::-1]
array([[10, 11, 12],
[ 7, 8, 9],
[ 4, 5, 6],
[ 1, 2, 3]])
x1[::-1,::-1]
array([[12, 11, 10],
[ 9, 8, 7],
[ 6, 5, 4],
[ 3, 2, 1]])
数组的复制
sub_a = x1[:2,:2]
sub_a
array([[1, 2],
[4, 5]])
sub_a[0][0] =100
sub_a
array([[100, 2],
[ 4, 5]])
x1
array([[100, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[ 10, 11, 12]])
### 通过切片可以获取到新的数组,即使赋值给新的变量,但是还是原来数组的视图,如果对切片数组中元素的值进行修改,原数组中也会改变
### numpy中的copy实现了深 考贝
sub = np.copy(x1[:2,:2])
sub
array([[100, 2],
[ 4, 5]])
sub[0][1] = 21
sub
array([[100, 21],
[ 4, 5]])
x1
array([[100, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[ 10, 11, 12]])
改变数组的维度
w = np.arange(1,25)
w
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24])
w.reshape(4,6) ###二维
array([[ 1, 2, 3, 4, 5, 6],
[ 7, 8, 9, 10, 11, 12],
[13, 14, 15, 16, 17, 18],
[19, 20, 21, 22, 23, 24]])
w.reshape((3,8))
array([[ 1, 2, 3, 4, 5, 6, 7, 8],
[ 9, 10, 11, 12, 13, 14, 15, 16],
[17, 18, 19, 20, 21, 22, 23, 24]])
### 三维
w.reshape((2,3,4))
array([[[ 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(w,(3,8))
array([[ 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(w,(4,3,2))
array([[[ 1, 2],
[ 3, 4],
[ 5, 6]],
[[ 7, 8],
[ 9, 10],
[11, 12]],
[[13, 14],
[15, 16],
[17, 18]],
[[19, 20],
[21, 22],
[23, 24]]])
将多维修改成一维数组
bb = np.reshape(w,(2,4,3))
bb
array([[[ 1, 2, 3],
[ 4, 5, 6],
[ 7, 8, 9],
[10, 11, 12]],
[[13, 14, 15],
[16, 17, 18],
[19, 20, 21],
[22, 23, 24]]])
bb.reshape(24)
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24])
bb.reshape(-1)
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24])
bb.ravel()
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24])
bb.flatten()
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24])
数组的拼接
### 创建两个二维数组
h1 = np.array([[1,2,3],[4,5,6]])
h2 = np.array([[11,12,13],[14,15,16]])
h1
array([[1, 2, 3],
[4, 5, 6]])
h2
array([[11, 12, 13],
[14, 15, 16]])
### 小平拼接
np.hstack([h1,h2])
array([[ 1, 2, 3, 11, 12, 13],
[ 4, 5, 6, 14, 15, 16]])
np.hstack((h1,h2))
array([[ 1, 2, 3, 11, 12, 13],
[ 4, 5, 6, 14, 15, 16]])
#### 垂直拼接
np.vstack((h1,h2))
array([[ 1, 2, 3],
[ 4, 5, 6],
[11, 12, 13],
[14, 15, 16]])
### concatenate拼接
np.concatenate((h1,h2),axis=0) ## axis=0是默认方向,垂直方向
array([[ 1, 2, 3],
[ 4, 5, 6],
[11, 12, 13],
[14, 15, 16]])
np.concatenate((h1,h2))
array([[ 1, 2, 3],
[ 4, 5, 6],
[11, 12, 13],
[14, 15, 16]])
np.concatenate((h1,h2),axis=1)
array([[ 1, 2, 3, 11, 12, 13],
[ 4, 5, 6, 14, 15, 16]])
### 三维数组 axis有0,1,2
one = np.arange(1,13).reshape(1,2,6)
two = np.arange(101,113).reshape(1,2,6)
one
array([[[ 1, 2, 3, 4, 5, 6],
[ 7, 8, 9, 10, 11, 12]]])
two
array([[[101, 102, 103, 104, 105, 106],
[107, 108, 109, 110, 111, 112]]])
np.concatenate((one,two),axis=0)
array([[[ 1, 2, 3, 4, 5, 6],
[ 7, 8, 9, 10, 11, 12]],
[[101, 102, 103, 104, 105, 106],
[107, 108, 109, 110, 111, 112]]])
np.concatenate((one,two),axis=1)
array([[[ 1, 2, 3, 4, 5, 6],
[ 7, 8, 9, 10, 11, 12],
[101, 102, 103, 104, 105, 106],
[107, 108, 109, 110, 111, 112]]])
np.concatenate((one,two),axis=2)
array([[[ 1, 2, 3, 4, 5, 6, 101, 102, 103, 104, 105, 106],
[ 7, 8, 9, 10, 11, 12, 107, 108, 109, 110, 111, 112]]])
数组的分隔
s = np.arange(1,13)
s
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
### 平均分隔
np.split(s,4)
[array([1, 2, 3]), array([4, 5, 6]), array([7, 8, 9]), array([10, 11, 12])]
### 按位置分隔
np.split(s,(4,6))
[array([1, 2, 3, 4]), array([5, 6]), array([ 7, 8, 9, 10, 11, 12])]
#### 二维
ss = np.array([[1,2,3,4],[5,6,7,8],[8,10,11,12],[13,14,15,16]])
ss
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 8, 10, 11, 12],
[13, 14, 15, 16]])
np.split(ss,2,axis=0)
[array([[1, 2, 3, 4],
[5, 6, 7, 8]]), array([[ 8, 10, 11, 12],
[13, 14, 15, 16]])]
np.split(ss,(2,3),axis=0)
[array([[1, 2, 3, 4],
[5, 6, 7, 8]]), array([[ 8, 10, 11, 12]]), array([[13, 14, 15, 16]])]
np.split(ss,2,axis=1)
[array([[ 1, 2],
[ 5, 6],
[ 8, 10],
[13, 14]]), array([[ 3, 4],
[ 7, 8],
[11, 12],
[15, 16]])]
np.split(ss,[3],axis=1)
[array([[ 1, 2, 3],
[ 5, 6, 7],
[ 8, 10, 11],
[13, 14, 15]]), array([[ 4],
[ 8],
[12],
[16]])]
## hsplit()小平方向分隔
np.hsplit(ss,2)
[array([[ 1, 2],
[ 5, 6],
[ 8, 10],
[13, 14]]), array([[ 3, 4],
[ 7, 8],
[11, 12],
[15, 16]])]
np.hsplit(ss,[3])
[array([[ 1, 2, 3],
[ 5, 6, 7],
[ 8, 10, 11],
[13, 14, 15]]), array([[ 4],
[ 8],
[12],
[16]])]
np.vsplit(ss,2) ### vsplit()垂直分隔
[array([[1, 2, 3, 4],
[5, 6, 7, 8]]), array([[ 8, 10, 11, 12],
[13, 14, 15, 16]])]
np.vsplit(ss,[1])
[array([[1, 2, 3, 4]]), array([[ 5, 6, 7, 8],
[ 8, 10, 11, 12],
[13, 14, 15, 16]])]
数组的转置
### 创建二维数组
tr = np.arange(1,25).reshape(8,3)
tr
array([[ 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 a[j][i] --->a[i][j]
tr.transpose()
array([[ 1, 4, 7, 10, 13, 16, 19, 22],
[ 2, 5, 8, 11, 14, 17, 20, 23],
[ 3, 6, 9, 12, 15, 18, 21, 24]])
tr.T
array([[ 1, 4, 7, 10, 13, 16, 19, 22],
[ 2, 5, 8, 11, 14, 17, 20, 23],
[ 3, 6, 9, 12, 15, 18, 21, 24]])
np.transpose(tr)
array([[ 1, 4, 7, 10, 13, 16, 19, 22],
[ 2, 5, 8, 11, 14, 17, 20, 23],
[ 3, 6, 9, 12, 15, 18, 21, 24]])
## 多维数组
trs = tr.reshape(2,3,4)
trs
array([[[ 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.transpose(trs) ## 默认是从a[i][j][k] ---->a[k][j][i]
array([[[ 1, 13],
[ 5, 17],
[ 9, 21]],
[[ 2, 14],
[ 6, 18],
[10, 22]],
[[ 3, 15],
[ 7, 19],
[11, 23]],
[[ 4, 16],
[ 8, 20],
[12, 24]]])
np.transpose(trs,(1,2,0)) ### 这里的1,2,0是数组维度的下标
array([[[ 1, 13],
[ 2, 14],
[ 3, 15],
[ 4, 16]],
[[ 5, 17],
[ 6, 18],
[ 7, 19],
[ 8, 20]],
[[ 9, 21],
[10, 22],
[11, 23],
[12, 24]]])
函数
###创建一个二维数组
v2 = np.arange(9).reshape(3,3)
v2
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
### 创建一个一维数组
v1 = np.array([10,10,10])
v1
array([10, 10, 10])
### 算术函数
np.add(v1,v2)
array([[10, 11, 12],
[13, 14, 15],
[16, 17, 18]])
v1+v2
array([[10, 11, 12],
[13, 14, 15],
[16, 17, 18]])
np.subtract(v1,v2)
array([[10, 9, 8],
[ 7, 6, 5],
[ 4, 3, 2]])
### out参数的使用
y = np.empty((3,3),dtype=np.int)
np.multiply(v2,10,out=y)
array([[ 0, 10, 20],
[30, 40, 50],
[60, 70, 80]])
### 三角函数
np.sin(np.array([0,30,60,90]))
array([ 0. , -0.98803162, -0.30481062, 0.89399666])
### 向上取整
f = np.array([1.0,4.55,123,0.567,25.33])
np.around(f)
array([ 1., 5., 123., 1., 25.])
np.ceil(f)
array([ 1., 5., 123., 1., 26.])
## 向下取整
np.floor(f)
array([ 1., 4., 123., 0., 25.])
统计函数
### power()幂次方
t = np.arange(1,13).reshape(3,4)
t
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
np.power(t,2)
array([[ 1, 4, 9, 16],
[ 25, 36, 49, 64],
[ 81, 100, 121, 144]], dtype=int32)
## power()中参数out的使用
x = np.arange(5)
y = np.zeros(10)
np.power(2,x,out=y[:5])
array([ 1., 2., 4., 8., 16.])
y
array([ 1., 2., 4., 8., 16., 0., 0., 0., 0., 0.])
求中位数
z = np.array([4,3,2,5,2,1])
np.median(z) ### 对数组排序 [1,2,2,3,4,5] 数组中元素个数为偶数 中位数指:中间两个数的平均值
2.5
np.median(np.array([4,3,2,5,6])) # 对数组排序 [2,2,3,4,5] 数组中元素个数为奇数 中位数指:中间的数
4.0
### 二维数组
z2 = np.arange(1,13).reshape(3,4)
z2
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
np.median(z2,axis=0)
array([5., 6., 7., 8.])
np.median(z2,axis=1)
array([ 2.5, 6.5, 10.5])
mean求平均值
np.mean(z)
2.8333333333333335
np.mean(z2)
6.5
np.mean(z2,axis=0)
array([5., 6., 7., 8.])
np.mean(z2,axis=1)
array([ 2.5, 6.5, 10.5])
#sum()
np.max(z)
5
np.sum(z)
17
np.min(z)
1
np.argmax(z) ### 返回最大值为的下标
3
np.argmin(z)
5
以上是关于numpy的主要内容,如果未能解决你的问题,请参考以下文章