Matlab数据可视化

内容:数据探索与可视化综合实践

利用Matlab对UCI‐Aliva分类数据集进行初步探索和分析:
(1)针对第A、B、C三个类别的数据对象,分别画出其10个属性的histogram、box plot和scatter plot;
(2)画出整个数据集的matrix plot,并基于此对该数据集的可分性进行初步分析;
(3)分别利用欧式距离、马氏距离和余弦距离作为相似性度量,计算数据对象之间的两两相似性(pairwise similarity),做出similarity matrix。并基于此分析那种相似性度量更合理。
(4)画出整个数据集的parallel coordinates图;
(5)从数据集中抽样500个数据对象,分别用star coordinates和Chernoff faces可视化;
(6)利用PCA方法对整个数据集进行降维,降至2维,并在2D坐标系下进行可视化。
数据集下载链接

快速导入txt数据

matlab可以非常方便地导入txt数据,过程如下:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
最后一步保存数据到路径下,下次直接load就可以

一、直方图、箱线图、散点图:

1.1 直方图

1.1.1 histogram函数

histogram函数参考链接
histogram(X):histogram(X) 基于 X 创建直方图。histogram 函数使用自动 bin 划分算法,然后返回均匀宽度的 bin(柱条),这些 bin 可涵盖 X 中的元素范围并显示分布的基本形状。histogram 将 bin 显示为矩形,这样每个矩形的高度就表示 bin 中的元素数量。
histogram(X,nbins): histogram(X,nbins) 使用标量 nbins 指定的 bin 数量
histogram(X,edges): 将 X 划分到由向量 edges 来指定 bin 边界的 bin 内。每个 bin 都包含左边界,但不包含右边界,除了同时包含两个边界的最后一个 bin 外。比如:

x = randn(1000,1);
edges = [-10 -2:0.25:2 10];%0.25代表中间没被剔除部分的bin间隔
h = histogram(x,edges);

在这里插入图片描述

将 Normalization 属性指定为 ‘countdensity’ 以使包含离群值的 bin 扁平化。现在,每个 bin 的区域(而不是高度)表示该 bin 的观测值频率。(好看些)

h.Normalization = 'countdensity';

在这里插入图片描述

histogram(‘BinEdges’,edges,‘BinCounts’,counts): 手动指定 bin 边界和关联的 bin 计数。histogram 绘制指定的 bin 计数,而不执行任何数据的 bin 划分。
histogram©: (其中 C 为分类数组)通过为 C 中的每个类别绘制一个条形来绘制直方图。
histogram(C,Categories): 仅绘制 Categories 指定的类别的子集。比如:
histogram(‘Categories’,Categories,‘BinCounts’,counts): 手动指定类别和关联的 bin 计数。histogram 绘制指定的 bin 计数,而不执行任何数据的 bin 划分。

A = [0 0 1 1 1 0 0 0 0 NaN NaN 1 0 0 0 1 0 1 0 1 0 0 0 1 1 1 1];
C = categorical(A,[1 0 NaN],{'yes','no','undecided'})
C = 1x27 categorical
  Columns 1 through 9

     no      no      yes      yes      yes      no      no      no      no 

  Columns 10 through 16

     undecided      undecided      yes      no      no      no      yes 

  Columns 17 through 25

     no      yes      no      yes      no      no      no      yes      yes 

  Columns 26 through 27

     yes      yes 

使用相对条形宽度 0.5 绘制投票的分类直方图。

h = histogram(C,'BarWidth',0.5)

在这里插入图片描述
**histogram(___,Name,Value):**使用前面的任何语法指定具有一个或多个 Name,Value 对组参数的其他选项。例如,可以指定 ‘BinWidth’ 和一个标量以调整 bin 的宽度,或指定 ‘Normalization’ 和一个有效选项(‘count’、‘probability’、‘countdensity’、‘pdf’、‘cumcount’ 或 ‘cdf’)以使用不同类型的归一化。有关属性列表,请参阅 Histogram 属性比如:
生成 1,000 个随机数并使用 ‘probability’ 归一化创建直方图。

x = randn(1000,1);
h = histogram(x,'Normalization','probability')
%计算条形高度的总和。通过该归一化,每个条形的高度等于在该 bin 间隔内选择观测值的概率,
%并且所有条形的高度总和为 1。
S = sum(h.Values)%1


在这里插入图片描述

