Use matlab draw a two-dimensional, 3-dimensional bubble FIG.

In learning fuzzy c-means clustering, suddenly thought can be represented for each sample belongs to the genus of the slave clusters (C) in the form of a bubble chart, so that you can get at the same time is a degree classification and slaves on a map (C) two types of information. In matlab FIG bubbles are not drawn specialized functions, but the parameters can be manually set to achieve the final effect of the bubble FIG. The following is the implementation code.

Bubble drawing two-dimensional map

% 这个脚本用来对模糊c均值聚类生成二维气泡图
clc
clear 
%% 获得模糊c均值聚类的结果数据
load fcmdata.dat
[centers,U] = fcm(fcmdata,2); % 返回聚类中心信息以及每个数据点奴属与各聚类中心的值
index1 = find(U(1,:)==max(U));% 使用find()函数找到属于第一个聚类中心的元素序号
index2 = find(U(2,:)==max(U));% 获得属于第二个聚类中心的元素序号

%% 绘制二维bubble图
figure
weight = normalize(max(U),'range')*600; % 一种扩大一组数据之间差距的方法,否则气泡效果不明显
weight(weight==0)=(1/20)*mean(weight);% 粗略将0值转化为1/20的均值(这是估计出来的)
% 绘制第一个簇的气泡图
for i=1:length(index1)
    scatter(fcmdata(index1(i),1),fcmdata(index1(i),2),weight(index1(i)),...
        'MarkerEdgeColor','k','MarkerEdgeAlpha',0,'MarkerFaceColor','r',...
        'MarkerFaceAlpha',0.3);
    hold on % 别忘了这句,要不然总是一个点
end
% 绘制第二个簇的气泡图
for i=1:length(index2)
    scatter(fcmdata(index2(i),1),fcmdata(index2(i),2),weight(index2(i)),...
        'MarkerEdgeColor','k','MarkerEdgeAlpha',0,'MarkerFaceColor','b',...
        'MarkerFaceAlpha',0.3);
    hold on % 别忘了这句,要不然总是一个点
end
plot(centers(:,1),centers(:,2),'xk','markersize',10,'linewidth',3);
hold off

The final draw results as shown below:
Here Insert Picture Description
bubble size of the samples representing each of the slave genus (C) can be seen but not most bubble size gap, only a small genus of slaves bubble appears at the junction of two classification, which It is in line with the implementation of the law. In fact, the size of the gap between bubbles FIG amplified is processed, if not subjected to this step, it is about the same size of the bubbles.

Bubble-dimensional rendering of FIG.

Principle is the same as the two-dimensional bubble chart, directly on the code.

% 使用这个脚本创建三维气泡图
clc
clear
%% 数据集的创建
rng default; % For reproducibility
X = [randn(20,3)*0.75+ones(20,3);randn(20,3)*0.5-ones(20,3)]; % 创建20个样本的二分类数据集
%% 进行模糊c均值聚类
Numclust = 2;
[centers,U] = fcm(X,Numclust);
index1 = find(U(1,:)==max(U)); % 获取簇1的标号
index2 = find(U(2,:)==max(U)); % 获取簇2的标号
weight = normalize(max(U),'range')*800; % 获取气泡权重
weight(weight==0) = (1/20)*mean(weight);  % 调好看一点
%% 绘制3维气泡图
figure
for i = 1:length(index1)
   scatter3(X(index1(i),1),X(index1(i),2),X(index1(i),3),weight(index1(i)),...
       'MarkerEdgeColor','k','MarkerEdgeAlpha',0,'MarkerFaceColor','r',...
        'MarkerFaceAlpha',0.3);
    hold on
end

for i = 1:length(index2)
   scatter3(X(index2(i),1),X(index2(i),2),X(index2(i),3),weight(index2(i)),...
       'MarkerEdgeColor','k','MarkerEdgeAlpha',0,'MarkerFaceColor','b',...
        'MarkerFaceAlpha',0.3);
    hold on
end
plot3(centers(:,1),centers(:,2),centers(:,3),'xk','markersize',10,'linewidth',3);
hold off

The final mapping results as shown below:
Here Insert Picture Description

Follow-up

Calm down and thought, the c value represented in a form of a bubble chart seems a little superfluous, but as a daily exercise program is helpful. matlab entry-white, welcome to the exhibitions!

Released five original articles · won praise 6 · views 2218

Guess you like

Origin blog.csdn.net/qq_35000721/article/details/100133142