- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试为机器学习中的 k 均值方法找到合适的 k 簇。我用的是Elbow法,但是耗时且复杂度高。谁能告诉我另一种方法来代替它。非常感谢
最佳答案
可用于评估聚类结果的指标是 silhouette coefficient 。这个值主要计算:
silhouette coefficient = 1 - (intra-cluster cohesion) / (inter-cluster separation)
值的范围是 -1 到 +1,但通常您希望值接近 1.0。因此,如果您运行聚类算法(例如 k-means 或分层聚类)来生成 3 个聚类,则可以调用 Silhouette 库来计算 Silhouette 系数值,例如0.50。如果您再次运行算法以生成 4 个簇,您可以计算另一个轮廓系数值,例如0.55。然后您可以得出结论,4 个聚类是更好的聚类,因为它具有更高的轮廓系数。
下面是一个示例数据集,我使用 R 在二维空间中创建了三个不同的集群。注意:由于集群之间存在如此明显的分离,真实世界的数据永远不会看起来如此干净。即使像 Fisher 的 Iris 数据集这样的简单数据,标记簇之间也存在重叠。
然后您可以使用 R 的轮廓库来计算轮廓系数。 (更多信息可以找到 at the STHDA website 。)下面是轮廓信息图。您想要的一个指标位于左下角,显示“平均轮廓宽度:xxx”。该值是所有水平条的平均值。
这是 K=2 个簇的轮廓系数。
plot(silhouette(kmeans(df, centers=2)$cluster, dist(df)))
这是 K=3 簇的轮廓系数。
plot(silhouette(kmeans(df, centers=3)$cluster, dist(df)))
这是 K=4 个簇的轮廓系数。
plot(silhouette(kmeans(df, centers=4)$cluster, dist(df)))
从轮廓系数来看,您可以得出结论,K=3 个聚类是最好的聚类,因为它具有最高的轮廓系数。
您只需扫描多个候选 K 值(例如 2 到 10 之间),同时跟踪找到的最高轮廓系数,即可以编程方式找到最佳 K 值。下面我已经做到了这一点,同时还构建了轮廓系数(y 轴)与 K(x 轴)的关系图。输出显示:
Best Silhouette coefficient=0.888926 occurred at k=3
library(cluster) # for silhouette
library(ggplot2) # for ggplot
library(scales) # for pretty_breaks
# Create sample 2-D data set with clusters around the points (1,1), (2,4), and (3,1)
x<- c(rnorm(n=25, mean=1,sd=.1), rnorm(n=25,mean=2,sd=.1),rnorm(n=25,mean=3,sd=.2))
y<- c(rnorm(n=25, mean=1,sd=.1), rnorm(n=25,mean=4,sd=.1),rnorm(n=25,mean=1,sd=.2))
df <- data.frame(x=x, y=y)
xMax <- max(x)
yMax <- max(y)
print(ggplot(df, aes(x,y)) + geom_point() + xlim(0, max(xMax, yMax)) + ylim(0, max(xMax,yMax)))
# Use the Iris data set.
#df <- subset(iris, select=-c(Species))
#df <- scale(df)
# Run through multiple candidate values of K clusters.
xValues <- c() # Holds the kvalues (x-axis)
yValues <- c() # Holds the silhouette coefficient values (y-axis)
bestKValue <- 0
bestSilhouetteCoefficient <- 0
kSequence <- seq(2, 5)
for (kValue in kSequence) {
xValues <- append(xValues, kValue)
kmeansResult <- kmeans(df, centers=kValue, nstart=5)
silhouetteResult <- silhouette(kmeansResult$cluster, dist(df))
silhouetteCoefficient <- mean(silhouetteResult[,3])
yValues <- append(yValues, silhouetteCoefficient)
if (silhouetteCoefficient > bestSilhouetteCoefficient) {
bestSilhouetteCoefficient <- silhouetteCoefficient
bestKValue <- kValue
}
}
# Create a dataframe for ggplot to plot the accumulated silhouette values.
dfSilhouette <- data.frame(k=xValues, silhouetteCoefficient=yValues)
# Create the ggplot line plot for silhouette coefficient.
silhouettePlot<- ggplot(data=dfSilhouette, aes(k)) +
geom_line(aes(y=silhouetteCoefficient)) +
xlab("k") +
ylab("Average silhouette width") +
ggtitle("Average silhouette width") +
scale_x_continuous(breaks=pretty_breaks(n=20))
print(silhouettePlot)
printf <- function(...) cat(sprintf(...))
printf("Best Silhouette coefficient=%f occurred at k=%d", bestSilhouetteCoefficient, bestKValue )
请注意,我使用了答案 here 中的 printf 函数.
与您相关的问题是 here .
关于machine-learning - 在查找 k 簇方面比 Elbow 更有用的另一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37767298/
我对构面有疑问,并根据构面进行了一些过滤。 我知道这是一个重复的问题,但我找不到答案。 我想知道如何在 flex 搜索中实现相同的功能。 假设我有一个有关汽车和某些方面的索引-例如模型和 颜色。 颜色
我正在尝试找到一种解决方案来为某些方面创建子方面列表。 我有一些产品的衣服尺码,它们存储在 solr 中 "Size_both":"W30L30","尺寸宽度":"W30","Size_length"
我正在尝试找到一种解决方案来为某些方面创建子方面列表。 我有一些产品的衣服尺码,它们存储在 solr 中 "Size_both":"W30L30","尺寸宽度":"W30","Size_length"
我对方面有疑问。他们不开火。我有小方面: @Aspect @Component public class SynchronizingAspect { @Pointcut("execution(
这是在 ruby 中启用散列自动生成的巧妙技巧(取自 facets): # File lib/core/facets/hash/autonew.rb, line 19 def self.a
这个问题在这里已经有了答案: 8年前关闭。 Possible Duplicate: Creating a facet_wrap plot with ggplot2 with different ann
XMLHttpRequest 能否从 http://mydomain.example/ 向 http://mydomain.example:81/ 发送请求? 最佳答案 要使两个文档被视为具有相同的来
我对 Elasticsearch 中的方面有一点问题。 我有一个表格视频,一个表格 channel ,一个 channel 有很多视频。 我只想在 X 个最新视频上显示每个 channel 的 %vi
假设我正在为 4 个人绘制数据图表:Alice、Bob、Chuck 和 Dana。我正在使用 ggplot2 制作一个多面图,每个人一个方面。我的磁盘上还有 4 张图像:Alice.png、Bob.p
我已经下载了收件箱,并且正在使用Pig和Hadoop处理电子邮件。我已经使用Pig和Wonderdog在ElasticSearch中为这些电子邮件编制了索引。 现在,我为收件箱中的每个电子邮件地址创建
我有一个模块如下: define([...], function(...){ function anothermethod() {...} function request() {....}
(defprotocol IAnimal "IAnimal" (report [o] (println (type o) " reporting.\n") (inner-repor
我有一个 Bean 需要向 InfluxDB 报告。数据库在表 INFLUX_DB_SERVER 中注册了 InfluxDB。如果你看一下代码,你会发现方法reportMemory做了很多工作,它构造
我的问题与分面有关。在下面的示例代码中,我查看了一些分面散点图,然后尝试在每个分面的基础上叠加信息(在本例中为平均线)。 tl;dr 版本是我的尝试失败了。要么我添加的平均线计算所有数据(不尊重方面变
假设我正在为 4 个人绘制数据图表:Alice、Bob、Chuck 和 Dana。我正在使用 ggplot2 制作一个多面图,每个人一个方面。我的磁盘上还有 4 张图像:Alice.png、Bob.p
尝试用两个方面包装服务类来获取此调用链: javanica..HystrixCommandAspect -> MyCustomAroundAspect -> MyService 遇到两个问题: Hys
我是 AspectJ 的初学者。我用它在我的网络驱动程序中截取屏幕截图。以下是我的包结构。 我想知道如何在 Browser 类中运行我的程序,以便它使用 Screenshots 类中定义的 Aspec
我在使用 spring aop 时遇到问题 (编辑:如果我的方法不是静态的,则代码可以正常工作) 我的包中有这个结构: aaa.bbb.ccc.Clase1.java aaa.bbb.ddd.Clas
我有一个通用存储库类,其中包含各种标记有 PostSharp 方面 (SecuredOperation) 的方法... public class Repository : IRepository, I
我有一个运行多线程的 Hibernate 事务方法“doImportImpl”。而某些记录需要依次导入,所以代码结构大致是这样的: public RecordResult doImportImpl(S
我是一名优秀的程序员,十分优秀!