加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Matlab help Fisher 判别分析

(2014-02-26 11:34:30)
标签:

classify

fisher

dicrimination

supervised

分类: 机器学习
一、判别分析的概念

判别分析是一种分类方式,它假设不同的类别产生数据基于不同的高斯分布。
(1)训练一个分类器时,拟合函数需要对每一类近似高斯分布函数的参数进行估计。
(2)预测一个数据的类别时,分类器通过寻找最小的误分类代价来确定数据的类别。
线性判别分析也称为 Fisher 判别分析。

二、建立并可视化一个判别分析分类器

1. 载入并显示数据
load fisheriris;

use petal length and petal width measurements
PL meas(:, 3);
PW meas(:, 4);
 
plot the data
h1 gscatter(PL,PW,species,'krb','ov^',[],'off');
set(h1,'LineWidth',2)
legend('Setosa','Versicolor','Virginica',...
       'Location','best')
http://s11/middle/002pSPISzy6GSY9u7dU1a&690help Fisher 判别分析" TITLE="Matlab help Fisher 判别分析" />

2. 建立一个 fisher 分类器
[PL,PW];
cls ClassificationDiscriminant.fit(X,species);

3. 绘制边界
hold on
cls.Coeffs(2,3).Const;
cls.Coeffs(2,3).Linear; 
Plot the curve [x,y]*L  0:
@(x1,x2) L(1)*x1 L(2)*x2;
h2 ezplot(f,[.9 7.1 2.5]);
set(h2,'Color','r','LineWidth',2)
 
cls.Coeffs(1,2).Const;
cls.Coeffs(1,2).Linear; 
Plot the curve [x1,x2]*L  0:
@(x1,x2) L(1)*x1 L(2)*x2;
h3 ezplot(f,[.9 7.1 2.5]);
set(h3,'Color','k','LineWidth',2)
axis([.9 7.1 2.5])
xlabel('Petal Length')
ylabel('Petal Width')
title( '{\bf Linear Classification with Fisher Training Data}')
http://s10/middle/002pSPISzy6GSY9wwHf89&690help Fisher 判别分析" TITLE="Matlab help Fisher 判别分析" />

4. 建立一个二次分类器
cqs ClassificationDiscriminant.fit(X,species,...
    'DiscrimType','quadratic');

5. 绘制分类边界
delete(h2); delete(h3) remove the linear plots
cqs.Coeffs(2,3).Const;
cqs.Coeffs(2,3).Linear; 
cqs.Coeffs(2,3).Quadratic;
Plot the curve [x1,x2]*L [x1,x2]*Q*[x1,x2]'=0:
@(x1,x2) L(1)*x1 L(2)*x2 Q(1,1)*x1.^2 ...
    (Q(1,2)+Q(2,1))*x1.*x2 Q(2,2)*x2.^2;
h2 ezplot(f,[.9 7.1 2.5]);
set(h2,'Color','r','LineWidth',2)
 
cqs.Coeffs(1,2).Const;
cqs.Coeffs(1,2).Linear; 
cqs.Coeffs(1,2).Quadratic;
Plot the curve [x1,x2]*L [x1,x2]*Q*[x1,x2]'=0:
@(x1,x2) L(1)*x1 L(2)*x2 Q(1,1)*x1.^2 ...
    (Q(1,2)+Q(2,1))*x1.*x2 Q(2,2)*x2.^2;
h3 ezplot(f,[.9 7.1 1.02]); plot the relevant
                                portion of the curve
set(h3,'Color','k','LineWidth',2)
axis([.9 7.1 2.5])
xlabel('Petal Length')
ylabel('Petal Width')
title( '{\bf Quadratic Classification with Fisher Training Data}')
hold off
http://s8/middle/002pSPISzy6GSY9yQOH97&690help Fisher 判别分析" TITLE="Matlab help Fisher 判别分析" />

三、线性分类器的改进

1. 处理奇异数据
判别分析需要数据近似满足高斯分布并且具有可逆的协方差矩阵。如果数据不满足这个要求,那么 ClassificationDiscriminant.fit 函数会失效。
这个时候就需要将 'discrimType' 设置为 'pseudoLinear'。

2. 选择 Discriminant 类型
http://s4/middle/002pSPISzy6GSY9BLSX73&690help Fisher 判别分析" TITLE="Matlab help Fisher 判别分析" />

3. 查看分类准确率
(1)查看误差比例
load fisheriris
obj = ClassificationDiscriminant.fit(meas,species);
resuberror = resubLoss(obj)

resuberror = 0.0200

结果说明误差率很低,也就是说基本上所有的数据都被正确分类。错误分类的个数为:
resuberror * obj.NObservations

ans = 3.0000

也就是说一共有三个数据被错分,我们可以查看是哪些数据出现了问题。
R = confusionmat(obj.Y,resubPredict(obj))

R =
    50     0     0
     0    48     2
     0     1    49

obj.ClassNames

ans = 
    'setosa'
    'versicolor'
    'virginica'

http://s14/middle/002pSPISzy6GSY9EFWd0d&690help Fisher 判别分析" TITLE="Matlab help Fisher 判别分析" />

当然,以上几步可以通过将预测分类结果与实际分类结果直接比较得到。只是使用自带函数会稍微方便些。

四、使用分类器进行分类
使用 predict 函数实现此功能。
load fisheriris
obj ClassificationDiscriminant.fit(meas,species);
meas(99:102,:); take four rows
[label score cost] predict(obj,X)

http://s16/middle/002pSPISzy6GSY9Hdbp2f&690help Fisher 判别分析" TITLE="Matlab help Fisher 判别分析" />


五、使用 classify 函数实现 Fisher 判别分类
这个其实就是 ClassificationDiscriminant.fit 的另一种实现方式。功能基本一致,用法可以参见 Matlab help。
















0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有