Merge pull request #30 from Harold-Ran/patch-2

Update K-means_Clustering.py
This commit is contained in:
Dod-o 2021-09-13 14:55:11 +08:00 committed by GitHub
commit 01f6d6c9eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -79,6 +79,7 @@ def cal_distance(xi, xj):
dist = 0
for col in range(len(xi)):
dist += (xi[col]-xj[col]) ** 2
dist = math.sqrt(dist)
return dist
@ -125,8 +126,8 @@ def Adjusted_Rand_Index(group_dict, Ylist, k):
if n in group_dict[i] and n in y_dict[j]:
group_array[i][j] += 1 #如果数据n同时在group_dict的类别i和y_dict的类别j中group_array[i][j]的数值加一
RI = 0 #定义兰德系数(RI)
sum_i = np.zeros(3) #定义一个数组用于保存聚类结果group_dict中每一类的个数
sum_j = np.zeros(3) #定义一个数组用于保存外部标签y_dict中每一类的个数
sum_i = np.zeros(k) #定义一个数组用于保存聚类结果group_dict中每一类的个数
sum_j = np.zeros(k) #定义一个数组用于保存外部标签y_dict中每一类的个数
for i in range(k):
for j in range(k):
sum_i[i] += group_array[i][j]
@ -189,4 +190,4 @@ if __name__ == "__main__":
group_dict, scores = Kmeans(Xarray, k, iters) #进行k均值聚类
end = time.time() #保存结束时间
print('Time:', end-start)
plt.plot(range(iters), scores) #绘制ARI得分折线图
plt.plot(range(iters), scores) #绘制ARI得分折线图