工程数据分析实验2
使用
Matlab2018b
进行绘图和数据统计分析的
实验内容
Fisher 1936 年收集了三种鸢尾花分别 50 个样本数据(Iris Data):Setosa、Virginica、Versicolour。选取的特征是花瓣(petals) 和萼片(sepals)长度和宽度。
- 分别求各类别各特征的均值、方差和中位数,用棒图图示上述结果
- 分别求各类别各特征的经验分布函数,并画图
- 分别求各类别各特征的正态概率图,根据结果做出是否服从正态分布的判断
- 用 JB 检验,检验各类别各特征是否服从正态分布,并与正态概率图进行比较,是否结论一致?
- 分别做各类别的多维 QQ 图,根据结果判别它们是否服从多维正态分布
Tips:数据文件为 iris.mat,用 load(’iris.mat’) 可以读取数据,返回的结果是一个 iris 的结构。
iris.X: 150 × 4 的矩阵,每行为一个样本,前 50 行为第一类,51-100 行为第二类,101-150 行为第三类,每列为一个特征。
iris.Y: 150 × 1 的向量,为每个样本的类别名
实验代码
matlab总代码
% 先从工作区中清除变量、释放系统内存; 防止多次执行,内存不释放导致出错
clear
% 载入数据
load('iris.mat');
% whos('-file','iris.mat')
% 提取数据
s1 = iris.X(1:50,:);
s2 = iris.X(51:100,:);
s3 = iris.X(101:150,:);
% 1. 分别求各类别各特征的均值、方差和中位数,用棒图图示上述结果
% 均值
avg_s1 = mean(s1);
avg_s2 = mean(s2);
avg_s3 = mean(s3);
%方差
var_s1 = var(s1);
var_s2 = var(s2);
var_s3 = var(s3);
% 中位数
median_s1 = median(s1);
median_s2 = median(s2);
median_s3 = median(s3);
%按均值、方差和中位数分组
avg_all =[avg_s1; avg_s2; avg_s3];
var_all = [var_s1; var_s2; var_s3];
median_all = [median_s1; median_s2; median_s3];
% 创建 figure
figure1 = figure;
% 创建 subplot
subplot1 = subplot(1,3,1,'Parent',figure1);
hold(subplot1,'on');
% 使用 bar 的矩阵输入创建多行
bar(avg_all,'Parent',subplot1);
% 创建 xlabel
xlabel({'均值'});
box(subplot1,'on');
% 设置其余坐标区属性
set(subplot1,'XTick',[1 2 3],'XTickLabel',...
{'setosa','versicolor','virginica'});
% 创建 subplot
subplot2 = subplot(1,3,2,'Parent',figure1);
hold(subplot2,'on');
% 使用 bar 的矩阵输入创建多行
bar(var_all,'Parent',subplot2);
% 创建 xlabel
xlabel({'方差'});
box(subplot2,'on');
% 设置其余坐标区属性
set(subplot2,'XTick',[1 2 3],'XTickLabel',...
{'setosa','versicolor','virginica'});
% 创建 subplot
subplot3 = subplot(1,3,3,'Parent',figure1);
hold(subplot3,'on');
% 使用 bar 的矩阵输入创建多行
bar1 = bar(median_all,'Parent',subplot3);
set(bar1(4),'DisplayName','4');
set(bar1(3),'DisplayName','3');
set(bar1(2),'DisplayName','2');
set(bar1(1),'DisplayName','1');
% 创建 xlabel
xlabel({'中位数'});
box(subplot3,'on');
% 设置其余坐标区属性
set(subplot3,'XTick',[1 2 3],'XTickLabel',...
{'setosa','versicolor','virginica'});
% 创建 legend
legend1 = legend(subplot3,'show');
% 2. 分别求各类别各特征的经验分布函数,并画图
figure2 = figure;
% setosa类的各特征的经验分布函数图
subplot(3,4,1);
[h_11,stats_11]=cdfplot(s1(:,1));
subplot(3,4,2);
[h_12,stats_12]=cdfplot(s1(:,2));
subplot(3,4,3)
[h_13,stats_13]=cdfplot(s1(:,3));
subplot(3,4,4);
[h_14,stats_14]=cdfplot(s1(:,4));
% versicolor类的各特征的经验分布函数图
subplot(3,4,5);
[h_21,stats_21]=cdfplot(s2(:,1));
set(h_21,'Color', 'r');
subplot(3,4,6)
[h_22,stats_22]=cdfplot(s2(:,2));
set(h_22,'Color', 'r');
subplot(3,4,7);
[h_23,stats_23]=cdfplot(s2(:,3));
set(h_23,'Color', 'r');
subplot(3,4,8);
[h_24,stats_24]=cdfplot(s2(:,4));
set(h_24,'Color', 'r');
% virginica类的各特征的经验分布函数图
subplot(3,4,9);
[h_31,stats_31]=cdfplot(s3(:,1));
set(h_31,'Color', 'black');
subplot(3,4,10);
[h_32,stats_32]=cdfplot(s3(:,2));
set(h_32,'Color', 'black');
subplot(3,4,11);
[h_33,stats_33]=cdfplot(s3(:,3));
set(h_33,'Color', 'black');
subplot(3,4,12);
[h_T4,stats_34]=cdfplot(s3(:,4));
set(h_T4,'Color', 'black');
% 3. 分别求各类别各特征的正态概率图,根据结果做出是否服从正态分布的判断
figure3 = figure;
% setosa类的正态概率图
subplot(3,4,1)
nl_11=normplot(s1(:,1));
subplot(3,4,2)
nl_12=normplot(s1(:,2));
subplot(3,4,3)
nl_13=normplot(s1(:,3));
subplot(3,4,4)
nl_14=normplot(s1(:,4));
set([nl_11,nl_12,nl_13,nl_14],'Color', 'blue', 'MarkerEdgeColor','black');
% versicolor类的正态概率图
subplot(3,4,5);
nl_21=normplot(s2(:,1));
subplot(3,4,6)
nl_22=normplot(s2(:,2));
subplot(3,4,7);
nl_23=normplot(s2(:,3));
subplot(3,4,8);
nl_24=normplot(s2(:,4));
set([nl_21,nl_22,nl_23,nl_24],'Color', 'r','MarkerEdgeColor','blue');
% virginica类的正态概率图
subplot(3,4,9);
nl_31=normplot(s3(:,1));
subplot(3,4,10);
nl_32=normplot(s3(:,2));
subplot(3,4,11);
nl_33=normplot(s3(:,3));
subplot(3,4,12);
nl_34=normplot(s3(:,4));
set([nl_31,nl_32,nl_33,nl_34],'Color', 'black','MarkerEdgeColor','r');
% 4. 用JB检验,检验各类别各特征是否服从正态分布,并与正态概率图进行比较,是否结论一致?
h_F1=jbtest(s1(:,1));
h_F2=jbtest(s1(:,2));
h_F3=jbtest(s1(:,3));
h_F4=jbtest(s1(:,4));
h_S1=jbtest(s2(:,1));
h_S2=jbtest(s2(:,2));
h_S3=jbtest(s2(:,3));
h_S4=jbtest(s2(:,4));
h_T1=jbtest(s3(:,1));
h_T2=jbtest(s3(:,2));
h_T3=jbtest(s3(:,3));
h_T4=jbtest(s3(:,4));
h1 = [h_F1,h_F2,h_F3,h_F4];
h2 = [h_S1,h_S2,h_S3,h_S4];
h3 = [h_T1,h_T2,h_T3,h_T4];
disp(['Setosa类的JB检验结果分别为 ',num2str(h1)]);
disp(['Virginica类的JB检验结果分别为 ',num2str(h2)]);
disp(['Versicolour类的JB检验结果分别为 ',num2str(h3)]);
% 5. 分别做各类别的多维QQ图,根据结果判别它们是否服从多维正态分布
[n,p]=size(s1);
d1=mahal(s1,s1);
d2=mahal(s2,s2);
d3=mahal(s3,s3);
dl1=sort(d1);
dl2=sort(d2);
dl3=sort(d3);
pt = ((1:n)-0.5)/n;
x = chi2inv(pt,p);
figure4 =figure;
subplot(1,3,1);
plot(dl1,x,'rx',(0:max([dl1;x'])),(0:max([dl1;x'])),'b-');
xlabel({'Setosa'});
subplot(1,3,2);
plot(dl2,x,'rx',(0:max([dl2;x'])),(0:max([dl2;x'])),'b-');
xlabel({'Virginica'});
title({'各类别的多维 QQ 图'});
subplot(1,3,3);
plot(dl3,x,'rx',(0:max([dl3;x'])),(0:max([dl3;x'])),'b-');
xlabel({'Versicolour'});
% 执行完成后从工作区中清除变量、释放系统内存;
% clear;
实验结果
绘图结果 :
图一
图二
图三
Setosa类的JB检验结果分别为 0 0 0 1
Virginica类的JB检验结果分别为 0 0 0 0
Versicolour类的JB检验结果分别为 0 0 0 0
图四
附加数据
iris.mat数据 (略)