?? fcmclust.m
字號:
function [center, U, obj_fcn] = FCMClust(data, cluster_n, options)
% FCMClust.m 采用模糊C均值對數據集data聚為cluster_n類
%
% 用法:
% 1. [center,U,obj_fcn] = FCMClust(Data,N_cluster,options);
% 2. [center,U,obj_fcn] = FCMClust(Data,N_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(100,2);
% [center,U,obj_fcn] = FCMClust(data,2);
% plot(data(:,1), data(:,2),'o');
% hold on;
% maxU = max(U);
% index1 = find(U(1,:) == maxU);
% index2 = find(U(2,:) == maxU);
% line(data(index1,1),data(index1,2),'marker','*','color','g');
% line(data(index2,1),data(index2,2),'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 = options(1); % 隸屬度矩陣U的指數
max_iter = options(2); % 最大迭代次數
min_impro = options(3); % 隸屬度最小變化量,迭代終止條件
display = options(4); % 每次迭代是否輸出信息標志
obj_fcn = zeros(max_iter, 1); % 初始化輸出參數obj_fcn
U = initfcm(cluster_n, data_n); % 初始化模糊分配矩陣,使U滿足列上相加為1,
% Main loop 主要循環
for i = 1:max_iter,
%在第k步循環中改變聚類中心ceneter,和分配函數U的隸屬度值;
[U, center, obj_fcn(i)] = stepfcm(data, U, cluster_n, expo);
if display,
fprintf('FCM:Iteration count = %d, obj. fcn = %f\n', i, obj_fcn(i));
end
% 終止條件判別
if i > 1,
if abs(obj_fcn(i) - obj_fcn(i-1)) < min_impro,
break;
end,
end
end
iter_n = i; % 實際迭代次數
obj_fcn(iter_n+1:max_iter) = [];
% 子函數
function U = initfcm(cluster_n, data_n)
% 初始化fcm的隸屬度函數矩陣
% 輸入:
% cluster_n ---- 聚類中心個數
% data_n ---- 樣本點數
% 輸出:
% U ---- 初始化的隸屬度矩陣
U = rand(cluster_n, data_n);
col_sum = sum(U);
U = U./col_sum(ones(cluster_n, 1), :);
% 子函數
function [U_new, center, obj_fcn] = stepfcm(data, U, cluster_n, expo)
% 模糊C均值聚類時迭代的一步
% 輸入:
% data ---- nxm矩陣,表示n個樣本,每個樣本具有m的維特征值
% U ---- 隸屬度矩陣
% cluster_n ---- 標量,表示聚合中心數目,即類別數
% expo ---- 隸屬度矩陣U的指數
% 輸出:
% U_new ---- 迭代計算出的新的隸屬度矩陣
% center ---- 迭代計算出的新的聚類中心
% obj_fcn ---- 目標函數值
mf = U.^expo; % 隸屬度矩陣進行指數運算結果
center = mf*data./((ones(size(data, 2), 1)*sum(mf'))'); % 新聚類中心(5.4)式
dist = distfcm(center, data); % 計算距離矩陣
obj_fcn = sum(sum((dist.^2).*mf)); % 計算目標函數值 (5.1)式
tmp = dist.^(-2/(expo-1));
U_new = tmp./(ones(cluster_n, 1)*sum(tmp)); % 計算新的隸屬度矩陣 (5.3)式
% 子函數
function out = distfcm(center, data)
% 計算樣本點距離聚類中心的距離
% 輸入:
% center ---- 聚類中心
% data ---- 樣本點
% 輸出:
% out ---- 距離
out = zeros(size(center, 1), size(data, 1));
for k = 1:size(center, 1), % 對每一個聚類中心
% 每一次循環求得所有樣本點到一個聚類中心的距離
out(k, :) = sqrt(sum(((data-ones(size(data,1),1)*center(k,:)).^2)',1));
end
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -