import numpy as np
# k-means算法的实现
# s = [[20, 5, 15], [9, 18, 10], [21, 10, 7], [17, 13, 25], [12, 21, 19]]
# 选定两点
# 1.求距离
# 两种类型
label1 = []
label2 = []
# 先假定两个中心点
mean1 = [20, 15, 14]
mean2 = [17, 19, 26]
ls = []
# 第一个参数是待聚合的数据,第二个和第三个参数分别是其中心点
def distance(t1, t2, t3):
global label1;
label1 = []
global label2;
label2 = []
length = len(t1[0])
for i in t1:
distance1 = 0
distance2 = 0
count = 0
while (count < length):
distance1 = distance1 + (i[count] - t2[count]) ** 2
distance2 = distance2 + (i[count] - t3[count]) ** 2
count = count + 1
if (distance1 > distance2):
label1.append(i)
else:
label2.append(i)
remeans(la1=label1, la2=label2)
# 2.重新计算中心值
def remeans(**label):
flag = 0
global mean1;
mean1 = []
global mean2;
mean2 = []
for key, value in label.items():
index = 0
for i in range(len(value[0])):
temp = np.array(value)
row = temp[:, index:index + 1]
row = [i for j in row for i in j]
length = len(row)
count = sum(i for i in row)
clx = count / length
if (flag == 0):
mean1.append(clx)
else:
mean2.append(clx)
index = index + 1
flag = flag + 1
# temp = np.array(temp)
# ls = np.array(ls)
# flag = 0
# while ((temp != ls).all() and flag < 20):
# 存储临时变量
temp1 = []
temp2 = []
flag = 0
t1 = [[20, 5, 15], [21, 10, 7], [17, 13, 25], [12, 22, 17], [21, 10, 7], [12, 21, 19]]
while (temp1 != mean1 and temp2 != mean2 and flag < 20):
temp1 = mean1
temp2 = mean2
distance(t1, temp1, temp2)
flag = flag + 1
print("class1", label1)
print("class", label2)
python之k-means算法实现(待修正)
最新推荐文章于 2022-04-30 11:30:42 发布