Machine learning · R
Model Selection and Clustering in R
Reproducible credit-approval model validation and Iris k-means clustering with kernlab and kknn.
Business question
How do we select a predictive model without using the final test set, and evaluate clusters without fitting to species labels?
Compares KNN and linear SVM with stratified five-fold cross-validation and a 60/20/20 split. The full Iris script covers the elbow method, k-means, and an exploratory predictor comparison. Species labels score predictor sets; that comparison is not an untouched-test performance estimate.
Model Selection and Clustering in R · source preview
1# Question 4.2: Iris clustering with k-means23data(iris)45dim(iris)6names(iris)7head(iris)8table(iris$Species)910# Step 2: I will Keep only the four predictors for k-means11# Species is not included.1213iris_predictors <- iris[, 1:4]1415head(iris_predictors)16summary(iris_predictors)1718# Step 3: Test possible numbers of clusters19# Elbow Method2021set.seed(6501)2223k_values <- 2:624within_ss <- numeric(length(k_values))2526for (i in seq_along(k_values)) {27 28 model <- kmeans(29 iris_predictors,30 centers = k_values[i],31 nstart = 5032 )33 34 within_ss[i] <- model$tot.withinss35}3637data.frame(38 k = k_values,39 total_within_cluster_sum_of_squares = within_ss40)4142plot(43 k_values,44 within_ss,45 type = "b",46 xlab = "Number of clusters (k)",47 ylab = "Total within-cluster sum of squares",48 main = "Elbow Method for Iris Data"49)5051# Step 4: Run k-means with k = 3 using all four predictors5253set.seed(6501)5455iris_kmeans_all <- kmeans(56 iris_predictors,57 centers = 3,58 nstart = 5059)6061cluster_table_all <- table(62 Cluster = iris_kmeans_all$cluster,63 Species = iris$Species64)6566cluster_table_all676869# Step 5: Compare all predictor combinations with k = 37071cluster_accuracy <- function(selected_predictors, actual_species) {72 73 set.seed(6501)74 75 model <- kmeans(76 selected_predictors,77 centers = 3,78 nstart = 5079 )80 81 comparison_table <- table(82 Cluster = model$cluster,83 Species = actual_species84 )85 86 sum(apply(comparison_table, 1, max)) / length(actual_species)87}8889predictor_names <- names(iris)[1:4]9091combination_results <- data.frame(92 predictors = character(),93 accuracy = numeric()94)9596for (size in seq_along(predictor_names)) {97 98 combinations <- combn(99 predictor_names,100 size,101 simplify = FALSE102 )103 104 for (variables in combinations) {105 106 current_accuracy <- cluster_accuracy(107 iris[, variables, drop = FALSE],108 iris$Species109 )110 111 combination_results <- rbind(112 combination_results,113 data.frame(114 predictors = paste(variables, collapse = " + "),115 accuracy = current_accuracy116 )117 )118 }119}120121combination_results <- combination_results[122 order(-combination_results$accuracy),123]124125combination_results126127128# Step 6: Final best k-means model129130best_predictors <- c(131 "Petal.Length",132 "Petal.Width"133)134135set.seed(6501)136137best_iris_model <- kmeans(138 iris[, best_predictors],139 centers = 3,140 nstart = 50141)142143best_cluster_table <- table(144 Cluster = best_iris_model$cluster,145 Species = iris$Species146)147148best_accuracy <- sum(149 apply(best_cluster_table, 1, max)150) / nrow(iris)151152best_cluster_table153best_accuracy