rm(list=ls())
library("dbscan")
dcdata = read.csv('dcdata.txt')
target = dcdata[,3]
dcdata = dcdata[,1:2]
d = dist(dcdata)
############ A
# Perform hierarchical clustering with single link
hc_single = hclust(d, method = "single")
clustersSingle = cutree(hc_single, k = 2)
########### B
# Perform hierarchical clustering with complete link
hc_complete = hclust(d, method = "complete")
clustersComplete = cutree(hc_complete, k = 2)
########### C
model = dbscan(dcdata, eps = 0.75, minPts = 5)
clusters1 = model$cluster
model = dbscan(dcdata, eps = 1, minPts = 5)
clusters2 = model$cluster
model = dbscan(dcdata, eps = 1.25, minPts = 5)
clusters3 = model$cluster
model = dbscan(dcdata, eps = 1.5, minPts = 5)
clusters4 = model$cluster
model = kmeans(dcdata, 2)
clusterKMeans = model$cluster
# Q1 - kmeans and complete linkage hierarchical
plot(dcdata, col = clusters1 + 1, pch = 15, main = "DBSCAN(eps = 0.75, minPts = 5)")
plot(dcdata, col = clusters2 + 1, pch = 15, main = "DBSCAN(eps = 1, minPts = 5)")
plot(dcdata, col = clusters3 + 1, pch = 15, main = "DBSCAN(eps = 1.25, minPts = 5)")
plot(dcdata, col = clusters4 + 1, pch = 15, main = "DBSCAN(eps = 1.5, minPts = 5)")
plot(dcdata, col = clusterKMeans + 1, pch = 15, main = "kMeans")
plot(dcdata, col = clustersSingle, pch = 15, main = "Single Linkage")
plot(dcdata, col = clustersComplete, pch = 15, main = "Complete Linkage")
# Q2
Accuracy(clustersSingle, target)
# Q3
Accuracy(clustersComplete, target)
Comments