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)
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)
nameNew = strcat('aAdjusted_',name);
nameNewExt = strcat(nameNew,'.m');
if exist(nameNewExt, 'file') == 2
% file already exists, no overwrite ...
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)');
fprintf(fido,'%s\n',l); % 'fgetl returns \n so it's embedded
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.