📄 kmeans2.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"><html><head> <title>Description of kmeans2</title> <meta name="keywords" content="kmeans2"> <meta name="description" content="KMEANS Find clusters with Forgy's batch-mode k-means clustering."> <meta http-equiv="Content-Type" content="text/html; charset=big5"> <meta name="generator" content="m2html © 2003 Guillaume Flandin"> <meta name="robots" content="index, follow"> <link type="text/css" rel="stylesheet" href="../m2html.css"></head><body><a name="_top"></a><div><a href="../index.html">Home</a> > <a href="index.html">dcpr</a> > kmeans2.m</div><!--<table width="100%"><tr><td align="left"><a href="../index.html"><img alt="<" border="0" src="../left.png"> Master index</a></td><td align="right"><a href="index.html">Index for dcpr <img alt=">" border="0" src="../right.png"></a></td></tr></table>--><h1>kmeans2</h1><h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2><div class="box"><strong>KMEANS Find clusters with Forgy's batch-mode k-means clustering.</strong></div><h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2><div class="box"><strong>function [center_index, U, obj_fcn] = kmeans2(distmat, cluster_n, options) </strong></div><h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2><div class="fragment"><pre class="comment">KMEANS Find clusters with Forgy's batch-mode k-means clustering.
This function is different from KMEANS in that the given input is
a distance matrix instead of a data matrix.
[CENTER, U, OBJ_FCN] = KMEANS2(DISTMAT, CLUSTER_N) applies
Forgy's batch-mode k-means clustering method to a given distance matrix.
Input and output arguments of this function are:
DISTMAT: Distance matrix whose elements are the pairwise
distances of the data set to be clustered.
The matrix is supposed to symmetric.
CLUSTER_N: number of clusters (greater than one)
CENTER: final cluster centers, where each row is a center
U: final fuzzy partition matrix (or MF matrix)
OBJ_FCN: values of the objective function during iterations
KMEANS2(DATA, CLUSTER_N, OPTIONS) use an additional argument OPTIONS to
control clustering parameters, stopping criteria, and/or iteration
info display:
OPTIONS(1): max. number of iterations (default: 100)
OPTIONS(2): min. amount of improvement (default: 1e-5)
OPTIONS(3): info display during iteration (default: 1)
If any entry of OPTIONS is NaN (not a number), the default value is
used instead. The clustering process stops when the max. number of
iteration is reached, or when the objective function improvement
between two consecutive iteration is less than the min. amount of
improvement specified.
Type "kmeans2" for a self demo.</pre></div><!-- crossreference --><h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2>This function calls:<ul style="list-style-image:url(../matlabicon.gif)"><li><a href="initkm2.html" class="code" title="function center_index = initkm2(distmat, cluster_n, method)">initkm2</a> INITKM2 Find the initial centers for a K-means clustering algorithm.</li><li><a href="stepkm2.html" class="code" title="function [center_index, obj_fcn, U] = stepkm(center_index, distmat)">stepkm2</a> STEPKM One step in k-means clustering.</li><li><a href="vecdist.html" class="code" title="function distmat = vecdist(mat1, mat2)">vecdist</a> VECDIST Distance between two set of vectors</li></ul>This function is called by:<ul style="list-style-image:url(../matlabicon.gif)"></ul><!-- crossreference --><h2><a name="_subfunctions"></a>SUBFUNCTIONS <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2><ul style="list-style-image:url(../matlabicon.gif)"><li><a href="#_sub1" class="code">function selfdemo</a></li></ul><h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../up.png"></a></h2><div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function [center_index, U, obj_fcn] = kmeans2(distmat, cluster_n, options)</a>0002 <span class="comment">%KMEANS Find clusters with Forgy's batch-mode k-means clustering.</span>0003 <span class="comment">%</span>0004 <span class="comment">% This function is different from KMEANS in that the given input is</span>0005 <span class="comment">% a distance matrix instead of a data matrix.</span>0006 <span class="comment">%</span>0007 <span class="comment">% [CENTER, U, OBJ_FCN] = KMEANS2(DISTMAT, CLUSTER_N) applies</span>0008 <span class="comment">% Forgy's batch-mode k-means clustering method to a given distance matrix.</span>0009 <span class="comment">% Input and output arguments of this function are:</span>0010 <span class="comment">%</span>0011 <span class="comment">% DISTMAT: Distance matrix whose elements are the pairwise</span>0012 <span class="comment">% distances of the data set to be clustered.</span>0013 <span class="comment">% The matrix is supposed to symmetric.</span>0014 <span class="comment">% CLUSTER_N: number of clusters (greater than one)</span>0015 <span class="comment">% CENTER: final cluster centers, where each row is a center</span>0016 <span class="comment">% U: final fuzzy partition matrix (or MF matrix)</span>0017 <span class="comment">% OBJ_FCN: values of the objective function during iterations</span>0018 <span class="comment">%</span>0019 <span class="comment">% KMEANS2(DATA, CLUSTER_N, OPTIONS) use an additional argument OPTIONS to</span>0020 <span class="comment">% control clustering parameters, stopping criteria, and/or iteration</span>0021 <span class="comment">% info display:</span>0022 <span class="comment">%</span>0023 <span class="comment">% OPTIONS(1): max. number of iterations (default: 100)</span>0024 <span class="comment">% OPTIONS(2): min. amount of improvement (default: 1e-5)</span>0025 <span class="comment">% OPTIONS(3): info display during iteration (default: 1)</span>0026 <span class="comment">%</span>0027 <span class="comment">% If any entry of OPTIONS is NaN (not a number), the default value is</span>0028 <span class="comment">% used instead. The clustering process stops when the max. number of</span>0029 <span class="comment">% iteration is reached, or when the objective function improvement</span>0030 <span class="comment">% between two consecutive iteration is less than the min. amount of</span>0031 <span class="comment">% improvement specified.</span>0032 <span class="comment">%</span>0033 <span class="comment">% Type "kmeans2" for a self demo.</span>0034 0035 <span class="comment">% Roger Jang, 20000206</span>0036 0037 <span class="keyword">if</span> nargin == 0, <a href="#_sub1" class="code" title="subfunction selfdemo">selfdemo</a>; <span class="keyword">return</span>; <span class="keyword">end</span>0038 <span class="keyword">if</span> nargin ~= 2 & nargin ~= 3,0039 error(<span class="string">'Too many or too few input arguments!'</span>);0040 <span class="keyword">end</span>0041 0042 data_n = size(distmat, 1);0043 0044 <span class="comment">% Change the following to set default options</span>0045 default_options = [ 100; <span class="comment">% max. number of iteration</span>0046 1e-5; <span class="comment">% min. amount of improvement</span>0047 1]; <span class="comment">% info display during iteration</span>0048 0049 <span class="keyword">if</span> nargin == 2,0050 options = default_options;0051 <span class="keyword">else</span>0052 <span class="comment">% If "options" is not fully specified, pad it with default values.</span>0053 <span class="keyword">if</span> length(options) < length(default_options),0054 tmp = default_options;0055 tmp(1:length(options)) = options;0056 options = tmp;0057 <span class="keyword">end</span>0058 <span class="comment">% If some entries of "options" are nan's, replace them with defaults.</span>0059 nan_index = find(isnan(options)==1);0060 options(nan_index) = default_options(nan_index);0061 <span class="keyword">end</span>0062 0063 max_iter = options(1); <span class="comment">% Max. iteration</span>0064 min_impro = options(2); <span class="comment">% Min. improvement</span>0065 display = options(3); <span class="comment">% Display info or not</span>0066 0067 obj_fcn = zeros(max_iter, 1); <span class="comment">% Array for objective function</span>0068 0069 center_index = <a href="initkm2.html" class="code" title="function center_index = initkm2(distmat, cluster_n, method)">initkm2</a>(distmat, cluster_n, 3); <span class="comment">% Initial center index</span>0070 <span class="comment">% Main loop</span>0071 <span class="keyword">for</span> i = 1:max_iter,0072 [center_index, obj_fcn(i), U] = <a href="stepkm2.html" class="code" title="function [center_index, obj_fcn, U] = stepkm(center_index, distmat)">stepkm2</a>(center_index, distmat);0073 <span class="keyword">if</span> display, 0074 fprintf(<span class="string">'Iteration count = %d, obj. fcn = %f\n'</span>, i, obj_fcn(i));0075 <span class="keyword">end</span>0076 <span class="comment">% check termination condition</span>0077 <span class="keyword">if</span> i > 1,0078 <span class="keyword">if</span> abs(obj_fcn(i) - obj_fcn(i-1)) < min_impro, <span class="keyword">break</span>; <span class="keyword">end</span>,0079 <span class="keyword">end</span>0080 <span class="keyword">end</span>0081 iter_n = i; <span class="comment">% Actual number of iterations</span>0082 obj_fcn(iter_n+1:max_iter) = [];0083 0084 <span class="comment">% ========== subfunctions ==========</span>0085 <a name="_sub1" href="#_subfunctions" class="code">function selfdemo</a>0086 data_n = 100;0087 data1 = ones(data_n, 1)*[0 0] + randn(data_n, 2)/5;0088 data2 = ones(data_n, 1)*[0 1] + randn(data_n, 2)/5;0089 data3 = ones(data_n, 1)*[1 0] + randn(data_n, 2)/5;0090 data = [data1; data2; data3];0091 distmat = <a href="vecdist.html" class="code" title="function distmat = vecdist(mat1, mat2)">vecdist</a>(data);0092 0093 cluster_n = 3;0094 [center_index, U, obj_fcn] = feval(mfilename, distmat, cluster_n);0095 plot(data(:, 1), data(:, 2), <span class="string">'o'</span>);0096 maxU = max(U);0097 index1 = find(U(1, :) == maxU);0098 index2 = find(U(2, :) == maxU);0099 index3 = find(U(3, :) == maxU);0100 line(data(index1, 1), data(index1, 2), <span class="keyword">...</span>0101 <span class="string">'linestyle'</span>, <span class="string">'none'</span>, <span class="string">'marker'</span>, <span class="string">'*'</span>, <span class="string">'color'</span>, <span class="string">'g'</span>);0102 line(data(index2, 1), data(index2, 2), <span class="keyword">...</span>0103 <span class="string">'linestyle'</span>, <span class="string">'none'</span>, <span class="string">'marker'</span>, <span class="string">'*'</span>, <span class="string">'color'</span>, <span class="string">'r'</span>);0104 line(data(index3, 1), data(index3, 2), <span class="keyword">...</span>0105 <span class="string">'linestyle'</span>, <span class="string">'none'</span>, <span class="string">'marker'</span>, <span class="string">'*'</span>, <span class="string">'color'</span>, <span class="string">'c'</span>);0106 axis equal;</pre></div><hr><address>Generated on Thu 30-Oct-2008 12:53:56 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/">m2html</a></strong> © 2003</address></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -