⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kmeanscluster.asv

📁 较简单的KMeans聚类算法实现
💻 ASV
字号:
function y=kMeansCluster(m,k,isRand)%%%%%%%%%%%%%%%%%  
% kMeansCluster - Simple k means clustering algorithm  
% Author: Kardi Teknomo, Ph.D.       
%              
% Purpose: classify the objects in data matrix based on the attributes  
% Criteria: minimize Euclidean distance between centroids and object points  
% For more explanation of the algorithm, see http://people.revoledu.com/kardi/tutorial/kMean/index.html    
% Output: matrix data plus an additional column represent the group of each object  

%      
% Example: m = [ 1 1; 2 1; 4 3; 5 4]  or in a nice form     
%          m = [ 1 1;                                  
%                2 1;    
%                4 3;  
%                5 4]   
%          k = 2    
% kMeansCluster(m,k) produces m = [ 1 1 1;  
%                                   2 1 1;   
%                                   4 3 2;   
%                                   5 4 2]   
% Input:%   m      - required, matrix data: objects in rows and attributes in columns    
%   k      - optional, number of groups (default = 1)
%   isRand - optional, if using random initialization isRand=1, otherwise input any number (default)
%            it will assign the first k data as initial centroids%% Local
%            Variables.(如果输入1,随机划分初始点,否则输入任何一个数,就设定前k个数为初始聚类中心)
%   f      - row number of data that belong to group i
%   c      - centroid coordinate size (1:k, 1:maxCol)
%   g      - current iteration group matrix size (1:maxRow)
%   i      - scalar iterator 
%   maxCol - scalar number of rows in the data matrix m = number of attributes
%   maxRow - scalar number of columns in the data matrix m = number of objects
%   temp   - previous iteration group matrix size (1:maxRow)
%   z      - minimum value (not needed)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if nargin<3,       
    isRand=0;   
end
if nargin<2,        
    k=1;       
end
[maxRow, maxCol]=size(m);
if maxRow<=k,    
    y=[m, 1:maxRow];%不懂
else
    % initial value of centroid    
    if isRand, 
        p = randperm(size(m,1));  
    % random initialization  随机初始化中心点
    for i=1:k     
        c(i,:)=m(p(i),:);      
    end
    else
        for i=1:k       
            c(i,:)=m(i,:);     
            % sequential initialization  选择前k个位初始化中心点
        end
    end
    temp=zeros(maxRow,1); 
    % initialize as zero vector   
    while 1,        
        d=DistMatrix(m,c); 
        % calculate objcets-centroid distances    
        [z,g]=min(d,[],2);  %找出d矩阵中的最小值,以z显示,该最小值在d中的位置以g显示
        % find group matrix g       
        if g==temp,           
            break;         
            % stop the iteration    
        else
            temp=g;       
            % copy group matrix to temporary variable    
        end
        for i=1:k       
            f=find(g==i); %找出g==i的变量的位置 
            if f           
                % only compute centroid if f is not empty     
                c(i,:)=mean(m(find(g==i),:),1);  %mean(A)求每一列的均值,mean(A,2)求每一行的均值        
            end
        end
    end
    y=[m,g]; 
end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -