PROGRAM-3
AIM: Implementation of K-means Algorithm.
THEORY:
Clustering is the process of grouping objects with similarities.Kmeans is a clustering algorithm
where k represents the number of clusters formed.This falls under the category of unsupervised
learning.In this algorithm the number of centroids are identified and the data points nearer to the
centroid are grouped together in one cluster. To implement this algorithm we use sklearn module
from python libraries.
PROGRAM:
from sklearn.cluster import KMeans
from sklearn import metrics
import matplotlib.pyplot as plt
import numpy as np
x1 = np.array([1,1,2,3,4,5,6,6,7,8,8,9])
x2 = np.array([1,4,3,2,2,3,4,2,8,8,1,1])
X = np.array(list(zip(x1, x2))).reshape(len(x1), 2)
colors = ['b', 'g', 'c']
markers = ['o', 'v', 's']
K=3
Y = KMeans(n_clusters=K).fit(X)
print(Y.cluster_centers_)
centers = np.array(Y.cluster_centers_)
plt.plot()
plt.title('k means centroids')
for i, l in enumerate(Y.labels_):
plt.plot(x1[i], x2[i], color=colors[l], marker=markers[l])
plt.xlim([0, 10])
plt.ylim([0, 10])
plt.scatter(centers[:,0], centers[:,1], marker="x", color='r')
plt.show()
OUTPUT:
PROGRAM-2
AIM: Implementation of DBSCAN Algorithm.
THEORY:
DBSCAN stands for density based spatial clustering of applications with noise.
It is a clustering algorithm which is density based.It uses two parameters i.e minimum no of
samples and eps(epsilon).It identifies a point and calculate its distance to the next point and reach
each and every node in the data and form clusters.
PROGRAM:
from sklearn.cluster import KMeans
from sklearn.cluster import DBSCAN
from sklearn import metrics
import numpy as np
import matplotlib.pyplot as plt
x1 = np.array([3, 1, 1, 2, 1, 6, 6, 6, 5, 6, 7, 8, 9, 8, 9, 9, 8])
x2 = np.array([5, 4, 6, 6, 5, 8, 6, 7, 6, 7, 1, 6, 1, 2, 3, 2, 3])
X = np.array(list(zip(x1, x2))).reshape(len(x1), 2)
colors = ['b', 'g', 'r']
markers = ['o','x','v']
Y= DBSCAN(eps=2,min_samples=3)
Y.fit(X)
print("dbscan labels",Y.labels_)
for i, l in enumerate(Y.labels_):
plt.plot(x1[i], x2[i], color=colors[l], marker=markers[l])
plt.xlim([0, 10])
plt.ylim([0, 10])
plt.show()
OUTPUT: