title: least squares linear classifier - three classes matlab example
body: <p>I am trying to understand how to implement a least square linear classifier for my dataset. The issue is that I have 3 classes and I am not sure how to get this done...</p>
  
  <p>Here is a try. This works for 2 classes however I am suspecting that something is different when we have M>2 classes. Also theory says that we should follow a slightly different process than this for M>2 classes but I don't get it...</p>
  
  <pre><code>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&lt;naive_probs2
      classified(j)=2;
    end
  end  
  Naive_Error=sum(true_labels~=classified)/150
  </code></pre>