
簡單易學的機器學習算法—譜聚類(Spectal Clustering)
一、復雜網絡中的一些基本概念
1、復雜網絡的表示
在復雜網絡的表示中,復雜網絡可以建模成一個圖,其中,V表示網絡中的節點的集合,E表示的是連接的集合。在復雜網絡中,復雜網絡可以是無向圖、有向圖、加權圖或者超圖。
2、網絡簇結構
網絡簇結構(network cluster structure)也稱為網絡社團結構(network community structure),是復雜網絡中最普遍和最重要的拓撲屬性之一。網絡簇是整個網絡中的稠密連接分支,具有同簇內部節點之間相互連接密集,不同簇的節點之間相互連接稀疏的特征。
3、復雜網絡的分類
復雜網絡主要分為:隨機網絡,小世界網絡和無標度網絡。
二、譜方法介紹
1、譜方法的思想
在譜聚類中定義了“截”函數的概念,當一個網絡被劃分成為兩個子網絡時,“截”即指子網間的連接密度。譜聚類的目的就是要找到一種合理的分割,使得分割后形成若干子圖,連接不同的子圖的邊的權重盡可能低,即“截”最小,同子圖內的邊的權重盡可能高。
2、“截”函數的具體表現形式
“截”表示的是子網間的密度,即邊比較少。以二分為例,將圖聚類成兩個類:S類和T類。假設用來表示圖的劃分,我們需要的結果為:
其中表示的是類別S和T之間的權重。對于K個不同的類別
,優化的目標為:
3、基本“截”函數的弊端
對于上述的“截”函數,最終會導致不好的分割,如二分類問題:
上述的“截”函數通常會將圖分割成一個點和其余n-1個點。
4、其他的“截”函數的表現形式
其中表示A類中包含的頂點的數目
三、Laplacian矩陣
1、Laplacian矩陣的定義
其中,d為圖的度矩陣,a為圖的鄰接矩陣。
2、度矩陣的定義
對于一個有n個頂點的圖,其鄰接矩陣為:
其中。
3、Laplacian矩陣的性質
Laplacian矩陣;L是對稱半正定矩陣;
性質3的證明:
4、不同的Laplacian矩陣
四、Laplacian矩陣與譜聚類中的優化函數的關系
1、由Laplacian矩陣到“截”函數
對于二個類別的聚類問題,優化的目標函數為:
其中,表示的是頂點的數目,對于確定的圖來說是個常數。由上述的推導可知,由
推導出了
,由此可知:Laplacian矩陣與有優化的目標函數
之間存在密切的聯系。
2、新的目標函數
由上式可得:
其中
3、轉化到Laplacian矩陣的求解
五、從二類別聚類到多類別聚類1、二類別聚類
2、多類別聚類
基于以上的分析,譜聚類的基本過程為:
對于給定的圖,求圖的度矩陣d和鄰接矩陣a;
計算圖的Laplacian矩陣;
對Laplacian矩陣進行特征值分解,取其前k個特征值對應的特征向量,構成的特征向量矩陣;
利用K-Means聚類算法對上述的的特征向量矩陣進行聚類,每一行代表一個樣本點。
2、利用相似度矩陣的構造方法
[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#這里只能這么做,不然始終找不到函數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代碼
[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;
個人的一點認識:譜聚類的過程相當于先進行一個非線性的降維,然后在這樣的低維空間中再利用聚類的方法進行聚類。
數據分析咨詢請掃描二維碼
若不方便掃碼,搜微信號:CDAshujufenxi
在本文中,我們將探討 AI 為何能夠加速數據分析、如何在每個步驟中實現數據分析自動化以及使用哪些工具。 數據分析中的AI是什么 ...
2025-05-20當數據遇見人生:我的第一個分析項目 記得三年前接手第一個數據分析項目時,我面對Excel里密密麻麻的銷售數據手足無措。那些跳動 ...
2025-05-20在數字化運營的時代,企業每天都在產生海量數據:用戶點擊行為、商品銷售記錄、廣告投放反饋…… 這些數據就像散落的拼圖,而相 ...
2025-05-19在當今數字化營銷時代,小紅書作為國內領先的社交電商平臺,其銷售數據蘊含著巨大的商業價值。通過對小紅書銷售數據的深入分析, ...
2025-05-16Excel作為最常用的數據分析工具,有沒有什么工具可以幫助我們快速地使用excel表格,只要輕松幾步甚至輸入幾項指令就能搞定呢? ...
2025-05-15數據,如同無形的燃料,驅動著現代社會的運轉。從全球互聯網用戶每天產生的2.5億TB數據,到制造業的傳感器、金融交易 ...
2025-05-15大數據是什么_數據分析師培訓 其實,現在的大數據指的并不僅僅是海量數據,更準確而言是對大數據分析的方法。傳統的數 ...
2025-05-14CDA持證人簡介: 萬木,CDA L1持證人,某電商中廠BI工程師 ,5年數據經驗1年BI內訓師,高級數據分析師,擁有豐富的行業經驗。 ...
2025-05-13CDA持證人簡介: 王明月 ,CDA 數據分析師二級持證人,2年數據產品工作經驗,管理學博士在讀。 學習入口:https://edu.cda.cn/g ...
2025-05-12CDA持證人簡介: 楊貞璽 ,CDA一級持證人,鄭州大學情報學碩士研究生,某上市公司數據分析師。 學習入口:https://edu.cda.cn/g ...
2025-05-09CDA持證人簡介 程靖 CDA會員大咖,暢銷書《小白學產品》作者,13年頂級互聯網公司產品經理相關經驗,曾在百度、美團、阿里等 ...
2025-05-07相信很多做數據分析的小伙伴,都接到過一些高階的數據分析需求,實現的過程需要用到一些數據獲取,數據清洗轉換,建模方法等,這 ...
2025-05-06以下的文章內容來源于劉靜老師的專欄,如果您想閱讀專欄《10大業務分析模型突破業務瓶頸》,點擊下方鏈接 https://edu.cda.cn/g ...
2025-04-30CDA持證人簡介: 邱立峰 CDA 數據分析師二級持證人,數字化轉型專家,數據治理專家,高級數據分析師,擁有豐富的行業經驗。 ...
2025-04-29CDA持證人簡介: 程靖 CDA會員大咖,暢銷書《小白學產品》作者,13年頂級互聯網公司產品經理相關經驗,曾在百度,美團,阿里等 ...
2025-04-28CDA持證人簡介: 居瑜 ,CDA一級持證人國企財務經理,13年財務管理運營經驗,在數據分析就業和實踐經驗方面有著豐富的積累和經 ...
2025-04-27數據分析在當今信息時代發揮著重要作用。單因素方差分析(One-Way ANOVA)是一種關鍵的統計方法,用于比較三個或更多獨立樣本組 ...
2025-04-25CDA持證人簡介: 居瑜 ,CDA一級持證人國企財務經理,13年財務管理運營經驗,在數據分析就業和實踐經驗方面有著豐富的積累和經 ...
2025-04-25在當今數字化時代,數據分析師的重要性與日俱增。但許多人在踏上這條職業道路時,往往充滿疑惑: 如何成為一名數據分析師?成為 ...
2025-04-24以下的文章內容來源于劉靜老師的專欄,如果您想閱讀專欄《劉靜:10大業務分析模型突破業務瓶頸》,點擊下方鏈接 https://edu.cda ...
2025-04-23