K Means Clustering

EastWest Airlines

Assignment 20

library(readxl)

data <- read_excel("C:\\Users\\RISHI RAHUL\\Desktop\\DS\\5 HClust\\Assignment\\EastWestAirlines.xlsx", sheet = "data")

data <- data[,-1]
normalized_data <- scale(data) 

plot(normalized_data)

km <- kmeans(normalized_data,4) #kmeans clustering
str(km)
## List of 9
##  $ cluster     : int [1:3999] 1 1 1 1 4 1 4 3 2 4 ...
##  $ centers     : num [1:4, 1:11] -0.301 1.235 -0.138 0.631 -0.127 ...
##   ..- attr(*, "dimnames")=List of 2
##   .. ..$ : chr [1:4] "1" "2" "3" "4"
##   .. ..$ : chr [1:11] "Balance" "Qual_miles" "cc1_miles" "cc2_miles" ...
##  $ totss       : num 43978
##  $ withinss    : num [1:4] 7669 8242 6540 6290
##  $ tot.withinss: num 28741
##  $ betweenss   : num 15237
##  $ size        : int [1:4] 2095 143 854 907
##  $ iter        : int 4
##  $ ifault      : int 0
##  - attr(*, "class")= chr "kmeans"
km$centers
##      Balance  Qual_miles  cc1_miles   cc2_miles   cc3_miles Bonus_miles
## 1 -0.3012579 -0.12654781 -0.5500445  0.01168634 -0.06031392  -0.5104477
## 2  1.2350103  0.56045759  0.2919732  0.18594222  1.44156994   1.0439261
## 3 -0.1382402  0.27801229 -0.2736871  0.02866055 -0.06275873  -0.2681790
## 4  0.6312966 -0.05782831  1.4821608 -0.08329518 -0.02887639   1.2669584
##   Bonus_trans Flight_miles_12mo Flight_trans_12 Days_since_enroll
## 1  -0.4557152       -0.21391206    -0.235025742       -0.24159282
## 2   1.8902231        3.62256578     3.874418317        0.31598547
## 3  -0.0897408       -0.01137554    -0.001558563        0.05307777
## 4   0.8390960       -0.06633564    -0.066518056        0.45823883
##       Award?
## 1 -0.7668234
## 2  0.9128067
## 3  1.2698112
## 4  0.4316923
km$cluster
##    [1] 1 1 1 1 4 1 4 3 2 4 1 4 1 1 1 4 4 3 4 3 4 3 1 1 1 1 1 1 4 3 4 1 4 1
##   [35] 1 4 3 1 4 3 1 4 3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[3809] 1 1 1 3 1 1 1 1 3 1 3 4 1 1 4 4 2 1 1 1 1 3 3 3 2 1 4 1 1 3 1 1 1 1
## [3843] 1 4 1 1 2 1 1 1 1 3 1 1 1 4 3 1 1 1 4 1 3 3 1 1 1 4 1 3 1 3 1 1 1 1
## [3877] 1 3 1 1 1 4 1 4 1 1 4 1 3 1 3 1 1 1 1 1 1 3 1 4 3 1 3 1 3 1 1 2 4 1
## [3911] 1 2 1 3 1 3 1 2 1 1 1 3 1 3 1 1 1 1 1 3 4 3 3 1 2 1 3 1 1 1 1 3 1 1
## [3945] 3 1 1 1 1 1 2 1 1 3 3 1 1 1 1 1 1 1 1 1 1 1 4 1 1 1 1 1 1 3 4 1 1 1
## [3979] 3 3 1 1 1 1 1 4 1 3 1 3 1 1 1 1 3 3 3 1 1
finalmodel <- data.frame(km$cluster,data)

View(finalmodel)

x <- aggregate(data[,1:11],by=list(km$cluster),FUN=mean)
x
##   Group.1   Balance Qual_miles cc1_miles cc2_miles cc3_miles Bonus_miles
## 1       1  43241.86   46.20907  1.302148  1.016229  1.000477    4817.041
## 2       2 198060.31  577.72028  2.461538  1.041958  1.293706   42356.671
## 3       3  59670.08  359.20258  1.682670  1.018735  1.000000   10668.064
## 4       4 137220.66   99.37486  4.100331  1.002205  1.006615   47743.118
##   Bonus_trans Flight_miles_12mo Flight_trans_12 Days_since_enroll
## 1    7.225298          160.5341       0.4821002          3619.638
## 2   29.755245         5532.4056      16.0699301          4771.112
## 3   10.740047          444.1276       1.3676815          4228.172
## 4   19.660419          367.1720       1.1212789          5064.884
##      Award?
## 1 0.0000000
## 2 0.8111888
## 3 0.9836066
## 4 0.5788313