100道NumPy练习题(61-80题)
七、随机数与统计
61.生成10个0-1之间的随机数
import numpy as np
a = np.random.rand(10)
print(a)
[0.28613361 0.37749818 0.46263267 0.26107433 0.28591036 0.04154898
0.76444883 0.93588597 0.07605215 0.58587586]
62.生成10个符合正态分布的随机数
import numpy as np
a = np.random.randn(10)
print(a)
[ 0.49453002 -0.11759588 -0.64466301 0.59821977 0.48681156 0.22169839
1.34682773 -0.18080486 -1.41018212 0.53211155]
63.生成10个整数随机数(范围1-100)
import numpy as np
a = np.random.randint(1,101,10)
print(a)
[64 54 96 77 88 93 86 56 82 90]
64.随机打乱一个数组
import numpy as np
a = np.array([1,2,3,4,5,6,7,8,9,10])
np.random.shuffle(a)
print(a)
[ 9 8 3 10 2 6 1 4 7 5]
65.从数组中进行随机抽样
import numpy as np
a = np.array([1,2,3,4,5,6,7,8,9,10])
b = np.random.choice(a,size = 3,replace = False)
print(b)
[6 5 1]
66.计算数组的百分位数
import numpy as np
a = np.array([1,2,3,4,5,6,7,8,9,10])
print(np.percentile(a,[25,50,75]))
[3.25 5.5 7.75]
67.计算数组的协方差矩阵
import numpy as np
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(np.cov(a))
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
68.计算数组的相关系数矩阵
import numpy as np
a = np.array([[1,2,3],[4,5,6],[7,8,9]])
print(np.corrcoef(a))
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
69.计算数组的直方图
import numpy as np
data = np.random.normal(0,1,1000)
x,y = np.histogram(data,bins = 10)
print(x)
print(y)
[ 15 50 124 233 254 205 100 16 2 1]
[-3.00513691 -2.30296978 -1.60080264 -0.89863551 -0.19646838 0.50569876
1.20786589 1.91003303 2.61220016 3.31436729 4.01653443]
70.计算数组的累积分布函数
import numpy as np
data = np.random.normal(0,1,100)
so = np.sort(data)
df = np.arange(1,len(so)+1) / len(so)
print(df)
[0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1 0.11 0.12 0.13 0.14
0.15 0.16 0.17 0.18 0.19 0.2 0.21 0.22 0.23 0.24 0.25 0.26 0.27 0.28
0.29 0.3 0.31 0.32 0.33 0.34 0.35 0.36 0.37 0.38 0.39 0.4 0.41 0.42
0.43 0.44 0.45 0.46 0.47 0.48 0.49 0.5 0.51 0.52 0.53 0.54 0.55 0.56
0.57 0.58 0.59 0.6 0.61 0.62 0.63 0.64 0.65 0.66 0.67 0.68 0.69 0.7
0.71 0.72 0.73 0.74 0.75 0.76 0.77 0.78 0.79 0.8 0.81 0.82 0.83 0.84
0.85 0.86 0.87 0.88 0.89 0.9 0.91 0.92 0.93 0.94 0.95 0.96 0.97 0.98
0.99 1. ]
八、线性代数运算
71.计算矩阵的行列式
import numpy as np
a = np.array([[1,2],[3,4]])
data = np.linalg.det(a)
print(data)
-2.0000000000000004
72.计算矩阵的逆
import numpy as np
a = np.array([[1,2],[3,4]])
data = np.linalg.inv(a)
print(data)
[[-2. 1. ]
[ 1.5 -0.5]]
73.计算矩阵的特征值和特征向量
import numpy as np
a = np.array([[1,2],[3,4]])
b = np.linalg.eig(a)
print(b)
EigResult(eigenvalues=array([-0.37228132, 5.37228132]), eigenvectors=array([[-0.82456484, -0.41597356],
[ 0.56576746, -0.90937671]]))
74.解线性方程组
import numpy as np
a = np.array([[1,2],[3,4]])
b = np.array([5,6])
x = np.linalg.solve(a,b)
print(x)
[-4. 4.5]
75.计算矩阵的奇异值分解
import numpy as np
a = np.array([[1,2],[3,4]])
u,s,v = np.linalg.svd(a)
print(u)
print(s)
print(v)
[[-0.40455358 -0.9145143 ]
[-0.9145143 0.40455358]]
[5.4649857 0.36596619]
[[-0.57604844 -0.81741556]
[ 0.81741556 -0.57604844]]
76.计算矩阵的QR分解
import numpy as np
a = np.array([[1,2],[3,4]])
q,r = np.linalg.qr(a)
print(q)
print(r)
[[-0.31622777 -0.9486833 ]
[-0.9486833 0.31622777]]
[[-3.16227766 -4.42718872]
[ 0. -0.63245553]]
77.计算矩阵的Cholesky分解
import numpy as np
a = np.array([[2,-1],[-1,2]])
k = np.linalg.cholesky(a)
print(k)
[[ 1.41421356 0. ]
[-0.70710678 1.22474487]]
78.计算矩阵的迹
import numpy as np
a = np.array([[1,2],[3,4]])
print(np.trace(a))
5
79.计算矩阵的范数
import numpy as np
a = np.array([[1,2],[3,4]])
b = np.linalg.norm(a)
print(b)
5.477225575051661
80.计算矩阵的秩
import numpy as np
a = np.array([[1,2],[3,4]])
b = np.linalg.matrix_rank(a)
print(b)
2
特别说明,上述题目已在jupyter notebook上运行过了。