cluster-analysis - 使用光谱聚类对看不见的点进行聚类

标签 cluster-analysis k-means

我正在使用 Spectral Clustering聚类我的数据的方法。实现似乎工作正常。但是,我有一个问题 - 我有一组看不见的点(训练集中不存在),并且想根据 k-means 得出的质心对这些点进行聚类(论文中的第 5 步)。然而,k-means 是在 k 个特征向量上计算的,因此质心是低维的。

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

最佳答案

迟到的答案,但这是在 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")

输出图片

Train data

Projection

Result

关于cluster-analysis - 使用光谱聚类对看不见的点进行聚类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31074113/

相关文章:

hadoop - 在mahout中使用clusterdump时出错

r - 使用 R 进行 K 中心聚类

linux - 如何使用 Mahout kmeans 算法将集群转储到本地文件系统

java - 如何计算重构误差?

python - 聚类问题

python - Python scikit-learn 每次运行后聚类结果的变化

algorithm - 基于关系权重对对象进行聚类的聚类算法

matlab - 在聚类 Matlab 之前从一组 3d 点中删除异常值

python - 在 KMeans 聚类(scikit 学习)之后查找聚类的长度(与聚类相关联的点数)

python - 使用无监督机器学习聚类图像