第九章思维导图总结:
import numpy as np
9.1
y = np.array([[1, 1, 0, 1, 0, 0, 1, 0, 1, 1]]).T
m = y.shape[0]
theta = np.array([[0.46, 0.55, 0.67]]).T # initialization
for i in range(100):
theta_old = theta.copy()
q_theta = (theta[0] * theta[1] ** y * (1 - theta[1]) ** (1 - y)) / (theta[0] * theta[1] ** y * (1 - theta[1]) ** (1 - y) +
((1 - theta[0]) * theta[2] ** y * (1 - theta[2]) ** (1 - y)))
theta[0] = q_theta.sum() / m
theta[1] = (q_theta.T @ y) / q_theta.sum()
theta[2] = ((1 - q_theta.T) @ y) / (1 - q_theta).sum()
print(f'iteration {i} with theta:',theta.T)
if np.linalg.norm(theta - theta_old) < 0.000001:
print(f'converge at iteragtion: {i}')
break
print(f'final theta: {theta.T}')
iteration 0 with theta: [[0.46186284 0.534595 0.65613464]]
iteration 1 with theta: [[0.46186284 0.534595 0.65613464]]
converge at iteragtion: 1
final theta: [[0.46186284 0.534595 0.65613464]]
9.2
F ( P ~ , θ ) = E p [ log P ( Y , Z ∣ θ ) ] + H ( P ~ ) = E p [ log P ( Y , Z ∣ θ ) ] − E P ~ log P ~ ( Z ) = ∑ z P ~ θ ( Z ) log P ( Y , Z ∣ θ ) − ∑ Z P ~ ( Z ) log P ~ ( Z ) = ∑ z P ( Z ∣ Y , θ ) log P ( Y , Z ∣ θ ) − ∑ Z P ( Z ∣ Y , θ ) log P ( Z ∣ Y , θ ) = ∑ z P ( Z ∣ Y , θ ) log P ( Y , Z ∣ θ ) P ( Z ∣ Y , θ ) = ∑ z P ( Z ∣ Y , θ ) log P ( Y ∣ θ ) = log P ( Y ∣ θ ) \begin{array}{l}F(\tilde{P}, \theta) \\ =E_{p}[\log P(Y, Z \mid \theta)]+H(\tilde{P}) \\ =E_{p}[\log P(Y, Z \mid \theta)]-E_{\tilde{P}} \log \tilde{P}(Z) \\ =\sum_{z} \tilde{P}_{\theta}(Z) \log P(Y, Z \mid \theta)-\sum_{Z} \tilde{P}(Z) \log \tilde{P}(Z) \\ =\sum_{z} P(Z \mid Y, \theta) \log P(Y, Z \mid \theta)-\sum_{Z} P(Z \mid Y, \theta) \log P(Z \mid Y, \theta) \\ =\sum_{z} P(Z \mid Y, \theta) \log \frac{P(Y, Z \mid \theta)}{P(Z \mid Y, \theta)} \\ =\sum_{z} P(Z \mid Y, \theta) \log P(Y \mid \theta) \\ =\log P(Y \mid \theta)\end{array} F(P~,θ)=Ep[logP(Y,Z∣θ)]+H(P~)=Ep[logP(Y,Z∣θ)]−EP~logP~(Z)=∑zP~θ(Z)logP(Y,Z∣θ)−∑ZP~(Z)logP~(Z)=∑zP(Z∣Y,θ)logP(Y,Z∣θ)−∑ZP(Z∣Y,θ)logP(Z∣Y,θ)=∑zP(Z∣Y,θ)logP(Z∣Y,θ)P(Y,Z∣θ)=∑zP(Z∣Y,θ)logP(Y∣θ)=logP(Y∣θ)
9.3
from scipy.stats import norm as Gaussian
y = np.array([[-67, -48, 6, 8, 14, 16, 23, 24, 28, 29, 41, 49, 56, 60, 75]]).T
m = y.shape[0]
k = 2 # number of sub_distribution
alpha = np.array([1.0 / k for _ in range(k)]).T # initialize parameters
miu = np.array([[y.mean() for _ in range(k)]]).T
sigma = np.array([[np.sqrt(y.var()) for _ in range(k)]]).T
for i in range(100):
alpha_old = alpha.copy()
miu_old = miu.copy()
sigma_old = sigma.copy()
gamma_hat = np.zeros((m, k))
for j in range(k):
gamma_hat[:, j, None] = alpha[j] * Gaussian.pdf(y, miu[j], sigma[j]**2)
gamma_hat = gamma_hat / gamma_hat.sum(axis=1).reshape((-1, 1))
miu = (gamma_hat.T @ y) / gamma_hat.sum(axis=0).T
sigma = (gamma_hat * (y - miu_old.T)**2).sum(axis=0).T / gamma_hat.sum(axis=0).T
sigma = np.sqrt(sigma)
alpha = gamma_hat.sum(axis=0).T / m
if np.sum((miu - miu_old)**2 + (sigma - sigma_old)**2 + (alpha - alpha_old)**2) < 1e-15:
print(f'converge at iteragtion: {i}')
break
print(f'final alpha: {alpha.T} \nfinal miu: {miu.T} \nfinal sigma:{sigma.T}')
converge at iteragtion: 0
final alpha: [0.5 0.5]
final miu: [[20.93333333 20.93333333]
[20.93333333 20.93333333]]
final sigma:[36.46453376 36.46453376]
看一下10个子模型(高斯分布)的结果:
from scipy.stats import norm as Gaussian
y = np.array([[-67, -48, 6, 8, 14, 16, 23, 24, 28, 29, 41, 49, 56, 60, 75]]).T
m = y.shape[0]
k = 10 # number of sub_distribution
alpha = np.array([1.0 / k for _ in range(k)]).T # initialize parameters
miu = np.array([[y.mean() for _ in range(k)]]).T
sigma = np.array([[np.sqrt(y.var()) for _ in range(k)]]).T
for i in range(100):
alpha_old = alpha.copy()
miu_old = miu.copy()
sigma_old = sigma.copy()
gamma_hat = np.zeros((m, k))
for j in range(k):
gamma_hat[:, j, None] = alpha[j] * Gaussian.pdf(y, miu[j], sigma[j]**2)
gamma_hat = gamma_hat / gamma_hat.sum(axis=1).reshape((-1, 1))
miu = (gamma_hat.T @ y) / gamma_hat.sum(axis=0).T
sigma = (gamma_hat * (y - miu_old.T)**2).sum(axis=0).T / gamma_hat.sum(axis=0).T
sigma = np.sqrt(sigma)
alpha = gamma_hat.sum(axis=0).T / m
if np.sum((miu - miu_old)**2 + (sigma - sigma_old)**2 + (alpha - alpha_old)**2) < 1e-15:
print(f'converge at iteragtion: {i}')
break
print(f'final alpha: {alpha.T} \nfinal miu: {miu.T} \nfinal sigma:{sigma.T}')
converge at iteragtion: 0
final alpha: [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]
final miu: [[20.93333333 20.93333333 20.93333333 20.93333333 20.93333333 20.93333333
20.93333333 20.93333333 20.93333333 20.93333333]
[20.93333333 20.93333333 20.93333333 20.93333333 20.93333333 20.93333333
20.93333333 20.93333333 20.93333333 20.93333333]
[20.93333333 20.93333333 20.93333333 20.93333333 20.93333333 20.93333333
20.93333333 20.93333333 20.93333333 20.93333333]
[20.93333333 20.93333333 20.93333333 20.93333333 20.93333333 20.93333333
20.93333333 20.93333333 20.93333333 20.93333333]
[20.93333333 20.93333333 20.93333333 20.93333333 20.93333333 20.93333333
20.93333333 20.93333333 20.93333333 20.93333333]
[20.93333333 20.93333333 20.93333333 20.93333333 20.93333333 20.93333333
20.93333333 20.93333333 20.93333333 20.93333333]
[20.93333333 20.93333333 20.93333333 20.93333333 20.93333333 20.93333333
20.93333333 20.93333333 20.93333333 20.93333333]
[20.93333333 20.93333333 20.93333333 20.93333333 20.93333333 20.93333333
20.93333333 20.93333333 20.93333333 20.93333333]
[20.93333333 20.93333333 20.93333333 20.93333333 20.93333333 20.93333333
20.93333333 20.93333333 20.93333333 20.93333333]
[20.93333333 20.93333333 20.93333333 20.93333333 20.93333333 20.93333333
20.93333333 20.93333333 20.93333333 20.93333333]]
final sigma:[36.46453376 36.46453376 36.46453376 36.46453376 36.46453376 36.46453376
36.46453376 36.46453376 36.46453376 36.46453376]
9.4
还没看非监督呢