Skip to content

Commit a956231

Browse files
test
1 parent 69f5574 commit a956231

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

PCA & Kmeans Clustering - 31376.R

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
data <- read.csv("Medical_Dataset_with_Disease_Types.csv")
2+
3+
data1 <- data[, 1:10]
4+
head(data1)
5+
6+
install.packages("factoextra")
7+
library(factoextra)
8+
9+
pca <- prcomp(data1, center = TRUE, scale. = TRUE)
10+
head(pca)
11+
12+
#SCREE PLOT
13+
fviz_eig(pca, addlabels = TRUE, ylim = c(0,60))
14+
15+
fviz_pca_biplot(pca,
16+
repel = TRUE,
17+
col.var = "blue",
18+
col.ind = "gray40")
19+
20+
pca_scores <- as.data.frame(pca$x)
21+
22+
#let's use first 4 pc for clustering
23+
pc_data <- pca_scores[, 1:4]
24+
head(pc_data)
25+
26+
#K means library
27+
library(dplyr)
28+
29+
set.seed(125)
30+
kmeans_result <- kmeans(pc_data, centers = 3, nstart = 25)
31+
32+
#add cluster result to pca data
33+
pc_data$Cluster <- as.factor(kmeans_result$cluster)
34+
pc_data$True_label <- data$Disease_Type
35+
36+
#performance
37+
table(Cluster = pc_data$Cluster,Actual_Disease = pc_data$True_label)
38+

0 commit comments

Comments
 (0)