编写生成数据x的代码,x=(x1,x2),x1和x2符合二维三模态的高斯混合模型,模态的权重分别为0.2,03,0.5。 
时间: 2023-03-01 12:17:46 浏览: 17
以下是使用 Python 生成数据 x 的代码:
```
import numpy as np
import matplotlib.pyplot as plt
from sklearn.mixture import GaussianMixture
np.random.seed(0)
# 定义三模态的高斯混合模型的参数
gmm = GaussianMixture(n_components=3, covariance_type='full',
weights=[0.2, 0.3, 0.5],
means=[[0, 0], [1, 1], [2, 2]],
precisions_chol=[[[1, 0], [0, 1]],
[[1, 0.5], [0.5, 1]],
[[1, -0.5], [-0.5, 1]]])
# 生成样本
x = gmm.sample(1000)
# 绘制数据
plt.scatter(x[:, 0], x[:, 1], s=5)
plt.show()
```
这段代码使用了 `numpy` 和 `scikit-learn` 库,其中:
- `np.random.seed(0)` 用于设置随机数生成器的种子,以便得到可重复的结果;
- `GaussianMixture` 是 `scikit-learn` 库中实现高斯混合模型的类;
- `gmm.sample(1000)` 用于生成 1000 个样本;
- `plt.scatter` 和 `plt.show` 用于绘制生成的数据。
相关推荐












