data=xlsread('D:\usedata\lastdata3.xlsx') data=data(:,1:2) [idx, centers] = kmeans(data, 4); options = [2, 100, 1e-5, 0]; [~, U, ~, ~, centers] = fcm(data, 4, options, centers); figure; plot(data(:,1), data(:,2), 'o'); hold on; maxU = max(U); index1 = find(U(1,:) == maxU); index2 = find(U(2,:) == maxU); index3 = find(U(3,:) == maxU); index4 = find(U(4,:) == maxU); line(data(index1,1), data(index1,2), 'linestyle', 'none', 'marker', 'o', 'color', 'g'); line(data(index2,1), data(index2,2), 'linestyle', 'none', 'marker', 'o', 'color', 'r'); line(data(index3,1), data(index3,2), 'linestyle', 'none', 'marker', 'o', 'color', 'b'); line(data(index4,1), data(index4,2), 'linestyle', 'none', 'marker', 'o', 'color', 'c'); plot(centers(1,1), centers(1,2), 'x', 'linewidth', 2, 'markersize', 8, 'color', 'k'); plot(centers(2,1), centers(2,2), 'x', 'linewidth', 2, 'markersize', 8, 'color', 'k'); plot(centers(3,1), centers(3,2), 'x', 'linewidth', 2, 'markersize', 8, 'color', 'k'); plot(centers(4,1), centers(4,2), 'x', 'linewidth', 2, 'markersize', 8, 'color', 'k'); disp('聚类中心:') disp(centers) for i = 1:size(centers,1) disp(['第',num2str(i),'个聚类的范围:']) index = find(U(i,:) == maxU); range = [min(data(index,1)), max(data(index,1)), min(data(index,2)), max(data(index,2))]; disp(range) end里加入kmeans聚类获得聚类中心
时间: 2023-06-24 11:08:48 浏览: 75
您可以尝试将k-means聚类的结果与fuzzy c-means聚类的结果进行比较,以评估哪种方法更适合您的数据集。以下是修改后的代码:
data=xlsread('D:\usedata\lastdata3.xlsx');
data=data(:,1:2);
% 使用k-means聚类获得聚类中心
[idx, centers_kmeans] = kmeans(data, 4);
options = [2, 100, 1e-5, 0];
[~, U, ~, ~, centers_fcm] = fcm(data, 4, options, centers_kmeans);
figure;
plot(data(:,1), data(:,2), 'o');
hold on;
maxU = max(U);
index1 = find(U(1,:) == maxU);
index2 = find(U(2,:) == maxU);
index3 = find(U(3,:) == maxU);
index4 = find(U(4,:) == maxU);
line(data(index1,1), data(index1,2), 'linestyle', 'none', 'marker', 'o', 'color', 'g');
line(data(index2,1), data(index2,2), 'linestyle', 'none', 'marker', 'o', 'color', 'r');
line(data(index3,1), data(index3,2), 'linestyle', 'none', 'marker', 'o', 'color', 'b');
line(data(index4,1), data(index4,2), 'linestyle', 'none', 'marker', 'o', 'color', 'c');
plot(centers_fcm(1,1), centers_fcm(1,2), 'x', 'linewidth', 2, 'markersize', 8, 'color', 'k');
plot(centers_fcm(2,1), centers_fcm(2,2), 'x', 'linewidth', 2, 'markersize', 8, 'color', 'k');
plot(centers_fcm(3,1), centers_fcm(3,2), 'x', 'linewidth', 2, 'markersize', 8, 'color', 'k');
plot(centers_fcm(4,1), centers_fcm(4,2), 'x', 'linewidth', 2, 'markersize', 8, 'color', 'k');
disp('k-means聚类中心:')
disp(centers_kmeans)
disp('fuzzy c-means聚类中心:')
disp(centers_fcm)
for i = 1:size(centers_fcm,1)
disp(['第',num2str(i),'个聚类的范围:'])
index = find(U(i,:) == maxU);
range = [min(data(index,1)), max(data(index,1)), min(data(index,2)), max(data(index,2))];
disp(range)
end
阅读全文