使用谱聚类对看不见的点进行聚类

2024-03-11

我在用谱聚类 http://papers.nips.cc/paper/2092-on-spectral-clustering-analysis-and-an-algorithm方法对我的数据进行聚类。实施似乎工作正常。但是,我有一个问题 - 我有一组看不见的点(训练集中不存在),并且希望根据 k 均值导出的质心对这些点进行聚类(论文中的步骤 5)。然而,k 均值是在 k 个特征向量上计算的,因此质心是低维的。

有谁知道一种方法,可用于将看不见的点映射到低维并计算投影点与步骤 5 中 k 均值导出的质心之间的距离。


迟到的答案,但这是如何做到的R。我自己一直在寻找,但最终我自己编写了代码。

##Let's use kernlab for all kernel stuff
library(kernlab)

##Let's generate two concentric circles to cluster
r1 = 1 + .1*rnorm(250) #inner
r2 = 2 + .1*rnorm(250) #outer
q1 = 2*pi*runif(500) #random angle distribution
q2 = 2*pi*runif(500) #random angle distribution

##This is our data now
data = cbind(x = c(r1*cos(q1),r2*cos(q2)), y = c(r1*sin(q1),r2*sin(q2)))

##Let's take a sample to define train and test data
t = sample(1:nrow(data), 0.95*nrow(data))
train = data[t,]
test = data[-t,]

##This is our data
plot(train, pch = 1, col = adjustcolor("black", alpha = .5))
points(test, pch = 16)
legend("topleft", c("train data","test data"), pch = c(1,16), bg = "white")


##The paper gives great instructions on how to perform spectral clustering
##so I'll be following the steps
##Ng, A. Y., Jordan, M. I., & Weiss, Y. (2002). On spectral clustering: Analysis and an algorithm. Advances in neural information processing systems, 2, 849-856.
##Pg.2 http://papers.nips.cc/paper/2092-on-spectral-clustering-analysis-and-an-algorithm.pdf
#1. Form the affinity matrix
k = 2L #This is the number ofo clusters we will train
K = rbfdot(sigma = 300) #Our kernel
A = kernelMatrix(K, train) #Caution choosing your kernel product function, some have higher numerical imprecision
diag(A) = 0
#2. Define the diagonal matrix D and the laplacean matrix L
D = diag(rowSums(A))
L = diag(1/sqrt(diag(D))) %*% A %*% diag(1/sqrt(diag(D)))
#3. Find the eigenvectors of L
X = eigen(L, symmetric = TRUE)$vectors[,1:k]
#4. Form Y from X
Y = X/sqrt(rowSums(X^2))
#5. Cluster (k-means)
kM = kmeans(Y, centers = k, iter.max = 100L, nstart = 1000L)
#6. This is the cluster assignment of the original data
cl = fitted(kM, "classes")
##Projection on eigen vectors, see the ranges, it shows how there's a single preferential direction
plot(jitter(Y, .1), ylab = "2nd eigenfunction", xlab = "1st eigenfunction", col = adjustcolor(rainbow(3)[2*cl-1], alpha = .5))

##LET'S TRY TEST DATA NOW
B = kernelMatrix(K, test, train) #The kernel product between train and test data

##We project on the learned eigenfunctions
f = tcrossprod(B, t(Y))
#This part is described in Bengio, Y., Vincent, P., Paiement, J. F., Delalleau, O., Ouimet, M., & Le Roux, N. (2003). Spectral clustering and kernel PCA are learning eigenfunctions (Vol. 1239). CIRANO.
#Pg.12 http://www.cirano.qc.ca/pdf/publication/2003s-19.pdf

##And assign clusters based on the centers in that space
new.cl = apply(as.matrix(f), 1, function(x) { which.max(tcrossprod(x,kM$centers)) } ) #This computes the distance to the k-means centers on the transformed space

##And here's our result
plot(train, pch = 1, col = adjustcolor(rainbow(3)[2*cl-1], alpha = .5))
points(test, pch = 16, col = rainbow(3)[2*new.cl-1])
legend("topleft", c("train data","test data"), pch = c(1,16), bg = "white")

