import numpy as np
import os
import matplotlib
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')
plt.rcParams['axes.labelsize'] = 14
plt.rcParams['xtick.labelsize'] = 12
plt.rcParams['ytick.labelsize'] = 12
np.random.seed(42)
from sklearn.datasets import make_blobs
# 指定五个中心,构造数据
blob_centers = np.array(
[[0.2, 2.3],
[-1.5, 2.3],
[-2.8, 1.8],
[-2.8, 2.8],
[-2.8, 1.3]])
blob_std = np.array([0.4, 0.3, 0.1, 0.1, 0.1]) # 发散程度不那么大
# 样本个数,中心点,发散程度
x, y = make_blobs(n_samples=2000,
centers=blob_centers,
cluster_std=blob_std,
random_state=7)
# print(x,y)
def plot_clusters(x, y=None):
plt.scatter(x[:, 0], x[:, 1], c=y, s=1)
plt.xlabel('$x_1$', fontsize=14)
plt.ylabel('$x_2$', fontsize=14, rotation=0)
#
Kmeans代码及其可视化
最新推荐文章于 2024-06-05 09:57:58 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)