%% 一幅图上绘制多个直方图
x = randn(2000,1);
y = 1 + randn(5000,1);
h1 = histogram(x);
hold on
h2 = histogram(y);
%% 由于直方图的示例大小和 bin 宽度不同,很难将它们进行比较。
%% 对这些直方图进行归一化,这样所有的条形高度相加的结果为 1 并使用统一的 bin 宽度。
h1.Normalization = 'probability';
h1.BinWidth = 0.25;
h2.Normalization = 'probability';
h2.BinWidth = 0.25;
%% 准确指定要使用的 bin 数量。
h1.NumBins = 15;
%% 通过向量指定 bin 边界。向量中的第一个值是第一个 bin 的左边界。
%% 最后一个值是最后一个 bin 的右边界。
h1.BinEdges = [-3:3];
%% 更改直方图条形的颜色。
h1.FaceColor = [0 0.5 0.5];%内部颜色
h1.EdgeColor = 'r';%框框颜色
%% 生成 5,000 个均值为 5、标准差为 2 的正态分布随机数。在 Normalization 设为 'pdf' 
%% 的情况下绘制直方图可生成概率密度函数的估计值。
x = 2*randn(5000,1) + 5;
histogram(x,'Normalization','pdf')
%% 使用 savefig 函数保存直方图。
y = histogram(randn(10));
savefig('histogram.fig');
clear all
close all
% 使用 openfig 重新将直方图加载到 MATLAB。openfig 也返回图窗 h 的句柄。
h = openfig('histogram.fig');

histogram(ax,___): 将图形绘制到 ax 指定的坐标区中,而不是当前坐标区 (gca) 中。选项 ax 可以位于前面的语法中的任何输入参数组合之前。
h = histogram(___): 返回 Histogram 对象。使用此语法可检查并调整直方图的属性。有关属性列表,请参阅 Histogram 属性

1.1.2 数据集绘图

%% 导入数据
load tran_data.mat
class=avilatr(:,end);   %这个方法读取出来是table
class1=avilatr{:,end};  %这个读取出来是字符串,可以用find操作

%% A类索引
class_A=find(class1=="A")';
A=avilatr{class_A,1:10};

%% 每一列属性
A1=A(:,1);A2=A(:,2);A3=A(:,3);A4=A(:,4);A5=A(:,5);A6=A(:,6);A7=A(:,7);A8=A(:,8);A9=A(:,9);A10=A(:,10);

%% B类索引
class_B=find(class1=="B")';
B=avilatr{class_B,1:10};

%% 每一列属性
B1=B(:,1);B2=B(:,2);B3=B(:,3);B4=B(:,4);B5=B(:,5);B6=B(:,6);B7=B(:,7);B8=B(:,8);B9=B(:,9);B10=B(:,10);

%% C类索引
class_C=find(class1=="C")';
C=avilatr{class_C,1:10};

%% 每一列属性
C1=C(:,1);C2=C(:,2);C3=C(:,3);C4=C(:,4);C5=C(:,5);C6=C(:,6);C7=C(:,7);C8=C(:,8);C9=C(:,9);C10=C(:,10);

%% histogram 1
figure(1)
suptitle('Histogram 1')
subplot(3,3,1);
histogram(A1,20);title('Attribute1 of A')
subplot(3,3,2);
histogram(A2,20);title('Attribute2 of A');
subplot(3,3,3);
histogram(A3,20);title('Attribute3 of A')

subplot(3,3,4);
histogram(B1,20);title('Attribute1 of B')
subplot(3,3,5);
histogram(B2,20);title('Attribute2 of B')
subplot(3,3,6);
histogram(B3,20);title('Attribute3 of B')

subplot(3,3,7);
histogram(C1,20);title('Attribute1 of C')
subplot(3,3,8);
histogram(C2,20);title('Attribute2 of C')
subplot(3,3,9);
histogram(C3,20);title('Attribute3 of C')

%% Histogram 2
figure(2)
suptitle('Histogram 2')
subplot(3,3,1)
histogram(A4,20);title('Attribute4 of A')
subplot(3,3,2)
histogram(A5,20);title('Attribute5 of A')
subplot(3,3,3)
histogram(A6,20);title('Attribute6 of A')

subplot(3,3,4)
histogram(B4,20);title('Attribute4 of B')
subplot(3,3,5)
histogram(B5,20);title('Attribute5 of B')
subplot(3,3,6)
histogram(B6,20);title('Attribute6 of B')

subplot(3,3,7)
histogram(C4,20);title('Attribute4 of C')
subplot(3,3,8)
histogram(C5,20);title('Attribute5 of C')
subplot(3,3,9)
histogram(C6,20);title('Attribute6 of C')