输出图片

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用谱聚类对看不见的点进行聚类 的相关文章

  • 如何在Python中绘制k距离图

    如何在 DBSCAN 中绘制 在 python 中 给定最小点值的距离图 我正在寻找拐点和相应的 epsilon 值 在 sklearn 中 我没有看到任何返回此类距离的方法 我错过了什么吗 您可能想使用 numpy 提供的矩阵运算来加速距
  • 对一维数据进行最佳聚类? [关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 有没有人有一篇论文解释如何CKmeans 1d dp http cran r project org
  • Rand Index函数(聚类性能评估)

    据我所知 python 中没有可用于 Rand Index 的软件包 而对于调整后的 Rand Index 您可以选择使用sklearn metrics adjusted rand score labels true labels pred
  • K 均值可用于帮助基于像素值的图像分离吗?

    我正在尝试根据像素值分离灰度图像 假设一个 bin 中的像素为 0 到 60 另一个 bin 中的像素为 60 120 120 180 依此类推 直到 255 范围在此大致等距案件 然而 通过使用 K 均值聚类是否可以更实际地测量我的像素值
  • 删除加权有向图中的循环

    这是我其他帖子的后续问题 具有大小约束的聚类算法 https stackoverflow com questions 30112428 algorithm for clustering with size constraints 我正在研究
  • Python:3 维空间中的 DBSCAN

    我一直在寻找 3 维点的 DBSCAN 实现 但运气不佳 有谁知道我的图书馆可以处理这个问题或者有这方面的经验吗 我假设 DBSCAN 算法可以处理 3 个维度 通过将 e 值设置为半径度量并通过欧几里德分离测量点之间的距离 如果有人尝试过
  • R 中的 PCA 多重图

    我有一个如下所示的数据集 India China Brasil Russia SAfrica Kenya States Indonesia States Argentina Chile Netherlands HongKong 0 0854
  • R 聚类-带有观察标签的轮廓

    我用以下方法进行层次聚类clusterR 中的包 使用silhouette函数 我可以获得树状图中任何给定高度 h 截止点的簇输出的轮廓图 run hierarchical clustering if require cluster ins
  • DBSCAN 算法可以创建少于 minPts 的簇吗?

    我刚刚编写了 DBSCAN 算法 我想知道 DBSCAN 算法是否可以允许集群中的点数少于所使用的 minPts 参数 我一直在使用http people cs nctu edu tw rsliang dbscan testdatagen
  • 使用 igraph 在 R 中进行 Louvain 社区检测 - 分配交替的组成员资格分配

    我一直在使用 igraph 在 R 中运行 Louvain 社区检测 感谢我之前查询的这个答案 https stackoverflow com questions 49834827 louvain community detection i
  • 计算 3D 平面的 Voronoi 图

    是否有代码 库可以计算 3D 平面 平行四边形 的 Voronoi 图 我检查了 Qhull 它似乎只能处理点 在它的示例中 Voro 可以处理不同大小的球体 但我找不到任何多边形 在这张图片中 3d 中的样本平面 https i stac
  • 如何总结组合列表

    我有一个包含 2 个元素组合的列表 如下所示 cbnl lt list c A B c B A c C D c E D c F G c H I c J K c I H c K J c G F c D C c E C c D E c C E
  • 如何从一个巨大的矩阵中获得最大可能的列序列和最少可能的行NA?

    我想从数据框中选择列 以便得到结果连续的列序列尽可能长 而带有 NA 的行数尽可能少 因为之后必须删除它们 我想这样做的原因是 我想运行TraMineR seqsubm 自动获取转移成本矩阵 按转移概率 并稍后运行cluster agnes
  • python scikit-learn 缺失数据聚类

    我想对缺少列的数据进行聚类 手动执行此操作 我将在没有此列的情况下计算缺少列的距离 使用 scikit learn 不可能出现丢失数据的情况 也没有机会指定用户距离函数 是否有机会对缺失数据进行聚类 示例数据 n samples 1500
  • 循环系统发育树上的节点标签

    我正在尝试创建循环系统发育树 我有这部分代码 fit lt hclust dist Data 4 method complete members NULL nclus 3 color c red blue green color list
  • 如何在 scipy 层次聚类中获取非单例簇 ID

    根据this http docs scipy org doc scipy reference generated scipy cluster hierarchy dendrogram html scipy cluster hierarchy
  • 如何在Python中的二值图像上使用kmeans聚类?

    我试图对两个不同的人采取二元面具 其他一切都是黑色的 现在我想使用将每个人分组到他们自己的集群中K means这样我最终就可以在它们周围绘制边界框 这是我到目前为止的代码 def kmeans img k values range 1 5
  • Apache Spark MLLib - 使用 IDF-TF 向量运行 KMeans - Java 堆空间

    我正在尝试从 大 文本文档集合 TF IDF 向量 在 MLLib 上运行 KMeans 文档通过 Lucene 英语分析器发送 稀疏向量由 HashingTF transform 函数创建 无论我使用的并行程度如何 通过合并函数 KMea
  • 在 mahout-0.6 上运行“Mahout in Action”中的示例代码时出现 IOException

    我正在学习 Mahout 并阅读 Mahout in Action 当我尝试运行第 7 章 Simple KMeans Clustering java 中的示例代码时 弹出了一个异常 线程 main 中的异常 java io IOExcep
  • 估计/选择 DBSCAN 的最佳超参数

    我需要根据不同介词的分布找到自然出现的名词类别 如施事 工具 时间 地点等 我尝试使用 k means 聚类 但帮助较少 效果不佳 我正在寻找的类有很多重叠 可能是因为类的非球状形状和 k means 中的随机初始化 我现在正在使用 DBS

随机推荐