Problem Statment

Smithsonian Butterfly Dataset adalah sebuah dataset yang berisi kumpulan kupu-kupu diseluruh dunia.Pada dataset ini terdapat 18 attribut yang tersedia. Akan tetapi, tidak semua attribut dapat digunakan. Oleh Karena itu, attribut yang akan digunakan sebagai berikut.

Tujuan

Import Library

library(stringr)
## Warning: package 'stringr' was built under R version 4.3.2
if (!require("tm")) {
  install.packages("tm")
  library(tm)
}
## Loading required package: tm
## Warning: package 'tm' was built under R version 4.3.2
## Loading required package: NLP
if (!require("wordcloud")) {
  install.packages("wordcloud")
  library(wordcloud)
}
## Loading required package: wordcloud
## Warning: package 'wordcloud' was built under R version 4.3.2
## Loading required package: RColorBrewer
library(factoextra)
## Warning: package 'factoextra' was built under R version 4.3.2
## Loading required package: ggplot2
## 
## Attaching package: 'ggplot2'
## The following object is masked from 'package:NLP':
## 
##     annotate
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
library(png)
library(ggplot2)
library(animation)
## Warning: package 'animation' was built under R version 4.3.2
library(ggplot2)
library(gridExtra)
library(grid)
library(cluster)
library(fpc)
## Warning: package 'fpc' was built under R version 4.3.2

Import Data

setwd("D:/S2-Matematika-ITS/Semester 2/Konsep Sains Data/EAS")
getwd()
## [1] "D:/S2-Matematika-ITS/Semester 2/Konsep Sains Data/EAS"
df <- read.csv('Data_EAS_KSD.csv')
df[df == ""] <- NA
head(df)
##                                            name               scientific_name
## 1                                    Paper Kite                 Idea leuconoe
## 2                                Doris Longwing              Heliconius doris
## 3 Asian Swallowtail, Chinese Yellow Swallowtail                Papilio xuthus
## 4                                       Postman Heliconius melpomene sticheli
## 5                    Red Postman, Small Postman    Heliconius erato notabilis
## 6                   Great Eggfly, Common Eggfly             Hypolimnas bolina
##   gender
## 1   <NA>
## 2   <NA>
## 3   <NA>
## 4   <NA>
## 5   <NA>
## 6 Female
##                                                                           taxonomy
## 1                Animalia, Arthropoda, Hexapoda, Insecta, Lepidoptera, Nymphalidae
## 2  Animalia, Arthropoda, Hexapoda, Insecta, Lepidoptera, Nymphalidae, Heliconiinae
## 3 Animalia, Arthropoda, Hexapoda, Insecta, Lepidoptera, Papilionidae, Papilioninae
## 4  Animalia, Arthropoda, Hexapoda, Insecta, Lepidoptera, Nymphalidae, Heliconiinae
## 5  Animalia, Arthropoda, Hexapoda, Insecta, Lepidoptera, Nymphalidae, Heliconiinae
## 6   Animalia, Arthropoda, Hexapoda, Insecta, Lepidoptera, Nymphalidae, Nymphalinae
##                                         region
## 1 US Mid Atlantic (PA, NJ, MD, DE, DC, VA, WV)
## 2 US Mid Atlantic (PA, NJ, MD, DE, DC, VA, WV)
## 3 US Mid Atlantic (PA, NJ, MD, DE, DC, VA, WV)
## 4 US Mid Atlantic (PA, NJ, MD, DE, DC, VA, WV)
## 5 US Mid Atlantic (PA, NJ, MD, DE, DC, VA, WV)
## 6 US Mid Atlantic (PA, NJ, MD, DE, DC, VA, WV)
##                                                                      locality
## 1 NMNH Butterfly Pavilion, North America, United States, District of Columbia
## 2         NMNH Insect Zoo, North America, United States, District of Columbia
## 3 NMNH Butterfly Pavilion, North America, United States, District of Columbia
## 4         NMNH Insect Zoo, North America, United States, District of Columbia
## 5         NMNH Insect Zoo, North America, United States, District of Columbia
## 6 NMNH Butterfly Pavilion, North America, United States, District of Columbia
##   date  usnm_no                                                            guid
## 1 <NA> EO401041 http://n2t.net/ark:/65665/35f90bc1d-2e3c-4798-a33c-611d7b358636
## 2 <NA> EO401027 http://n2t.net/ark:/65665/3f4d0cc10-f3b4-4323-8454-1371681e92b4
## 3 <NA> EO401045 http://n2t.net/ark:/65665/33446acdf-f0fd-44f0-8e6a-d789385acdcc
## 4 <NA> EO401024 http://n2t.net/ark:/65665/359f1f2d7-bd05-4964-9de8-034a14006aa6
## 5 <NA> EO401021 http://n2t.net/ark:/65665/3f50ca15b-8f27-45c7-9fc9-cfe993468eca
## 6 <NA> EO401038 http://n2t.net/ark:/65665/34351238c-8542-43cc-8bec-8d84b5489d3f
##                         edan_url                                         source
## 1 edanmdm:nmnheducation_11038234 Smithsonian Education and Outreach collections
## 2 edanmdm:nmnheducation_11038220 Smithsonian Education and Outreach collections
## 3 edanmdm:nmnheducation_11038238 Smithsonian Education and Outreach collections
## 4 edanmdm:nmnheducation_11038217 Smithsonian Education and Outreach collections
## 5 edanmdm:nmnheducation_11038213 Smithsonian Education and Outreach collections
## 6 edanmdm:nmnheducation_11038230 Smithsonian Education and Outreach collections
##   stage sim_score
## 1    NA 0.8055201
## 2    NA 0.8108422
## 3    NA 0.8135633
## 4    NA 0.8137358
## 5    NA 0.8144597
## 6    NA 0.8148395
# Count NA values in each column
na_count <- colSums(is.na(df))
# Print the results
cat("Number of NA values in each column:\n")
## Number of NA values in each column:
print(na_count)
##            name scientific_name          gender        taxonomy          region 
##               0               0              72               0             895 
##        locality            date         usnm_no            guid        edan_url 
##             727             858             289               0               0 
##          source           stage       sim_score 
##               0            1000               0

