I have csv file 3 columns+ 1 column of label (true) I need to compare the I used kmeans to find a three-cluster solution. When I compare the cluster labels with the truth using a confusion matrix table I get an error Why?
#Insatll required packages:
install.packages("factoextra")
install.packages("caret")
install.packages("dplyr")
install.packages("mlbench")
install.packages("tidyr")
#Import required library:
library(factoextra)
library(caret)
library(dplyr)
library(mlbench)
library(tidyr)
library(e1071)
data_a <- read.csv(file = 'data_a.csv')
colnames(data_a)[1:4] <- c(' ',' ',' ','class')
head(data_a)
summary(data_a)
# Compute k-means with k = 3
dfa<-data_a[1:3]
set.seed(123)
kmean_model <- kmeans(dfa, 3, nstart = 25)
# Print the results
print(kmean_model)
fviz_cluster(kmean_model, data = data_a)
kmean_model_cluster <- as.data.frame(kmean_model$cluster)
names(kmean_model_cluster)[1] <- 'class'
head(kmean_model_cluster)
head(data_a)
kmean_model_cluster$class <- as.factor(ifelse(kmean_model_cluster=='1','2','3'))
kmean_model_cluster$class
confusionMatrix(data_a$class, kmean_model_cluster$class)
Also, the result of kmean_model_cluster$class not good