资源简介
采用matlab实现了k均值基本算法、谱聚类算法。里面有300个二维坐标作为待分类点。
代码片段和文件信息
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
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2014-10-21 09:11 sc_test1\
文件 3270 2014-09-26 19:32 sc_test1\circledata.mat
文件 4309 2011-05-06 05:06 sc_test1\k_means.m
文件 3109 2014-09-30 00:18 sc_test1\sc.m
文件 423 2014-09-30 19:46 sc_test1\sc_test.m
相关资源
- MATLAB编写的基于DCT变换的图像编码
- chan-vese的matlab代码实现
- 802.11a OFDM MATLAB仿真代码!(推荐)
- 在matlab中运行的zigzag扫描程序
- matlab rssi定位代码
- Huffman编解码算法及matlab实现
- 基于DCT数字水印算法的Matlab实现源代
- 粒子群算法详解+matlab代码说明
- 费诺编码fanomatlab程序
- 图像NMF分解MATLAB代码
-
simuli
nk逆变器 - PID控制程序 matlab
- Matlab高阶统计量工具箱
- 光伏MPPT控制
- FM解调 仿真方法.docx
- GA matlab算法程序
- EMD工具箱matlab
- 非局部去噪算法,matlab实现
- 基于神经网络的ASK 解调器及性能研究
- 特征检测小波包matlab程序与数据
- 纹理分割matlab程序和论文
- 火力分配的遗传算法matlab程序
- 用Matlab实现的遗传算法程序源代码
- 利用PSO训练BP神经网络的matlab代码
- 电力系统广域时滞系统鲁棒控制的论
- MATLAB用中心差分法实现多自由度的振
- MATLAB拓扑优化88行经典99行优化版
- 图像缩放之双立方插值matlab实现
- NSCT变换的工具箱
- Contourlet工具箱
评论
共有 条评论