資源簡介
我現在這在用這個聚類算法,這個源程序寫的很簡潔,而且里面的注釋很明白,我自己一直都在用哦
代碼片段和文件信息
function?[center?U?obj_fcn]?=?FCMClust(data?cluster_n?options)
%?FCMClust.m???采用模糊C均值對數據集data聚為cluster_n類?
%?用法:
%???1.??[centerUobj_fcn]?=?FCMClust(DataN_clusteroptions);
%???2.??[centerUobj_fcn]?=?FCMClust(DataN_cluster);
%?輸入:
%???data????????----?nxm矩陣表示n個樣本每個樣本具有m的維特征值
%???N_cluster???----?標量表示聚合中心數目即類別數
%???options?????----?4x1矩陣,其中
%???????options(1):??隸屬度矩陣U的指數,>1??????????????????(缺省值:?2.0)
%???????options(2):??最大迭代次數???????????????????????????(缺省值:?100)
%???????options(3):??隸屬度最小變化量迭代終止條件???????????(缺省值:?1e-5)
%???????options(4):??每次迭代是否輸出信息標志????????????????(缺省值:?1)
%?輸出:
%???center??????----?聚類中心
%???U???????????----?隸屬度矩陣
%???obj_fcn?????----?目標函數值
%???Example:
%???????data?=?rand(1002);
%???????[centerUobj_fcn]?=?FCMClust(data2);
%???????plot(data(:1)?data(:2)‘o‘);
%???????hold?on;
%???????maxU?=?max(U);
%???????index1?=?find(U(1:)?==?maxU);
%???????index2?=?find(U(2:)?==?maxU);
%???????line(data(index11)data(index12)‘marker‘‘*‘‘color‘‘g‘);
%???????line(data(index21)data(index22)‘marker‘‘*‘‘color‘‘r‘);
%???????plot([center([1?2]1)][center([1?2]2)]‘*‘‘color‘‘k‘)
%???????hold?off;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if?nargin?~=?2?&?nargin?~=?3????%判斷輸入參數個數只能是2個或3個
error(‘Too?many?or?too?few?input?arguments!‘);
end
data_n?=?size(data?1);?%?求出data的第一維(rows)數即樣本個數
in_n?=?size(data?2);???%?求出data的第二維(columns)數,即特征值長度
%?默認操作參數
default_options?=?[2; %?隸屬度矩陣U的指數
????100;????????????????%?最大迭代次數?
????1e-5;???????????????%?隸屬度最小變化量迭代終止條件
????1];?????????????????%?每次迭代是否輸出信息標志?
if?nargin?==?2
options?=?default_options;
?else???????%分析有options做參數時候的情況
%?如果輸入參數個數是二那么就調用默認的option;
if?length(options)?4?%如果用戶給的opition數少于4個那么其他用默認值;
tmp?=?default_options;
tmp(1:length(options))?=?options;
options?=?tmp;
????end
????%?返回options中是數的值為0(如NaN)不是數時為1
nan_index?=?find(isnan(options)==1);
????%將denfault_options中對應位置的參數賦值給options中不是數的位置.
options(nan_index)?=?default_options(nan_index);
if?options(1)?<=?1?%如果模糊矩陣的指數小于等于1
error(‘The?exponent?should?be?greater?than?1!‘);
end
end
%將options?中的分量分別賦值給四個變量;
expo?=?opti
評論
共有 條評論