function [Lpred, w] = FishersLDA(Dtrain, Ltrain, Dtest)
% Binary Classification using Fisher's linear discriminant
% 1. Find the optimal data projection direction w using the training dataset
% 2. And use Fisher's linear discriminant to make classification for testing dataset
% Note: the class labels use 1 and -1 to represent the two classes to be classified.
% Inputs:
% Dtrain - training dataset
% Ltrain - class labels of training patterns
% Dtest - testing patterns
% Outputs
% Lpred - predicted class labels of the testing patterns
% w - optimal weight vector for data projection
%--------Fisher Linear Discriminant---------%
idx1 = find(Ltrain==1); % the index for class 1
idx2 = find(Ltrain==-1); % the index for class -1
Dtrain_c1 = Dtrain(idx1, :); % the training samples of class 1
Dtrain_c2 = Dtrain(idx2, :); % the training samples of class 2
N_c1 = length(idx1); % the number of samples in class 1
N_c2 = length(idx2); % the number of samples in class 2
%---First, obtain the optimal direction w for data projection Y = wTx --------%
% Complete the implementation here
C = unique(Ltrain);
Lpred = [];
w = [];
for iC = 1:length(C);
cl = C(iC);
idx = find(Ltrain==cl);
data = Dtrain(idx,:);
mu = mean(data);
sigma = cov(data);
% P = length(idx)/length(Ltrain);
% Wi = -0.5*inv(sigma);
li = inv(sigma)*mu';
w = [w, li];
%wio = (-0.5*mu*li)-0.5*log(det(sigma))+log(P);
%for j = 1:size(Dtest,1)
% x = Dtest(j,:);
% prior = P;
% G(iC,j) = x*Wi*x'+li'*x'+wio;
% end
end
m1 = mean(Dtrain_c1);
m2 = mean(Dtrain_c2);
diff = m1-m2;
Sb = diff*diff';
Scat_between = w'*Sb*w;
store1 = [];
for iC = 1:N_c1;
%cl = C(iC);
%idx1 = find(Ltrain==cl);
xi = Dtrain(iC, :);
mult1 = (xi-m1)*(xi-m1)';
store1 = [store1, mult1];
end
S1 = sum(store1);
for iC = 1:N_c2;
cl = C(iC);
idx2 = find(Ltrain==-1);
xj = Dtrain(idx2, :);
mult2 = (xj-m2)*(xj-m2)';
store2 = [store2, mult2];
end
S2 = sum(store2);
s1sq = w'*S1*w;
s2sq = w'*S2*w;
Sw = s1sq+s2sq;
Scat_within = w'*Sw*w;
Jw = Scat_between/Scat_within;
wmax = max(Jw);
%
%
%
%
%---Second, perform Bayesian Classification on Projected Data in 1D Space-----%
% Complete the implementation here
%
%
%
%
%
%---------------------------------------------------------------%
FishersLDA判别分析_判别分析_fisher判别分析_FishersLDA判别分析_Fisher线性判别_模式判别分析_
版权申诉
69 浏览量
2021-10-04
01:36:56
上传
评论
收藏 2KB ZIP 举报
何欣颜
- 粉丝: 69
- 资源: 4732
最新资源
- 论文(最终)_20240430235101.pdf
- 基于python编写的Keras深度学习框架开发,利用卷积神经网络CNN,快速识别图片并进行分类
- 最全空间计量实证方法(空间杜宾模型和检验以及结果解释文档).txt
- 5uonly.apk
- 蓝桥杯Python组的历年真题
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 前端开发技术实验报告:内含4四实验&实验报告
- Highlight Plus v20.0.1
- 林周瑜-论文.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论0