首页 > 编程语言 >简单易学的机器学习算法——谱聚类(Spectal Clustering)

简单易学的机器学习算法——谱聚类(Spectal Clustering)

时间:2023-06-28 15:36:12浏览次数:37  
标签:Clustering sum cluster num 聚类 clusters data centers Spectal


简单易学的机器学习算法——谱聚类(Spectal Clustering)_特征值

简单易学的机器学习算法——谱聚类(Spectal Clustering)_谱聚类_02


上述的“截”函数通常会将图分割成一个点和其余

个点。

4、其他的“截”函数的表现形式

简单易学的机器学习算法——谱聚类(Spectal Clustering)_ci_03


性质3的证明:



4、不同的Laplacian矩阵


    除了上述的拉普拉斯矩阵,还有规范化的 Laplacian 矩阵形式:



四、Laplacian矩阵与谱聚类中的优化函数的关系

1、由Laplacian矩阵到“截”函数

简单易学的机器学习算法——谱聚类(Spectal Clustering)_机器学习_04

简单易学的机器学习算法——谱聚类(Spectal Clustering)_机器学习_05


简单易学的机器学习算法——谱聚类(Spectal Clustering)_特征向量_06


简单易学的机器学习算法——谱聚类(Spectal Clustering)_特征值_07

简单易学的机器学习算法——谱聚类(Spectal Clustering)_特征值_08

七、实验代码

1、自己实现的一个


#coding:UTF-8
'''
Created on 2015年5月12日

@author: zhaozhiyong
'''
from __future__ import division
import scipy.io as scio
from scipy import sparse
from scipy.sparse.linalg.eigen import arpack#这里只能这么做,不然始终找不到函数eigs
from numpy import *


def spectalCluster(data, sigma, num_clusters):
    print "将邻接矩阵转换成相似矩阵"
    #先完成sigma != 0
    print "Fixed-sigma谱聚类"
    data = sparse.csc_matrix.multiply(data, data)

    data = -data / (2 * sigma * sigma)
    
    S = sparse.csc_matrix.expm1(data) + sparse.csc_matrix.multiply(sparse.csc_matrix.sign(data), sparse.csc_matrix.sign(data))   
    
    #转换成Laplacian矩阵
    print "将相似矩阵转换成Laplacian矩阵"
    D = S.sum(1)#相似矩阵是对称矩阵
    D = sqrt(1 / D)
    n = len(D)
    D = D.T
    D = sparse.spdiags(D, 0, n, n)
    L = D * S * D
    
    #求特征值和特征向量
    print "求特征值和特征向量"
    vals, vecs = arpack.eigs(L, k=num_clusters,tol=0,which="LM")  
    
    # 利用k-Means
    print "利用K-Means对特征向量聚类"
    #对vecs做正规化
    sq_sum = sqrt(multiply(vecs,vecs).sum(1))
    m_1, m_2 = shape(vecs)
    for i in xrange(m_1):
        for j in xrange(m_2):
            vecs[i,j] = vecs[i,j]/sq_sum[i]
    
    myCentroids, clustAssing = kMeans(vecs, num_clusters)
    
    for i in xrange(shape(clustAssing)[0]):
        print clustAssing[i,0]
    

def randCent(dataSet, k):
    n = shape(dataSet)[1]
    centroids = mat(zeros((k,n)))#create centroid mat
    for j in range(n):#create random cluster centers, within bounds of each dimension
        minJ = min(dataSet[:,j]) 
        rangeJ = float(max(dataSet[:,j]) - minJ)
        centroids[:,j] = mat(minJ + rangeJ * random.rand(k,1))
    return centroids

def distEclud(vecA, vecB):
    return sqrt(sum(power(vecA - vecB, 2))) #la.norm(vecA-vecB)

def kMeans(dataSet, k):
    m = shape(dataSet)[0]
    clusterAssment = mat(zeros((m,2)))#create mat to assign data points to a centroid, also holds SE of each point
    centroids = randCent(dataSet, k)
    clusterChanged = True
    while clusterChanged:
        clusterChanged = False
        for i in range(m):#for each data point assign it to the closest centroid
            minDist = inf; minIndex = -1
            for j in range(k):
                distJI = distEclud(centroids[j,:],dataSet[i,:])
                if distJI < minDist:
                    minDist = distJI; minIndex = j
            if clusterAssment[i,0] != minIndex: clusterChanged = True
            clusterAssment[i,:] = minIndex,minDist**2
        #print centroids
        for cent in range(k):#recalculate centroids
            ptsInClust = dataSet[nonzero(clusterAssment[:,0].A==cent)[0]]#get all the point in this cluster
            centroids[cent,:] = mean(ptsInClust, axis=0) #assign centroid to mean 
    return centroids, clusterAssment


if __name__ == '__main__':
    # 导入数据集
    matf = 'E://data_sc//corel_50_NN_sym_distance.mat'
    dataDic = scio.loadmat(matf)
    data = dataDic['A']
    # 谱聚类的过程
    spectalCluster(data, 20, 18)


2、网上提供的一个Matlab代码




function [cluster_labels evd_time kmeans_time total_time] = sc(A, sigma, num_clusters)
%SC Spectral clustering using a sparse similarity matrix (t-nearest-neighbor).
%
%   Input  : A              : N-by-N sparse distance matrix, where
%                             N is the number of data
%            sigma          : sigma value used in computing similarity,
%                             if 0, apply self-tunning technique
%            num_clusters   : number of clusters
%
%   Output : cluster_labels : N-by-1 vector containing cluster labels
%            evd_time       : running time for eigendecomposition
%            kmeans_time    : running time for k-means
%            total_time     : total running time

%
% Convert the sparse distance matrix to a sparse similarity matrix,
% where S = exp^(-(A^2 / 2*sigma^2)).
% Note: This step can be ignored if A is sparse similarity matrix.
%
disp('Converting distance matrix to similarity matrix...');
tic;
n = size(A, 1);

if (sigma == 0) % Selftuning spectral clustering
  % Find the count of nonzero for each column
  disp('Selftuning spectral clustering...');
  col_count = sum(A~=0, 1)';
  col_sum = sum(A, 1)';
  col_mean = col_sum ./ col_count;
  [x y val] = find(A);
  A = sparse(x, y, -val.*val./col_mean(x)./col_mean(y)./2);
  clear col_count col_sum col_mean x y val;
else % Fixed-sigma spectral clustering
  disp('Fixed-sigma spectral clustering...');
  A = A.*A;
  A = -A/(2*sigma*sigma);
end

% Do exp function sequentially because of memory limitation
num = 2000;
num_iter = ceil(n/num);
S = sparse([]);
for i = 1:num_iter
  start_index = 1 + (i-1)*num;
  end_index = min(i*num, n);
  S1 = spfun(@exp, A(:,start_index:end_index)); % sparse exponential func
  S = [S S1];
  clear S1;
end
clear A;
toc;

%
% Do laplacian, L = D^(-1/2) * S * D^(-1/2)
%
disp('Doing Laplacian...');
D = sum(S, 2) + (1e-10);
D = sqrt(1./D); % D^(-1/2)
D = spdiags(D, 0, n, n);
L = D * S * D;
clear D S;
time1 = toc;

%
% Do eigendecomposition, if L =
%   D^(-1/2) * S * D(-1/2)    : set 'LM' (Largest Magnitude), or
%   I - D^(-1/2) * S * D(-1/2): set 'SM' (Smallest Magnitude).
%
disp('Performing eigendecomposition...');
OPTS.disp = 0;
[V, val] = eigs(L, num_clusters, 'LM', OPTS);
time2 = toc;

%
% Do k-means
%
disp('Performing kmeans...');
% Normalize each row to be of unit length
sq_sum = sqrt(sum(V.*V, 2)) + 1e-20;
U = V ./ repmat(sq_sum, 1, num_clusters);
clear sq_sum V;
cluster_labels = k_means(U, [], num_clusters);
total_time = toc;

%
% Calculate and show time statistics
%
evd_time = time2 - time1
kmeans_time = total_time - time2
total_time
disp('Finished!');


function cluster_labels = k_means(data, centers, num_clusters)
%K_MEANS Euclidean k-means clustering algorithm.
%
%   Input    : data           : N-by-D data matrix, where N is the number of data,
%                               D is the number of dimensions
%              centers        : K-by-D matrix, where K is num_clusters, or
%                               'random', random initialization, or
%                               [], empty matrix, orthogonal initialization
%              num_clusters   : Number of clusters
%
%   Output   : cluster_labels : N-by-1 vector of cluster assignment
%
%   Reference: Dimitrios Zeimpekis, Efstratios Gallopoulos, 2006.
%              http://scgroup.hpclab.ceid.upatras.gr/scgroup/Projects/TMG/

