R Markdown

library(readr)
seeds_dataset_3_ <- read_delim("C:/Users/lazarus/Downloads/seeds_dataset (3).txt", 
                               delim = "\t", escape_double = FALSE, 
                               trim_ws = TRUE)
## Warning: One or more parsing issues, call `problems()` on your data frame for details,
## e.g.:
##   dat <- vroom(...)
##   problems(dat)
## Rows: 209 Columns: 8
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: "\t"
## chr (1): 1
## dbl (7): 15.26, 14.84, 0.871, 5.763, 3.312, 2.221, 5.22
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
View(seeds_dataset_3_)

sd <- seeds_dataset_3_
str(sd)
## spc_tbl_ [209 × 8] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ 15.26: num [1:209] 14.9 14.3 13.8 16.1 14.4 ...
##  $ 14.84: num [1:209] 14.6 14.1 13.9 15 14.2 ...
##  $ 0.871: num [1:209] 0.881 0.905 0.895 0.903 0.895 ...
##  $ 5.763: num [1:209] 5.55 5.29 5.32 5.66 5.39 ...
##  $ 3.312: num [1:209] 3.33 3.34 3.38 3.56 3.31 ...
##  $ 2.221: num [1:209] 1.02 2.7 2.26 1.35 2.46 ...
##  $ 5.22 : num [1:209] 4.96 4.83 4.8 5.17 4.96 ...
##  $ 1    : chr [1:209] "1" "1" "1" "1" ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   `15.26` = col_double(),
##   ..   `14.84` = col_double(),
##   ..   `0.871` = col_double(),
##   ..   `5.763` = col_double(),
##   ..   `3.312` = col_double(),
##   ..   `2.221` = col_double(),
##   ..   `5.22` = col_double(),
##   ..   `1` = col_character()
##   .. )
##  - attr(*, "problems")=<externalptr>
feature_name<- c('area', 'perimeter','compactness','length_of_kernel','width_of_kernel','asymetry_coefficient','length_of_kernel_groove', 'type_of_seed')
colnames(sd)<-feature_name
View(sd)
summary(sd)
##       area         perimeter      compactness     length_of_kernel
##  Min.   :10.59   Min.   :12.41   Min.   :0.8081   Min.   :0.8189  
##  1st Qu.:12.26   1st Qu.:13.45   1st Qu.:0.8576   1st Qu.:5.2430  
##  Median :14.34   Median :14.29   Median :0.8740   Median :5.5160  
##  Mean   :14.85   Mean   :14.56   Mean   :0.8713   Mean   :5.5630  
##  3rd Qu.:17.32   3rd Qu.:15.73   3rd Qu.:0.8878   3rd Qu.:5.9800  
##  Max.   :21.18   Max.   :17.25   Max.   :0.9183   Max.   :6.6750  
##                                  NA's   :3                        
##  width_of_kernel asymetry_coefficient length_of_kernel_groove
##  Min.   :2.630   Min.   :0.7651       Min.   :3.485          
##  1st Qu.:2.955   1st Qu.:2.6400       1st Qu.:5.045          
##  Median :3.244   Median :3.6000       Median :5.228          
##  Mean   :3.281   Mean   :3.7006       Mean   :5.408          
##  3rd Qu.:3.568   3rd Qu.:4.7730       3rd Qu.:5.879          
##  Max.   :5.325   Max.   :8.4560       Max.   :6.735          
##  NA's   :1                            NA's   :4              
##  type_of_seed      
##  Length:209        
##  Class :character  
##  Mode  :character  
##                    
##                    
##                    
## 
any(is.na(sd))
## [1] TRUE
sd<-na.omit(sd)
str(sd)
## tibble [202 × 8] (S3: tbl_df/tbl/data.frame)
##  $ area                   : num [1:202] 14.9 14.3 13.8 16.1 14.4 ...
##  $ perimeter              : num [1:202] 14.6 14.1 13.9 15 14.2 ...
##  $ compactness            : num [1:202] 0.881 0.905 0.895 0.903 0.895 ...
##  $ length_of_kernel       : num [1:202] 5.55 5.29 5.32 5.66 5.39 ...
##  $ width_of_kernel        : num [1:202] 3.33 3.34 3.38 3.56 3.31 ...
##  $ asymetry_coefficient   : num [1:202] 1.02 2.7 2.26 1.35 2.46 ...
##  $ length_of_kernel_groove: num [1:202] 4.96 4.83 4.8 5.17 4.96 ...
##  $ type_of_seed           : chr [1:202] "1" "1" "1" "1" ...
##  - attr(*, "na.action")= 'omit' Named int [1:7] 7 35 60 135 169 170 201
##   ..- attr(*, "names")= chr [1:7] "7" "35" "60" "135" ...
sd<-sd[,-8]
View(sd)
df_sc<-as.data.frame(scale(sd))
str(df_sc)
## 'data.frame':    202 obs. of  7 variables:
##  $ area                   : num  -0.00846 -0.21003 -0.36377 0.42202 -0.17928 ...
##  $ perimeter              : num  -0.0133 -0.3789 -0.4931 0.3066 -0.2875 ...
##  $ compactness            : num  0.435 1.456 1.05 1.388 1.033 ...
##  $ length_of_kernel       : num  -0.1927 -0.7843 -0.7101 0.0412 -0.5706 ...
##  $ width_of_kernel        : num  0.182 0.192 0.303 0.785 0.126 ...
##  $ asymetry_coefficient   : num  -1.794 -0.677 -0.97 -1.57 -0.835 ...
##  $ length_of_kernel_groove: num  -0.941 -1.207 -1.247 -0.498 -0.941 ...
dist_mat<-dist(df_sc, method='euclidean')
hclust_avg<-hclust(dist_mat, method='average')
plot(hclust_avg)

