FCM聚类算法(matlab编程)

FCM聚类算法(matlab编程)

function [center, U, obj_fcn] = fcm(data, cluster_n, options)
%FCM Data set clustering using fuzzy c-means clustering.
%
%   [CENTER, U, OBJ_FCN] = FCM(DATA, N_CLUSTER) finds N_CLUSTER number of
%   clusters in the data set DATA. DATA is size M-by-N, where M is the number of
%   data points and N is the number of coordinates for each data point. The
%   coordinates for each cluster center are returned in the rows of the matrix
%   CENTER. The membership function matrix U contains the grade of membership of
%   each DATA point in each cluster. The values 0 and 1 indicate no membership
%   and full membership respectively. Grades between 0 and 1 indicate that the
%   data point has partial membership in a cluster. At each iteration, an
%   objective function is minimized to find the best location for the clusters
%   and its values are returned in OBJ_FCN.
%
%   [CENTER, ...] = FCM(DATA,N_CLUSTER,OPTIONS) specifies a vector of options
%   for the clustering process:
%       OPTIONS(1): exponent for the matrix U             (default: 2.0)
%       OPTIONS(2): maximum number of iterations          (default: 100)
%       OPTIONS(3): minimum amount of improvement         (default: 1e-5)
%       OPTIONS(4): info display during iteration         (default: 1)
%   The clustering process stops when the maximum number of iterations
%   is reached, or when the objective function improvement between two
%   consecutive iterations is less than the minimum amount of improvement
%   specified. Use NaN to select the default value.
%
%   Example
%       data = rand(100,2);
%       [center,U,obj_fcn] = fcm(data,2);
%       plot(data(:,1), data(:,2),'o');
%       hold on;
%       maxU = max(U);
%       % Find the data points with highest grade of membership in cluster 1
%       index1 = find(U(1,:) == maxU);
%       % Find the data points with highest grade of membership in cluster 2
%       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 the cluster centers
%       plot([center([1 2],1)],[center([1 2],2)],'*','color','k')
%       hold off;
%
%   See also FCMDEMO, INITFCM, IRISFCM, DISTFCM, STEPFCM.

%   Roger Jang, 12-13-94, N. Hickey 04-16-01
%   Copyright 1994-2002 The MathWorks, Inc. 

if nargin ~= 2 & nargin ~= 3,
	error('Too many or too few input arguments!');
end

data_n = size(data, 1);
in_n = size(data, 2);

% Change the following to set default options
default_options = [2;	% exponent for the partition matrix U
		100;	% max. number of iteration
		1e-5;	% min. amount of improvement
		1];	% info display during iteration 

if nargin == 2,
	options = default_options;
else
	% If "options" is not fully specified, pad it with default values.
	if length(options) < 4,
		tmp = default_options;
		tmp(1:length(options)) = options;
		options = tmp;
	end
	% If some entries of "options" are nan's, replace them with defaults.
	nan_index = find(isnan(options)==1);
	options(nan_index) = default_options(nan_index);
	if options(1) <= 1,
		error('The exponent should be greater than 1!');
	end
end

expo = options(1);		% Exponent for U
max_iter = options(2);		% Max. iteration
min_impro = options(3);		% Min. improvement
display = options(4);		% Display info or not

obj_fcn = zeros(max_iter, 1);	% Array for objective function

U = initfcm(cluster_n, data_n);			% Initial fuzzy partition
% Main loop
for i = 1:max_iter,
	[U, center, obj_fcn(i)] = stepfcm(data, U, cluster_n, expo);
	if display, 
		fprintf('Iteration count = %d, obj. fcn = %f\n', i, obj_fcn(i));
	end
	% check termination condition
	if i > 1,
		if abs(obj_fcn(i) - obj_fcn(i-1)) < min_impro, break; end,
	end
end

iter_n = i;	% Actual number of iterations 
obj_fcn(iter_n+1:max_iter) = [];

该算法是matlab自带工具包,调用语句 [center, u, obj] = fcm(data,c,[m 100 1e-6 0]);*(center聚类中心,u隶属度矩阵,obj为目标函数的值,data为输入数据,c为聚类个数,m为模糊隶属度的值,100为迭代次数,1e-6为阈值)*

以下是使用 MATLAB 实现 FCM 聚类的示例代码: ```matlab % 加载数据 load('data.mat'); % 载入数据,data.mat 是数据文件的名称,需要将数据存储在一个名为 data 的变量中 % 设置参数 num_clusters = 3; % 聚类数量 max_iterations = 100; % 最大迭代次数 fuzziness = 2; % 模糊因子 % 初始化隶属度矩阵 [m, n] = size(data); % 获取数据集的大小 U = rand(m, num_clusters); % 随机初始化隶属度矩阵 U = U ./ sum(U, 2); % 归一化隶属度矩阵 % 迭代更新隶属度和聚类中心 for iter = 1:max_iterations % 更新聚类中心 centroids = U' * data ./ sum(U)'; % 计算隶属度矩阵 distances = pdist2(data, centroids); % 计算样本与聚类中心的距离 U_new = distances.^(-2/(fuzziness-1)); % 根据距离计算新的隶属度矩阵 U_new = U_new ./ sum(U_new, 2); % 归一化隶属度矩阵 % 判断是否达到停止条件 if norm(U_new - U) < 1e-5 break; end U = U_new; % 更新隶属度矩阵 end % 根据隶属度矩阵确定最终的聚类结果 [~, labels] = max(U, [], 2); % 绘制聚类结果 scatter(data(:, 1), data(:, 2), [], labels); ``` 注意:在上述代码中,需要将数据存储在一个名为 `data` 的变量中,并且数据应该是一个 `m x n` 的矩阵,其中 `m` 是样本数量,`n` 是特征数量。你需要根据你的实际数据进行适当的修改。此外,代码中的 `data.mat` 是示例数据文件的名称,你需要将其替换为你的数据文件的名称。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

好乐day_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值