%
% Parameter setting
%
iter = 0;
qold = inf;
threshold = 0.001;

%
% Check if with initial centers
%
if strcmp(centers, 'random')
  disp('Random initialization...');
  centers = random_init(data, num_clusters);
elseif isempty(centers)
  disp('Orthogonal initialization...');
  centers = orth_init(data, num_clusters);
end

%
% Double type is required for sparse matrix multiply
%
data = double(data);
centers = double(centers);

%
% Calculate the distance (square) between data and centers
%
n = size(data, 1);
x = sum(data.*data, 2)';
X = x(ones(num_clusters, 1), :);
y = sum(centers.*centers, 2);
Y = y(:, ones(n, 1));
P = X + Y - 2*centers*data';

%
% Main program
%
while 1
  iter = iter + 1;

  % Find the closest cluster for each data point
  [val, ind] = min(P, [], 1);
  % Sum up data points within each cluster
  P = sparse(ind, 1:n, 1, num_clusters, n);
  centers = P*data;
  % Size of each cluster, for cluster whose size is 0 we keep it empty
  cluster_size = P*ones(n, 1);
  % For empty clusters, initialize again
  zero_cluster = find(cluster_size==0);
  if length(zero_cluster) > 0
    disp('Zero centroid. Initialize again...');
    centers(zero_cluster, :)= random_init(data, length(zero_cluster));
    cluster_size(zero_cluster) = 1;
  end
  % Update centers
  centers = spdiags(1./cluster_size, 0, num_clusters, num_clusters)*centers;

  % Update distance (square) to new centers
  y = sum(centers.*centers, 2);
  Y = y(:, ones(n, 1));
  P = X + Y - 2*centers*data';

  % Calculate objective function value
  qnew = sum(sum(sparse(ind, 1:n, 1, size(P, 1), size(P, 2)).*P));
  mesg = sprintf('Iteration %d:\n\tQold=%g\t\tQnew=%g', iter, full(qold), full(qnew));
  disp(mesg);

  % Check if objective function value is less than/equal to threshold
  if threshold >= abs((qnew-qold)/qold)
    mesg = sprintf('\nkmeans converged!');
    disp(mesg);
    break;
  end
  qold = qnew;
end

cluster_labels = ind';


%-----------------------------------------------------------------------------
function init_centers = random_init(data, num_clusters)
%RANDOM_INIT Initialize centroids choosing num_clusters rows of data at random
%
%   Input : data         : N-by-D data matrix, where N is the number of data,
%                          D is the number of dimensions
%           num_clusters : Number of clusters
%
%   Output: init_centers : K-by-D matrix, where K is num_clusters
rand('twister', sum(100*clock));
init_centers = data(ceil(size(data, 1)*rand(1, num_clusters)), :);

function init_centers = orth_init(data, num_clusters)
%ORTH_INIT Initialize orthogonal centers for k-means clustering algorithm.
%
%   Input : data         : N-by-D data matrix, where N is the number of data,
%                          D is the number of dimensions
%           num_clusters : Number of clusters
%
%   Output: init_centers : K-by-D matrix, where K is num_clusters