library(dendextend)
## Warning: package 'dendextend' was built under R version 4.3.3
## 
## ---------------------
## Welcome to dendextend version 1.17.1
## Type citation('dendextend') for how to cite the package.
## 
## Type browseVignettes(package = 'dendextend') for the package vignette.
## The github page is: https://github.com/talgalili/dendextend/
## 
## Suggestions and bug-reports can be submitted at: https://github.com/talgalili/dendextend/issues
## You may ask questions at stackoverflow, use the r and dendextend tags: 
##   https://stackoverflow.com/questions/tagged/dendextend
## 
##  To suppress this message use:  suppressPackageStartupMessages(library(dendextend))
## ---------------------
## 
## Attaching package: 'dendextend'
## The following object is masked from 'package:stats':
## 
##     cutree

avg_dend_obj <- as.dendrogram(hclust_avg)
avg_col_dend <- color_branches(avg_dend_obj, h = 3)
plot(avg_col_dend)

hclust_avg
## 
## Call:
## hclust(d = dist_mat, method = "average")
## 
## Cluster method   : average 
## Distance         : euclidean 
## Number of objects: 202
cut_avg<-cutree(hclust_avg, k=3)

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
seeds_df_cl <- mutate(df_sc, cluster = cut_avg)
seeds_df_cl
##             area    perimeter compactness length_of_kernel width_of_kernel
## 1   -0.008456597 -0.013309465  0.43474889      -0.19272564      0.18170996
## 2   -0.210028049 -0.378886149  1.45640030      -0.78428689      0.19225446
## 3   -0.363768986 -0.493128863  1.05030455      -0.71006057      0.30297173
## 4    0.422018028  0.306570134  1.38800523       0.04119973      0.78538270
## 5   -0.179279861 -0.287491978  1.03320578      -0.57060507      0.12635132
## 6   -0.073369438 -0.074238912  0.38345258      -0.17248210     -0.01336333
## 7    0.589424827  0.664530637  0.16116859       0.92966625      0.52967852
## 8    0.524511987  0.504590838  0.72970264       0.54953753      0.63512354
## 9    0.121369083  0.199943601 -0.05684071       0.16715954     -0.05817746
## 10  -0.298856146 -0.325572882  0.37062850      -0.45364239     -0.16625861
## 11  -0.346686660 -0.432199415  0.72970264      -0.45139311     -0.17153086
## 12  -0.384267778 -0.401734692  0.21246490      -0.36142181     -0.28488426
## 13  -0.397933639 -0.409350873  0.14834451      -0.35467397     -0.39560153
## 14  -0.107534090 -0.234178711  1.21274285      -0.64932995      0.18170996
## 15  -0.312522007 -0.576906853  2.02493436      -1.17116345      0.31351623
## 16   0.268277091  0.123781792  1.49059784      -0.25345626      0.65884867
## 17  -0.069952972 -0.287491978  1.89669359      -0.97772517      0.53231465
## 18  -0.746413098 -0.774927557 -0.09958763      -0.93049024     -0.56694968
## 19  -0.254442097 -0.142784540 -0.53560623       0.04119973     -0.35605964
## 20  -0.271524424 -0.249411073  0.05430129      -0.26920124     -0.25325075
## 21   0.333189931  0.238024506  1.19136939      -0.04877157      0.64039579
## 22  -0.965066876 -1.033877708 -0.19363086      -1.21614910     -0.86483186
## 23   0.035957451  0.131397973 -0.22355370       0.33585571     -0.05026909
## 24   0.439100355  0.436045210  0.59718719       0.43482413      0.41368900
## 25  -0.643919139 -0.630220119 -0.29194878      -0.55036153     -0.62758057
## 26  -0.739580167 -0.698765747 -0.62110007      -0.55036153     -0.81210936
## 27  -0.271524424 -0.310340521  0.47322112      -0.22196631     -0.11353610
## 28  -0.497011132 -0.432199415 -0.45011238      -0.27819837     -0.52477168
## 29  -0.596088626 -0.584523034 -0.20218024      -0.41765387     -0.76202297
## 30   0.199947785  0.268489229  0.06285067       0.26387868      0.28188273
## 31  -0.278357354 -0.135168359 -0.77071429       0.17390738     -0.20580049
## 32  -0.329604334 -0.317956701  0.07994944      -0.12299788     -0.30070101
## 33   0.049623313  0.070468525  0.29795874       0.16266097      0.16852933
## 34   0.442516820  0.519823200  0.10559759       0.41907916      0.52704240
## 35   0.743165765  0.603601190  1.58036638       0.43257485      1.10435388
## 36  -0.035788319 -0.051390369  0.48604519       0.03670116      0.06308431
## 37  -0.213444514 -0.317956701  1.00328294      -0.54586296      0.08944557
## 38  -0.466262945 -0.561674491  0.69123041      -0.65607780     -0.28488426
## 39  -0.479928806 -0.561674491  0.61001126      -0.64932995     -0.27961200
## 40  -0.596088626 -0.790159919  1.28113793      -1.12842709     -0.16625861
## 41   0.203364250  0.207559782  0.47322112       0.53379256      0.34778586
## 42   0.070122104 -0.036158007  1.18282001      -0.13649358      0.52177015
## 43  -0.377434848 -0.416967053  0.36207912      -0.59309789     -0.28752038
## 44   0.155533736  0.131397973  0.64848349       0.13791887      0.33987749
## 45   0.029124521 -0.020925646  0.74252672      -0.15673712      0.29769948
## 46  -0.039204785 -0.051390369  0.46894642      -0.21296918      0.07099269
## 47  -0.015289528  0.062852345 -0.14233455       0.08618537     -0.01599945
## 48  -0.162197535 -0.142784540  0.17826736      -0.12299788      0.02090630
## 49   0.299025278  0.245640687  0.91351440       0.07718824      0.44795863
## 50  -0.141698743  0.017155259 -0.73224206       0.16940882     -0.39823765
## 51  -0.196362188 -0.234178711  0.52024273      -0.30518975     -0.17153086
## 52  -0.131449347  0.009539078 -0.65102291       0.22789016     -0.39823765
## 53   0.042790382  0.139014154 -0.21927901       0.14016815     -0.13726123
## 54  -0.151948139 -0.180865445  0.46467173      -0.56610650      0.29769948
## 55   0.005209264 -0.119935998  1.26831385      -0.57510363      0.38996387
## 56   0.162366667  0.139014154  0.63138472       0.05019686      0.40841675
## 57  -0.954817480 -0.851089366 -1.35634711      -1.08119216     -0.61176382
## 58  -1.255466425 -1.490848563  0.55871496      -1.65925772     -1.01509102
## 59  -0.869405848 -1.064342432  0.91351440      -1.26788259     -0.58540256
## 60  -0.575589834 -0.569290672 -0.12523578      -0.55036153     -0.51159105
## 61  -0.725914306 -0.774927557  0.02865313      -0.84951608     -0.62758057
## 62  -0.691749653 -0.828240823  0.72542795      -1.12617781     -0.38242090
## 63  -0.192945722 -0.165633083  0.07140006      -0.02178018     -0.19525599
## 64  -0.305689077 -0.226562530 -0.36034385      -0.06901511     -0.27961200
## 65  -0.182696327 -0.150400721  0.07140006      -0.15898640     -0.29279263
## 66  -0.742996633 -0.637836300 -1.07421743      -0.51212373     -1.00718264
## 67   0.931071355  1.060572045 -0.15515863       1.24006721      0.78274657
## 68   0.661170598  0.824470437 -0.36889323       0.80595572      0.57976491
## 69   0.804662140  0.870167522  0.22956366       0.76097007      0.86973871
## 70   1.436708217  1.273825111  1.58891576       1.15684377      1.75547688
## 71   0.654337667  0.702611542  0.32788158       0.84869208      0.58503716
## 72   0.637255341  0.786389532 -0.30477285       0.64625667      0.45850313
## 73   0.825160931  1.007258779 -0.47148584       0.95440835      0.36623874
## 74   1.983342662  2.012594660  0.22956366       2.11278876      1.44968632
## 75   1.378628307  1.448997272  0.17399267       1.81138493      0.98836436
## 76   0.756831626  0.733076265  0.78099895       0.47306193      0.79592720
## 77   0.555260174  0.573136466  0.48604519       0.52929399      0.53495077
## 78   1.303466071  1.220511844  1.14434778       0.82394998      1.56303972
## 79   1.809102932  1.753644509  0.78954833       1.45149975      1.58149260
## 80   1.593865620  1.639401795  0.29795874       1.67417870      1.33896905
## 81   1.573366828  1.616553252  0.30223343       1.63369162      1.41541669
## 82   1.149725133  1.144350035  0.68695572       1.19958013      1.01999787
## 83   1.358129515  1.273825111  1.11015024       0.99939400      1.31788005
## 84   1.392294168  1.578472348 -0.50995807       2.04531029      1.07008425
## 85   2.143916530  1.997362298  1.19564408       2.09929306      2.02699781
## 86   2.041422571  1.875503404  1.37518115       1.82263134      2.02436168
## 87   1.774938279  1.829806318  0.15689390       2.11728732      1.37323868
## 88   1.317131932  1.228128025  1.17427062       1.19733085      1.40223606
## 89   1.334214258  1.296673654  0.84084464       1.42225908      1.13071514
## 90   1.259052022  1.113885311  1.52479538       0.89367773      1.57094809
## 91   1.180473321  1.471845815 -1.09986559       2.30847632      0.58240103
## 92   0.671419994  0.809238075 -0.26202593       1.12310453      0.52440627
## 93   1.505037522  1.525159081  0.45184765       1.57745956      1.43914182
## 94   1.392294168  1.509926719 -0.09531294       1.82038205      0.75902144
## 95   1.115560480  1.273825111 -0.30904754       1.42000980      0.65357642
## 96   1.303466071  1.334754558  0.43047419       1.30304712      1.10699001
## 97   0.514262591  0.504590838  0.66985695       0.17615667      0.68784605
## 98   1.054064105  0.969177874  1.20846816       0.56303323      1.13335126
## 99   1.556284501  1.456613453  1.17854532       1.06462319      1.65530411
## 100  1.460623474  1.555623805  0.03292783       1.64043947      1.09908163
## 101  1.382044772  1.395684006  0.51169334       1.36827630      1.29415492
## 102  1.341047189  1.296673654  0.88786625       0.89367773      1.37587481
## 103  1.347880119  1.205279483  1.48204846       1.15234520      1.42859732
## 104  0.931071355  0.969177874  0.38772727       0.88468060      0.81438008
## 105  1.720274835  1.776493052  0.18254205       2.32871986      1.31524392
## 106  1.245386161  1.243360387  0.66558226       1.15459448      1.08062875
## 107  1.211221508  1.167198578  0.90496502       1.05112750      1.33106067
## 108  1.528952779  1.624169433  0.02865313       1.49198683      1.38905544
## 109  1.443541147  1.311906015  1.39227992       1.22207295      1.68166537
## 110  1.446957612  1.540391443  0.05430129       1.39301841      1.24670466
## 111  2.072170759  2.027827022  0.63993411       2.07680024      1.91628053
## 112  1.419625890  1.418532548  0.61856065       1.74615574      1.19925440
## 113  1.385461237  1.228128025  1.57181699       0.92516768      1.66848474
## 114  1.450374078  1.418532548  0.77244956       1.36152846      1.45232245
## 115  1.361545980  1.250976568  1.27686323       1.32104138      1.33106067
## 116  1.751023022  1.761260690  0.43474889       1.91935048      1.56303972
## 117  1.822768793  1.768876871  0.80237241       1.51897822      1.83983290
## 118  1.105311084  1.167198578  0.26803589       0.94316194      0.78801882
## 119  0.432267424  0.603601190 -0.51850746       0.27512509      0.32406073
## 120  1.204388578  1.052955864  1.57181699       0.76546864      1.33633293
## 121  0.370771049  0.230408325  1.51624599      -0.62233856      0.83810521
## 122  1.313715467  1.212895663  1.23839101       1.06012462      1.59467322
## 123  1.279550814  1.388067825 -0.04829132       1.45149975      0.86973871
## 124  1.050647640  0.961561693  1.21274285       0.76321936      1.11489838
## 125  1.795437071  1.860271042  0.10987228       1.96433612      1.34160518
## 126  0.903739633  0.816854256  1.20419347       0.34035428      1.12280676
## 127  1.159974529  0.992026417  1.70433245       0.76321936      1.29415492
## 128  1.378628307  1.319522196  0.99473355       1.13435094      1.47868370
## 129  0.162366667  0.238024506 -0.01409379       0.54953753      0.01036180
## 130  0.428850959  0.565520285 -0.27912470       0.46181552      0.34514974
## 131  0.223863042  0.230408325  0.48604519       0.30661504      0.37941937
## 132  0.838826792  0.893016065  0.32360689       1.13660022      0.81701620
## 133  0.227279507  0.428429029 -0.77926368       0.63051170     -0.08717484
## 134  0.237528903  0.397964305 -0.55270499       0.43257485      0.05781206
## 135  0.452766216  0.451277571  0.60146188       0.52254615      0.54813140
## 136 -0.626836813 -0.508361224 -0.98017421      -0.37716679     -0.71193659
## 137 -0.541425181 -0.493128863 -0.41164015      -0.22196631     -0.50368267
## 138 -0.534592251 -0.485512682 -0.38171731      -0.56385722     -0.50104655
## 139 -0.917236362 -0.965332080 -0.24492716      -0.93498881     -0.78311197
## 140 -1.053894973 -0.904402632 -1.86076079      -0.73255340     -1.28397582
## 141 -1.262299355 -1.110039517 -2.31815284      -0.81127828     -1.52122711
## 142 -1.187137119 -1.110039517 -1.60000457      -1.04295436     -1.43687110
## 143 -0.824991799 -0.858705547 -0.21927901      -0.83826967     -0.78311197
## 144 -0.753246028 -0.668301024 -0.93315259      -0.57060507     -0.93073500
## 145 -1.405790897 -1.262363136 -2.57463437      -0.72580555     -1.62403601
## 146 -1.050478508 -1.033877708 -0.91177913      -0.84726680     -1.11789991
## 147 -0.988982133 -0.813008461 -1.96762809      -0.52786871     -1.28661194
## 148 -0.903570501 -0.752079014 -1.60855395      -0.52112086     -1.13635279
## 149 -1.272548751 -1.178585145 -1.89495832      -0.94398594     -1.50541036
## 150 -1.211052376 -1.170968965 -1.39909404      -1.04520364     -1.34197058
## 151 -1.269132286 -1.170968965 -1.95052932      -0.87650747     -1.55286062
## 152 -1.217885307 -1.308060221 -0.48430992      -1.31961609     -1.09417478
## 153 -0.947984550 -0.653068662 -2.68577637      -0.55261081     -1.36833184
## 154 -1.077810230 -0.813008461 -2.68150167      -0.44014669     -1.54495224
## 155 -1.166638327 -1.041493889 -1.90778240      -0.75504622     -1.50013811
## 156 -0.807909473 -0.698765747 -1.21528227      -0.42440172     -1.01509102
## 157 -0.985565668 -0.957715899 -0.88185629      -0.65157923     -1.19698368
## 158 -0.975316272 -0.896786451 -1.25375450      -0.83826967     -1.09944703
## 159 -0.804493008 -0.774927557 -0.64674822      -0.68981703     -0.78047585
## 160 -1.286214612 -1.368989668 -0.64674822      -1.41408595     -1.23916169
## 161 -0.958233945 -1.094807155  0.35780443      -1.20265341     -0.85165124
## 162 -0.842074126 -0.759695195 -1.05711867      -0.72130699     -0.96764076
## 163 -0.941151619 -0.866321728 -1.13833782      -0.50087732     -1.12580829
## 164 -1.214468841 -1.117655698 -1.78809102      -1.04295436     -1.57131350
## 165 -1.146139536 -1.132888060 -1.08704151      -1.06319790     -1.10471929
## 166 -1.241800564 -1.231898412 -1.24093043      -1.24089121     -1.32088158
## 167 -1.197386515 -1.148120422 -1.42901688      -1.13292565     -1.32088158
## 168 -1.392125036 -1.239514593 -2.60883191      -0.81352756     -1.64248889
## 169 -1.402374432 -1.536545649 -0.50995807      -1.48156442     -1.16798630
## 170 -1.245217029 -1.201433688 -1.51451072      -1.02046154     -1.46059623
## 171 -1.422873224 -1.414686754 -1.62565272      -1.11268211     -1.63985276
## 172 -1.170054793 -1.170968965 -1.01009705      -1.03395723     -1.33406220
## 173 -0.920652827 -0.851089366 -1.09559089      -0.63583426     -0.97818526
## 174 -1.193970050 -1.247130774 -0.63819884      -1.23639264     -1.28924807
## 175 -0.835241195 -0.896786451 -0.01409379      -0.90799742     -0.65130570
## 176 -0.927485758 -0.934867356 -0.55697969      -0.89900029     -0.93600725
## 177 -1.111974883 -1.155736603 -0.57407846      -1.19590556     -1.09153866
## 178 -0.688333188 -0.622603938 -0.71941799      -0.32543329     -0.62758057
## 179 -1.142723071 -0.972948261 -2.18563739      -0.62233856     -1.53177162
## 180 -1.057311439 -0.866321728 -2.18563739      -0.50987445     -1.44477947
## 181 -1.364793314 -1.361373488 -1.44184096      -1.24089121     -1.55286062
## 182 -1.255466425 -1.346141126 -0.49285930      -1.23864192     -1.16798630
## 183 -1.474120203 -1.658404543 -0.26202593      -1.66600557     -1.25761456
## 184 -1.357960383 -1.361373488 -1.36489650      -1.33536106     -1.44214335
## 185 -1.241800564 -1.315676402 -0.62537476      -1.23414336     -1.21280043
## 186 -1.036812647 -1.193817507  0.36635381      -1.14192278     -0.82001773
## 187 -1.395541501 -1.338524945 -1.93770524      -1.03395723     -1.67148627
## 188 -0.954817480 -1.003412984 -0.30049816      -0.90799742     -0.76202297
## 189 -0.719081376 -0.851089366  0.64420880      -1.07894288     -0.36396802
## 190 -0.722497841 -0.805392280  0.32788158      -0.93498881     -0.55376906
## 191 -0.524342855 -0.614987757  0.59718719      -0.71905770     -0.35869577
## 192 -0.780577751 -0.698765747 -0.97589952      -0.51662229     -0.93073500
## 193 -0.732747237 -0.919634994  1.08877678      -1.27463044     -0.28752038
## 194 -0.862572917 -0.873937909 -0.42873892      -0.94623522     -0.72511721
## 195 -1.272548751 -1.422302935 -0.12523578      -1.41858451     -1.19698368
## 196 -0.753246028 -0.896786451  0.70405449      -1.02720938     -0.45623241
## 197 -0.865989383 -0.851089366 -0.60827599      -0.97997445     -0.80156485
## 198 -0.927485758 -1.056726251  0.31505751      -1.13067637     -0.74620622
## 199 -1.255466425 -1.300444040 -0.84765875      -1.12392852     -1.23652556
## 200 -0.582422764 -0.706381928  0.74252672      -0.90799742     -0.08453872
## 201 -1.047062043 -1.049110070 -0.80491183      -1.04520364     -1.12844441
## 202 -0.889904640 -0.950099718 -0.10813701      -0.89225244     -0.76465910
##     asymetry_coefficient length_of_kernel_groove cluster
## 1            -1.79447044             -0.94122576       1
## 2            -0.67716286             -1.20653517       1
## 3            -0.96961695             -1.24704042       1
## 4            -1.57047719             -0.49769325       1
## 5            -0.83468927             -0.94122576       1
## 6            -0.08760198             -0.40858169       1
## 7            -1.11517933              0.92404111       2
## 8            -1.16237079              0.22735077       2
## 9             0.54848568             -0.21618175       3
## 10           -1.32986723             -0.85008895       1
## 11            0.17826538             -1.38273302       3
## 12           -0.38670276             -1.11134782       3
## 13           -0.52229512             -1.20653517       3
## 14            0.31053440             -1.29564672       3
## 15            1.00777155             -1.29564672       3
## 16           -1.40829810             -0.75895213       1
## 17           -1.29663381             -1.56298139       1
## 18            0.25536692             -1.02628679       3
## 19           -0.42924154             -0.49566799       1
## 20           -0.68447421             -0.40858169       1
## 21           -1.96256508             -0.66781531       1
## 22           -1.53059709             -0.93109945       1
## 23           -1.28068177             -0.85008895       1
## 24           -1.87090731             -0.23035858       1
## 25           -0.22917635             -1.20653517       3
## 26           -0.80677319             -1.11742361       3
## 27           -0.64060609             -0.77515423       1
## 28           -0.12415874             -0.65566373       3
## 29           -1.90274493             -0.73869950       1
## 30           -0.20325428             -0.39035433       2
## 31            0.13439727             -0.24656069       3
## 32           -1.05934719             -0.82781106       1
## 33           -1.05602385             -0.12301967       1
## 34           -0.59474397              0.21519919       2
## 35           -0.50634307              0.12811290       1
## 36           -0.40265480             -0.22630806       1
## 37            1.97220541             -0.85008895       3
## 38           -0.75160572             -0.49161746       1
## 39           -0.97626364             -0.49566799       1
## 40           -0.83535394             -1.29159620       1
## 41            0.66014998              0.21722446       2
## 42           -0.39202011             -0.48756694       1
## 43           -1.43422017             -0.93109945       1
## 44           -1.56250117             -0.58477954       1
## 45           -0.50501374             -0.49769325       1
## 46           -0.67383951             -0.62731006       1
## 47           -1.05602385             -0.14124703       1
## 48            0.17095403             -0.56047639       1
## 49            1.24638751             -0.57667849       3
## 50            0.26467228             -0.05011021       3
## 51           -0.25908643             -0.39845538       1
## 52           -1.48672897              0.13418869       1
## 53           -1.18629885              0.03697608       1
## 54           -0.60870201             -0.76300265       1
## 55           -1.71205156             -0.67389110       1
## 56           -1.14243074             -0.40250591       1
## 57           -1.47277094             -1.82626553       1
## 58           -0.96297027             -1.45361721       1
## 59           -0.33087062             -1.65209295       1
## 60            0.29192368             -0.67389110       3
## 61           -1.68945284             -1.29362146       1
## 62           -0.90780279             -1.64804242       1
## 63           -1.59839326             -0.54832482       1
## 64           -0.99753303             -0.58477954       1
## 65           -1.49802834             -0.24453542       1
## 66           -0.12282941             -0.71642161       3
## 67            0.23808554              1.29466417       2
## 68            0.63622191              0.92404111       2
## 69            0.54582701              0.74986852       2
## 70           -0.51963644              1.33314416       2
## 71            0.19022941              0.85113166       2
## 72            0.79906567              0.75796958       2
## 73            0.07058910              1.01517793       2
## 74            0.48733619              2.08654186       2
## 75            0.89477792              1.90629348       2
## 76           -0.57148058              0.65873171       2
## 77            1.20584274              0.93011690       2
## 78            1.06759171              0.92809164       2
## 79            0.96722678              1.55187252       2
## 80           -1.49271099              1.72604511       2
## 81           -0.50235506              1.54782200       2
## 82           -0.84731797              1.57212515       2
## 83           -1.37506468              1.39390204       2
## 84           -0.01781180              2.18172920       2
## 85            1.37068050              1.64098408       2
## 86            0.86287384              1.82325771       2
## 87           -1.17167615              2.08249133       2
## 88           -0.39733746              1.28048733       2
## 89           -0.31957125              1.28048733       2
## 90            1.51757222              0.92404111       2
## 91            0.80770636              2.08046607       2
## 92           -0.01448846              1.10631475       2
## 93           -0.16005084              1.65516092       2
## 94           -1.04605382              2.09059238       2
## 95           -0.57480392              1.72604511       2
## 96           -1.01680841              1.36959889       2
## 97            0.33180378              0.39949809       2
## 98           -1.09656862              0.84303061       2
## 99            0.39228861              1.19137578       2
## 100          -0.23981104              1.63693355       2
## 101          -0.23249969              1.47288728       2
## 102          -0.77420444              0.92809164       2
## 103          -0.58145060              1.57820094       2
## 104           0.01940963              1.02935477       2
## 105          -0.30960123              2.28704286       2
## 106          -1.31590919              0.95847058       2
## 107          -0.98556900              0.75594431       2
## 108          -0.02645249              1.10226422       2
## 109          -1.06931721              1.01922845       2
## 110           1.97021140              1.28048733       2
## 111           0.63755125              1.81313140       2
## 112          -0.97692831              1.50326622       2
## 113           0.40956999              0.66683276       2
## 114          -0.42126552              1.54782200       2
## 115          -0.05237455              1.10428948       2
## 116          -0.43522356              1.82123245       2
## 117           1.45110538              1.55389779       2
## 118          -0.06566792              1.19542630       2
## 119           0.37766590              0.57164541       2
## 120          -0.48773236              0.98074846       2
## 121          -0.25376908             -0.56047639       1
## 122           0.31252840              1.15694631       2
## 123           0.44745609              1.37972520       2
## 124          -0.97094629              1.00910214       2
## 125          -1.20158623              1.54782200       2
## 126           1.09550778              0.48658438       2
## 127          -0.58543862              1.09618843       2
## 128          -0.53824716              1.06986002       2
## 129           0.49464754              0.75796958       2
## 130           0.36437254              0.75796958       2
## 131           0.83362843              0.86328323       2
## 132          -0.12748208              1.11441580       2
## 133          -0.71637829              0.92809164       2
## 134          -0.65988148              0.67088328       2
## 135           0.03403234              1.01517793       2
## 136           1.05429834             -0.05213547       3
## 137           2.20483935              0.03900134       3
## 138           1.51358421             -0.23035858       3
## 139           1.16396863             -0.40453117       3
## 140           0.50062956             -0.49161746       3
## 141           1.62923651             -0.29516699       3
## 142          -0.99487435             -0.58477954       3
## 143           0.46739614             -0.84806368       3
## 144          -0.30428388             -0.21213122       3
## 145           1.15931595             -0.45921326       3
## 146           0.98184948             -0.23035858       3
## 147           2.17625861             -0.30529330       3
## 148           0.69006005             -0.12301967       3
## 149          -0.25642775             -0.85008895       3
## 150           0.21947482             -0.31947014       3
## 151           1.39261456             -0.40858169       3
## 152          -0.24645773             -0.84603842       3
## 153           0.73592217             -0.40655643       3
## 154           0.43881540             -0.22428280       3
## 155           1.11013049             -0.22428280       3
## 156          -0.42259486              0.14228974       3
## 157           0.36769588             -0.22833332       3
## 158           0.84426312             -0.75895213       3
## 159           0.46606680             -0.49566799       3
## 160           1.77479889             -0.75287634       3
## 161          -1.00816772             -0.73869950       1
## 162           0.80172435             -0.30529330       3
## 163          -0.05303922             -0.16757544       3
## 164           0.41156399             -0.58477954       3
## 165           1.99214546             -0.94122576       3
## 166           0.39295328             -0.85211421       3
## 167           1.24306417             -0.67186584       3
## 168           0.97320880             -0.47744062       3
## 169           0.70135942             -0.72452266       3
## 170           1.07490306             -0.66579005       3
## 171           0.65416796             -0.92704893       3
## 172           1.43448867             -0.84806368       3
## 173          -1.36708866             -0.49161746       3
## 174           0.82365840             -1.20653517       3
## 175           0.84359846             -0.55440060       3
## 176           0.75719156             -0.53212271       3
## 177           0.99115484             -0.57870376       3
## 178           1.63987121             -0.21213122       3
## 179           0.22878018             -0.48351641       3
## 180           0.78444297             -0.13922177       3
## 181           0.30654638             -0.94122576       3
## 182           2.52986220             -0.93920050       3
## 183           0.83562244             -1.26931831       3
## 184           1.11677717             -0.76097739       3
## 185           0.17760071             -0.85008895       3
## 186          -0.08029063             -0.58477954       3
## 187           0.75453289             -0.67186584       3
## 188           0.27530697             -0.82781106       3
## 189           0.76782626             -1.02628679       3
## 190           1.17327399             -0.93717524       3
## 191           0.63289857             -0.66781531       3
## 192          -0.27370913             -0.38427854       3
## 193          -0.59142063             -1.19640886       1
## 194           1.16596263             -0.76097739       3
## 195           0.22146883             -1.20045938       3
## 196           3.14933315             -0.85211421       3
## 197           0.13373260             -0.85008895       3
## 198          -0.05769190             -1.11539835       3
## 199           0.40358797             -0.84603842       3
## 200           3.05561491             -0.73869950       3
## 201          -0.07962596             -0.76300265       3
## 202           1.27563292             -0.72452266       3
count(seeds_df_cl,cluster)
##   cluster  n
## 1       1 48
## 2       2 73
## 3       3 81
library(ggplot2)
ggplot(seeds_df_cl, aes(x=area, y = perimeter, color = factor(cluster))) +geom_point()