%% Histogram 3

figure(3)
suptitle('Histogram 3')

subplot(3,4,1)
histogram(A7,20);title('Attribute7 of A')
subplot(3,4,2)
histogram(A8,20);title('Attribute8 of A')
subplot(3,4,3)
histogram(A9,20);title('Attribute9 of A')
subplot(3,4,4)
histogram(A10,20);title('Attribute10 of A')

subplot(3,4,5)
histogram(B7,20);title('Attribute7 of B')
subplot(3,4,6)
histogram(B8,20);title('Attribute8 of B')
subplot(3,4,7)
histogram(B9,20);title('Attribute9 of B')
subplot(3,4,8)
histogram(B10,20);title('Attribute10 of B')

subplot(3,4,9)
histogram(C7,20);title('Attribute7 of C')
subplot(3,4,10)
histogram(C8,20);title('Attribute8 of C')
subplot(3,4,11)
histogram(C9,20);title('Attribute9 of C')
subplot(3,4,12)
histogram(C10,20);title('Attribute10 of C')

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

1.2 箱线图

%% 导入数据
load tran_data.mat
class=avilatr(:,end);   %这个方法读取出来是table,不好操作
class1=avilatr{:,end};  %这个读取出来是字符串,可以用find操作

%% A类索引
class_A=find(class1=="A")';
A=avilatr{class_A,1:10};
% 每一列属性
A1=A(:,1);A2=A(:,2);A3=A(:,3);A4=A(:,4);A5=A(:,5);A6=A(:,6);A7=A(:,7);A8=A(:,8);A9=A(:,9);A10=A(:,10);

%% B类索引
class_B=find(class1=="B")';
B=avilatr{class_B,1:10};
% 每一列属性
B1=B(:,1);B2=B(:,2);B3=B(:,3);B4=B(:,4);B5=B(:,5);B6=B(:,6);B7=B(:,7);B8=B(:,8);B9=B(:,9);B10=B(:,10);

%% C类索引
class_C=find(class1=="C")';
C=avilatr{class_C,1:10};
% 每一列属性
C1=C(:,1);C2=C(:,2);C3=C(:,3);C4=C(:,4);C5=C(:,5);C6=C(:,6);C7=C(:,7);C8=C(:,8);C9=C(:,9);C10=C(:,10);

%% box_plot
figure(1)
subplot(2,3,1)
boxplot(A,'Labels',{'1','2','3','4','5','6','7','8','9','10'},'Whisker',1,'DataLim',[-1.5,2])
title('A')
subplot(2,3,4)
boxplot(A,'Labels',{'1','2','3','4','5','6','7','8','9','10'},'Whisker',1)
title('A')

subplot(2,3,2)
boxplot(B,'Labels',{'1','2','3','4','5','6','7','8','9','10'},'Whisker',1,'DataLim',[-0.5,2])
title('B')
subplot(2,3,5)
boxplot(B,'Labels',{'1','2','3','4','5','6','7','8','9','10'},'Whisker',1)
title('B')

subplot(2,3,3)
boxplot(C,'Labels',{'1','2','3','4','5','6','7','8','9','10'},'Whisker',1,'DataLim',[-2.5,2])
title('C')
subplot(2,3,6)
boxplot(C,'Labels',{'1','2','3','4','5','6','7','8','9','10'},'Whisker',1)
title('C')

由于离群值的影响,箱线图被压缩的很小(如图中的第二行),因此我把y轴范围设置了一下重新画了一幅放在第一行
在这里插入图片描述

1.3 散点图


%% 导入数据
load tran_data.mat
class=avilatr(:,end);   %这个方法读取出来是table,不好操作
class1=avilatr{:,end};  %这个读取出来是字符串,可以用find操作

%% A类索引
class_A=find(class1=="A")';
A=avilatr{class_A,1:10};
% 每一列属性
A1=A(:,1);A2=A(:,2);A3=A(:,3);A4=A(:,4);A5=A(:,5);A6=A(:,6);A7=A(:,7);A8=A(:,8);A9=A(:,9);A10=A(:,10);

%% B类索引
class_B=find(class1=="B")';
B=avilatr{class_B,1:10};
% 每一列属性
B1=B(:,1);B2=B(:,2);B3=B(:,3);B4=B(:,4);B5=B(:,5);B6=B(:,6);B7=B(:,7);B8=B(:,8);B9=B(:,9);B10=B(:,10);