Analisis Data

Gender

df_gender <- df$gender
df_gender <- data.frame(table(na.omit(tolower(df_gender))))
df_gender <- df_gender[order(-df_gender$Freq), ]
print(df_gender)
##     Var1 Freq
## 1      1  496
## 7 pinned  393
## 6   male   18
## 5 female   10
## 8  slide    6
## 2      2    2
## 3      3    2
## 4      4    1
library(ggplot2)
ggplot(df_gender, aes(x = reorder(Var1, -Freq), y = Freq)) +
  geom_bar(stat = 'identity', fill = 'blue') +
  labs(x = 'Gender', y = 'Frekuwensi', title = 'Frekuwensi Gender pada Data')

## Source

df_source <- df$source
df_source <- data.frame(table(na.omit(tolower(df_source))))
print(df_source)
##                                             Var1 Freq
## 1                        nmnh - entomology dept.  905
## 2 smithsonian education and outreach collections   95

Taxonomy

df_taxonomy <- df[,4]
a=df_taxonomy[1]
b=df_taxonomy[29]
print(a)
## [1] "Animalia, Arthropoda, Hexapoda, Insecta, Lepidoptera, Nymphalidae"
print(b)
## [1] "Animalia, Arthropoda, Insecta, Lepidoptera, Papilionidae, Papilioninae"

Data akan dipotong mulai dari Lepidoptera.

substr(a,55,nchar(a))
## [1] "Nymphalidae"
substr(b,45,nchar(b))
## [1] "Papilionidae, Papilioninae"
my_list <- c()

extrak_text <- function(data){
  for(i in data){
    if(substr(i,1,39)=="Animalia, Arthropoda, Hexapoda, Insecta"){
      #print(substr(i,42,nchar(i)))
      my_list <- c(my_list, substr(i,55,nchar(i)))
    }
    else{
      #print(substr(i,33,nchar(i)))
      my_list <- c(my_list, substr(i,45,nchar(i)))
    }
  }
  return(data.frame(my_list))
}
df_taxonomy_p <- extrak_text(df_taxonomy)
df_taxonomy_p <- data.frame(lapply(df_taxonomy_p, function(x) gsub(",", "", x)))
a <- c()
b <- c()

