1.1 Python 基础
1.1.1 列表推导式与条件赋值
L = []
def my_func(x):
return 2+x
for i in range(5):
L.append(my_func(i))
L
[2, 3, 4, 5, 6]
[my_func(i) for i in range(5)]
[2, 3, 4, 5, 6]
[m+'_'+n for m in ['a','b'] for n in ['c','d']]
['a_c', 'a_d', 'b_c', 'b_d']
value = 'cat' if 2>1 else 'dog'
value
'cat'
a,b='cat','dog'
condition = 2>1
if condition:
value=a
else:
value=b
value
'cat'
L=[1,2,3,4,5,6,7]
[i if i<=5 else 5 for i in L]
[1, 2, 3, 4, 5, 5, 5]
1.1.2 匿名函数与 map 方法
my_func=lambda x:2+x
print(my_func(3))
multi_para_func=lambda a,b:a+b
multi_para_func(1,2)
5
3
[(lambda x:2+x)(i) for i in range(5)]
[2, 3, 4, 5, 6]
list(map(lambda x:2+x,range(5)))
[2, 3, 4, 5, 6]
list(map(lambda x,y:str(x)+'_'+y,range(5),list('abcde')))
['0_a', '1_b', '2_c', '3_d', '4_e']
1.1.3 zip 对象与 enumerate 方法
l1,l2,l3=list('abc'),list('def'),list('hij')
list(zip(l1,l2,l3))
[('a', 'd', 'h'), ('b', 'e', 'i'), ('c', 'f', 'j')]
tuple(zip(l1,l2,l3))
(('a', 'd', 'h'), ('b', 'e', 'i'), ('c', 'f', 'j'))
for i,j,k in zip(l1,l2,l3):
print(i,j,k)
a d h
b e i
c f j
l=list('abcd')
for index,value in enumerate(l):
print(index,value)
0 a
1 b
2 c
3 d
for index,value in zip(range(len(l)),l):
print(index,value)
0 a
1 b
2 c
3 d
for index in range(len(l)):
print(index)
0
1
2
3
for index in list(l):
print(index)
a
b
c
d
dict(zip(l1,l2))
{'a': 'd', 'b': 'e', 'c': 'f'}
zipped = list(zip(l1,l2,l3))
zipped
[('a', 'd', 'h'), ('b', 'e', 'i'), ('c', 'f', 'j')]
list(zip(*zipped))
[('a', 'b', 'c'), ('d', 'e', 'f'), ('h', 'i', 'j')]
1.2 Numpy 基础
1.2.1 np 数组的构造
import numpy as np
np.array([1,2,3])
array([1, 2, 3])
np.linspace(1,5,11)
array([1. , 1.4, 1.8, 2.2, 2.6, 3. , 3.4, 3.8, 4.2, 4.6, 5. ])
np.arange(1,5,2)
array([1, 3])
np.zeros((2,3))
array([[0., 0., 0.],
[0., 0., 0.]])
np.eye(3)
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
np.eye(3,k=1)
array([[0., 1., 0.],
[0., 0., 1.],
[0., 0., 0.]])
np.full((2,3),10)
array([[10, 10, 10],
[10, 10, 10]])
np.full((2,3),[1,2,3])
array([[1, 2, 3],
[1, 2, 3]])
np.random.rand(3)
array([0.05150622, 0.12623442, 0.05012188])
np.random.rand(3,3)
array([[0.49889527, 0.79718226, 0.9787408 ],
[0.22907704, 0.63938829, 0.88717653],
[0.98590688, 0.27542163, 0.84398524]])
a,b=5,15
(b-a)*np.random.rand(3)+a
array([ 8.94476537, 6.69380248, 10.53014755])
np.random.randn(3)
array([ 0.67401196, -0.61472109, 1.09121873])
np.random.randn(2,2)
array([[ 0.6208236 , 0.64515623],
[-0.59163504, -0.32420474]])
sigma,mu=2.5,3
mu+np.random.randn(3)*sigma
array([-0.56333074, 4.77478195, 7.04589299])
low,high,size=5,15,(2,2)
np.random.randint(low,high,size)
array([[ 7, 11],
[ 5, 11]])
my_list=['a','b','c','d']
np.random.choice(my_list,2,replace=False,p=[0.1,0.7,0.1,0.1])
array(['b', 'd'], dtype='<U1')
np.random.choice(my_list,(3,3))
array([['b', 'a', 'b'],
['a', 'd', 'a'],
['a', 'b', 'c']], dtype='<U1')
np.random.permutation(my_list)
array(['a', 'b', 'd', 'c'], dtype='<U1')
np.random.seed(0)
np.random.rand()
0.5488135039273248
np.random.seed(0)
np.random.rand()
0.5488135039273248
1.2.2 np 数组的变形与合并
np.zeros((2,3)).T
array([[0., 0.],
[0., 0.],
[0., 0.]])
np.zeros((2,3))
array([[0., 0., 0.],
[0., 0., 0.]])
np.r_[np.zeros((2,3)),np.zeros((2,3))]
array([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
np.c_[np.zeros((2,3)),np.zeros((2,3))]
array([[0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0.]])
np.zeros(2)
array([0., 0.])
np.array([0,0])
array([0, 0])
np.r_[np.array([0,0]),np.zeros(2)]
array([0., 0., 0., 0.])
print(np.array([0,0]))
print(np.zeros((2,3)))
np.c_[np.array([0,0]),np.zeros((2,3))]
[0 0]
[[0. 0. 0.]
[0. 0. 0.]]
array([[0., 0., 0., 0.],
[0., 0., 0., 0.]])
target=np.arange(8).reshape(2,4)
target
array([[0, 1, 2, 3],
[4, 5, 6, 7]])
target.reshape((4,2),order='C')
array([[0, 1],
[2, 3],
[4, 5],
[6, 7]])
target.reshape((4,2),order='F')
array([[0, 2],
[4, 6],
[1, 3],
[5, 7]])
target.reshape((4,-1))
array([[0, 1],
[2, 3],
[4, 5],
[6, 7]])
target=np.ones((3,1))
target
array([[1.],
[1.],
[1.]])
target.reshape(-1)
array([1., 1., 1.])
target=np.arange(9).reshape(3,3)
target
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
target[:-1,[0,2]]
array([[0, 2],
[3, 5]])
target[np.ix_([True, False, True], [True, False, True])]
array([[0, 2],
[6, 8]])
target[np.ix_([1,2], [True, False, True])]
array([[3, 5],
[6, 8]])
new=target.reshape(-1)
new
array([0, 1, 2, 3, 4, 5, 6, 7, 8])
new[new%2==0]
array([0, 2, 4, 6, 8])
1.2.4 常用函数
a=np.array([[-1,1,-1,0]])
a
array([[-1, 1, -1, 0]])
np.where(a>0,a,5)
array([[5, 1, 5, 5]])
a = np.array([-2,-5,0,1,3,-1])
np.nonzero(a)
(array([0, 1, 3, 4, 5], dtype=int64),)
np.argmax(a)
4
a.argmax()
4
a=np.array([0,1])
print(a.any())
print(a.all())
True
False
a=np.array([1,2,4])
a.cumprod()
array([1, 2, 8], dtype=int32)
a.cumsum()
array([1, 3, 7], dtype=int32)
np.diff(a)
array([1, 2])
target=np.arange(5)
target
target.max()
4
np.quantile(target,0.5)
2.0
target=np.array([1,2,np.nan])
target
array([ 1., 2., nan])
target.max()
nan
np.nanmax(target)
2.0
np.nanquantile(target,0.5)
1.5
target1=np.array([1,3,5,9])
target2=np.array([1,5,3,-9])
np.cov(target1,target2)
array([[ 11.66666667, -16.66666667],
[-16.66666667, 38.66666667]])
np.corrcoef(target1,target2)
array([[ 1. , -0.78470603],
[-0.78470603, 1. ]])
target=np.arange(1,10).reshape(3,-1)
target
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
target.sum(0)
array([12, 15, 18])
target.sum(1)
array([ 6, 15, 24])
res=3*np.ones((2,2))+1
res
array([[4., 4.],
[4., 4.]])
res=1/res
res
array([[0.25, 0.25],
[0.25, 0.25]])
'''【b】二维数组之间的操作
当两个数组维度完全一致时,使用对应元素的操作,否则会报错,除非其中的某个数组的维度是 m × 1 或者
1 × n ,那么会扩充其具有 1 的维度为另一个数组对应维度的大小。例如,1 × 2 数组和 3 × 2 数组做逐元素
运算时会把第一个数组扩充为 3 × 2 ,扩充时的对应数值进行赋值。但是,需要注意的是,如果第一个数组
的维度是 1 × 3 ,那么由于在第二维上的大小不匹配且不为 1 ,此时报错。'''
res=np.ones((3,2))
res
array([[1., 1.],
[1., 1.],
[1., 1.]])
res*np.array([[2,3]])
array([[2., 3.],
[2., 3.],
[2., 3.]])
res * np.array([[2],[3],[4]])
array([[2., 2.],
[3., 3.],
[4., 4.]])
res * np.array([[2]])
array([[2., 2.],
[2., 2.],
[2., 2.]])
np.ones(3)
array([1., 1., 1.])
np.ones((2,3))
array([[1., 1., 1.],
[1., 1., 1.]])
np.ones(3) + np.ones((2,3))
array([[2., 2., 2.],
[2., 2., 2.]])
np.ones((2,1))
array([[1.],
[1.]])
np.ones((2,1))+np.ones(3)
array([[2., 2., 2.],
[2., 2., 2.]])
np.ones(1)
array([1.])
np.ones(1) + np.ones((2,3))
array([[2., 2., 2.],
[2., 2., 2.]])
1.2.6 向量与矩阵的计算
a = np.array([1,2,3])
b = np.array([1,3,5])
a.dot(b)
22
martix_target = np.arange(4).reshape(-1,2)
martix_target
array([[0, 1],
[2, 3]])
np.linalg.norm(martix_target, 'fro')
3.7416573867739413
np.linalg.norm(martix_target, np.inf)
5.0
np.linalg.norm(martix_target, 2)
3.702459173643833
vector_target = np.arange(4)
vector_target
array([0, 1, 2, 3])
np.linalg.norm(vector_target, np.inf)
3.0
np.linalg.norm(vector_target, 2)
3.7416573867739413
np.linalg.norm(vector_target, 3)
3.3019272488946263
a = np.arange(4).reshape(-1,2)
a
array([[0, 1],
[2, 3]])
b = np.arange(-4,0).reshape(-1,2)
b
array([[-4, -3],
[-2, -1]])
a@b
array([[ -2, -1],
[-14, -9]])
1.3 练习
1.3.1 Ex1:利用列表推导式写矩阵乘法
M1 = np.random.rand(2,3)
M2 = np.random.rand(3,4)
print(M1)
print(M2)
[[0.60484552 0.73926358 0.03918779]
[0.28280696 0.12019656 0.2961402 ]]
[[0.11872772 0.31798318 0.41426299 0.0641475 ]
[0.69247212 0.56660145 0.26538949 0.52324805]
[0.09394051 0.5759465 0.9292962 0.31856895]]
M1.shape[0]
2
M2.shape[1]
4
res = np.empty((M1.shape[0],M2.shape[1]))
res
array([[0.00000000e+000, 0.00000000e+000, 0.00000000e+000,
0.00000000e+000],
[0.00000000e+000, 7.25288368e-321, 1.24610926e-306,
1.42410974e-306]])
for i in range(M1.shape[0]):
for j in range(M2.shape[1]):
item=0
for k in range(M1.shape[1]):
item+=M1[i][k]*M2[k][j]
res[i][j]=item
res
array([[0.58741267, 0.63376859, 0.48317497, 0.43810157],
[0.14462935, 0.32859231, 0.42425732, 0.17537505]])
M1@M2
array([[0.58741267, 0.63376859, 0.48317497, 0.43810157],
[0.14462935, 0.32859231, 0.42425732, 0.17537505]])
((M1@M2-res)<1e-15).all()
True
1.3.2 Ex2:更新矩阵
A=np.arange(1,10).reshape(3,-1)
A
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
1/A
array([[1. , 0.5 , 0.33333333],
[0.25 , 0.2 , 0.16666667],
[0.14285714, 0.125 , 0.11111111]])
(1/A).sum(1)
array([1.83333333, 0.61666667, 0.37896825])
(1/A).sum(1).reshape(-1,1)
array([[1.83333333],
[0.61666667],
[0.37896825]])
A*((1/A).sum(1).reshape(-1,1))
array([[1.83333333, 3.66666667, 5.5 ],
[2.46666667, 3.08333333, 3.7 ],
[2.65277778, 3.03174603, 3.41071429]])
1.3.3 Ex3:卡方统计量
np.random.seed(0)
A = np.random.randint(10, 20, (8, 5))
A
array([[15, 10, 13, 13, 17],
[19, 13, 15, 12, 14],
[17, 16, 18, 18, 11],
[16, 17, 17, 18, 11],
[15, 19, 18, 19, 14],
[13, 10, 13, 15, 10],
[12, 13, 18, 11, 13],
[13, 13, 17, 10, 11]])
A.sum(0)
array([120, 111, 129, 116, 101])
A.sum(1)
array([68, 73, 80, 79, 85, 61, 67, 64])
A.sum(0)*(A.sum(1).reshape(-1,1))
array([[ 8160, 7548, 8772, 7888, 6868],
[ 8760, 8103, 9417, 8468, 7373],
[ 9600, 8880, 10320, 9280, 8080],
[ 9480, 8769, 10191, 9164, 7979],
[10200, 9435, 10965, 9860, 8585],
[ 7320, 6771, 7869, 7076, 6161],
[ 8040, 7437, 8643, 7772, 6767],
[ 7680, 7104, 8256, 7424, 6464]])
B=A.sum(0)*(A.sum(1).reshape(-1,1))/A.sum()
B
array([[14.14211438, 13.08145581, 15.20277296, 13.67071057, 11.90294627],
[15.18197574, 14.04332756, 16.32062392, 14.67590988, 12.77816291],
[16.63778163, 15.38994801, 17.88561525, 16.08318891, 14.0034662 ],
[16.42980936, 15.19757366, 17.66204506, 15.88214905, 13.82842288],
[17.67764298, 16.35181976, 19.0034662 , 17.08838821, 14.87868284],
[12.68630849, 11.73483536, 13.63778163, 12.26343154, 10.67764298],
[13.93414211, 12.88908146, 14.97920277, 13.46967071, 11.72790295],
[13.3102253 , 12.31195841, 14.3084922 , 12.86655113, 11.20277296]])
((A-B)**2/B).sum()
11.842696601945802
1.3.4 Ex4:改进矩阵计算的性能
np.random.seed(0)
m, n, p = 100, 80, 50
B = np.random.randint(0, 2, (m, p))
B
array([[0, 1, 1, ..., 1, 0, 1],
[0, 1, 1, ..., 1, 1, 0],
[1, 0, 0, ..., 1, 1, 1],
...,
[1, 0, 0, ..., 1, 1, 0],
[0, 0, 0, ..., 1, 1, 0],
[0, 1, 1, ..., 1, 1, 1]])
U = np.random.randint(0, 2, (p, n))
U
array([[1, 0, 1, ..., 1, 1, 0],
[0, 0, 0, ..., 0, 0, 1],
[0, 1, 0, ..., 0, 1, 1],
...,
[1, 0, 1, ..., 1, 0, 1],
[0, 0, 1, ..., 0, 0, 0],
[1, 1, 0, ..., 0, 0, 1]])
Z = np.random.randint(0, 2, (m, n))
Z
array([[1, 0, 0, ..., 1, 0, 0],
[0, 1, 1, ..., 1, 1, 0],
[1, 0, 1, ..., 0, 1, 1],
...,
[0, 1, 0, ..., 1, 1, 0],
[1, 0, 0, ..., 1, 1, 0],
[0, 0, 0, ..., 0, 0, 1]])
B**2
array([[0, 1, 1, ..., 1, 0, 1],
[0, 1, 1, ..., 1, 1, 0],
[1, 0, 0, ..., 1, 1, 1],
...,
[1, 0, 0, ..., 1, 1, 0],
[0, 0, 0, ..., 1, 1, 0],
[0, 1, 1, ..., 1, 1, 1]], dtype=int32)
B1=(B**2).sum(1).reshape(-1,1).T
B1
array([[30, 26, 26, 19, 23, 28, 32, 28, 27, 23, 21, 27, 25, 28, 26, 22,
27, 27, 24, 15, 21, 25, 24, 26, 22, 29, 27, 31, 30, 25, 27, 25,
27, 24, 25, 24, 21, 28, 25, 29, 24, 22, 24, 25, 25, 25, 33, 27,
25, 25, 29, 21, 28, 31, 24, 26, 26, 24, 32, 28, 22, 27, 31, 27,
24, 20, 23, 25, 25, 29, 20, 22, 24, 29, 27, 23, 30, 20, 25, 24,
26, 26, 26, 15, 29, 30, 31, 30, 28, 30, 24, 16, 25, 20, 26, 28,
20, 23, 28, 23]], dtype=int32)
U1=(U**2).sum(0)
U1
array([27, 26, 25, 32, 26, 21, 24, 27, 27, 29, 26, 25, 23, 29, 27, 22, 24,
29, 30, 29, 25, 24, 21, 25, 24, 25, 27, 28, 26, 25, 24, 27, 28, 27,
26, 24, 22, 27, 28, 28, 23, 21, 26, 23, 32, 26, 22, 24, 21, 28, 19,
25, 22, 22, 28, 27, 24, 28, 25, 16, 25, 27, 25, 26, 24, 22, 26, 21,
22, 27, 23, 26, 25, 27, 25, 20, 27, 29, 25, 26], dtype=int32)
BU1=2*B@U
BU1
array([[32, 34, 24, ..., 30, 32, 34],
[24, 26, 30, ..., 28, 30, 28],
[26, 30, 30, ..., 32, 24, 32],
...,
[26, 24, 30, ..., 22, 22, 24],
[30, 28, 32, ..., 30, 30, 30],
[24, 22, 26, ..., 16, 26, 24]])
B1.T*U1-BU1
array([[778, 746, 726, ..., 840, 718, 746],
[678, 650, 620, ..., 726, 620, 648],
[676, 646, 620, ..., 722, 626, 644],
...,
[595, 574, 545, ..., 645, 553, 574],
[726, 700, 668, ..., 782, 670, 698],
[597, 576, 549, ..., 651, 549, 574]])
((B1.T*U1-BU1)*Z).sum()
2486869
1.3.5 Ex5:连续整数的最大长度
'''输入一个整数的 Numpy 数组,返回其中递增连续整数子数组的最大长度。例如,输入 [1,2,5,6,7],[5,6,7] 为
具有最大长度的递增连续整数子数组,因此输出 3;输入 [3,2,1,2,3,4,6],[1,2,3,4] 为具有最大长度的递增连
续整数子数组,因此输出 4。请充分利用 Numpy 的内置函数完成。(提示:考虑使用 nonzero, diff 函数)'''
x=np.array([1,2,5,6,7])
x
array([1, 2, 5, 6, 7])
np.diff(x)!=1
array([False, True, False, False])
np.r_[1,np.diff(x)!=1,1]
array([1, 0, 1, 0, 0, 1], dtype=int32)
np.nonzero(np.r_[1,np.diff(x)!=1,1])
(array([0, 2, 5], dtype=int64),)
np.diff(np.nonzero(np.r_[1,np.diff(x)!=1,1]))
array([[2, 3]], dtype=int64)
np.diff(np.nonzero(np.r_[1,np.diff(x)!=1,1])).max()
3