%
% Find the num_clusters centers which are orthogonal to each other
%
Uniq = unique(data, 'rows'); % Avoid duplicate centers
num = size(Uniq, 1);
first = ceil(rand(1)*num); % Randomly select the first center
init_centers = zeros(num_clusters, size(data, 2)); % Storage for centers
init_centers(1, :) = Uniq(first, :);
Uniq(first, :) = [];
c = zeros(num-1, 1); % Accumalated orthogonal values to existing centers for non-centers
% Find the rest num_clusters-1 centers
for j = 2:num_clusters
  c = c + abs(Uniq*init_centers(j-1, :)');
  [minimum, i] = min(c); % Select the most orthogonal one as next center
  init_centers(j, :) = Uniq(i, :);
  Uniq(i, :) = [];
  c(i) = [];
end
clear c Uniq;


个人的一点认识:谱聚类的过程相当于先进行一个非线性的降维,然后在这样的低维空间中再利用聚类的方法进行聚类。



欢迎大家一起讨论,如有问题欢迎留言,欢迎大家转载。



参考

1、从拉普拉斯矩阵说到谱聚类

2、谱聚类(spectral clustering)

3、谱聚类算法(Spectral Clustering



标签:Clustering,sum,cluster,num,聚类,clusters,data,centers,Spectal
From: https://blog.51cto.com/u_16161414/6572360

相关文章

  • R语言K-Means(K均值聚类)和层次聚类算法对微博用户特征数据研究
    全文链接:https://tecdat.cn/?p=32955原文出处:拓端数据部落公众号本文就将采用K-means算法和层次聚类对基于用户特征的微博数据帮助客户进行聚类分析。首先对聚类分析作系统介绍。其次对聚类算法进行文献回顾,对其概况、基本思想、算法进行详细介绍,再是通过一个仿真实验具体来强化......
  • 机器学习.周志华《9 聚类》
    目录:聚类任务性能度量距离计算原型聚类密度聚类层次聚类方法聚类任务聚类:经典的无监督学习方法,无监督学习的目标是通过对无标记训练样本的学习,发掘和揭示数据集本身潜在的结构与规律,即不依赖于训练数据集的类标记信息。聚类则是试图将数据集的样本划分为若干个互不相交的类簇,从而每......
  • SPSS Modeler用K-means(K-均值)聚类、CHAID、CART决策树分析31省市土地利用情况和GDP数
    全文链接:http://tecdat.cn/?p=32840原文出处:拓端数据部落公众号随着经济的快速发展和城市化进程的不断推进,土地资源的利用和管理成为了一项极为重要的任务。而对于全国各省市而言,如何合理利用土地资源,通过科学的方法进行规划和管理,是提高土地利用效率的关键。本文旨在应用SPSS......
  • 聚类分析(文末送书)
    目录聚类分析是什么一、定义和数据类型聚类应用聚类分析方法的性能指标聚类分析中常用数据结构有数据矩阵和相异度矩阵聚类分析方法分类二、K-means聚类算法划分聚类方法对数据集进行聚类时包含三个要点K-Means算法流程:K-means聚类算法的特点三、k-medoids算法基本思想K-medoids......
  • MATLAB改进模糊C均值聚类FCM在电子商务信用评价应用:分析淘宝网店铺数据
    全文链接:http://tecdat.cn/?p=32794原文出处:拓端数据部落公众号近年来电子商务发展迅速,随之而来的信用问题给消费者带来诸多困扰,造成电子商务网上各种交易问题产生的原因是多方面的,但总的来说是缺乏有效的信用评价体系。目前各电子商务网站虽然都建立了信用评价体系,但是各网站提......
  • k均值聚类算法_异常数据检测
    k均值聚类_异常检测先来张图,快速理解正常数据应该分布在两个簇中异常数据,距离两个簇都很远fromsklearn.clusterimportKMeansfromscipy.spatial.distanceimportcdistimportnumpyasnpimportmatplotlib.pyplotaspltif__name__=='__main__':#正常......
  • R语言k-prototype聚类新能源汽车行业上市公司分析混合型数据集
    本文的研究目的是基于R语言的k-prototype算法,帮助客户对新能源汽车行业上市公司进行混合型数据集的聚类分析。通过对公司的财务数据、市场表现和发展战略等多个方面的变量进行聚类分析,我们可以将这些公司划分为不同的类别,并分析不同类别的特点和发展趋势。这样的研究结果对于投......
  • K-Means聚类分析-有标签
    模型亮点初始测试集上评分为0.51,调参后测试集上评分为0.75数据集由sklearn自带-----------------------------------------以下为模型具体实现-----------------------------------------Step1.数据读取fromsklearn.datasetsimportload_irisiris=load_iris()x=iris.d......
  • 0001. Kmeans聚类算法
    一、Kmeans原理Kmeans算法是一种常见的聚类算法,用于将数据集划分成k个不重叠的簇。其主要思想是通过迭代的方式将样本电话分到不同的簇中,使得同一簇内的样本点相似度较高,不同簇之间的相似度较低。Kmeans算法的详细步骤:初始化:选择k个初始聚类中心,可以是随机选择或者根据某种启......
  • R语言上市公司经营绩效实证研究 ——因子分析、聚类分析、正态性检验、信度检验
    全文链接:http://tecdat.cn/?p=32747原文出处:拓端数据部落公众号随着我国经济的快速发展,上市公司的经营绩效成为了一个备受关注的话题。本文旨在探讨上市公司经营绩效的相关因素,并运用数据处理、图示、检验和分析等方法进行深入研究,帮助客户对我国45家上市公司的16项财务指标进行......