class pattern:
x=0
y=0
def __init__(self,x,y):
self.x=x
self.y=y
def dis(a,b):
return ((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y))**0.5
eps=0.00001
maxn=1000050
inf=4000000000000000000
print("请输入需要将模式分为的类别数 c :")
c=input()
c=int(c)
print("请输入模式总数 n :")
n=input()
n=int(n)
print("请输入各模式的两个特征点 :")
a=[]
b=[]
for i in range(n):
x,y=map(int,input().split())
tmp=pattern(x,y)
a.append(tmp)
for i in range(c):
tmp=pattern(a[i].x,a[i].y)
b.append(tmp)
ans=0
while ans<c:
cnt=[0]*maxn
belong=[-1]*maxn
for i in range(n):
minn=4000000000000000000
mini=-1
for j in range(c):
dis_to_center=dis(a[i],b[j])
if dis_to_center<minn:
minn=dis_to_center
mini=j
belong[i]=mini
cnt[mini]=cnt[mini]+1
sum=[]
for i in range(c):
tmp=pattern(0,0)
sum.append(tmp)
for i in range(n):
sum[belong[i]].x+=a[i].x
sum[belong[i]].y+=a[i].y
print(a[i].x,a[i].y,belong[i],sum[belong[i]].x,sum[belong[i]].y)
ans=0
for i in range(n):
print(i,"号点",a[i].x,a[i].y)
for i in range(c):
if cnt[i]!=0:
sum[i].x=sum[i].x/cnt[i]
sum[i].y=sum[i].y/cnt[i]
if abs(sum[i].x-b[i].x)<eps and abs(sum[i].y-b[i].y)<eps:
ans=ans+1
b[i].x=sum[i].x
b[i].y=sum[i].y
if a[1].x!=1 or a[1].y!=0:
print("i=",i)
for i in range(n):
print(i,"号点",a[i].x,a[i].y)
print("C均值聚类算法已经完成!
c个类中心分别为
")
for i in range(c):
print("第",i+1,"类的聚类中心的两个特征值分别为",b[i].x,b[i].y)
print()
for i in range(c):
print("属于第",i+1,"类的点有:")
for j in range(n):
if belong[j]==i:
print(j+1,"号点",a[j].x,a[j].y)
print()