import numpy as np
## 创建一个一维数组
np.array([1,2,3])
array([1, 2, 3])
np.array([1,2,3])
array([1, 2, 3])
np.array([[1,2,3]])
array([[1, 2, 3]])
np.array([[1,2,3]]).T
array([[1],[2],[3]])
np.ones(4)
array([1., 1., 1., 1.])
np.zeros(4)
array([0., 0., 0., 0.])
np.random.random(3)
array([0.76998129, 0.65607576, 0.23038928])
常见错误
1.保留中括号
a = np.array([[1,2,3]])
a.dtype
dtype('int32')
a = np.array([[1.,2,3]])
a.dtype
dtype('float64')
a = np.array([[1,2,'3']])
a.dtype
dtype('<U11')
常用的api
## 创建一个数组1-10
np.arange(1,11)
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
np.arange(-2,2,0.5)# 0.5是步长
array([-2. , -1.5, -1. , -0.5, 0. , 0.5, 1. , 1.5])
## 把数据切割几段
np.linspace(0,2,5)
array([0. , 0.5, 1. , 1.5, 2. ])
np.linspace(0,100,10)
array([ 0. , 11.11111111, 22.22222222, 33.33333333,44.44444444, 55.55555556, 66.66666667, 77.77777778,88.88888889, 100. ])
## 创建高维度数组
np.ones([2,3])
array([[1., 1., 1.],[1., 1., 1.]])
np.zeros([3,3])
array([[0., 0., 0.],[0., 0., 0.],[0., 0., 0.]])
创建对角矩阵
a =np.eye(3)
b = np.array([1,2,3])
np.dot(a,b)
array([1., 2., 3.])
c = np.array([4,5,6])
np.diag(c)
array([[4, 0, 0],[0, 5, 0],[0, 0, 6]])
numpy数组的属性
c = np.array([1,2,3,4])
c.ndim
1
- 数组的形状
a= np.array([[2,3,4],[1,3,7]])
a.shape
(2, 3)
a.size #数量
6
a.dtype
dtype('int32')
数组的运算
data = np.array([1,2])
ones = np.ones(2)
data
array([1, 2])
ones
array([1., 1.])
data+ones
array([2., 3.])
data - ones
array([0., 1.])
data * ones
array([1., 2.])
data/ones
array([1., 2.])
data * 3
array([3, 6])
data ** 2
array([1, 4], dtype=int32)
np.sin(data)
array([0.84147098, 0.90929743])
np.exp(data)
array([2.71828183, 7.3890561 ])
np.sqrt(data)
array([1. , 1.41421356])
布尔过滤
a = np.array([168,159,165,178,190])
a >=175
array([False, False, False, True, True])
a = [10,20,30,40]
a * 2
[10, 20, 30, 40, 10, 20, 30, 40]
索引入门
a = np.array([1,2,3])
a[1]
2
#索引范围
a[0:2]
array([1, 2])
a[1:30]
array([2, 3])
a[1:]
array([2, 3])
a[:5]
array([1, 2, 3])
a[:]
array([1, 2, 3])
a[-1]
3
a[-3:-1]
array([1, 2])
numpy数组的元素操作
data = np.array([1,2,3,4,5,6,7,8,9,8,7,6,5,4])
data.max()
9
data.min()
1
data.sum()
75
data.mean() #求平均值
5.357142857142857
二位数组切片
data = np.array([[1,2],[3,4],[5,6]
])
data
array([[1, 2],[3, 4],[5, 6]])
data[0][1]
2
data[0,1]
2
data[1:3]
array([[3, 4],[5, 6]])
data[0:2,0:1]
array([[1],[3]])
numpy 数据形状的改变
data= np.array([1,2,3,4,5,6])
data.shape
(6,)
data.reshape(2,3)
array([[1, 2, 3],[4, 5, 6]])
data.reshape(3,2)
## 注意行乘以列等于数据的总量
array([[1, 2],[3, 4],[5, 6]])
data.reshape(3,-1)
array([[1, 2],[3, 4],[5, 6]])
数据降维
c = np.array([[1,2,3],[2,3,4]
])
c.ravel()
array([1, 2, 3, 2, 3, 4])
a = np.array([
[3000,3500],
[4500,5000],
[10000,11000]]
)
np.sum(a)
37000
np.sum(a,axis=0)
array([17500, 19500])
np.sum(a,axis=1)
array([ 6500, 9500, 21000])
np.max(a)
11000
np.min(a)
3000
np.mean(a)
6166.666666666667
np.median(a)
4750.0
方差
np.var(a)
9888888.888888888
标准差
np.std(a)
3144.660377352201
随机数
np.random.rand(2,3)
array([[0.14406139, 0.58255917, 0.56687269],[0.45931544, 0.27684285, 0.53764072]])
np.random.randint(2,6,5)
array([2, 3, 2, 4, 5])
np.random.randn(10)
array([ 0.13193888, 0.09997268, -0.071677 , -0.08255967, -0.4487725 ,-1.75318345, -0.46725208, 0.34519292, -0.30273565, -0.58354462])
创建正太分布规律的数据
np.random.normal(175,15,size=(4,4))
array([[190.6869503 , 162.3662239 , 155.35991852, 185.87871385],[179.38113865, 171.48428447, 179.59261062, 169.09543353],[166.4724072 , 185.78899473, 168.35652325, 163.75069691],[164.24513277, 186.98536138, 164.74146814, 176.38007905]])
np.random.randn(4,4)
array([[-3.08839751e-01, 7.66951959e-01, -4.02554299e-01,6.24276206e-01],[-6.56442394e-01, -5.90405577e-01, -2.12262531e+00,-2.38134835e-01],[-2.17110459e-01, -6.00174700e-01, 1.06536890e-01,1.70518271e+00],[ 2.33097080e-01, -4.03758442e-01, 2.64799870e-01,6.67566195e-04]])
np.average(np.random.randn(400,400))
-0.005990795466101744
np.std(np.random.randn(400,400))
1.0005126387054453
numpy数据打散
a = np.arange(10)
a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.random.permutation(a)
array([6, 1, 7, 5, 9, 4, 2, 3, 8, 0])
a
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
直接修改原始数组
np.random.shuffle(a)
a
array([2, 0, 9, 1, 7, 6, 4, 3, 8, 5])
numpy指定随机数种子
np.random.seed(3)
np.random.randint(24)
10
numpy逻辑操作
a = np.array([True,False,True,True,False])
np.all(a)
False
np.any(a)
True
a = np.array([50,55,65,57,58,66,100])
a >19
array([ True, True, True, True, True, True, True])
numpy数据排序
b = np.random.randn(10)
b
array([ 0.87226552, -0.01554175, 1.16668206, -2.15861048, 0.75620445,-1.69049879, -0.34879409, -1.72951471, 0.69631161, 0.56496171])
b.sort()
b
array([-2.15861048, -1.72951471, -1.69049879, -0.34879409, -0.01554175,0.56496171, 0.69631161, 0.75620445, 0.87226552, 1.16668206])
不修改元素族排序
a = np.random.randn(10)
a
array([ 0.47986141, -0.29594453, -0.42030926, -0.85961379, -0.40791451,1.24757841, -1.82938368, -1.59651767, 0.69031204, 0.92812246])
a.argsort()
array([6, 7, 3, 2, 4, 1, 0, 8, 9, 5], dtype=int64)
a
array([ 0.47986141, -0.29594453, -0.42030926, -0.85961379, -0.40791451,1.24757841, -1.82938368, -1.59651767, 0.69031204, 0.92812246])
a[a.argsort()]
array([-1.82938368, -1.59651767, -0.85961379, -0.42030926, -0.40791451,-0.29594453, 0.47986141, 0.69031204, 0.92812246, 1.24757841])
二维数组
b = np.random.randn(3,3)
b
array([[ 1.58653631, 0.40130586, -1.26968864],[ 0.46920629, 1.30385884, 1.23128365],[ 0.22948181, 0.27360762, -0.666784 ]])
b.sort(axis=1)
b
array([[-1.26968864, 0.40130586, 1.58653631],[ 0.46920629, 1.23128365, 1.30385884],[-0.666784 , 0.22948181, 0.27360762]])
b.sort(axis=0)
b
array([[-1.26968864, 0.22948181, 0.27360762],[-0.666784 , 0.40130586, 1.30385884],[ 0.46920629, 1.23128365, 1.58653631]])
b.argsort(axis=1)
array([[1, 0],[1, 0],[1, 0],...,[1, 0],[0, 1],[1, 0]], dtype=int64)
np.set_printoptions(suppress=True)
totalpoint = 1000
b = np.random.rand(totalpoint,2)
b
array([[0.03393228, 0.16615602],[0.07044839, 0.25010443],[0.37316487, 0.33790676],...,[0.2200427 , 0.60226645],[0.04297848, 0.38231248],[0.45174169, 0.16641803]])
c = np.sqrt(np.sum(b**2,axis=1))
c
array([0.73849201, 0.78324931, 0.2300633 , ..., 0.77652285, 0.78353274,0.62629663])
c < 1
array([ True, True, True, ..., False, True, True])
np.sum(c<=1)
785401387
行向量和列向量
a = np.array([1,2,3])
a
array([1, 2, 3])
a.transpose()
array([1, 2, 3])
ID数组(一维数组)
b = np.arange(10)
b
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
b[::2]
array([0, 2, 4, 6, 8])
数组倒序排列
b
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
b[::-1]
2D(二位数组)
a = np.array([[1,2],[3,4],[5,6]
])
a
array([[1, 2],[3, 4],[5, 6]])
a[1,1]
4
data = np.arange(25).reshape(5,5)
data
array([[ 0, 1, 2, 3, 4],[ 5, 6, 7, 8, 9],[10, 11, 12, 13, 14],[15, 16, 17, 18, 19],[20, 21, 22, 23, 24]])
data[1,2:4]
array([7, 8])
data[3:,3:]
array([[18, 19],[23, 24]])
data[::2,2::2]
array([[ 2, 4],[12, 14],[22, 24]])
data[:,1:2]
array([[ 1],[ 6],[11],[16],[21]])
data[[0,2,4],[0,2,4]]
array([ 0, 12, 24])
data[[1,3,4],3:]
array([[ 8, 9],[18, 19],[23, 24]])
数组拆分和合并
data = np.arange(9).reshape(3,3)
data
array([[0, 1, 2],[3, 4, 5],[6, 7, 8]])
np.vsplit(data,3)
[array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])]
np.hsplit(data,3)
[array([[0],[3],[6]]),array([[1],[4],[7]]),array([[2],[5],[8]])]
数组的合并
a = np.array([[1,2],[3,4]
])
b = np.array([[7,8],[9,10]
])
np.vstack((a,b))
array([[ 1, 2],[ 3, 4],[ 7, 8],[ 9, 10]])
np.hstack((a,b))
array([[ 1, 2, 7, 8],[ 3, 4, 9, 10]])
numpy 高级操作,索引数组
b = np.array([1,3,5,2,4,6])
b[np.argsort(b)]
array([1, 2, 3, 4, 5, 6])
## mnumpy广播机制
a = np.arange(3)
a + 1
array([1, 2, 3])
其他常用的api
b = np.array([1,2,3,4,5,6,7,8,9])
np.argmax(b)
8
np.argmin(b)
0
np.nonzero(b)
(array([0, 1, 2, 3, 4, 5, 6, 7, 8], dtype=int64),)
np.count_nonzero(b)
9