K means Clustering – Introduction
We are given a data set of items, with certain features, and values for these features (like a vector). The task is to categorize those items into groups. To achieve this, we will use the kMeans algorithm; an unsupervised learning algorithm.
Here is an python code that simply divides the vector into group of clusters using kmeans algorithm.
import math
x=[185,170,168,179,182,188]
y=[72,56,60,68,72,77]
k=2
clust1=[“cluster1”]
clust2=[“cluster2”]
k1=[]
k2=[]
k1.append([x[0],y[0]])
k2.append([x[1],y[1]])
for i in range(len(x)):
c1=(x[i]-k1[0][0])**2
c2=(y[i]-k1[0][1])**2
print(c1)
print(c2)
print(math.sqrt((c1+c2)))
clust1.append(math.sqrt((c1+c2)))
for i in range(len(x)):
c11=(k2[0][0]-x[i])**2
c22=(k2[0][1]-y[i])**2
print(c11)
print(c22)
print(math.sqrt((c11+c22)))
clust2.append(math.sqrt((c11+c22)))
if clust1[i] > clust2[i]:
k2[0]=[(k2[0][0]+x[i])/2,(k2[0][1]+y[i])/2]
print(k2)
else:
k1[0]=[(k1[0][0]+x[i])/2,(k1[0][1]+y[i])/2]
print(k1)
print(k1)
print(k2)
print(clust1)
print(clust2)