for (i in seq_len(nrow(df_taxonomy_p))) {
  teks_baris <- df_taxonomy_p$my_list[i]
  word_count <- str_count(teks_baris, "\\w+")
  
  if (word_count == 1) {
    kata_terpisah <- strsplit(teks_baris, " ")[[1]]
    a <- c(a, kata_terpisah[1])
    b <- c(b, NA)
  } else if (word_count == 2) {
    kata_terpisah <- strsplit(teks_baris, " ")[[1]]
    a <- c(a, kata_terpisah[1])
    b <- c(b, kata_terpisah[2])
  }
}

df_taxonomy_clean = data.frame(order_Level=a,family_level=b)
head(df_taxonomy_clean)
##    order_Level family_level
## 1  Nymphalidae         <NA>
## 2  Nymphalidae Heliconiinae
## 3 Papilionidae Papilioninae
## 4  Nymphalidae Heliconiinae
## 5  Nymphalidae Heliconiinae
## 6  Nymphalidae  Nymphalinae

By Order Level

print(data.frame(table(df_taxonomy_clean$order_Level)))
##             Var1 Freq
## 1      Arctiidae   32
## 2    Geometridae   27
## 3       Glossata    1
## 4  Lasiocampidae    1
## 5     Lycaenidae    7
## 6      Noctuidae    1
## 7    Nymphalidae  128
## 8   Papilionidae  121
## 9       Pieridae  667
## 10     Pyralidae    2
## 11   Saturniidae    8
## 12    Sphingidae    1
## 13   Tortricidae    2
## 14     Uraniidae    1
# Membuat Corpus dari DataFrame
corpus <- Corpus(VectorSource(df_taxonomy_clean$order_Level))

# Praproses teks
corpus <- tm_map(corpus, content_transformer(tolower))
## Warning in tm_map.SimpleCorpus(corpus, content_transformer(tolower)):
## transformation drops documents
corpus <- tm_map(corpus, removePunctuation)
## Warning in tm_map.SimpleCorpus(corpus, removePunctuation): transformation drops
## documents
corpus <- tm_map(corpus, removeNumbers)
## Warning in tm_map.SimpleCorpus(corpus, removeNumbers): transformation drops
## documents
corpus <- tm_map(corpus, removeWords, stopwords("en"))
## Warning in tm_map.SimpleCorpus(corpus, removeWords, stopwords("en")):
## transformation drops documents
# Membuat word cloud
wordcloud(words = unlist(corpus), min.freq = 1, scale=c(5,1), colors=brewer.pal(8, "Dark2"))

By Family Name

print(data.frame(table(df_taxonomy_clean$family_level)))
##             Var1 Freq
## 1     Biblidinae    2
## 2     Charaxinae   75
## 3     Coliadinae  139
## 4       Danainae    1
## 5  Dismorphiinae   36
## 6      Ennominae   12
## 7  Epipaschiinae    1
## 8       Erebinae    1
## 9    Gelechiidae    1
## 10   Geometrinae    1
## 11  Heliconiinae   17
## 12   Heliconinae    1
## 13   Larentiinae   11
## 14 Limenitidinae    2
## 15    Lycaeninae    1
## 16     Morphinae    3
## 17   Nymphalinae   14
## 18 Oenochrominae    1
## 19  Papilioninae  117
## 20  Parnassiinae    2
## 21      Pierinae  486
## 22 Polyommatinae    2
## 23   Saturniinae    3
## 24     Satyrinae    1
## 25    Sterrhinae    2
# Membuat Corpus dari DataFrame
corpus <- Corpus(VectorSource(df_taxonomy_clean$family_level))

# Praproses teks
corpus <- tm_map(corpus, content_transformer(tolower))
## Warning in tm_map.SimpleCorpus(corpus, content_transformer(tolower)):
## transformation drops documents
corpus <- tm_map(corpus, removePunctuation)
## Warning in tm_map.SimpleCorpus(corpus, removePunctuation): transformation drops
## documents
corpus <- tm_map(corpus, removeNumbers)
## Warning in tm_map.SimpleCorpus(corpus, removeNumbers): transformation drops
## documents
corpus <- tm_map(corpus, removeWords, stopwords("en"))
## Warning in tm_map.SimpleCorpus(corpus, removeWords, stopwords("en")):
## transformation drops documents
# Membuat word cloud
wordcloud(words = unlist(corpus), min.freq = 1, scale=c(5,1), colors=brewer.pal(8, "Dark2"))

Name

# Membuat Corpus dari DataFrame
corpus <- Corpus(VectorSource(df$name))