%% C类索引
class_C=find(class1=="C")';
C=avilatr{class_C,1:10};
% 每一列属性
C1=C(:,1);C2=C(:,2);C3=C(:,3);C4=C(:,4);C5=C(:,5);C6=C(:,6);C7=C(:,7);C8=C(:,8);C9=C(:,9);C10=C(:,10);

%% box_plot
figure(1)
X=[A; B; C];
Cylinders=cellstr([class1(class_A,1);class1(class_B,1);class1(class_C,1)]);
color = lines(3);
xnames = {'F1','F2','F3','F4','F5','F6','F7','F8','F9','F10'};
[h,ax] = gplotmatrix(X,[],Cylinders,'gbr','.o.',[],[],'variable',xnames);
title('Scatter Plot','FontName','Times New Roman','FontSize',20)

在这里插入图片描述

二、matrix plot


%% 导入数据
load tran_data.mat
avilatr=sortrows(avilatr,11);
class=avilatr(:,end);   %这个方法读取出来是table,不好操作
class1=avilatr{:,end};  %这个读取出来是字符串,可以用find操作
all=avilatr{:,1:10};
%% A类索引
class_A=find(class1=="A")';
A=avilatr{class_A,1:10};
% 每一列属性
A1=A(:,1);A2=A(:,2);A3=A(:,3);A4=A(:,4);A5=A(:,5);A6=A(:,6);A7=A(:,7);A8=A(:,8);A9=A(:,9);A10=A(:,10);

%% B类索引
class_B=find(class1=="B")';
B=avilatr{class_B,1:10};
% 每一列属性
B1=B(:,1);B2=B(:,2);B3=B(:,3);B4=B(:,4);B5=B(:,5);B6=B(:,6);B7=B(:,7);B8=B(:,8);B9=B(:,9);B10=B(:,10);

%% C类索引
class_C=find(class1=="C")';
C=avilatr{class_C,1:10};
% 每一列属性
C1=C(:,1);C2=C(:,2);C3=C(:,3);C4=C(:,4);C5=C(:,5);C6=C(:,6);C7=C(:,7);C8=C(:,8);C9=C(:,9);C10=C(:,10);

%% box_plot
image(all,'CDataMapping','scaled')
colorbar
caxis([0,1]);
set(gca,'XTickLabel',{'F1','F2','F3','F4','F5','F6','F7','F8','F9','F10'},'FontName','Times New Roman','FontSize',10);
set(gca,'FontName','Times New Roman','FontSize',12);
% set(gca,'YTickLabel',{'A(1-4286)','B(4287-4291)','C(4292-4394)','D(4395-4746)','E(4747-5841)','F(5842-7802)','G(7803-8248)','H(8249-8767)','I(8768-9598)','W(9599-9642)','X(9643-10164)','Y(10165-10430)'});
text(-1,6000,{'A(1-4286)','B(4287-4291)','C(4292-4394)','D(4395-4746)','E(4747-5841)','F(5842-7802)','G(7803-8248)','H(8249-8767)','I(8768-9598)','W(9599-9642)','X(9643-10164)','Y(10165-10430)'},'FontName','Times New Roman','FontSize',15);
title('Matrix Plot','FontName','Times New Roman','FontSize',20)

%% similarity matrix

%% 字体设置
% s = settings; 
% s.matlab.fonts.codefont.Size.TemporaryValue = 11; % points
% s.matlab.fonts.codefont.Name.TemporaryValue = 'Monospaced'%字体

在这里插入图片描述

三、similarity matrix

%% 导入数据
load tran_data.mat
class=avilatr(:,end);   %这个方法读取出来是table,不好操作
class1=avilatr{:,end};  %这个读取出来是字符串,可以用find操作
all=avilatr{:,1:10};

%% Similarity matrix

%% 欧式
figure('color',[1 1 1]);
subplot(1,3,1)
Y1=pdist2(all,all);
imagesc(Y1)
colorbar
caxis([0 10])
title('Euclidean Distance','FontName','Times New Roman','FontSize',20)

%% 马氏
subplot(1,3,2)
Y2=pdist2(all,all,'mahalanobis');
imagesc(Y2)
colorbar
caxis([0 10])
title('Mahalanobis Distance','FontName','Times New Roman','FontSize',20)


%% 余弦
subplot(1,3,3)
Y3=pdist2(all,all,'cosine');
imagesc(Y3)
colorbar
caxis([0 10])
title('Cosine Distance','FontName','Times New Roman','FontSize',20)


