Load Required Libraries
library(cluster)
library(factoextra)
## Loading required package: ggplot2
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
library(Rtsne)
library(imager)
## Loading required package: magrittr
##
## Attaching package: 'imager'
## The following object is masked from 'package:magrittr':
##
## add
## The following objects are masked from 'package:stats':
##
## convolve, spectrum
## The following object is masked from 'package:graphics':
##
## frame
## The following object is masked from 'package:base':
##
## save.image
library(nnet)
library(caret)
## Loading required package: lattice
library(dplyr)
##
## Attaching package: 'dplyr'
## The following object is masked from 'package:imager':
##
## where
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(stats)
# Define File Paths (Modify as needed)
train_dir <- "C:\\Users\\HP_VICTUS\\OneDrive\\Pictures\\Assignment\\dataset\\Train"
test_dir <- "C:\\Users\\HP_VICTUS\\OneDrive\\Pictures\\Assignment\\dataset\\Test"
animal_classes <- list.files(train_dir)
# Function to Load and Preprocess Images
load_and_preprocess_images <- function(folder_path, target_size = c(64, 64)) {
images <- list()
labels <- c()
for (animal in animal_classes) {
img_files <- list.files(paste0(folder_path, "/", animal), full.names = TRUE)
for (img_path in img_files) {
img <- load.image(img_path) # Load the image
img <- resize(img, target_size[1], target_size[2]) # Resize the image to 64x64
img <- as.numeric(img) / 255.0 # Flatten and normalize the image (0 to 1)
images[[length(images) + 1]] <- img
labels <- c(labels, animal)
}
}
images <- do.call(rbind, images) # Combine image vectors into a matrix
list(images = images, labels = labels)
}
# Load Training and Testing Datasets
train_data <- load_and_preprocess_images(train_dir)
test_data <- load_and_preprocess_images(test_dir)
# Convert Labels to Factors and Numeric Encoding
train_data$labels <- factor(train_data$labels)
test_data$labels <- factor(test_data$labels)
train_labels <- as.numeric(train_data$labels) - 1
test_labels <- as.numeric(test_data$labels) - 1
# K-Means Clustering and Visualization with PCA
set.seed(42) # Set seed for reproducibility
# Perform PCA (Optional: Modify the number of components)
pca_train <- prcomp(train_data$images, center = TRUE, scale. = TRUE)
train_pca <- pca_train$x[, 1:2] # Select the first 2 principal components
# K-Means Clustering with the assumed number of clusters
kmeans_result <- kmeans(train_pca, centers = length(animal_classes))
# Visualize clusters using PCA
fviz_cluster(kmeans_result, data = train_pca, geom = "point", ellipse = TRUE) + ggtitle("K-Means Clustering with PCA")

# Silhouette Plot for K-Means Clustering
silhouette_score <- silhouette(kmeans_result$cluster, dist(train_pca))
fviz_silhouette(silhouette_score) + ggtitle("Silhouette Plot for K-Means Clustering")
## cluster size ave.sil.width
## 1 1 15 0.27
## 2 2 31 0.29
## 3 3 35 0.41
## 4 4 40 0.26
## 5 5 21 0.35
## 6 6 32 0.39
## 7 7 59 0.36
## 8 8 19 0.48

# Multinomial Logistic Regression with PCA
# Perform PCA again (modify the number of components)
pca_train <- prcomp(train_data$images, center = TRUE, scale. = TRUE)
pca_test <- predict(pca_train, newdata = test_data$images)
num_components <- 100 # Increase the number of components (e.g., 100)
train_pca <- pca_train$x[, 1:num_components]
test_pca <- pca_test[, 1:num_components]
# Train the multinomial logistic regression model
logistic_model <- multinom(as.factor(train_labels) ~ ., data = as.data.frame(train_pca), maxit = 200)
## # weights: 816 (707 variable)
## initial value 524.019269
## iter 10 value 159.632697
## iter 20 value 125.513875
## iter 30 value 117.905106
## iter 40 value 113.311864
## iter 50 value 106.660721
## iter 60 value 96.372454
## iter 70 value 83.800005
## iter 80 value 75.559504
## iter 90 value 68.064457
## iter 100 value 53.850238
## iter 110 value 30.218898
## iter 120 value 22.218339
## iter 130 value 19.987521
## iter 140 value 16.222232
## iter 150 value 12.247929
## iter 160 value 9.702401
## iter 170 value 5.428634
## iter 180 value 3.385133
## iter 190 value 2.069053
## iter 200 value 0.922692
## final value 0.922692
## stopped after 200 iterations
# Predict on the PCA-transformed test data
predictions <- predict(logistic_model, as.data.frame(test_pca))
# Evaluate model performance using confusion matrix
conf_matrix <- confusionMatrix(as.factor(predictions), as.factor(test_labels))
print(conf_matrix)
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1 2 3 4 5 6 7
## 0 7 0 1 3 0 1 0 0
## 1 1 3 3 1 2 1 1 4
## 2 0 2 1 1 1 4 2 2
## 3 1 1 0 25 5 0 2 0
## 4 1 1 1 3 2 0 1 3
## 5 0 1 2 1 1 4 2 0
## 6 0 1 1 0 0 1 3 0
## 7 1 2 2 2 0 0 0 2
##
## Overall Statistics
##
## Accuracy : 0.4159
## 95% CI : (0.324, 0.5124)
## No Information Rate : 0.3186
## P-Value [Acc > NIR] : 0.01854
##
## Kappa : 0.3014
##
## Mcnemar's Test P-Value : NA
##
## Statistics by Class:
##
## Class: 0 Class: 1 Class: 2 Class: 3 Class: 4 Class: 5
## Sensitivity 0.63636 0.27273 0.09091 0.6944 0.18182 0.36364
## Specificity 0.95098 0.87255 0.88235 0.8831 0.90196 0.93137
## Pos Pred Value 0.58333 0.18750 0.07692 0.7353 0.16667 0.36364
## Neg Pred Value 0.96040 0.91753 0.90000 0.8608 0.91089 0.93137
## Prevalence 0.09735 0.09735 0.09735 0.3186 0.09735 0.09735
## Detection Rate 0.06195 0.02655 0.00885 0.2212 0.01770 0.03540
## Detection Prevalence 0.10619 0.14159 0.11504 0.3009 0.10619 0.09735
## Balanced Accuracy 0.79367 0.57264 0.48663 0.7888 0.54189 0.64750
## Class: 6 Class: 7
## Sensitivity 0.27273 0.18182
## Specificity 0.97059 0.93137
## Pos Pred Value 0.50000 0.22222
## Neg Pred Value 0.92523 0.91346
## Prevalence 0.09735 0.09735
## Detection Rate 0.02655 0.01770
## Detection Prevalence 0.05310 0.07965
## Balanced Accuracy 0.62166 0.55660
# Calculate accuracy
accuracy <- mean(predictions == test_labels)
print(paste("Accuracy: ", round(accuracy * 100, 2), "%"))
## [1] "Accuracy: 41.59 %"
library(clustertend)
## Package `clustertend` is deprecated. Use package `hopkins` instead.
hopkins_statistic <- hopkins(train_pca)
## Warning in hopkins(train_pca): Package `clustertend` is deprecated. Use
## package `hopkins` instead.
print(paste("Hopkins Statistic:", hopkins_statistic))
## [1] "Hopkins Statistic: 0.313934370147783"