# Praproses teks
corpus <- tm_map(corpus, content_transformer(tolower))
## Warning in tm_map.SimpleCorpus(corpus, content_transformer(tolower)):
## transformation drops documents
corpus <- tm_map(corpus, removePunctuation)
## Warning in tm_map.SimpleCorpus(corpus, removePunctuation): transformation drops
## documents
corpus <- tm_map(corpus, removeNumbers)
## Warning in tm_map.SimpleCorpus(corpus, removeNumbers): transformation drops
## documents
corpus <- tm_map(corpus, removeWords, stopwords("en"))
## Warning in tm_map.SimpleCorpus(corpus, removeWords, stopwords("en")):
## transformation drops documents
# Membuat word cloud
wordcloud(words = unlist(corpus), min.freq = 8, scale=c(5,1), colors=brewer.pal(8, "Dark2"))

Scientific Name

# Membuat Corpus dari DataFrame
corpus <- Corpus(VectorSource(df$scientific_name))

# Praproses teks
corpus <- tm_map(corpus, content_transformer(tolower))
## Warning in tm_map.SimpleCorpus(corpus, content_transformer(tolower)):
## transformation drops documents
corpus <- tm_map(corpus, removePunctuation)
## Warning in tm_map.SimpleCorpus(corpus, removePunctuation): transformation drops
## documents
corpus <- tm_map(corpus, removeNumbers)
## Warning in tm_map.SimpleCorpus(corpus, removeNumbers): transformation drops
## documents
corpus <- tm_map(corpus, removeWords, stopwords("en"))
## Warning in tm_map.SimpleCorpus(corpus, removeWords, stopwords("en")):
## transformation drops documents
# Membuat word cloud
wordcloud(words = unlist(corpus), min.freq = 8, scale=c(5,1), colors=brewer.pal(8, "Dark2"))

Image Clustering

Import Data

df_image = read.csv('data_path.csv')
head(df_image)
##                                                                 path
## 1 D:/S2-Matematika-ITS/Semester 2/Konsep Sains Data/EAS/Data/000.png
## 2 D:/S2-Matematika-ITS/Semester 2/Konsep Sains Data/EAS/Data/001.png
## 3 D:/S2-Matematika-ITS/Semester 2/Konsep Sains Data/EAS/Data/002.png
## 4 D:/S2-Matematika-ITS/Semester 2/Konsep Sains Data/EAS/Data/003.png
## 5 D:/S2-Matematika-ITS/Semester 2/Konsep Sains Data/EAS/Data/004.png
## 6 D:/S2-Matematika-ITS/Semester 2/Konsep Sains Data/EAS/Data/005.png

Process Data

# Fungsi untuk membaca gambar dan mengonversi ke vektor
read_and_convert_image <- function(file_path) {
  img <- readPNG(file_path, native = TRUE)
  img_vector <- as.vector(img)
  return(img_vector)
}

# Fungsi untuk membaca dan mengonversi beberapa gambar dalam sebuah folder
read_and_convert_images_in_folder <- function(folder_path) {
  image_files <- list.files(folder_path, pattern = "\\.(png|PNG)$", full.names = TRUE)
  #print(image_files)
  img_list <- lapply(image_files, read_and_convert_image)
  img_matrix <- do.call(rbind, img_list)
  return(list(matrix = img_matrix, files = image_files))
}

# Fungsi untuk melakukan reduksi dimensi menggunakan PCA
perform_pca <- function(data_matrix, num_dimensions) {
  # Remove constant columns
  non_constant_cols <- apply(data_matrix, 2, function(col) length(unique(col)) > 1)
  data_matrix_no_const <- data_matrix[, non_constant_cols]

  # Perform PCA on the modified data
  pca_result <- prcomp(data_matrix_no_const, center = TRUE, scale. = TRUE)

  # Return the specified number of dimensions
  return(pca_result$x[, 1:num_dimensions])
}
# Folder yang berisi gambar-gambar
folder_path <- "D:/S2-Matematika-ITS/Semester 2/Konsep Sains Data/EAS/Data"

# Baca dan konversi gambar-gambar di dalam folder
image_data <- read_and_convert_images_in_folder(folder_path)
## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile

