矩阵的逆
import numpy as np
A = np.mat("0 1 2;1 0 3;4 -3 8")
print "A\n", A
'''
A
[[ 0 1 2]
[ 1 0 3]
[ 4 -3 8]]
'''
inverse = np.linalg.inv(A)
print "inverse of A\n", inverse
'''
inverse of A
[[-4.5 7. -1.5]
[-2. 4. -1. ]
[ 1.5 -2. 0.5]]
'''
print "Check\n", A * inverse
'''
Check
[[ 1. 0. 0.]
[ 0. 1. 0.]
[ 0. 0. 1.]]
'''
求解线性方程组
import numpy as np
A = np.mat("1 -2 1;0 2 -8;-4 5 9")
print "A\n", A
'''
A
[[ 1 -2 1]
[ 0 2 -8]
[-4 5 9]]
'''
b = np.array([0, 8, -9])
print "b\n", b
'''
b
[ 0 8 -9]
'''
x = np.linalg.solve(A, b)
print "Solution", x
'''
Solution [ 29. 16. 3.]
'''
print "Check\n", np.dot(A , x)
'''
Check
[[ 0. 8. -9.]]
'''
特征值和特征向量
import numpy as np
A = np.mat("3 -2;1 0")
print "A\n", A
'''
A
[[ 3 -2]
[ 1 0]]
'''
print "Eigenvalues", np.linalg.eigvals(A)
eigenvalues, eigenvectors = np.linalg.eig(A)
print "First tuple of eig", eigenvalues
print "Second tuple of eig\n", eigenvectors
'''
First tuple of eig [ 2. 1.]
Second tuple of eig
[[ 0.89442719 0.70710678]
[ 0.4472136 0.70710678]]
'''
for i in range(len(eigenvalues)):
print "Left", np.dot(A, eigenvectors[:,i])
print "Right", eigenvalues[i] * eigenvectors[:,i]
print
'''
Left [[ 1.78885438]
[ 0.89442719]]
Right [[ 1.78885438]
[ 0.89442719]]
Left [[ 0.70710678]
[ 0.70710678]]
Right [[ 0.70710678]
[ 0.70710678]]
'''
奇异值分解
import numpy as np
A = np.mat("4 11 14;8 7 -2")
print "A\n", A
'''
A
[[ 4 11 14]
[ 8 7 -2]]
'''
U, Sigma, V = np.linalg.svd(A, full_matrices=False)
print "U"
print U
'''
U
[[-0.9486833 -0.31622777]
[-0.31622777 0.9486833 ]]
'''
print "Sigma"
print Sigma
'''
Sigma
[ 18.97366596 9.48683298]
'''
print "V"
print V
'''
V
[[-0.33333333 -0.66666667 -0.66666667]
[ 0.66666667 0.33333333 -0.66666667]]
'''
print "Product\n", U * np.diag(Sigma) * V
'''
Product
[[ 4. 11. 14.]
[ 8. 7. -2.]]
'''
print np.linalg.eigvals(A * A.T) ** 0.5
'''
[ 18.97366596 9.48683298]
'''
广义逆矩阵
import numpy as np
A = np.mat("4 11 14;8 7 -2")
print "A\n", A
'''
A
[[ 4 11 14]
[ 8 7 -2]]
'''
pseudoinv = np.linalg.pinv(A)
print "Pseudo inverse\n", pseudoinv
'''
Pseudo inverse
[[-0.00555556 0.07222222]
[ 0.02222222 0.04444444]
[ 0.05555556 -0.05555556]]
'''
print "Check", A * pseudoinv
'''
Check [[ 1.00000000e+00 0.00000000e+00]
[ 8.32667268e-17 1.00000000e+00]]
'''
计算矩阵行列式
import numpy as np
A = np.mat("3 4;5 6")
print "A\n", A
'''
A
[[ 3. 4.]
[ 5. 6.]]
'''
print "Determinant", np.linalg.det(A)
快速傅里叶变换(FFT)
import numpy as np
from matplotlib.pyplot import plot, show
x = np.linspace(0, 2 * np.pi, 30)
wave = np.cos(x)
transformed = np.fft.fft(wave)
print np.all(np.abs(np.fft.ifft(transformed) - wave) < 10 ** -9)
plot(transformed)
show()
调整频率
import numpy as np
from matplotlib.pyplot import plot, show
x = np.linspace(0, 2 * np.pi, 30)
wave = np.cos(x)
transformed = np.fft.fft(wave)
shifted = np.fft.fftshift(transformed)
print np.all(np.abs(np.fft.ifftshift(shifted) - transformed) < 10 ** -9)
plot(transformed, lw=2)
plot(shifted, lw=3)
show()
随机游走
import numpy as np
from matplotlib.pyplot import plot, show
cash = np.zeros(10000)
cash[0] = 1000
outcome = np.random.binomial(9, 0.5, size=len(cash))
for i in range(1, len(cash)):
if outcome[i] < 5:
cash[i] = cash[i - 1] - 1
elif outcome[i] < 10:
cash[i] = cash[i - 1] + 1
else:
raise AssertionError("Unexpected outcome " + outcome)
print outcome.min(), outcome.max()
plot(np.arange(len(cash)), cash)
show()
模拟游戏节目
import numpy as np
from matplotlib.pyplot import plot, show
points = np.zeros(100)
outcomes = np.random.hypergeometric(25, 1, 3, size=len(points))
for i in range(len(points)):
if outcomes[i] == 3:
points[i] = points[i - 1] + 1
elif outcomes[i] == 2:
points[i] = points[i - 1] - 6
else:
print outcomes[i]
plot(np.arange(len(points)), points)
show()
绘制正态分布
import numpy as np
import matplotlib.pyplot as plt
N=10000
normal_values = np.random.normal(size=N)
dummy, bins, dummy = plt.hist(normal_values, np.sqrt(N), normed=True, lw=1)
sigma = 1
mu = 0
plt.plot(bins, 1/(sigma * np.sqrt(2 * np.pi)) * np.exp( - (bins - mu)**2 / (2 * sigma**2) ),lw=2)
plt.show()
绘制对数正态分布
import numpy as np
import matplotlib.pyplot as plt
N=10000
lognormal_values = np.random.lognormal(size=N)
dummy, bins, dummy = plt.hist(lognormal_values, np.sqrt(N), normed=True, lw=1)
sigma = 1
mu = 0
x = np.linspace(min(bins), max(bins), len(bins))
pdf = np.exp(-(np.log(x) - mu)**2 / (2 * sigma**2))/ (x * sigma * np.sqrt(2 * np.pi))
plt.plot(x, pdf,lw=3)
plt.show()