Info

This question is closed. Reopen it to edit or answer.

Classification Learner App - generate a more generic function for your model training that could take varying size of prediction tables? One step beyond the default function generation

1 view (last 30 days)
Andy
Andy on 27 May 2018
Closed: MATLAB Answer Bot on 20 Aug 2021
This is a question with an answer, that could easily translate into a feature implementation. When one generates a function from a Classification Learner App on a found model, typically the Function content is really static:
1) It is tied to the original Prediction Table Column Names / Properties. 2) As a consequence, the function cannot be used on a different table that might have more or less columns [but where we still have y as the last column] (why would you do that is another topic all-together).
I found that modifying the original function file with the below function:
function nameNewExt = aAdjustTrainClassifier(~,trainClassFile)
% aAdjustTrainClassifier
%
%
fidi=fopen(trainClassFile,'r');
[~,name,~]=fileparts(trainClassFile);
nameNew = strcat('aAdjusted_',name);
nameNewExt = strcat(nameNew,'.m');
if exist(nameNewExt, 'file') == 2
% file already exists, no overwrite ...
else
fido=fopen(nameNewExt,'w');
while ~feof(fidi)
l=fgetl(fidi); % read line
if startsWith(l,'predictorNames = ')
% modify line here
l='predictorNames = inputTable.Properties.VariableNames(:,1:end-1);';
elseif startsWith(l,'isCategoricalPredictor = ')
l='isCategoricalPredictor = false(1,length(predictorNames));';
elseif contains(l,strcat('function [trainedClassifier, validationAccuracy] ='," ",name,'(trainingData)'))
l=strcat('function [trainedClassifier, validationAccuracy] ='," ",nameNew,'(trainingData)');
elseif startsWith(l,'trainedClassifier.RequiredVariables = ')
l=strcat('trainedClassifier.RequiredVariables ='," ",'inputTable.Properties.VariableNames(:,1:end-1)');
end
fprintf(fido,'%s\n',l); % 'fgetl returns \n so it's embedded
end
fclose(fido);
end
fclose(fidi);
fclose('all');
end
does the trick. Some static assumptions made (as for my use-case), and this might not work in all instances, however - would be grateful if you could provide feedback / improvements. Another reason for posting is - this could easily translate into a GREAT feature to be implemented.
Thanks

Answers (1)

Bernhard Suhm
Bernhard Suhm on 12 Jun 2018
But if your table has fewer columns (predictors) typically you need to retrain the model, or at least have performance deteriorate significantly!?

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by