gpt4 book ai didi

r - 向集群添加标签

转载 作者:行者123 更新时间:2023-12-03 23:16:24 24 4
gpt4 key购买 nike

我是 R 的新手,正在尝试根据行业对一些数据进行聚类。我了解到 K 均值无法处理因子和分类数据。我已经从我的数据集中删除了名为“工业”的因素——67 个不同的观察结果,但希望在模型完成后为每个观察结果分配一个标签。本质上,我希望我的最终结果看起来像样本美国犯罪数据集。任何帮助将不胜感激。

我的结果 enter image description here

我的理想结果集
enter image description here

代码:

library(tidyverse) # data manipulation
library(cluster) # clustering algorithms
library(factoextra) # clustering algorithms & visualization
library(ggplot2) ## used for plotting
library(gridExtra) ## used for plotting
library(robustbase)

###Read in dataset
df <- read.csv('my_data')
df2 <- scale(df)

### Subset of Data -- looking at percentage closed won and total opportunities
dat = df2[,c(1,3)]

# initial cluster split
k2 <- kmeans(dat, centers = 2, nstart = 25)
str(k2)
k2
fviz_cluster(k2, data = dat)

### Additional Plots
k3 <- kmeans(dat, centers = 3, nstart = 25)
k4 <- kmeans(dat, centers = 4, nstart = 25)
k5 <- kmeans(dat, centers = 5, nstart = 25)

# comparing plots
p1 <- fviz_cluster(k2, geom = "point", data = dat) + ggtitle("k = 2")
p2 <- fviz_cluster(k3, geom = "point", data = dat) + ggtitle("k = 3")
p3 <- fviz_cluster(k4, geom = "point", data = dat) + ggtitle("k = 4")
p4 <- fviz_cluster(k5, geom = "point", data = dat) + ggtitle("k = 5")

grid.arrange(p1, p2, p3, p4, nrow = 2)

## Computing gap statistics
set.seed(123)
gap_stat <- clusGap(df, FUN = kmeans, nstart = 25,
K.max = 10, B = 50)

## Visualization
fviz_gap_stat(gap_stat)

# Compute k-means clustering with k = 4
set.seed(123)
final <- kmeans(dat, 4, nstart = 25)
print(final)

## final visualization
fviz_cluster(final, data = dat)

最佳答案

我认为你需要做的就是:

rownames(df) <- df$Industry

然后缩放和子集。行业名称将出现在聚类图上,而不是行号。

关于r - 向集群添加标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50107157/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com