为了便于可视化,样本数据为随机生成的二维样本点. from matplotlib import pyplot as plt import numpy as np import random def kmeans(a, k): def randomChoose(a, k): # 从数组a中随机选取k个元素,返回一个list args = np.arange(len(a)) # 元素下标 for i in range(k): x = np.random.randint(i, len(a)) args…