# spectral.rar

• PUDN用户
了解作者
• DOS
开发工具
• 36KB
文件大小
• rar
文件格式
• 0
收藏次数
• 1 积分
下载积分
• 13
下载次数
• 2015-09-20 10:49
上传日期

spectral.rar
• 谱聚类代码
• New Folder
• spectral_cluster.m
1.8KB
• 2a.mat
5.3KB
• 2b.mat
5KB
• dist2.m
858B
• Untitledss2.m
988B
• gcut.m
3.5KB
• 2d.mat
15KB
• 2c.mat
6.2KB
• Untitled.m
144B

function [clusts,distortion]=gcut(A,nClusts) %% [clusts,distortion]=gcut(A,nClusts) %% %% Graph partitioning using spectral clustering. %% Input: %% A = Affinity matrix %% nClusts = number of clusters %% %% Output: %% clusts = a cell array with indices for each cluster %% distortion = the distortion of the final clustering %% %% Algorithm steps: %% 1. Obtain Laplacian of the affinity matrix %% 2. Compute eigenvectors of Laplacian %% 3. Normalize the rows of the eigenvectors %% 4. Kmeans on the rows of the normalized eigenvectors %% %% Original code by Yair Weiss %% Modified and Updated by Lihi Zelnik-Manor %% %%%%%%%% Compute the Laplacian %degs = sum(A, 2); %for i=1:size(A) %D(i,i)=degs(i); %end % compute normalized Laplacian %L = inv(D)*A; npix = size(A,1); useSparse = issparse(A); dd = 1./(sum(A)+eps); %ddd=sum(A); %DDD=diag(ddd); %A=DDD-A; dd = sqrt(dd); if(useSparse) DD = sparse(1:npix,1:npix,dd); else DD = diag(dd); end %B=B+eye(npix); %degs = sum(A, 2); %for i=1:size(A) %D(i,i)=degs(i); %end L = DD*A*DD; %L=D-A; %%%%%%% Compute eigenvectors if (useSparse) opts.issym = 1; opts.isreal = 1; opts.disp = 0; [V,ss] = eigs(L,nClusts,1,opts); % [VV,ss]=svds(L,nClusts,1,opts); else [V,ss] = svd(L); V = V(:,1:nClusts); end %%%%%%% Normalize rows of V for i=1:size(V,1); V(i,:)=V(i,:)/(norm(V(i,:))+1e-10); end t=size(ss,1); tic; %%%%%%%%%%%%%%%%%% Kmeans %%%%%% Try 50 runs of k-means and save the one with minimal distortion bestC = {}; %% a variable to keep the best clustering so far bestD = 1/eps; %% a variable to remember the best distortion so far for nRuns=1:50 %%%%%% Initialize centers %% First center is set to one entry picked randomly [dd,pp] = max(rand(size(V,1),1)); mu(1,:) = V(pp,:); %% The other centers are selected to be farthest from previous centers for i=2:nClusts ip = V*mu'; minip = max(abs(ip')); [yy,ii] = min(minip); mu(i,:) = V(ii,:); end %%%%%%%%%%% and now run K means for tt=1:10 %% 10 iterations for kmeans distM = dist2(V,mu); %% initialize distance between points and centers [yy,ii] = min(distM'); %% assign points to nearest center distort = 0; distort_across = 0; clear clusts; for nn=1:nClusts I = find(ii==nn); %% indices of points in cluster nn J = find(ii~=nn); %% indices of points not in cluster nn clusts{nn} = I; %% save into clusts cell array if (length(I)>0) mu(nn,:) = mean(V(I,:)); %% update mean %% Compute within class distortion muB = repmat(mu(nn,:),length(I),1); distort = distort+sum(sum((V(I,:)-muB).^2)); %% Compute across class distortion muB = repmat(mu(nn,:),length(J),1); distort_across = distort_across + sum(sum((V(J,:)-muB).^2)); end end %% Set distortion as the ratio between the within %% class scatter and the across class scatter distort = distort/(distort_across+eps); if (distort<bestD) %% save result if better than the best so far bestD=distort; bestC=clusts; end end end %% Finally, delete empty clusters pp=1; for nn=1:nClusts if (length(bestC{nn})>0) clusts{pp} = bestC{nn}; pp = pp+1; end end distortion = bestD; ttt=toc; disp(['k-means computation took ' num2str(ttt) ' seconds']);

相关推荐
• MATLAB中K-Means聚类.zip
K-Means聚类分析是一种较为有效的聚类分析手段，也是应用较多的一种聚类分析手段，在这里介绍一种利用MATLAB有效实现聚类分析的手段。
• 聚类分析matlab.zip
将数据进行聚类分析，输入一个n乘以2的矩阵，作为需要分类的数据，运行程序得到数据的分类情况。
• 模糊C值聚类MATLAB程序
能够直接在MATLAB中使用的程序代码，直接填入EXCLE数据，就可以使用
• C均值聚类法，MATLAB
采用C均值聚类算法对男女生样本数据中的身高、体重2个特征进行聚类分析，考察不同的类别初始值以及类别数对聚类结果的影响，并以友好的方式图示化结果。
• Matlab的c均值聚类程序
Matlab平台编写的C均值聚类程序，已在Matlab环境下验证
• 系统聚类法及Matlab软件对系统聚类法的实现
系统聚类方法的具体步骤及Matlab软件对系统聚类法的实现 系统聚类方法的具体步骤及Matlab软件对系统聚类法的实现
• MATLAB聚类分析可用代码.zip
对于聚类分析有很多的手段和实现的软件，聚类分析也有很多的算法，在这里提供一种应用范围较广的K-MEANS聚类分析方法，提供有效的代码。
• FCMClust(模糊c均值聚类算法MATLAB实现）
FCMClust(模糊c均值聚类算法MATLAB实现）
• FCM 聚类 Matlab源代码
FCM聚类Matlab源代码，可指定分类数目，里面有详尽解释，也有分类结果。
• Kmeans聚类matlab程序
用Kmeans聚类方法实现随即点的分类，在二维平面上以均匀分布随机多个点，输入所需聚类数以得到聚类结果。