How to perform fuzzy c means clustering using custom initializations (initial fuzzy membership metric, datapoints, cluster centers.. etc)?
2 次查看(过去 30 天)
显示 更早的评论
% Define the data points as coordinates (X, Y)
Data = [
1, 3;
8, 2;
1, 2;
3, 1
];
% Defining total number of clusters
NumOfClusters = 2;
% Create the initial fuzzy partition matrix (U0)
U0 = [
1, 1, 0, 0;
0, 0, 1, 1
]; % Provided initial partition matrix
% Define initial cluster center values
initial_centers = [
2.0, 2.35;
2.15, 1.9
];
% Fuzzy c-means options
fuzzy_options = [2.0];
% Perform Fuzzy C-Means clustering with initializations
[~, cluster_centers, membership] = fcm(Data', NumOfClusters, fuzzy_options, U0, [], initial_centers);
% Displaying the updated partition matrix
disp('Updated Partition Matrix (Membership):');
disp(membership);
% Plotting the clustered data with different colors for each cluster
colors = distinguishable_colors(NumOfClusters); % Generate distinct colors for clusters
figure;
for i = 1:NumOfClusters
idx = find(membership(i, :) == max(membership));
scatter(Data(idx, 1), Data(idx, 2), 100, colors(i, :), 'filled');
hold on;
end
plot(cluster_centers(:, 1), cluster_centers(:, 2), 'kx', 'MarkerSize', 15, 'LineWidth', 2); % Plot cluster centers
hold off;
% Creating a legend dynamically based on the number of clusters
legend_strings = cell(1, NumOfClusters);
for i = 1:NumOfClusters
legend_strings{i} = ['Cluster ' num2str(i)];
end
legend_strings{NumOfClusters + 1} = 'Cluster Centers';
legend(legend_strings);
title('Fuzzy C-Means Clustering with Initializations');
I want to initialize the datapoints, initial fuzzy partition matrix U0, initial_cluster centers but i am getting the following error:
Error using fcm Too many input arguments. Error in (line 28) [~, cluster_centers, membership] = fcm(Data', NumOfClusters, fuzzy_options, U0, [], initial_centers);
When i remove the initial fuzzy partition matrix U0 and initial cluster centers the code works.
0 个评论
回答(1 个)
Govind KM
2024-9-2
Starting from MATLAB R2023b, Initial estimate for cluster centers can be specified by creating an “fcmOptions” object and setting the “ClusterCenters” property as follows:
Data = [1,3;
8,2;
1,2;
3,1];
NumOfClusters=2;
%Specify initial cluster centers as an C-by-N matrix,
%where C is the number of clusters and N is the number of data features.
initial_centers=[2.0, 2.35;2.15, 1.9];
%Create an fcmOptions object and set the ClusterCenters property
options = fcmOptions(NumClusters=NumOfClusters,ClusterCenters=initial_centers);
[centers,U] = fcm(Data,options);
You can also refer to this example in the documentation:
Hope this helps!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Clustering 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!