我试图了解如何为我的数据集实现最小二乘线性分类器。问题是我有3个课程,我不知道如何完成这个课程......
这是一个尝试。这适用于2个班级,但是当我们有M> 2班级时,我怀疑某些东西是不同的。理论上说,对于M> 2类,我们应该遵循一个略微不同的过程,但我不明白...
class1=load('class_1.dat');
class2=load('class_2.dat');
class3=load('class_3.dat');
[m1_ML,S1_ML]=Gaussian_ML_estimate(class1');
[m2_ML,S2_ML]=Gaussian_ML_estimate(class2');
[m3_ML,S3_ML]=Gaussian_ML_estimate(class3');
m1_ML
S1_ML
m2_ML
S2_ML
m3_ML
S3_ML
for i=1:50
for j=1:4
CLASS(i,j)=class1(i,j);
CLASS(i+50,j)=class2(i,j);
CLASS(i+100,j)=class3(i,j);
end
end
CLASS1=CLASS';
for i=1:50
true_labels(i)=1;
true_labels(i+50)=2;
true_labels(i+100)=3;
end
classified=ones(1,150);
n=150;
for j = 1:n
inds = true(n,1);
inds(j) = false;
X1= CLASS1(:,inds);
Y1= CLASS1(:,~inds);
TL1=true_labels(:,inds);
TL2=true_labels(:,~inds);
for i=1:4
[m11(i),S11(i)]=Gaussian_ML_estimate(X1(i,find(TL1==1)));
end
for i=1:4
[m22(i),S22(i)]=Gaussian_ML_estimate(X1(i,find(TL1==2)));
end
for i=1:4
perFeature1(i,:)=normpdf(Y1(i,:),m11(i),sqrt(S11(i)));
perFeature2(i,:)=normpdf(Y1(i,:),m22(i),sqrt(S22(i)));
end
naive_probs1=prod(perFeature1);
naive_probs2=prod(perFeature2);
if naive_probs1<naive_probs2
classified(j)=2;
end
end
Naive_Error=sum(true_labels~=classified)/150