## Warning in readPNG(file_path, native = TRUE): libpng warning: iCCP: known
## incorrect sRGB profile
# Reduksi dimensi menggunakan PCA
num_dimensions <- 2
pca_result <- perform_pca(image_data$matrix, num_dimensions)
# Menentukan jumlah cluster optimal menggunakan metode Elbow
fviz_nbclust(pca_result, kmeans, method = "wss") +
  labs(title = "Metode Elbow untuk Menentukan Jumlah Cluster Optimal")

Iterasi

k_optimal <- 3 
km <- kmeans.ani(pca_result, centers = k_optimal)

kmeans_result <- kmeans(pca_result, centers = k_optimal, nstart = 25)
fviz_cluster(kmeans_result, data = pca_result)

# 1. Inertia (within-cluster sum of squares)
inertia <- kmeans_result$tot.withinss
cat("Inertia:", inertia, "\n")
## Inertia: 6228132
# 2. Silhouette Score
silhouette <- silhouette(kmeans_result$cluster, dist(pca_result))
avg_silhouette <- mean(silhouette[, "sil_width"])
cat("Silhouette Score:", avg_silhouette, "\n")
## Silhouette Score: 0.4516955
kmeans_result$cluster
##    [1] 2 2 2 2 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 2 3 3 3 2 2 3 2 2 3 3 3 3 2
##   [38] 2 3 2 2 3 2 3 2 2 2 2 3 1 2 3 3 3 2 2 3 2 2 2 2 2 3 2 3 2 3 2 2 2 2 2 2 3
##   [75] 2 2 2 1 2 2 3 2 2 1 1 3 3 2 2 2 2 2 2 3 2 2 2 3 2 3 2 2 3 3 3 2 2 2 3 2 2
##  [112] 3 2 2 2 2 3 2 3 2 3 2 3 2 3 3 2 3 2 2 3 3 2 3 2 3 2 2 2 3 3 3 2 2 1 2 2 3
##  [149] 2 2 1 2 2 2 3 2 3 2 2 2 2 2 2 3 2 2 3 3 3 3 2 3 3 2 2 2 2 2 3 3 2 2 2 2 2
##  [186] 2 3 2 3 2 3 2 3 3 2 2 3 3 2 2 3 1 2 2 3 3 2 3 3 2 2 2 2 2 3 3 2 2 3 2 2 3
##  [223] 3 3 2 3 3 3 2 2 3 2 3 3 2 3 2 2 3 3 3 3 2 2 2 3 2 2 2 3 2 2 1 2 2 3 2 2 2
##  [260] 2 2 2 2 2 2 2 3 2 3 3 3 3 3 2 3 3 2 2 2 3 3 2 3 3 3 2 1 2 3 2 2 2 3 2 2 3
##  [297] 2 2 3 3 3 2 3 3 3 2 2 3 2 2 3 2 3 2 3 2 3 2 2 3 2 3 2 2 3 3 2 2 2 3 1 3 3
##  [334] 3 3 3 3 3 2 2 3 3 2 2 2 3 3 2 2 3 2 2 3 3 3 2 3 3 3 2 3 2 3 1 3 3 3 2 2 3
##  [371] 2 3 3 3 3 2 3 3 2 3 3 2 2 2 2 2 3 3 3 3 2 3 3 3 3 3 1 2 3 3 3 3 2 3 2 2 2
##  [408] 3 2 2 3 2 3 2 2 3 3 2 3 3 2 2 2 2 3 3 2 3 2 2 3 2 3 3 3 2 3 2 3 2 3 2 2 3
##  [445] 2 2 2 3 2 3 3 2 3 2 3 3 1 3 3 3 3 3 3 3 3 2 3 3 3 3 3 3 2 2 3 3 3 3 3 3 2
##  [482] 3 3 2 3 2 2 3 2 1 3 2 3 3 2 2 3 3 3 3 3 2 3 3 3 3 3 3 1 3 2 2 3 1 1 3 3 3
##  [519] 3 2 2 2 3 1 3 2 1 2 2 2 3 3 3 2 3 2 3 3 2 2 3 2 2 2 3 2 3 2 2 3 3 3 3 3 3
##  [556] 2 3 3 3 3 3 2 3 3 3 2 3 2 3 3 2 2 3 1 3 2 3 2 3 3 3 2 2 3 2 2 2 3 2 3 2 3
##  [593] 3 3 3 3 2 2 3 3 3 3 3 3 3 2 3 2 2 3 2 3 2 3 3 3 3 3 2 3 3 2 3 3 2 3 2 3 2
##  [630] 3 2 3 3 3 3 2 1 3 3 3 2 2 3 3 3 3 3 2 2 3 2 2 3 3 3 2 3 3 3 2 1 2 3 3 3 3
##  [667] 2 3 3 3 2 3 3 2 3 2 3 3 3 2 3 2 3 3 2 3 3 3 2 2 2 2 3 2 2 3 3 3 3 3 2 3 3
##  [704] 2 3 2 2 2 2 3 2 2 3 3 2 2 3 2 3 3 3 3 3 3 3 1 3 3 3 3 2 2 2 3 3 3 2 2 3 2
##  [741] 3 3 2 3 2 2 2 2 2 3 3 2 1 2 3 3 2 3 2 3 2 2 1 3 2 3 3 2 2 3 3 3 3 3 2 2 3
##  [778] 2 3 2 3 2 3 2 2 2 2 3 3 3 2 1 3 3 3 2 2 3 3 2 3 3 2 3 3 2 2 3 3 3 3 2 3 3
##  [815] 3 3 3 3 3 3 3 3 3 2 3 2 2 3 3 3 3 3 1 2 3 2 1 3 2 3 3 2 3 3 3 3 1 3 3 3 3
##  [852] 2 1 2 2 3 3 2 2 2 3 3 3 3 1 2 3 2 3 2 2 1 2 3 1 2 1 3 2 2 3 2 2 3 1 1 1 3
##  [889] 3 2 3 3 3 3 3 2 3 3 3 1 1 3 3 3 3 2 3 3 2 3 1 2 3 3 3 3 2 3 3 3 3 2 3 2 2
##  [926] 3 2 2 3 1 3 2 1 3 2 3 3 2 3 1 3 3 3 1 3 3 3 3 3 3 2 1 2 3 2 2 2 1 3 3 2 2
##  [963] 1 2 3 2 3 3 3 3 3 3 3 3 2 2 2 3 3 3 3 1 3 3 3 3 3 3 2 3 3 3 3 2 3 3 3 3 3
## [1000] 2
df_image$cluster <- kmeans_result$cluster
head(df_image)
##                                                                 path cluster
## 1 D:/S2-Matematika-ITS/Semester 2/Konsep Sains Data/EAS/Data/000.png       2
## 2 D:/S2-Matematika-ITS/Semester 2/Konsep Sains Data/EAS/Data/001.png       2
## 3 D:/S2-Matematika-ITS/Semester 2/Konsep Sains Data/EAS/Data/002.png       2
## 4 D:/S2-Matematika-ITS/Semester 2/Konsep Sains Data/EAS/Data/003.png       2
## 5 D:/S2-Matematika-ITS/Semester 2/Konsep Sains Data/EAS/Data/004.png       2
## 6 D:/S2-Matematika-ITS/Semester 2/Konsep Sains Data/EAS/Data/005.png       2