在这里插入图片描述

四、parallel coordinates


%% 导入数据
clear;
load tran_data.mat
avilatr=sortrows(avilatr,11);
class=avilatr(:,end);   %这个方法读取出来是table,不好操作
class1=avilatr{:,end};  %这个读取出来是字符串,可以用find操作
all=avilatr{:,1:10};


%% parallel coordinate
species=cellstr(avilatr{:,11});
labels = {'F1','F2','F3','F4','F5','F6','F7','F8','F9','F10'};
parallelcoords(all,'Group',species,'Labels',labels)
title('Parallel Coordinate Plot','FontName','Times New Roman','FontSize',20)
ylim([-10,10]);


%% 字体设置
% s = settings; 
% s.matlab.fonts.codefont.Size.TemporaryValue = 11; % points
% s.matlab.fonts.codefont.Name.TemporaryValue = 'Monospaced'%字体

在这里插入图片描述
在这里插入图片描述

五、用star coordinates和Chernoff faces可视化


%% 导入数据
clear;
load tran_data.mat
avilatr=sortrows(avilatr,11);
class=avilatr(:,end);   %这个方法读取出来是table,不好操作
class1=avilatr{:,end};  %这个读取出来是字符串,可以用find操作
all=avilatr{:,1:10};

%% 随机抽样500个
S = size(all,1);
SampleRows = randperm(S); %将一列序号随机打乱,序号必须是整数。
SampleRows = SampleRows(1:500);
SampleM = all(SampleRows,:);

%% Chernoff_faces
figure('color',[1 1 1]);
glyphplot(SampleM,'glyph','face');
title('Chernoff faces','FontName','Times New Roman','FontSize',20)

%% Star_coordinates
figure('color',[1 1 1]);
% glyphplot(SampleM,'standardize','column','obslabels',mode,'grid',[2 2],'page','scroll');
glyphplot(SampleM);


%% 字体设置
% s = settings; 
% s.matlab.fonts.codefont.Size.TemporaryValue = 11; % points
% s.matlab.fonts.codefont.Name.TemporaryValue = 'Monospaced'%字体

在这里插入图片描述
在这里插入图片描述

六、PCA和t-SNE

%% 导入数据
clear;
load tran_data.mat
avilatr=sortrows(avilatr,11);
class=avilatr(:,end);   %这个方法读取出来是table,不好操作
class1=avilatr{:,end};  %这个读取出来是字符串,可以用find操作
all=avilatr{:,1:10};

%% PCA
[COEFF,SCORE,latent]=pca(all);
pcaData=SCORE(:,1:2);            %取前k个主成分
figure('color',[1 1 1]);
species=cellstr(avilatr{:,11});
gscatter(pcaData(:,1),pcaData(:,2),class1(:,1))
title('PCA','FontName','Times New Roman','FontSize',20)

%% t-SNE
labels=class1;
% Set parameters
no_dims = 2;
init_dims = 30;
perplexity = 30;
mappedX = tsne(all);
figure('color',[1 1 1]);
gscatter(mappedX(:,1), mappedX(:,2), labels);
title('t-SNE','FontName','Times New Roman','FontSize',20)

%% 去除离群点
[all_without_outlier,TF]=rmoutliers(all,'mean',1);
index=find(TF==1)';
class_without_outlier=class1;
class_without_outlier(index,:)=[];
% PCA
[COEFF_without_outlier,SCORE_without_outlier,latent_without_outlier]=pca(all_without_outlier);
pcaData_without_outlier=SCORE_without_outlier(:,1:2);            %取前k个主成分
figure('color',[1 1 1]);
gscatter(pcaData_without_outlier(:,1),pcaData_without_outlier(:,2),class_without_outlier(:,1))
title('PCA without outlier','FontName','Times New Roman','FontSize',20)
%t-SNE
mappedX_without_outlier = tsne(all_without_outlier);
figure('color',[1 1 1]);
gscatter(mappedX_without_outlier(:,1), mappedX_without_outlier(:,2), class_without_outlier(:,1));
title('t-SNE without outlier','FontName','Times New Roman','FontSize',20)
%% 字体设置
% s = settings; 
% s.matlab.fonts.codefont.Size.TemporaryValue = 11; % points
% s.matlab.fonts.codefont.Name.TemporaryValue = 'Monospaced'%字体

在这里插入图片描述
在这里插入图片描述
仅作记录,记录一下画图,方便后面使用,有问题的话可以讨论~

  • 32
    点赞
  • 145
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值