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