Agrupamiento o Clustering es una técnica de aprendizaje automático no supervidaso que agrupa datos en función de su similitud.
Algunos usos típicos de esta técnica son:
#install.packages("cluster") # Análisis de agrupamiento
library(cluster)
#install.packages("ggplot2") # Graficar
library(ggplot2)
#install.packages("data.table") # Manejo de muchos datos
library(data.table)
#install.packages("factoextra") # Gráfica de optimización de número de clústers
library(factoextra)
#install.packages("datasets")
library(datasets)
#install.packages("tidyverse")
library(tidyverse)
Agrupa los siguientes ocho puntos.
df1 <- data.frame(x=c(2,2,8,5,7,6,1,4), y=c(10,5,4,8,5,4,2,9))
summary(df1)
## x y
## Min. :1.000 Min. : 2.000
## 1st Qu.:2.000 1st Qu.: 4.000
## Median :4.500 Median : 5.000
## Mean :4.375 Mean : 5.875
## 3rd Qu.:6.250 3rd Qu.: 8.250
## Max. :8.000 Max. :10.000
str(df1)
## 'data.frame': 8 obs. of 2 variables:
## $ x: num 2 2 8 5 7 6 1 4
## $ y: num 10 5 4 8 5 4 2 9
plot(df1$x, df1$y)
# datos_escalados <- scale(datos_originales)
grupos1 <- 3
set.seed(123)
clusters1 <- kmeans(df1, grupos1)
clusters1
## K-means clustering with 3 clusters of sizes 2, 3, 3
##
## Cluster means:
## x y
## 1 1.500000 3.500000
## 2 3.666667 9.000000
## 3 7.000000 4.333333
##
## Clustering vector:
## [1] 2 1 3 2 3 3 1 2
##
## Within cluster sum of squares by cluster:
## [1] 5.000000 6.666667 2.666667
## (between_SS / total_SS = 85.8 %)
##
## Available components:
##
## [1] "cluster" "centers" "totss" "withinss" "tot.withinss"
## [6] "betweenss" "size" "iter" "ifault"
set.seed(123)
optimizacion1 <- clusGap(df1, FUN=kmeans, nstart=2, K.max = 7)
plot(optimizacion1, xlab="Número de clusters K", main="Optimización de clusters")
#El óptimo es el primer punto más alto
fviz_cluster(clusters1, data=df1)
df1_clusters <- cbind(df1, cluster = clusters1$cluster)
head(df1_clusters)
## x y cluster
## 1 2 10 2
## 2 2 5 1
## 3 8 4 3
## 4 5 8 2
## 5 7 5 3
## 6 6 4 3
La técica de clustering permite identificar patrones o grupos naturales en los datos sin necesidad de etiquetas previas
La base de datos USArrests contiene estadísticas en arrestos por cada 100,000 residentes por agresión, asesinato y violación en cada uno de los 50 estados de EE.UU. en 1973.
df2 <- USArrests
df2 <- df2 %>% select(-UrbanPop)
summary(df2)
## Murder Assault Rape
## Min. : 0.800 Min. : 45.0 Min. : 7.30
## 1st Qu.: 4.075 1st Qu.:109.0 1st Qu.:15.07
## Median : 7.250 Median :159.0 Median :20.10
## Mean : 7.788 Mean :170.8 Mean :21.23
## 3rd Qu.:11.250 3rd Qu.:249.0 3rd Qu.:26.18
## Max. :17.400 Max. :337.0 Max. :46.00
str(df2)
## 'data.frame': 50 obs. of 3 variables:
## $ Murder : num 13.2 10 8.1 8.8 9 7.9 3.3 5.9 15.4 17.4 ...
## $ Assault: int 236 263 294 190 276 204 110 238 335 211 ...
## $ Rape : num 21.2 44.5 31 19.5 40.6 38.7 11.1 15.8 31.9 25.8 ...
df2_escalado <- scale(df2)
summary(df2_escalado)
## Murder Assault Rape
## Min. :-1.6044 Min. :-1.5090 Min. :-1.4874
## 1st Qu.:-0.8525 1st Qu.:-0.7411 1st Qu.:-0.6574
## Median :-0.1235 Median :-0.1411 Median :-0.1209
## Mean : 0.0000 Mean : 0.0000 Mean : 0.0000
## 3rd Qu.: 0.7949 3rd Qu.: 0.9388 3rd Qu.: 0.5277
## Max. : 2.2069 Max. : 1.9948 Max. : 2.6444
grupos2 <- 5
set.seed(123)
clusters2 <- kmeans(df2_escalado, grupos2)
clusters2
## K-means clustering with 5 clusters of sizes 7, 14, 17, 4, 8
##
## Cluster means:
## Murder Assault Rape
## 1 0.8292944 1.3313823 0.90560938
## 2 -1.0812577 -1.0779212 -1.00700542
## 3 -0.2754591 -0.2999280 -0.12336985
## 4 0.4562038 0.9358314 2.26533514
## 5 1.5238170 0.8908337 0.09934463
##
## Clustering vector:
## Alabama Alaska Arizona Arkansas California
## 5 4 1 3 4
## Colorado Connecticut Delaware Florida Georgia
## 4 2 3 1 5
## Hawaii Idaho Illinois Indiana Iowa
## 2 2 1 3 2
## Kansas Kentucky Louisiana Maine Maryland
## 3 3 5 2 1
## Massachusetts Michigan Minnesota Mississippi Missouri
## 3 1 2 5 3
## Montana Nebraska Nevada New Hampshire New Jersey
## 3 2 4 2 3
## New Mexico New York North Carolina North Dakota Ohio
## 1 1 5 2 3
## Oklahoma Oregon Pennsylvania Rhode Island South Carolina
## 3 3 3 2 5
## South Dakota Tennessee Texas Utah Vermont
## 2 5 5 3 2
## Virginia Washington West Virginia Wisconsin Wyoming
## 3 3 2 2 3
##
## Within cluster sum of squares by cluster:
## [1] 3.380664 5.645542 9.205038 1.346517 4.683603
## (between_SS / total_SS = 83.5 %)
##
## Available components:
##
## [1] "cluster" "centers" "totss" "withinss" "tot.withinss"
## [6] "betweenss" "size" "iter" "ifault"
set.seed(123)
optimizacion2 <- clusGap(df2_escalado, FUN=kmeans, nstart=1, K.max = 10)
plot(optimizacion2, xlab="Número de clusters K", main="Optimización de clusters")
#El óptimo es el primer punto más alto
fviz_cluster(clusters2, data=df2_escalado)
df2_clusters <- cbind(df2, cluster = clusters2$cluster)
head(df2_clusters)
## Murder Assault Rape cluster
## Alabama 13.2 236 21.2 5
## Alaska 10.0 263 44.5 4
## Arizona 8.1 294 31.0 1
## Arkansas 8.8 190 19.5 3
## California 9.0 276 40.6 4
## Colorado 7.9 204 38.7 4
df2_clusters %>% group_by(cluster) %>% summarise_all(mean) %>% mutate(indicador_inseguridad = Murder + Assault + Rape)
## # A tibble: 5 × 5
## cluster Murder Assault Rape indicador_inseguridad
## <int> <dbl> <dbl> <dbl> <dbl>
## 1 1 11.4 282. 29.7 323.
## 2 2 3.08 80.9 11.8 95.8
## 3 3 6.59 146. 20.1 172.
## 4 4 9.78 249. 42.4 301.
## 5 5 14.4 245 22.2 282.
df2_clusters <- df2_clusters %>%
mutate(cluster = case_when(
cluster == 1 ~ "Inseguridad muy alta",
cluster == 4 ~ "Inseguridad alta",
cluster == 5 ~ "Inseguridad media",
cluster == 3 ~ "Inseguridad baja",
cluster == 2 ~ "Inseguridad muy baja",
))
head(df2_clusters)
## Murder Assault Rape cluster
## Alabama 13.2 236 21.2 Inseguridad media
## Alaska 10.0 263 44.5 Inseguridad alta
## Arizona 8.1 294 31.0 Inseguridad muy alta
## Arkansas 8.8 190 19.5 Inseguridad baja
## California 9.0 276 40.6 Inseguridad alta
## Colorado 7.9 204 38.7 Inseguridad alta
La técica de clustering permite identificar patrones o grupos naturales en los datos sin necesidad de etiquetas previas.
La base de datos ventas tiene los registros entre el
1 de diciembre del 2010 y el 9 de diciembre del 2011 de las ventas de
una empresa minorista en línea sin tienda física, basada en Reino
Unido.
La empresa vende principalmente regalos únicos para toda ocasión y
muchos de sis clientes son mayoristas.
Objetivo: Segmentar clientes, asignarles nombres y características de
comportamiento y proponer sugerencias a las empresas para aumentar las
ventas.
df3 <- read.csv("C:\\Users\\natal\\OneDrive\\Carrera\\7moSemestre\\Modulo2\\ventas.csv")
summary(df3)
## Ticket Producto Cantidad Fecha
## Length :522064 Length :522064 Min. :-9600.00 Length :522064
## N.unique : 21663 N.unique : 4183 1st Qu.: 1.00 N.unique : 305
## N.blank : 0 N.blank : 1455 Median : 3.00 N.blank : 0
## Min.nchar: 6 Min.nchar: 0 Mean : 10.09 Min.nchar: 10
## Max.nchar: 7 Max.nchar: 36 3rd Qu.: 10.00 Max.nchar: 10
## Max. :80995.00
##
## Hora Precio Cliente País
## Length :522064 Min. :-11062.060 Min. :12346 Length :522064
## N.unique : 739 1st Qu.: 1.250 1st Qu.:13950 N.unique : 30
## N.blank : 0 Median : 2.080 Median :15265 N.blank : 0
## Min.nchar: 8 Mean : 3.827 Mean :15317 Min.nchar: 3
## Max.nchar: 8 3rd Qu.: 4.130 3rd Qu.:16837 Max.nchar: 20
## Max. : 13541.330 Max. :18287
## NAs :134041
str(df3)
## 'data.frame': 522064 obs. of 8 variables:
## $ Ticket : chr "536365" "536365" "536365" "536365" ...
## $ Producto: chr "WHITE HANGING HEART T-LIGHT HOLDER" "WHITE METAL LANTERN" "CREAM CUPID HEARTS COAT HANGER" "KNITTED UNION FLAG HOT WATER BOTTLE" ...
## $ Cantidad: int 6 6 8 6 6 2 6 6 6 32 ...
## $ Fecha : chr "01/12/2010" "01/12/2010" "01/12/2010" "01/12/2010" ...
## $ Hora : chr "08:26:00" "08:26:00" "08:26:00" "08:26:00" ...
## $ Precio : num 2.55 3.39 2.75 3.39 3.39 7.65 4.25 1.85 1.85 1.69 ...
## $ Cliente : int 17850 17850 17850 17850 17850 17850 17850 17850 17850 13047 ...
## $ País : chr "United Kingdom" "United Kingdom" "United Kingdom" "United Kingdom" ...
df3$Venta <- df3$Cantidad * df3$Precio
library(dplyr)
tickets_cliente <- df3 %>%
group_by(Cliente, Ticket) %>%
summarise(
Total_Ticket = sum(Venta, na.rm = TRUE),
.groups = "drop"
)
ticket_promedio <- tickets_cliente %>%
group_by(Cliente) %>%
summarise(
Ticket_Promedio = mean(Total_Ticket, na.rm = TRUE),
.groups = "drop"
)
frecuencia <- df3 %>%
group_by(Cliente) %>%
summarise(
Frecuencia_Compra = n_distinct(Ticket),
.groups = "drop"
)
clientes_cluster <- ticket_promedio %>%
left_join(frecuencia, by = "Cliente")
datos_cluster <- clientes_cluster %>%
select(Ticket_Promedio, Frecuencia_Compra) %>%
scale()
grupos3 <- 12
set.seed(123)
clusters3 <- kmeans(
datos_cluster,
centers = grupos3,
nstart = 25
)
clusters3
## K-means clustering with 12 clusters of sizes 516, 1739, 62, 21, 6, 1, 181, 2, 2, 1322, 387, 59
##
## Cluster means:
## Ticket_Promedio Frecuencia_Compra
## 1 0.10354027 -0.033389249
## 2 -0.14164163 -0.049420664
## 3 0.03754585 0.529372851
## 4 2.06100520 0.166563706
## 5 -0.04315654 2.078258064
## 6 0.04134286 64.972072929
## 7 0.34100718 -0.022830586
## 8 7.57526472 -0.037928721
## 9 44.52918206 -0.065814052
## 10 -0.03505242 -0.043792656
## 11 -0.05238290 0.134955527
## 12 0.81655862 -0.009885846
##
## Clustering vector:
## [1] 9 1 12 10 11 2 7 10 7 4 1 7 2 11 10 10 10 7 10 1 1 4 10 1
## [25] 10 10 10 2 10 1 10 10 11 7 12 10 2 2 10 1 4 10 2 2 4 11 1 2
## [49] 10 11 7 1 1 10 10 2 11 7 12 10 1 11 7 2 2 7 2 7 10 10 7 2
## [73] 12 2 1 4 2 7 10 10 10 1 10 2 10 10 3 7 1 11 1 1 12 1 1 7
## [97] 1 11 1 10 10 11 10 2 2 2 7 2 11 12 1 2 2 10 2 2 1 2 1 7
## [121] 10 1 1 10 10 1 1 2 11 1 10 2 10 10 2 10 7 10 4 10 7 11 10 10
## [145] 1 1 2 2 7 2 2 11 10 12 10 10 1 2 1 10 2 10 7 3 10 1 2 2
## [169] 10 2 7 10 10 2 11 11 7 2 10 4 10 2 2 11 10 7 11 2 11 2 2 1
## [193] 2 12 10 10 7 1 1 10 1 10 10 2 11 10 11 2 10 10 7 1 1 10 1 7
## [217] 1 1 1 10 2 10 1 10 10 1 11 10 2 10 2 7 7 2 1 10 1 2 2 11
## [241] 2 7 7 1 2 10 1 2 12 10 2 3 3 11 10 1 2 4 1 10 2 2 10 2
## [265] 10 7 10 2 10 1 1 11 11 10 11 7 7 10 1 1 2 10 3 10 2 2 10 10
## [289] 10 1 2 10 12 2 10 2 10 2 2 1 10 10 4 11 5 7 10 4 4 7 12 2
## [313] 11 7 1 7 2 12 7 7 7 7 10 2 1 10 10 10 10 2 2 10 2 10 10 10
## [337] 10 2 12 10 2 1 2 2 2 1 10 2 12 2 2 10 10 10 2 2 2 2 7 2
## [361] 2 10 10 1 2 10 11 1 3 1 11 2 2 7 1 2 10 10 10 2 10 1 1 2
## [385] 1 1 1 10 2 10 10 2 7 11 10 2 10 1 2 10 10 1 2 10 2 2 2 2
## [409] 3 2 2 1 10 10 11 10 1 2 1 1 2 2 3 10 2 2 10 10 2 2 12 2
## [433] 10 10 10 2 7 10 2 10 10 10 10 1 1 2 10 10 11 2 1 2 11 1 2 7
## [457] 2 2 5 2 1 2 7 2 10 2 1 10 1 11 2 2 2 10 2 10 2 2 1 2
## [481] 2 11 2 2 2 2 11 11 11 1 10 2 3 11 10 2 7 10 10 10 1 2 10 10
## [505] 10 10 10 1 1 10 10 11 10 10 11 10 10 2 2 10 2 10 10 2 2 2 2 11
## [529] 2 2 7 2 3 2 2 12 11 2 2 5 11 10 2 7 11 2 1 3 2 2 3 2
## [553] 2 2 10 10 10 2 3 1 1 2 2 2 10 10 7 11 10 2 10 2 2 10 4 1
## [577] 11 2 1 2 1 10 2 2 2 2 11 10 7 1 2 10 10 10 2 2 10 1 10 2
## [601] 10 10 2 2 2 7 1 10 11 2 11 11 2 1 2 2 2 10 2 2 10 1 3 2
## [625] 2 2 2 1 10 10 10 10 10 2 2 2 10 2 7 7 2 2 10 2 11 2 10 7
## [649] 10 2 10 2 2 1 2 10 10 2 2 2 1 2 2 2 10 1 10 2 3 10 11 11
## [673] 11 11 1 2 2 2 10 2 1 2 10 10 2 2 1 10 10 10 2 2 1 10 2 10
## [697] 2 2 10 11 2 10 10 10 10 10 10 12 10 2 3 10 10 2 2 1 1 11 7 10
## [721] 10 10 7 1 1 7 12 1 10 7 10 10 2 2 1 10 10 1 1 1 1 10 10 1
## [745] 1 2 7 2 10 10 1 10 7 1 10 2 1 2 1 1 10 10 1 2 11 10 1 10
## [769] 10 10 2 2 2 3 2 2 10 10 10 11 2 2 7 12 1 11 2 2 2 10 1 10
## [793] 1 2 1 10 10 10 1 10 10 1 10 2 1 10 1 2 1 2 1 10 1 3 2 1
## [817] 2 2 10 1 2 2 10 2 2 10 10 10 2 1 2 11 10 10 10 10 10 2 2 10
## [841] 7 2 2 10 7 2 1 2 2 10 2 2 2 2 10 1 1 2 1 10 2 1 2 10
## [865] 10 2 2 2 2 3 1 2 2 2 2 10 2 2 11 7 10 2 2 2 1 10 1 2
## [889] 10 2 2 2 11 7 2 2 11 10 2 2 1 2 1 11 2 10 10 2 10 10 10 2
## [913] 11 2 10 10 2 2 1 2 10 10 10 2 2 2 2 2 10 1 10 11 10 10 7 2
## [937] 10 2 7 10 2 1 2 10 2 10 2 10 10 10 2 2 1 1 10 2 2 10 11 2
## [961] 10 2 2 2 2 7 2 2 2 10 2 7 2 1 3 11 2 10 11 2 11 2 10 10
## [985] 10 11 2 2 2 2 2 2 2 2 2 1 1 2 11 1 2 2 10 10 1 1 10 10
## [1009] 2 10 2 11 2 1 10 2 2 10 10 2 1 2 11 11 10 10 2 1 10 12 1 3
## [1033] 11 1 10 10 3 2 2 2 2 11 2 2 10 1 2 3 10 1 10 12 10 10 10 2
## [1057] 2 10 1 10 2 10 1 7 10 2 10 2 10 10 10 2 2 2 10 2 10 2 2 2
## [1081] 2 11 10 10 10 10 1 12 7 7 3 2 2 2 2 11 10 2 2 10 12 1 11 11
## [1105] 2 1 10 2 2 7 11 7 2 1 2 2 2 10 10 10 11 2 7 2 1 10 2 2
## [1129] 2 1 2 1 11 10 2 1 2 10 10 2 2 10 2 10 2 1 10 10 7 10 2 2
## [1153] 2 2 7 2 2 2 10 2 2 12 1 2 2 2 2 2 2 2 2 11 11 2 10 10
## [1177] 10 10 2 7 2 10 11 2 11 10 1 1 10 2 2 11 10 10 11 1 1 10 1 1
## [1201] 10 10 10 2 1 7 10 10 2 10 10 2 2 2 11 7 11 2 10 1 2 1 2 11
## [1225] 2 2 10 2 10 10 7 10 3 2 2 2 1 11 11 2 11 7 10 2 2 10 2 2
## [1249] 10 2 2 2 10 2 10 2 2 11 2 4 2 2 11 10 4 2 11 2 7 10 2 7
## [1273] 10 10 2 11 2 10 10 2 10 2 1 10 7 2 2 10 2 1 11 2 11 1 10 10
## [1297] 2 10 2 7 7 2 2 10 10 10 1 2 2 1 11 10 2 1 1 2 2 10 2 2
## [1321] 7 2 2 2 10 3 10 2 10 11 11 10 11 1 10 2 2 2 2 2 10 2 10 10
## [1345] 11 7 2 10 10 11 2 2 10 11 1 10 10 10 11 1 10 10 10 2 1 2 2 2
## [1369] 11 10 2 2 11 7 1 2 10 1 1 1 1 7 2 1 11 2 10 1 1 2 2 2
## [1393] 10 7 2 11 10 1 12 2 10 2 10 11 1 10 2 10 3 11 2 1 10 1 7 1
## [1417] 11 10 10 2 10 1 2 10 10 2 10 1 2 10 10 11 2 10 10 10 2 11 2 2
## [1441] 2 1 10 2 10 2 10 2 2 2 10 2 10 1 2 11 11 2 2 2 2 10 2 2
## [1465] 1 10 2 2 1 11 1 1 2 11 2 11 1 11 10 2 2 10 10 10 2 7 10 2
## [1489] 2 11 2 1 2 10 10 1 10 10 11 2 2 10 10 2 2 2 2 12 10 10 10 10
## [1513] 2 2 2 2 10 2 2 11 2 12 2 1 11 2 10 10 2 2 11 2 1 10 2 10
## [1537] 2 10 10 10 2 10 2 2 7 10 2 1 2 2 10 10 10 10 2 2 11 1 2 1
## [1561] 10 2 2 10 2 2 10 10 2 2 2 2 2 11 11 3 10 2 1 2 10 11 7 2
## [1585] 10 2 10 10 2 2 11 7 1 1 10 10 10 2 2 2 1 2 1 11 10 11 1 1
## [1609] 2 2 10 11 2 2 2 2 2 2 10 2 2 11 2 10 2 2 2 10 10 2 10 2
## [1633] 1 5 7 10 2 2 2 10 2 10 10 2 2 10 1 10 10 10 2 10 10 10 1 10
## [1657] 2 2 10 10 10 4 2 10 10 2 10 1 2 2 2 11 2 2 2 10 10 2 3 10
## [1681] 2 10 10 1 10 12 2 2 2 1 11 2 2 10 2 2 10 2 2 2 1 11 11 10
## [1705] 11 2 10 11 2 11 10 11 2 2 2 10 2 2 2 1 10 10 11 2 11 1 3 10
## [1729] 2 1 1 2 2 2 10 10 10 1 10 11 1 10 10 1 2 2 10 10 2 10 2 7
## [1753] 10 2 10 11 10 2 2 2 2 2 10 2 2 2 2 2 2 11 2 2 11 2 10 10
## [1777] 2 2 11 11 2 10 10 1 2 2 2 2 2 10 11 1 2 2 10 2 10 2 1 1
## [1801] 1 10 3 10 2 1 2 10 10 10 1 10 2 2 10 10 2 7 2 11 10 10 2 1
## [1825] 1 11 2 2 10 2 1 10 12 10 2 2 10 1 1 11 2 10 2 11 10 1 1 10
## [1849] 1 10 2 10 10 2 10 1 1 1 1 10 2 11 2 2 2 10 10 1 10 10 2 2
## [1873] 2 2 1 2 10 2 10 2 10 2 10 10 2 10 2 10 2 10 2 2 2 2 10 2
## [1897] 2 2 1 10 7 3 1 2 10 10 2 2 2 2 2 10 11 2 1 11 10 2 10 2
## [1921] 10 2 12 11 1 2 2 2 3 2 2 1 11 10 11 10 2 2 1 10 2 2 2 1
## [1945] 11 11 2 3 2 10 1 7 1 10 7 2 1 10 1 1 7 3 2 2 2 10 1 10
## [1969] 10 10 2 1 10 2 2 8 10 10 2 10 1 10 10 2 1 2 1 7 11 10 10 10
## [1993] 2 10 10 2 1 7 2 1 10 10 11 10 2 10 2 2 10 2 10 1 10 11 10 1
## [2017] 1 10 1 11 11 10 10 10 1 10 3 2 2 10 1 2 2 7 10 2 2 2 10 2
## [2041] 2 10 2 10 2 11 3 10 1 11 4 10 2 10 2 10 10 10 2 2 10 10 2 2
## [2065] 11 2 2 11 10 10 10 1 10 2 10 2 2 7 2 10 2 11 10 10 10 10 10 11
## [2089] 10 10 7 2 10 11 7 10 2 10 7 2 10 10 10 2 2 2 10 1 2 10 10 10
## [2113] 11 10 10 1 2 2 10 2 11 2 2 10 10 11 11 2 1 2 11 1 1 11 1 2
## [2137] 10 2 10 5 2 2 10 10 2 10 2 10 7 1 2 10 2 10 10 10 7 2 2 10
## [2161] 12 10 10 2 10 2 2 2 2 2 10 10 2 11 11 10 10 10 10 10 10 10 10 10
## [2185] 12 11 2 10 1 10 10 7 10 10 10 1 10 2 1 2 10 2 1 10 2 10 10 2
## [2209] 10 10 11 10 2 10 1 7 1 2 2 2 2 2 10 1 2 2 2 10 2 2 10 2
## [2233] 2 2 1 10 1 2 2 1 10 10 2 2 2 2 2 2 2 2 2 7 11 10 2 2
## [2257] 10 10 10 10 10 1 7 7 2 7 7 2 10 1 10 10 1 1 3 10 7 2 2 1
## [2281] 10 1 2 2 2 2 11 11 2 10 11 10 11 2 10 10 10 2 11 2 11 10 7 10
## [2305] 10 2 2 1 2 2 2 11 2 11 1 2 10 2 10 2 11 10 10 2 2 10 2 2
## [2329] 2 10 11 2 10 10 2 7 2 2 2 11 2 2 10 2 2 10 2 10 10 10 2 10
## [2353] 11 11 2 2 2 11 2 10 10 2 11 2 3 2 10 1 10 11 10 2 10 10 11 1
## [2377] 11 2 10 2 10 7 10 2 2 1 2 10 10 2 10 10 10 1 2 2 2 10 11 2
## [2401] 2 10 7 10 2 2 2 10 1 1 2 7 1 10 2 10 10 2 11 2 2 2 2 11
## [2425] 2 11 10 10 10 2 1 2 10 2 1 2 2 2 2 10 2 11 1 1 10 2 2 10
## [2449] 1 2 10 2 2 1 2 11 10 2 2 2 10 2 10 2 8 11 10 2 2 10 2 10
## [2473] 2 2 10 1 1 10 2 12 1 10 2 2 10 2 2 7 2 10 12 10 10 2 1 10
## [2497] 1 11 1 10 10 10 10 10 11 11 10 10 7 2 10 10 10 1 1 10 1 2 10 10
## [2521] 2 1 1 11 10 1 10 7 1 2 2 10 12 10 2 10 2 2 11 2 2 2 2 11
## [2545] 2 3 10 10 2 11 2 2 10 10 2 11 2 10 2 1 2 10 2 2 2 2 2 2
## [2569] 2 1 2 2 2 10 2 10 2 1 2 2 10 2 2 2 2 2 2 10 2 10 10 2
## [2593] 2 10 11 10 2 2 2 11 2 10 10 2 12 10 2 10 10 2 11 11 10 2 2 2
## [2617] 2 2 10 11 10 1 10 2 1 7 1 11 10 11 1 2 2 2 10 2 10 10 2 1
## [2641] 4 1 2 2 10 2 2 2 11 10 3 2 2 2 2 2 11 10 1 2 2 10 2 2
## [2665] 3 10 10 11 2 10 10 2 10 7 2 2 10 2 10 2 10 1 1 2 1 10 2 10
## [2689] 10 10 10 10 10 1 2 11 2 10 2 1 1 10 2 2 2 10 2 10 10 2 10 2
## [2713] 7 1 2 10 10 2 10 7 10 10 2 2 2 11 1 2 2 10 10 2 10 2 10 12
## [2737] 1 10 1 3 10 1 2 2 10 1 1 10 2 11 2 10 2 2 10 12 10 11 2 10
## [2761] 1 11 2 10 10 3 10 10 2 2 2 10 10 10 2 2 7 10 12 10 2 10 11 11
## [2785] 2 2 2 11 10 10 1 10 10 2 10 2 2 10 10 10 7 1 10 2 10 2 7 2
## [2809] 10 10 7 10 2 2 10 1 10 2 2 2 10 2 12 11 1 2 1 10 2 2 10 2
## [2833] 12 11 1 2 7 7 2 1 2 10 7 10 10 10 1 1 1 1 12 2 10 10 2 10
## [2857] 11 10 10 2 2 12 10 2 1 10 10 2 2 11 10 10 10 1 2 2 2 2 2 11
## [2881] 1 2 2 7 2 2 2 1 1 2 2 2 1 10 2 2 10 2 2 11 2 2 2 1
## [2905] 11 1 2 2 2 10 11 10 10 2 1 10 2 2 1 1 2 1 12 10 10 2 2 2
## [2929] 10 2 11 10 10 7 2 10 11 2 2 10 2 2 11 10 2 2 10 2 2 10 2 2
## [2953] 3 10 2 10 11 2 10 10 2 10 2 2 2 1 2 2 2 2 9 10 2 10 10 10
## [2977] 2 10 10 2 11 10 2 2 10 10 2 2 2 2 2 2 10 2 10 2 2 1 2 2
## [3001] 2 10 2 1 10 7 2 1 1 2 2 2 10 1 1 2 10 2 10 2 10 2 2 2
## [3025] 2 10 11 3 10 2 2 10 2 12 10 1 1 10 2 10 10 11 10 10 2 11 10 2
## [3049] 1 2 11 10 2 7 2 2 10 2 11 10 10 10 10 2 10 2 10 10 2 2 2 11
## [3073] 2 2 10 2 2 2 2 2 1 2 11 1 2 2 10 1 1 2 2 1 2 2 2 2
## [3097] 10 10 11 10 10 2 1 2 10 11 10 2 10 1 10 2 10 1 7 1 2 1 11 2
## [3121] 2 2 10 2 2 10 2 10 11 1 11 2 10 11 2 2 10 2 12 2 10 10 2 2
## [3145] 1 2 2 2 7 11 11 11 2 1 1 11 10 11 11 10 10 10 1 1 10 2 10 11
## [3169] 10 2 10 10 10 11 2 1 2 10 10 2 10 10 10 2 11 11 10 10 2 12 2 2
## [3193] 12 1 2 2 10 10 10 1 1 11 2 1 11 2 10 2 10 2 2 11 10 2 2 11
## [3217] 2 10 11 2 10 2 10 2 2 2 11 10 10 7 10 10 2 2 2 10 2 2 10 10
## [3241] 10 11 11 2 11 11 1 10 2 10 10 2 2 10 2 10 10 2 2 11 10 3 10 10
## [3265] 11 2 2 10 2 10 2 2 2 2 10 10 2 2 2 2 10 2 7 10 2 2 2 10
## [3289] 2 2 2 2 2 1 2 2 2 2 2 2 2 10 12 10 11 10 2 2 10 2 11 2
## [3313] 2 2 10 10 2 11 2 11 10 2 2 11 2 2 11 2 10 10 10 1 7 11 10 11
## [3337] 10 2 2 10 10 2 2 11 2 10 2 2 2 2 2 2 10 10 2 2 10 2 10 10
## [3361] 1 10 10 12 1 7 2 2 2 2 10 2 2 2 2 11 1 1 2 2 2 10 2 1
## [3385] 11 10 10 2 2 2 2 2 10 2 2 10 2 2 7 2 10 10 2 10 7 2 1 10
## [3409] 10 1 1 2 10 1 10 10 1 2 11 2 2 10 11 11 10 11 10 2 10 2 2 2
## [3433] 2 7 10 12 10 10 1 10 11 10 10 10 10 2 1 10 2 10 12 2 2 2 2 10
## [3457] 2 2 10 10 2 10 10 10 2 2 1 1 2 2 10 7 10 1 1 2 10 2 1 2
## [3481] 10 2 2 10 1 1 11 2 10 2 2 10 2 10 1 2 10 10 2 2 2 1 2 2
## [3505] 10 2 2 10 2 2 7 2 10 1 10 10 2 11 2 10 2 2 11 2 11 2 10 2
## [3529] 10 2 11 2 11 11 2 2 2 10 2 2 11 10 1 3 7 2 10 10 2 10 2 2
## [3553] 2 2 2 10 2 2 10 2 2 2 1 2 1 2 2 1 10 2 2 2 10 10 1 11
## [3577] 2 2 2 2 2 2 2 11 2 1 2 12 11 3 7 10 10 2 2 2 10 2 2 1
## [3601] 2 1 7 2 7 10 2 2 2 11 2 2 2 10 2 7 7 2 2 2 2 2 2 2
## [3625] 11 1 2 11 2 2 2 2 2 2 2 11 10 3 2 2 2 10 10 10 10 3 2 2
## [3649] 2 7 10 2 10 11 10 12 10 1 2 10 2 10 2 10 1 7 2 11 2 10 10 10
## [3673] 7 2 3 2 2 10 2 10 10 2 10 2 10 2 10 10 4 2 2 2 2 2 10 10
## [3697] 10 2 2 10 10 10 10 2 10 2 2 2 1 10 2 10 2 10 11 10 2 10 10 1
## [3721] 10 2 2 10 11 1 10 10 10 1 10 4 11 1 2 2 2 2 10 1 2 2 2 2
## [3745] 10 10 2 11 11 10 2 2 2 10 2 10 2 2 2 2 2 10 2 12 10 2 2 2
## [3769] 2 2 2 2 10 1 10 10 2 2 2 11 2 2 2 3 2 1 10 10 1 1 10 1
## [3793] 1 2 10 12 2 2 7 10 2 2 2 11 10 11 10 2 2 10 2 2 10 1 10 1
## [3817] 2 11 2 2 10 11 10 10 2 1 10 2 11 10 2 10 7 2 11 11 10 2 10 10
## [3841] 10 11 2 10 10 10 10 2 1 2 10 12 10 10 3 2 3 10 7 2 10 2 10 7
## [3865] 11 2 2 2 10 10 11 10 2 1 10 10 10 10 11 11 2 2 2 2 1 2 10 11
## [3889] 2 11 10 1 1 10 11 7 7 11 10 10 10 2 11 2 2 10 10 2 10 2 2 10
## [3913] 11 2 10 3 11 10 2 1 2 10 2 10 1 11 2 2 2 10 2 2 2 2 2 2
## [3937] 2 10 1 2 11 2 10 2 2 10 11 11 10 1 1 11 3 2 10 2 2 1 10 10
## [3961] 10 7 2 2 2 2 2 11 1 10 5 2 12 2 2 3 2 2 2 10 7 11 2 2
## [3985] 11 10 1 2 11 10 2 2 11 2 10 2 2 10 2 10 2 2 2 2 10 2 2 2
## [4009] 10 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 10 2 2 10 2 7 11 2
## [4033] 2 10 2 2 2 2 10 2 10 10 2 10 1 2 11 10 2 2 2 10 3 2 10 11
## [4057] 2 2 2 1 2 3 2 2 11 2 2 10 2 1 2 10 2 11 10 10 10 2 10 2
## [4081] 1 2 2 2 2 2 2 2 11 2 2 2 2 10 11 2 2 2 2 2 2 10 2 2
## [4105] 2 1 2 2 2 2 2 2 2 2 2 2 11 2 2 11 2 2 2 10 1 10 2 2
## [4129] 11 2 10 11 10 10 2 10 2 2 11 1 11 11 2 7 1 2 1 2 10 2 12 2
## [4153] 7 1 10 10 10 10 2 2 4 1 2 10 10 3 2 2 2 2 2 10 3 2 2 10
## [4177] 10 10 10 2 2 2 10 7 2 10 2 7 1 1 11 1 2 2 2 2 10 2 2 10
## [4201] 2 1 10 10 10 10 10 2 2 10 2 11 1 2 10 2 10 10 10 10 10 2 10 1
## [4225] 1 2 2 7 2 7 10 11 2 10 2 1 10 2 10 2 2 2 10 10 10 10 11 10
## [4249] 2 10 11 2 11 11 2 10 11 10 10 2 10 1 2 2 10 10 11 1 10 10 10 2
## [4273] 10 4 10 2 11 1 10 2 2 10 10 2 2 2 1 2 2 10 2 2 2 2 2 11
## [4297] 1 6
##
## Within cluster sum of squares by cluster:
## [1] 2.5383447 3.4323340 3.9121518 7.3948538 4.0007985 0.0000000 2.5629165
## [8] 0.3643482 7.6489770 2.8416771 3.6110120 2.6703749
## (between_SS / total_SS = 99.5 %)
##
## Available components:
##
## [1] "cluster" "centers" "totss" "withinss" "tot.withinss"
## [6] "betweenss" "size" "iter" "ifault"
set.seed(123)
optimizacion3 <- clusGap(datos_cluster, FUN=kmeans, nstart=25, K.max = 15, B=50)
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: Quick-TRANSfer stage steps exceeded maximum (= 214900)
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: Quick-TRANSfer stage steps exceeded maximum (= 214900)
## Warning: did not converge in 10 iterations
## Warning: Quick-TRANSfer stage steps exceeded maximum (= 214900)
## Warning: Quick-TRANSfer stage steps exceeded maximum (= 214900)
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: Quick-TRANSfer stage steps exceeded maximum (= 214900)
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: Quick-TRANSfer stage steps exceeded maximum (= 214900)
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: Quick-TRANSfer stage steps exceeded maximum (= 214900)
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
## Warning: did not converge in 10 iterations
plot(optimizacion3, xlab="Número de clusters K", main="Optimización de clusters")
#El óptimo es el primer punto más alto
fviz_cluster(clusters3, data=datos_cluster)
clientes_cluster$cluster <- clusters3$cluster
head(clientes_cluster)
## # A tibble: 6 × 4
## Cliente Ticket_Promedio Frecuencia_Compra cluster
## <int> <dbl> <int> <int>
## 1 12346 77184. 1 9
## 2 12347 616. 7 1
## 3 12349 1758. 1 12
## 4 12350 334. 1 10
## 5 12352 313. 8 11
## 6 12353 89 1 2
clientes_cluster %>%
group_by(cluster) %>%
summarise(
Ticket_Promedio = mean(Ticket_Promedio),
Frecuencia_Compra = mean(Frecuencia_Compra),
Clientes = n()
)
## # A tibble: 12 × 4
## cluster Ticket_Promedio Frecuencia_Compra Clientes
## <int> <dbl> <dbl> <int>
## 1 1 602. 3.24 516
## 2 2 160. 2.38 1739
## 3 3 483. 33.5 62
## 4 4 4132. 14 21
## 5 5 338. 117. 6
## 6 6 490. 3500 1
## 7 7 1030. 3.81 181
## 8 8 14075. 3 2
## 9 9 80710. 1.5 2
## 10 10 352. 2.68 1322
## 11 11 321. 12.3 387
## 12 12 1888. 4.51 59
head(clientes_cluster)
## # A tibble: 6 × 4
## Cliente Ticket_Promedio Frecuencia_Compra cluster
## <int> <dbl> <int> <int>
## 1 12346 77184. 1 9
## 2 12347 616. 7 1
## 3 12349 1758. 1 12
## 4 12350 334. 1 10
## 5 12352 313. 8 11
## 6 12353 89 1 2
La técica de clustering permite identificar patrones o grupos naturales en los datos sin necesidad de etiquetas previas