-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCODE_asteroids_analysis_KNN.R
97 lines (58 loc) · 2.53 KB
/
CODE_asteroids_analysis_KNN.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
setwd("~/Github/AsteroidsClassification")
install.packages('fpc')
install.packages('cluster')
install.packages('seriation')
library(cluster)
#load dataset RObject as asteroids_data
load("DATA_asteroids_dataset.RData")
numeric_covariates = asteroids_data[,c("Orbit.Axis..AU." , "Orbit.Eccentricity" , "Orbit.Inclination..deg." , "Perihelion.Argument..deg.",
"Node.Longitude..deg." , "Mean.Anomoly..deg." , "Perihelion.Distance..AU." , "Aphelion.Distance..AU.",
"Orbital.Period..yr.", "Minimum.Orbit.Intersection.Distance..AU.", "Asteroid.Magnitude"
)]
set.seed(22)
fit = kmeans(numeric_covariates, 4)
fit
fit$cluster
### barplot cluster frequency
img_name_plot <- paste("IMG_asteroids_model_KNN_", "barplot_k_4", ".png", sep = "")
png(img_name_plot,res = 800, height = 10, width = 15, unit='in')
barplot(table(fit$cluster), main = "Frequency by cluster",
col = rainbow(4))
dev.off()
img_name_plot <- paste("IMG_asteroids_model_KNN_", "barplot_k_6", ".png", sep = "")
png(img_name_plot, res = 800, height = 10, width = 15, unit='in')
fit6 = kmeans(numeric_covariates, 6)
fit6
barplot(table(fit6$cluster), main = "Frequency by cluster",
col = rainbow(6))
dev.off()
### barplot Classification class
barplot(table(asteroids_data$Classification), main= "Frequency Asteroids Classes", col=rainbow(4))
kms = silhouette(fit$cluster,dist(numeric_covariates))
kms
plot(kms)
img_name_plot <- paste("IMG_asteroids_model_KNN_", "silouhette_score_per_k", ".png", sep = "")
png(img_name_plot,res = 800, height = 10, width = 15, unit='in')
silhouette_score <- function(k){
km <- kmeans(numeric_covariates, centers = k, nstart=25)
ss <- silhouette(km$cluster, dist(numeric_covariates))
mean(ss[, 3])
}
k <- 2:10
avg_sil <- sapply(k, silhouette_score)
plot(k, type='b', avg_sil, xlab='Number of clusters', ylab='Average Silhouette Scores', frame=FALSE)
dev.off()
### dissimilarity clusters
library('seriation')
dissplot(dist(numeric_covariates), labels=fit6$cluster, options=list(main='Kmeans Clustering with K=6'))
### fine tuninig
fit$cluster
### cluster evaluation
cluser.evaluation(ground_truth, fit$cluser)
### measuring the silhouette score at the number of clusters
library(fpc)
nk = 2:4
set.seed(22)
Sw < sapply(nk, function(k) { cluster.stats(dist(numeric_covariates),kmeans(numeric_covariates, centers=k)$cluster)$avg.silwidth})
SW
plot(SW, type='I', xlab='number of clusters', ylab='average silhoutte')