Vizualisasi

Cluster 1

data_cluster1 <- df_image[df_image$cluster==1, ]
head(data_cluster1)
##                                                                   path cluster
## 50  D:/S2-Matematika-ITS/Semester 2/Konsep Sains Data/EAS/Data/049.png       1
## 78  D:/S2-Matematika-ITS/Semester 2/Konsep Sains Data/EAS/Data/077.png       1
## 84  D:/S2-Matematika-ITS/Semester 2/Konsep Sains Data/EAS/Data/083.png       1
## 85  D:/S2-Matematika-ITS/Semester 2/Konsep Sains Data/EAS/Data/084.png       1
## 145 D:/S2-Matematika-ITS/Semester 2/Konsep Sains Data/EAS/Data/144.png       1
## 151 D:/S2-Matematika-ITS/Semester 2/Konsep Sains Data/EAS/Data/150.png       1
# Path to six images in PNG format
image_paths <- data_cluster1$path

# Ukuran gambar
img_width <- 0.5  # Ganti dengan lebar yang diinginkan
img_height <- 0.5  # Ganti dengan tinggi yang diinginkan
# Ukuran plot
plot_width <- 0.7  # Ganti dengan lebar plot yang diinginkan
plot_height <- 0.7  # Ganti dengan tinggi plot yang diinginkan

