#Load required libraries library(imager) library(caret) library(magick) library(tidyverse) library(skimr) library(DataExplorer) library(ggplot2) library(neuralnet) library(dplyr) library(nnet)
image_folder <- (“C:/Users/Kavyadinesh/Downloads/New”) # Function to convert an image to grayscale convert_to_grayscale <- function(image_path, save_path) { # Read the image img <- image_read(image_path) # Convert to grayscale grayscale_img <- image_convert(img, colorspace = ‘gray’) # Save the grayscale image image_write(grayscale_img, save_path) }
subdirs <- list.dirs(image_folder, full.names = TRUE, recursive = TRUE)
for (subdir in subdirs) { image_files <- list.files(subdir, pattern = “\.(jpg|png)$”, full.names = TRUE)
for (file in image_files) { # Define where to save the grayscale image save_path <- file.path(subdir, paste0(“grayscale_”, basename(file)))
convert_to_grayscale(file, save_path)
cat("Converted", basename(file), "to grayscale and saved as", save_path, "\n")
} }
image_folder <- (“C:/Users/Kavyadinesh/Downloads/Grayscale”)
image_to_vector <- function(image_path) { # Read the image img <- image_read(image_path) # Convert image to grayscale (if not already grayscale) grayscale_img <- image_convert(img, colorspace = ‘gray’) # Resize the image to a fixed size (optional, e.g., 100x100 pixels) resized_img <- image_resize(grayscale_img, “100x100!”) # Convert the image to a matrix of pixel values img_matrix <- as.integer(image_data(resized_img)[1,,]) # Flatten the matrix into a vector as.vector(img_matrix) }
image_data_list <- list() image_labels <- c()
subdirs <- list.dirs(image_folder, full.names = TRUE, recursive = TRUE) for (subdir in subdirs) { image_files <- list.files(subdir, pattern = “\.(jpg|png)$”, full.names = TRUE) # Get the label (subfolder name) label <- basename(subdir) for (file in image_files) { # Convert the image to a vector of pixel values image_vector <- image_to_vector(file) # Add the image vector to the list image_data_list[[length(image_data_list) + 1]] <- image_vector # Store the label for this image image_labels <- c(image_labels, label) } }
image_data_df <- do.call(rbind, image_data_list) image_data_df <- as.data.frame(image_data_df) # Add the labels as a new column image_data_df$label <- image_labels
csv_file_path <- (“C:/Users/Kavyadinesh/Downloads/Grayscale/data.csv”)
write.csv(image_data_df, csv_file_path, row.names = FALSE)
cat(“Dataset has been saved as a CSV at”,csv_file_path)
#PREPROCESSIN AND EDA # Load the CSV file data <- read.csv(“C:/Users/Kavyadinesh/Downloads/Grayscale/data.csv”)
head(data)
str(data)
summary(data)
sum(is.na(data)) # Total missing values
colSums(is.na(data))
data_clean <- na.omit(data) data_clean
duplicated_rows <- data[duplicated(data), ]
data_clean <- data %>% distinct()
data_clean <- data_clean %>% mutate(across(where(is.character), as.factor))
table(data_clean$your_categorical_column)
#PERFORMING LOGISTIC REGRESSION data\(label <- as.factor(data\)label) data$label
set.seed(123) # Set seed for reproducibility sample <- sample(c(TRUE, FALSE), nrow(data), replace = TRUE, prob = c(0.7, 0.3)) train_data <- data[sample, ] test_data <- data[!sample, ]
str(train_data)
logistic_model <- glm(label ~ ., data = train_data, family = binomial)
summary(logistic_model)
#K-MEANS CLUSTERING
library(tidyverse) names(data) clustering_data <- data %>% select(V1, V2, V3)
set.seed(123)
kmeans_result <- kmeans(clustering_data, centers = 3)
print(kmeans_result)
data\(cluster <- as.factor(kmeans_result\)cluster)
head(data)
#NEURAL NETWORK # NEURAL NETWORK WITH 1 HIDDEN LAYER 5 NEURONS # Set seed for reproducibility set.seed(123)
data <- data.frame( feature1 = rnorm(100), feature2 = rnorm(100), label = as.factor(sample(c(0, 1), 100, replace = TRUE)) # Binary target )
train_index <- createDataPartition(data$label, p = 0.8, list = FALSE) train_data <- data[train_index, ] test_data <- data[-train_index, ]
train_data_scaled <- as.data.frame(scale(train_data[, -3])) # Scale features only train_data_scaled\(label <- train_data\)label # Add label back
data <- data.frame( feature1 = rnorm(100), feature2 = rnorm(100), label = as.factor(sample(c(0, 1), 100, replace = TRUE)) # Binary target )
train_index <- createDataPartition(data$label, p = 0.8, list = FALSE) train_data <- data[train_index, ] test_data <- data[-train_index, ]
train_data_scaled <- as.data.frame(scale(train_data[, -3])) # Scale features only train_data_scaled\(label <- train_data\)label # Add label back
data <- data.frame( feature1 = rnorm(100), feature2 = rnorm(100), label = as.factor(sample(c(0, 1), 100, replace = TRUE)) # Binary target )
train_index <- createDataPartition(data$label, p = 0.8, list = FALSE) train_data <- data[train_index, ] test_data <- data[-train_index, ]
train_data_scaled <- as.data.frame(scale(train_data[, -3])) # Scale features only train_data_scaled\(label <- train_data\)label # Add label back