[笔记] 使用numpy手写k-means算法
代码包括数据生成、可视化。 注意:下面代码仅供参考,实际使用还需加上一些约束,如迭代次数需要有个最大值,等等。 import numpy as np from matplotlib import pyplot as plt # - generate random data def generate_data(n_point_per_cate, center_point_list): """ n_point_per_cate: point number per category center_point_list: center point list """ points_list = [] for point in center_point_list: points_list.append(np.random.randn(n_point_per_cate, 2) + np.array(point)) return np.concatenate(points_list, axis=0) # - generate random data data = generate_data(100, [[3,4], [10,-4], [-5,0]]) data.shape (300, 2) # - visulize data plt.scatter(data[:,0], data[:,1]) #