K-Mean Clustering

 K-Mean Clustering

K-mean clustering is also one of the most popular algorithm of Machine Learning lies in unsupervised learning. It works by making clusters. You have to clear the concept about the k-mean clustering before understanding this code. Furthermore, this algorithm is in scratch format using simple Numpy and also we using matplotlib for plotting the data. 


import numpy as np
import matplotlib.pyplot as plt
var=np.array([[1.0,1.0],[1.5,2.0],[3.0,4.0],[5.0,7.0],[3.5,5.0],[4.5,5.0],[3.5,4.5]])

if len(var)>1:
    centroid1=var[0]
    centroid2=var[1]
    x=y=0
    plot1=np.array([centroid1])
    plot2=np.array([centroid2])
    x+=1
    y+=1
    for i in range(2,len(var)):
        ed1=np.sqrt(((centroid1[0]-var[i][0])**2)+((centroid1[1]-var[i][1])**2))
        ed2=np.sqrt(((centroid2[0]-var[i][0])**2)+((centroid2[1]-var[i][1])**2))
        if(ed1>ed2):
            centroid2[0]=(centroid2[0]+var[i][0])/2
            centroid2[1]=(centroid2[1]+var[i][1])/2
            plot2=np.append(plot2,centroid2)
            y+=1
        else:
            centroid1[0]=(centroid1[0]+var[i][0])/2
            centroid1[1]=(centroid1[1]+var[i][1])/2
            plot1=np.append(plot1,centroid1)
    print("Cluster 1 is ",plot1)
    print("Cluster 2 is ",plot2)

Comments

Popular posts from this blog

Multiple inheritance,friend function and multiple file in oop(object oriented programming)

Concepts of OOP (object oriented programming)

Concepts in c++........