From: <as...@us...> - 2011-12-22 13:22:15
|
Revision: 9456 http://octave.svn.sourceforge.net/octave/?rev=9456&view=rev Author: asnelt Date: 2011-12-22 13:22:04 +0000 (Thu, 22 Dec 2011) Log Message: ----------- Initial commit. Modified Paths: -------------- trunk/octave-forge/main/statistics/INDEX trunk/octave-forge/main/statistics/NEWS Added Paths: ----------- trunk/octave-forge/main/statistics/inst/kmeans.m Modified: trunk/octave-forge/main/statistics/INDEX =================================================================== --- trunk/octave-forge/main/statistics/INDEX 2011-12-21 21:33:00 UTC (rev 9455) +++ trunk/octave-forge/main/statistics/INDEX 2011-12-22 13:22:04 UTC (rev 9456) @@ -62,8 +62,9 @@ Fitting gamfit Clustering + kmeans + linkage pdist - linkage squareform Reading and Writing caseread Modified: trunk/octave-forge/main/statistics/NEWS =================================================================== --- trunk/octave-forge/main/statistics/NEWS 2011-12-21 21:33:00 UTC (rev 9455) +++ trunk/octave-forge/main/statistics/NEWS 2011-12-22 13:22:04 UTC (rev 9456) @@ -1,9 +1,9 @@ Summary of important user-visible changes for statistics 1XXXXXXXX: ------------------------------------------------------------------- - ** The following functions are new in 1.1.0: + ** The following functions are new in 1.1.X: - monotone_smooth + monotone_smooth kmeans ** Bug fixes on the function: Added: trunk/octave-forge/main/statistics/inst/kmeans.m =================================================================== --- trunk/octave-forge/main/statistics/inst/kmeans.m (rev 0) +++ trunk/octave-forge/main/statistics/inst/kmeans.m 2011-12-22 13:22:04 UTC (rev 9456) @@ -0,0 +1,84 @@ +## Copyright (C) 2011 Soren Hauberg +## +## This program is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program; if not, write to the Free Software +## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +function [classes, centers] = kmeans (data, k, varargin) + ## Input checking + if (!ismatrix (data) || !isreal (data)) + error ("kmeans: first input argument must be a DxN real data matrix"); + endif + if (!isscalar (k)) + error ("kmeans: second input argument must be a scalar"); + endif + + [N, D] = size (data); + + ## (so far) Harcoded options + maxiter = Inf; + start = "sample"; + + ## Find initial clusters + switch (lower (start)) + case "sample" + idx = randperm (N) (1:k); + centers = data (idx, :); + otherwise + error ("kmeans: unsupported initial clustering parameter"); + endswitch + + ## Run the algorithm + D = zeros (N, k); + iterations = 0; + prevcenters = centers; + while (true) + ## Compute distances + for i = 1:k + D (:, i) = sum (( data - repmat (centers (i, :), N, 1)).^2, 2); + endfor + + ## Classify + [tmp, classes] = min (D, [], 2); + + ## Recompute centers + for i = 1:k + centers (i, :) = mean (data (classes == i, :)); + endfor + + ## Check for convergence + iterations++; + if (all (centers (:) == prevcenters (:)) || iterations >= maxiter) + break; + endif + prevcenters = centers; + endwhile +endfunction + +%!demo +%! ## Generate a two-cluster problem +%! C1 = randn (100, 2) + 1; +%! C2 = randn (100, 2) - 1; +%! data = [C1; C2]; +%! +%! ## Perform clustering +%! [idx, centers] = kmeans (data, 2); +%! +%! ## Plot the result +%! figure +%! plot (data (idx==1, 1), data (idx==1, 2), 'ro'); +%! hold on +%! plot (data (idx==2, 1), data (idx==2, 2), 'bs'); +%! plot (centers (:, 1), centers (:, 2), 'kv', 'markersize', 10); +%! hold off + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |