【发布时间】:2019-11-03 02:49:46
【问题描述】:
我的集群脚本没有返回我想要的结果,我试图弄清楚为什么会这样,或者我是否应该使用不同的代码命令。我正在尝试使用细胞核的 x 和 y 坐标进行 k 均值聚类分析,每个聚类使用不同的颜色。不幸的是,我的代码返回的是一个到处都是颜色的图,而不是一个非常紧凑的图,在一个区域中包含一种颜色的集群。
我尝试过不同的选项,例如按比例标准化,但基本上没有什么明显的变化。
df = read.csv("C:/Users/chelsie/Desktop/Results.csv")
df = df[,-1]
###################### Elbow Method
library(factoextra)
library(NbClust)
fviz_nbclust(df, kmeans, method = "wss") +
geom_vline(xintercept = 4, linetype = 2)+
labs(subtitle = "Elbow method")
###################### Clustering Script
set.seed(20)
k = 5
clusters <- kmeans(df[,1], k)
# Save the cluster number in the dataset as column 'Borough'
df$clusterId <- as.factor(clusters$cluster)
#plotcluster(df[,1], df$clusterId)
library(ggplot2)
library(ggthemes)
library(ggplot2)
library(ggthemes)
ggplot(df, aes(x = X, y = Y,color = clusterId)) +
geom_point()+ theme_economist() +
scale_color_economist()
我希望结果类似于this 网站上的彩色图表。但是,我的集群图片非常错误,我不知道为什么。我输入的数据点是x和y,看起来像this
【问题讨论】:
-
当你这样做时: clusters
-
1) 您显示的数据对我来说似乎没有 5 个集群。 2) 不知道
df的样子...clusters <- kmeans(df[,1], k)看起来是在输入一列数字 - 而不是 x 和 y。 -
你有没有可能在策划一个不同的
clusterId?因为它应该有 k=5 个级别,但它没有。怪 R,它有一个愚蠢的语法。尝试rm(clusterId)并重新启动您的环境。
标签: r machine-learning cluster-analysis