10、高斯混合的密度估计
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
from sklearn import mixture
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
n_samples = 300
# 产生随机样本,两个分量
np.random.seed(0)
# 生成以(20,20)为中心的球形数据
shifted_gaussian = np.random.randn(n_samples, 2) + np.array([20, 20])
# 生成零中心拉伸高斯数据
C = np.array([[0.0, -0.7], [3.5, 0.7]])
stretched_gaussian = np.dot(np.random.randn(n_samples, 2), C)
# 将两个数据集连接到最终的训练集
X_train = np.vstack([shifted_gaussian, stretched_gaussian])
# 拟合具有两个分量的高斯混合模型
clf = mixture.GaussianMixture(n_components=2, covariance_type="full")
clf.fit(X_train)
# 将模型预测的分数显示为等高线图
x = np.linspace(-20.0, 30.0)
y = np.linspace(-20.0, 40.0)
X, Y = np.meshgrid(x, y)
XX = np.array([X.ravel(), Y.ravel()]).T
Z = -clf.score_samples(XX)
Z = Z.reshape(X.shape)
CS = plt.contour(
X, Y, Z, norm=LogNorm(vmin=1.0, vmax=1000.0), levels=np.logspace(0, 3, 10)
)
CB = plt.colorbar(CS, shrink=0.8, extend="both")
plt.scatter(X_train[:, 0], X_train[:, 1], 0.8)
plt.title("高斯混合的密度估计")
plt.axis("tight")
plt.show()