本次博客我们介绍一下机器学习里经典的聚类算法K-means,它属于典型的无监督学习算法。其核心思想也非常的简单,大致思路如下:
下面我们举个栗子,记录有某音乐网站6位用户点播的歌曲流派,给定用户名称和九种音乐类型的播放数量,通过K-means将6位用户分成3簇(把播放数量当成数组或者向量进行簇质心的迭代运算)
用户/项目 | 项目1 | 项目2 | 项目3 | 项目4 | 项目5 | 项目6 | 项目7 | 项目8 | 项目9 |
---|---|---|---|---|---|---|---|---|---|
用户1 | 10 | 6 | 4 | 0 | 0 | 0 | 0 | 0 | 0 |
用户2 | 0 | 0 | 0 | 8 | 9 | 0 | 0 | 0 | 0 |
用户3 | 0 | 0 | 0 | 0 | 0 | 4 | 4 | 0 | 0 |
用户4 | 0 | 0 | 0 | 9 | 6 | 0 | 0 | 2 | 0 |
用户5 | 4 | 0 | 3 | 0 | 0 | 0 | 0 | 0 | 3 |
用户6 | 0 | 0 | 1 | 0 | 0 | 8 | 0 | 0 | 0 |
import numpy as np user_item_matrix = np.array([[10,6,4,0,0,0,0,0,0], [0,0,0,8,9,0,0,0,0], [0,0,0,0,0,4,4,0,0], [0,0,0,9,6,0,0,2,0], [4,0,3,0,0,0,0,0,3], [0,0,1,0,0,8,0,0,0]]) print("初始矩阵为:") print(user_item_matrix) K1 = user_item_matrix[0] K2 = user_item_matrix[1] K3 = user_item_matrix[2] number = 0 while(1): number += 1 print(f"\n############# cluster iter :{number} #############") cluster_one = [] cluster_two = [] cluster_three = [] distance = [] for item in user_item_matrix: distance.append([np.sqrt(sum((item - K1)**2)),np.sqrt(sum((item - K2)**2)),np.sqrt(sum((item - K3)**2))]) distance = np.array(distance) print("compute distance result\n",distance) num = distance.argmin(axis=1) print("cluster result\n",num) for i in range(len(num)): if(num[i]==0): cluster_one.append(i) elif(num[i]==1): cluster_two.append(i) else: cluster_three.append(i) K1_new = np.mean([user_item_matrix[i] for i in cluster_one ]) K2_new = np.mean([user_item_matrix[i] for i in cluster_two ]) K3_new = np.mean([user_item_matrix[i] for i in cluster_three ]) if(K1_new.all()==K1.all() and K2_new.all()==K2.all() and K3_new.all()==K3.all()): print("\n################ cluster result ################") print("cluster_one",cluster_one) print("cluster_two",cluster_two) print("cluster_three",cluster_three) break else: K1 = np.mean([user_item_matrix[i] for i in cluster_one]) K2 = np.mean([user_item_matrix[i] for i in cluster_two]) K3 = np.mean([user_item_matrix[i] for i in cluster_three])