K-Mean Clustering using Hamming Distance
K-Mean Clustering using Hamming Distance
I already uploaded the post for K-Mean Clustering but that was using Euclidean distance and now I made this algorithm using Hamming Distance. This is completely scratch code which I made using Numpy. This will be good if you understand the topic of k-mean clustering.
import numpy as np
var=np.array([["small","green","Irregular","no"],["large","red","irregular","yes"],["large","red","circle","yes"],
["large","green","circle","no"],["large","green","irregular","no"],["small","red","circle","yes"],
["large","green","irregular","no"],["small","red","irrgular","no"],["small","green","circle","no"],
["large","red","circle","yes"]])
centroid=np.array(["small","red","circle"])
distance=np.zeros(10)
index=np.zeros(10);
yes=0
no=0
k=5
if(len(var)>1):
for i in range(len(var)):
for j in range(len(var[i])-1):
if(centroid[j]!=var[i][j]):
distance[i]=distance[i]+1
index[i]=i
size=len(distance)
for step in range(size):
min_idx = step
for i in range(step + 1, size):
if distance[i] < distance[min_idx]:
min_idx = i
(distance[step], distance[min_idx]) = (distance[min_idx], distance[step])
(index[step], index[min_idx]) = (index[min_idx], index[step])
index = index.astype(int)
for i in range(k):
if((var[index[i]][3])=="yes"):
yes=yes+1
else:
no=no+1
if(yes>no):
print("Decision is yes")
else:
print("Decision is no")
Comments
Post a Comment