
簡(jiǎn)單易學(xué)的機(jī)器學(xué)習(xí)算法—譜聚類(Spectal Clustering)
一、復(fù)雜網(wǎng)絡(luò)中的一些基本概念
1、復(fù)雜網(wǎng)絡(luò)的表示
在復(fù)雜網(wǎng)絡(luò)的表示中,復(fù)雜網(wǎng)絡(luò)可以建模成一個(gè)圖,其中,V表示網(wǎng)絡(luò)中的節(jié)點(diǎn)的集合,E表示的是連接的集合。在復(fù)雜網(wǎng)絡(luò)中,復(fù)雜網(wǎng)絡(luò)可以是無(wú)向圖、有向圖、加權(quán)圖或者超圖。
2、網(wǎng)絡(luò)簇結(jié)構(gòu)
網(wǎng)絡(luò)簇結(jié)構(gòu)(network cluster structure)也稱為網(wǎng)絡(luò)社團(tuán)結(jié)構(gòu)(network community structure),是復(fù)雜網(wǎng)絡(luò)中最普遍和最重要的拓?fù)鋵傩灾?。網(wǎng)絡(luò)簇是整個(gè)網(wǎng)絡(luò)中的稠密連接分支,具有同簇內(nèi)部節(jié)點(diǎn)之間相互連接密集,不同簇的節(jié)點(diǎn)之間相互連接稀疏的特征。
3、復(fù)雜網(wǎng)絡(luò)的分類
復(fù)雜網(wǎng)絡(luò)主要分為:隨機(jī)網(wǎng)絡(luò),小世界網(wǎng)絡(luò)和無(wú)標(biāo)度網(wǎng)絡(luò)。
二、譜方法介紹
1、譜方法的思想
在譜聚類中定義了“截”函數(shù)的概念,當(dāng)一個(gè)網(wǎng)絡(luò)被劃分成為兩個(gè)子網(wǎng)絡(luò)時(shí),“截”即指子網(wǎng)間的連接密度。譜聚類的目的就是要找到一種合理的分割,使得分割后形成若干子圖,連接不同的子圖的邊的權(quán)重盡可能低,即“截”最小,同子圖內(nèi)的邊的權(quán)重盡可能高。
2、“截”函數(shù)的具體表現(xiàn)形式
“截”表示的是子網(wǎng)間的密度,即邊比較少。以二分為例,將圖聚類成兩個(gè)類:S類和T類。假設(shè)用來(lái)表示圖的劃分,我們需要的結(jié)果為:
其中表示的是類別S和T之間的權(quán)重。對(duì)于K個(gè)不同的類別
,優(yōu)化的目標(biāo)為:
3、基本“截”函數(shù)的弊端
對(duì)于上述的“截”函數(shù),最終會(huì)導(dǎo)致不好的分割,如二分類問(wèn)題:
上述的“截”函數(shù)通常會(huì)將圖分割成一個(gè)點(diǎn)和其余n-1個(gè)點(diǎn)。
4、其他的“截”函數(shù)的表現(xiàn)形式
其中表示A類中包含的頂點(diǎn)的數(shù)目
三、Laplacian矩陣
1、Laplacian矩陣的定義
其中,d為圖的度矩陣,a為圖的鄰接矩陣。
2、度矩陣的定義
對(duì)于一個(gè)有n個(gè)頂點(diǎn)的圖,其鄰接矩陣為:
其中。
3、Laplacian矩陣的性質(zhì)
Laplacian矩陣;L是對(duì)稱半正定矩陣;
性質(zhì)3的證明:
4、不同的Laplacian矩陣
四、Laplacian矩陣與譜聚類中的優(yōu)化函數(shù)的關(guān)系
1、由Laplacian矩陣到“截”函數(shù)
對(duì)于二個(gè)類別的聚類問(wèn)題,優(yōu)化的目標(biāo)函數(shù)為:
其中,表示的是頂點(diǎn)的數(shù)目,對(duì)于確定的圖來(lái)說(shuō)是個(gè)常數(shù)。由上述的推導(dǎo)可知,由
推導(dǎo)出了
,由此可知:Laplacian矩陣與有優(yōu)化的目標(biāo)函數(shù)
之間存在密切的聯(lián)系。
2、新的目標(biāo)函數(shù)
由上式可得:
其中
3、轉(zhuǎn)化到Laplacian矩陣的求解
五、從二類別聚類到多類別聚類1、二類別聚類
2、多類別聚類
基于以上的分析,譜聚類的基本過(guò)程為:
對(duì)于給定的圖,求圖的度矩陣d和鄰接矩陣a;
計(jì)算圖的Laplacian矩陣;
對(duì)Laplacian矩陣進(jìn)行特征值分解,取其前k個(gè)特征值對(duì)應(yīng)的特征向量,構(gòu)成的特征向量矩陣;
利用K-Means聚類算法對(duì)上述的的特征向量矩陣進(jìn)行聚類,每一行代表一個(gè)樣本點(diǎn)。
2、利用相似度矩陣的構(gòu)造方法
注意:在第一種方法中,求解的是Laplacian矩陣的前個(gè)最小特征值對(duì)應(yīng)的特征向量,在第二種方法中,求解的是Laplacian矩陣的前K個(gè)最大特征值對(duì)應(yīng)的特征向量
七、實(shí)驗(yàn)代碼
1、自己實(shí)現(xiàn)的一個(gè)
[python] view plain copy 在CODE上查看代碼片派生到我的代碼片
#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#這里只能這么做,不然始終找不到函數(shù)eigs
from numpy import *
def spectalCluster(data, sigma, num_clusters):
print "將鄰接矩陣轉(zhuǎn)換成相似矩陣"
#先完成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))
#轉(zhuǎn)換成Laplacian矩陣
print "將相似矩陣轉(zhuǎn)換成Laplacian矩陣"
D = S.sum(1)#相似矩陣是對(duì)稱矩陣
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對(duì)特征向量聚類"
#對(duì)vecs做正規(guī)化
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__':
# 導(dǎo)入數(shù)據(jù)集
matf = 'E://data_sc//corel_50_NN_sym_distance.mat'
dataDic = scio.loadmat(matf)
data = dataDic['A']
# 譜聚類的過(guò)程
spectalCluster(data, 20, 18)
2、網(wǎng)上提供的一個(gè)Matlab代碼
[plain] view plain copy 在CODE上查看代碼片派生到我的代碼片
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!');
[plain] view plain copy 在CODE上查看代碼片派生到我的代碼片
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;
個(gè)人的一點(diǎn)認(rèn)識(shí):譜聚類的過(guò)程相當(dāng)于先進(jìn)行一個(gè)非線性的降維,然后在這樣的低維空間中再利用聚類的方法進(jìn)行聚類。
數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼
若不方便掃碼,搜微信號(hào):CDAshujufenxi
LSTM 模型輸入長(zhǎng)度選擇技巧:提升序列建模效能的關(guān)鍵? 在循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)家族中,長(zhǎng)短期記憶網(wǎng)絡(luò)(LSTM)憑借其解決長(zhǎng)序列 ...
2025-07-11CDA 數(shù)據(jù)分析師報(bào)考條件詳解與準(zhǔn)備指南? ? 在數(shù)據(jù)驅(qū)動(dòng)決策的時(shí)代浪潮下,CDA 數(shù)據(jù)分析師認(rèn)證愈發(fā)受到矚目,成為眾多有志投身數(shù) ...
2025-07-11數(shù)據(jù)透視表中兩列相乘合計(jì)的實(shí)用指南? 在數(shù)據(jù)分析的日常工作中,數(shù)據(jù)透視表憑借其強(qiáng)大的數(shù)據(jù)匯總和分析功能,成為了 Excel 用戶 ...
2025-07-11尊敬的考生: 您好! 我們誠(chéng)摯通知您,CDA Level I和 Level II考試大綱將于 2025年7月25日 實(shí)施重大更新。 此次更新旨在確保認(rèn) ...
2025-07-10BI 大數(shù)據(jù)分析師:連接數(shù)據(jù)與業(yè)務(wù)的價(jià)值轉(zhuǎn)化者? ? 在大數(shù)據(jù)與商業(yè)智能(Business Intelligence,簡(jiǎn)稱 BI)深度融合的時(shí)代,BI ...
2025-07-10SQL 在預(yù)測(cè)分析中的應(yīng)用:從數(shù)據(jù)查詢到趨勢(shì)預(yù)判? ? 在數(shù)據(jù)驅(qū)動(dòng)決策的時(shí)代,預(yù)測(cè)分析作為挖掘數(shù)據(jù)潛在價(jià)值的核心手段,正被廣泛 ...
2025-07-10數(shù)據(jù)查詢結(jié)束后:分析師的收尾工作與價(jià)值深化? ? 在數(shù)據(jù)分析的全流程中,“query end”(查詢結(jié)束)并非工作的終點(diǎn),而是將數(shù) ...
2025-07-10CDA 數(shù)據(jù)分析師考試:從報(bào)考到取證的全攻略? 在數(shù)字經(jīng)濟(jì)蓬勃發(fā)展的今天,數(shù)據(jù)分析師已成為各行業(yè)爭(zhēng)搶的核心人才,而 CDA(Certi ...
2025-07-09【CDA干貨】單樣本趨勢(shì)性檢驗(yàn):捕捉數(shù)據(jù)背后的時(shí)間軌跡? 在數(shù)據(jù)分析的版圖中,單樣本趨勢(shì)性檢驗(yàn)如同一位耐心的偵探,專注于從單 ...
2025-07-09year_month數(shù)據(jù)類型:時(shí)間維度的精準(zhǔn)切片? ? 在數(shù)據(jù)的世界里,時(shí)間是最不可或缺的維度之一,而year_month數(shù)據(jù)類型就像一把精準(zhǔn) ...
2025-07-09CDA 備考干貨:Python 在數(shù)據(jù)分析中的核心應(yīng)用與實(shí)戰(zhàn)技巧? ? 在 CDA 數(shù)據(jù)分析師認(rèn)證考試中,Python 作為數(shù)據(jù)處理與分析的核心 ...
2025-07-08SPSS 中的 Mann-Kendall 檢驗(yàn):數(shù)據(jù)趨勢(shì)與突變分析的有力工具? ? ? 在數(shù)據(jù)分析的廣袤領(lǐng)域中,準(zhǔn)確捕捉數(shù)據(jù)的趨勢(shì)變化以及識(shí)別 ...
2025-07-08備戰(zhàn) CDA 數(shù)據(jù)分析師考試:需要多久?如何規(guī)劃? CDA(Certified Data Analyst)數(shù)據(jù)分析師認(rèn)證作為國(guó)內(nèi)權(quán)威的數(shù)據(jù)分析能力認(rèn)證 ...
2025-07-08LSTM 輸出不確定的成因、影響與應(yīng)對(duì)策略? 長(zhǎng)短期記憶網(wǎng)絡(luò)(LSTM)作為循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)的一種變體,憑借獨(dú)特的門控機(jī)制,在 ...
2025-07-07統(tǒng)計(jì)學(xué)方法在市場(chǎng)調(diào)研數(shù)據(jù)中的深度應(yīng)用? 市場(chǎng)調(diào)研是企業(yè)洞察市場(chǎng)動(dòng)態(tài)、了解消費(fèi)者需求的重要途徑,而統(tǒng)計(jì)學(xué)方法則是市場(chǎng)調(diào)研數(shù) ...
2025-07-07CDA數(shù)據(jù)分析師證書(shū)考試全攻略? 在數(shù)字化浪潮席卷全球的當(dāng)下,數(shù)據(jù)已成為企業(yè)決策、行業(yè)發(fā)展的核心驅(qū)動(dòng)力,數(shù)據(jù)分析師也因此成為 ...
2025-07-07剖析 CDA 數(shù)據(jù)分析師考試題型:解鎖高效備考與答題策略? CDA(Certified Data Analyst)數(shù)據(jù)分析師考試作為衡量數(shù)據(jù)專業(yè)能力的 ...
2025-07-04SQL Server 字符串截取轉(zhuǎn)日期:解鎖數(shù)據(jù)處理的關(guān)鍵技能? 在數(shù)據(jù)處理與分析工作中,數(shù)據(jù)格式的規(guī)范性是保證后續(xù)分析準(zhǔn)確性的基礎(chǔ) ...
2025-07-04CDA 數(shù)據(jù)分析師視角:從數(shù)據(jù)迷霧中探尋商業(yè)真相? 在數(shù)字化浪潮席卷全球的今天,數(shù)據(jù)已成為企業(yè)決策的核心驅(qū)動(dòng)力,CDA(Certifie ...
2025-07-04CDA 數(shù)據(jù)分析師:開(kāi)啟數(shù)據(jù)職業(yè)發(fā)展新征程? ? 在數(shù)據(jù)成為核心生產(chǎn)要素的今天,數(shù)據(jù)分析師的職業(yè)價(jià)值愈發(fā)凸顯。CDA(Certified D ...
2025-07-03