代码:
# 导入必要的库
import matplotlib.pyplot as plt # 用于绘制图形
from sklearn.cluster import KMeans # KMeans 聚类算法
import numpy as np # 数值计算库
# 定义 class1 到 class4 的数据点,模拟四个不同的类(每个类7个二维点)
class1_points = np.array([[1.9, 1.2],
[1.5, 2.1],
[1.9, 0.5],
[1.5, 0.9],
[0.9, 1.2],
[1.1, 1.7],
[1.4, 1.1]])
class2_points = np.array([[-1.9, 1.2],
[-1.5, 2.1],
[-1.9, 0.5],
[-1.5, 0.9],
[-0.9, 1.2],
[-1.1, 1.7],
[-1.4, 1.1]])
class3_points = np.array([[1.9, -1.2],
[1.5, -2.1],
[1.9, -0.5],
[1.5, -0.9],
[0.9, -1.2],