matlabdata-analysis

How to use output of Matlabs classificaion learner app


I'm just trying to wrap my head around the classification learner app in MATLAB.

Generating test data with this code:

clear
clc
close all

%----------------------------
x1 = 1:0.01:3;
r = -1 + (1+1)*rand(1,201);
x1 = x1 + r;

y1 = 1:0.01:3;
r = -1 + (1+1)*rand(1,201);
y1 = y1 + r;

x1 = x1';
y1 = y1';
label1 = zeros(length(y1),1);

Tclust1 = [label1,x1,y1];
%----------------------------
x2 = 4:0.01:6;
r = -1 + (1+1)*rand(1,201);
x2 = x2 + r;

y2 = 10:0.01:12;
r = -1 + (1+1)*rand(1,201);
y2 = y2 + r;

x2 = x2';
y2 = y2';
label2 = ones(length(y2),1);

Tclust2 = [label2,x2,y2];

%----------------------------
x3 = 7:0.01:9;
r = -1 + (1+1)*rand(1,201);
x3 = x3 + r;

y3 = 12:0.01:14;
r = -1 + (1+1)*rand(1,201);
y3 = y3 + r;

x3 = x3';
y3 = y3';
label3 = label2+1;

Tclust3 = [label3,x3,y3];
%----------------------------
T = [Tclust1;Tclust2;Tclust3];

writematrix(T,'Tclust_comb_v3.xls')

scatter(x1,y1)
hold on
scatter(x2,y2)
scatter(x3,y3)
title('Test data')
xlabel('x')
ylabel('y')
legend('cluster 1','cluster 2','location','eastoutside') 

And then feeding that into the classification learner app, to produce a linear discriminant model I've called trainedModel_LD

When I do this MATLAB very helpfully says

Structure  'trainedModel_LD' exported from Classification Learner. 
To make predictions on a new table, T: 
  yfit = trainedModel_LD.predictFcn(T) 
For more information, see How to predict using an exported model.
Variables have been created in the base workspace.

Which is fine, but how do I actually look at the output? Or any other output? Unlike the fitLm function for fitting a linear model, simply trying plot(trainedModel_LD) doesn't work (not enough inputs) and I think plot(yfit) is showing the predictions the model makes, not in the most useful way.

For example, the LD should allow me to take a plot of the raw data and draw a boundary line between the clusters (as shown in the LDA help (https://au.mathworks.com/help/stats/create-and-visualize-discriminant-analysis-classifier.html)), but how do I plot those? Where is the information in the model?

TIA


Solution

  • Partition data with

    hpartition = cvpartition(n,'kfold',folds)
    
    idx_Train = training(hpartition,qq);
    data_Train = data(idx_Train,1:2);
    labels_Train = labels(idx_Train);
    
        %test data
    idx_Test = test(hpartition,qq);
    data_Test = data(idx_Test,1:2);
    labels_Test = labels(idx_Test);
    

    Then do the linear discriminant analysis

    %train hte model on the training data
    MdlLinear = fitcdiscr(data_Train,labels_Train);
    

    And then apply the model to the test data

    %apply to the testing data
    pred_lbl = predict(MdlLinear,data_Test);
    iscorrect=pred_lbl==labels_Test;
    % iscorrect=iscorrect(:,2);
    accuracy = (sum(iscorrect)/length(iscorrect))*100;