在 R 中使用混合变量进行聚类
Clustering with mix variables in R
我正在尝试使用连续变量和分类变量按照 kmeans 方法构建聚类模型。
目标是根据性别、年龄、职业、计费计划、单元phone和某些应用程序的使用情况创建集群
我正在为如何处理分类数据而苦苦挣扎,我知道我应该将它们变成假人,但不太确定如何同时处理所有分类变量。
谢谢
table 看起来像:
ID 性别年龄职业计划单元格phoneAmazon Prime GB DL Apple Music GB DL Audible DB DL
C001 NR 56 学生 Archaius SAMSUNG 0 0 0.498829165
C002 M 25 管理层 马拉维 华为 0 0 1
C003 H 32 Archaius Apple 教授 0 0 0.632005841
从分类变量创建虚拟变量的一种可能解决方案是“fastDummies”包:
library(fastDummies)
df <- data.frame(NR1 = c(1,2,3),
NR2 = c(0.1, 0.5, 0.7),
FA1 = factor(c("A","B","C")),
FA2 = factor(c("5","6","7")))
str(df)
'data.frame': 3 obs. of 4 variables:
$ NR1: num 1 2 3
$ NR2: num 0.1 0.5 0.7
$ FA1: Factor w/ 3 levels "A","B","C": 1 2 3
$ FA2: Factor w/ 3 levels "5","6","7": 1 2 3
# one variable per factor level
fastDummies::dummy_cols(df)
NR1 NR2 FA1 FA2 FA1_A FA1_B FA1_C FA2_5 FA2_6 FA2_7
1 1 0.1 A 5 1 0 0 1 0 0
2 2 0.5 B 6 0 1 0 0 1 0
3 3 0.7 C 7 0 0 1 0 0 1
# encoding where where there are n-1 columns per factor (as in case of all being 0 it implies the last is 1 already)
fastDummies::dummy_cols(df, remove_first_dummy = TRUE)
NR1 NR2 FA1 FA2 FA1_B FA1_C FA2_6 FA2_7
1 1 0.1 A 5 0 0 0 0
2 2 0.5 B 6 1 0 1 0
3 3 0.7 C 7 0 1 0 1
我正在尝试使用连续变量和分类变量按照 kmeans 方法构建聚类模型。
目标是根据性别、年龄、职业、计费计划、单元phone和某些应用程序的使用情况创建集群
我正在为如何处理分类数据而苦苦挣扎,我知道我应该将它们变成假人,但不太确定如何同时处理所有分类变量。
谢谢
table 看起来像:
ID 性别年龄职业计划单元格phoneAmazon Prime GB DL Apple Music GB DL Audible DB DL C001 NR 56 学生 Archaius SAMSUNG 0 0 0.498829165 C002 M 25 管理层 马拉维 华为 0 0 1 C003 H 32 Archaius Apple 教授 0 0 0.632005841
从分类变量创建虚拟变量的一种可能解决方案是“fastDummies”包:
library(fastDummies)
df <- data.frame(NR1 = c(1,2,3),
NR2 = c(0.1, 0.5, 0.7),
FA1 = factor(c("A","B","C")),
FA2 = factor(c("5","6","7")))
str(df)
'data.frame': 3 obs. of 4 variables:
$ NR1: num 1 2 3
$ NR2: num 0.1 0.5 0.7
$ FA1: Factor w/ 3 levels "A","B","C": 1 2 3
$ FA2: Factor w/ 3 levels "5","6","7": 1 2 3
# one variable per factor level
fastDummies::dummy_cols(df)
NR1 NR2 FA1 FA2 FA1_A FA1_B FA1_C FA2_5 FA2_6 FA2_7
1 1 0.1 A 5 1 0 0 1 0 0
2 2 0.5 B 6 0 1 0 0 1 0
3 3 0.7 C 7 0 0 1 0 0 1
# encoding where where there are n-1 columns per factor (as in case of all being 0 it implies the last is 1 already)
fastDummies::dummy_cols(df, remove_first_dummy = TRUE)
NR1 NR2 FA1 FA2 FA1_B FA1_C FA2_6 FA2_7
1 1 0.1 A 5 0 0 0 0
2 2 0.5 B 6 1 0 1 0
3 3 0.7 C 7 0 1 0 1