# Membuat plot dengan grid
par(mfrow = c(2, 3), mar = c(2, 2, 2, 2))
for (i in 1:6) {
  img <- readPNG(image_paths[i])
  plot(0, 0, type = "n", axes = FALSE, ann = FALSE, 
       xlim = c(-plot_width/2, plot_width/2), ylim = c(-plot_height/2, plot_height/2))
  rasterImage(img, -img_width/2, -img_height/2, img_width/2, img_height/2)
  title(paste("Gambar", i))
}

# Mengatur layout kembali ke satu plot
par(mfrow = c(1, 1))

Cluster 2

data_cluster2 <- df_image[df_image$cluster==2, ]
head(data_cluster2)
##                                                                 path cluster
## 1 D:/S2-Matematika-ITS/Semester 2/Konsep Sains Data/EAS/Data/000.png       2
## 2 D:/S2-Matematika-ITS/Semester 2/Konsep Sains Data/EAS/Data/001.png       2
## 3 D:/S2-Matematika-ITS/Semester 2/Konsep Sains Data/EAS/Data/002.png       2
## 4 D:/S2-Matematika-ITS/Semester 2/Konsep Sains Data/EAS/Data/003.png       2
## 5 D:/S2-Matematika-ITS/Semester 2/Konsep Sains Data/EAS/Data/004.png       2
## 6 D:/S2-Matematika-ITS/Semester 2/Konsep Sains Data/EAS/Data/005.png       2
# Path to six images in PNG format
image_paths <- data_cluster2$path

# Ukuran gambar
img_width <- 0.5  # Ganti dengan lebar yang diinginkan
img_height <- 0.5  # Ganti dengan tinggi yang diinginkan
# Ukuran plot
plot_width <- 0.7  # Ganti dengan lebar plot yang diinginkan
plot_height <- 0.7  # Ganti dengan tinggi plot yang diinginkan

# Membuat plot dengan grid
par(mfrow = c(2, 3), mar = c(2, 2, 2, 2))
for (i in 1:6) {
  img <- readPNG(image_paths[i])
  plot(0, 0, type = "n", axes = FALSE, ann = FALSE, 
       xlim = c(-plot_width/2, plot_width/2), ylim = c(-plot_height/2, plot_height/2))
  rasterImage(img, -img_width/2, -img_height/2, img_width/2, img_height/2)
  title(paste("Gambar", i))
}

# Mengatur layout kembali ke satu plot
par(mfrow = c(1, 1))

Cluster 3

data_cluster3 <- df_image[df_image$cluster==3, ]
head(data_cluster3)
##                                                                  path cluster
## 7  D:/S2-Matematika-ITS/Semester 2/Konsep Sains Data/EAS/Data/006.png       3
## 21 D:/S2-Matematika-ITS/Semester 2/Konsep Sains Data/EAS/Data/020.png       3
## 22 D:/S2-Matematika-ITS/Semester 2/Konsep Sains Data/EAS/Data/021.png       3
## 23 D:/S2-Matematika-ITS/Semester 2/Konsep Sains Data/EAS/Data/022.png       3
## 25 D:/S2-Matematika-ITS/Semester 2/Konsep Sains Data/EAS/Data/024.png       3
## 26 D:/S2-Matematika-ITS/Semester 2/Konsep Sains Data/EAS/Data/025.png       3
# Path to six images in PNG format
image_paths <- data_cluster3$path

# Ukuran gambar
img_width <- 0.5  # Ganti dengan lebar yang diinginkan
img_height <- 0.5  # Ganti dengan tinggi yang diinginkan
# Ukuran plot
plot_width <- 0.7  # Ganti dengan lebar plot yang diinginkan
plot_height <- 0.7  # Ganti dengan tinggi plot yang diinginkan

# Membuat plot dengan grid
par(mfrow = c(2, 3), mar = c(2, 2, 2, 2))
for (i in 1:6) {
  img <- readPNG(image_paths[i])
  plot(0, 0, type = "n", axes = FALSE, ann = FALSE, 
       xlim = c(-plot_width/2, plot_width/2), ylim = c(-plot_height/2, plot_height/2))
  rasterImage(img, -img_width/2, -img_height/2, img_width/2, img_height/2)
  title(paste("Gambar", i))
}

# Mengatur layout kembali ke satu plot
par(mfrow = c(1, 1))