library(clValid)
## Warning: package 'clValid' was built under R version 4.3.3
## Loading required package: cluster
dunn(dist_mat,cut_avg)
## [1] 0.1056525
#This is an algorithm for the k-means clustering applied to the iris dataset
set.seed(1234) #setting a seed for the random number generator
data(iris) 
ir3<-kmeans(iris[,-5],center=3, iter.max=200) #not using species info
ir3
## K-means clustering with 3 clusters of sizes 50, 62, 38
## 
## Cluster means:
##   Sepal.Length Sepal.Width Petal.Length Petal.Width
## 1     5.006000    3.428000     1.462000    0.246000
## 2     5.901613    2.748387     4.393548    1.433871
## 3     6.850000    3.073684     5.742105    2.071053
## 
## Clustering vector:
##   [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
##  [38] 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
##  [75] 2 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 2 3 3 3 3 2 3 3 3 3
## [112] 3 3 2 2 3 3 3 3 2 3 2 3 2 3 3 2 2 3 3 3 3 3 2 3 3 3 3 2 3 3 3 2 3 3 3 2 3
## [149] 3 2
## 
## Within cluster sum of squares by cluster:
## [1] 15.15100 39.82097 23.87947
##  (between_SS / total_SS =  88.4 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
## [6] "betweenss"    "size"         "iter"         "ifault"
ir3$betweenss
## [1] 602.5192
table(ir3$cluster, iris$Species)
##    
##     setosa versicolor virginica
##   1     50          0         0
##   2      0         48        14
##   3      0          2        36
cm<- table(ir3$cluster, iris$Species)
1-sum(diag(cm))/sum(cm)
## [1] 0.1066667