- use that code (instead of the Classification Learner App) on a different computer to train a new model that uses a different training set? Or,
- Export a model created with that training data to classify new data on that, or a different, computer?
Export function Classification Learner App
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi i am trying to train a classifier based on the function i exported from the classification learner app.
after i have done that, a function appears, but no training seems to happen even after replacing the training data with my new dataset.The code below is not the actual code. I am aware on some things that are missing, i removed them or else i would not be able to post this question
My goal here is to be able to use this function on another device and replicate the hyperparameters i have used in the classification learner app to train the dataset. I tried copying '[trainedClassifier, validationAccuracy] = trainClassifier(trainingData)' into the command window and running it but it just says this. any idea how i can get this function running?
clear,clc;
load('training_set.mat');
trainingData = dataset_train;
function [trainedClassifier, validationAccuracy] = trainClassifier(trainingData)
% [trainedClassifier, validationAccuracy] = trainClassifier(trainingData)
% Returns a trained classifier and its accuracy. This code recreates the
% classification model trained in Classification Learner app. Use the
% generated code to automate training the same model with new data, or to
% learn how to programmatically train models.
%
% Input:
% trainingData: A table containing the same predictor and response
% columns as those imported into the app.
%
% Output:
% trainedClassifier: A struct containing the trained classifier. The
% struct contains various fields with information about the trained
% classifier.
%
% trainedClassifier.predictFcn: A function to make predictions on new
% data.
%
% validationAccuracy: A double containing the accuracy as a
% percentage. In the app, the Models pane displays this overall
% accuracy score for each model.
%
% Use the code to train the model with new data. To retrain your
% classifier, call the function from the command line with your original
% data or new data as the input argument trainingData.
%
% For example, to retrain a classifier trained with the original data set
% T, enter:
% [trainedClassifier, validationAccuracy] = trainClassifier(T)
%
% To make predictions with the returned 'trainedClassifier' on new data T2,
% use
% yfit = trainedClassifier.predictFcn(T2)
%
% T2 must be a table containing at least the same predictor columns as used
% during training. For details, enter:
% trainedClassifier.HowToPredict
% Auto-generated by MATLAB on 13-Nov-2022 22:25:12
% Extract predictors and response
% This code processes the data into the right shape for training the
% model.
inputTable = trainingData;
predictorNames = {'feature 1',etc...}
predictors = inputTable(:, predictorNames);
response = inputTable.Group;
isCategoricalPredictor = {false, false, etc.....}
% Train a classifier
% This code specifies all the classifier options and trains the classifier.
template = templateSVM(...
'KernelFunction', 'linear', ...
'PolynomialOrder', [], ...
'KernelScale', 'auto', ...
'BoxConstraint', 1, ...
'Standardize', true);
classificationSVM = fitcecoc(...
predictors, ...
response, ...
'Learners', template, ...
'Coding', 'onevsall', ...
'ClassNames', ['D'; 'E'; 'H'; 'L'; 'O'; 'R'; 'W']);
% Create the result struct with predict function
predictorExtractionFcn = @(t) t(:, predictorNames);
svmPredictFcn = @(x) predict(classificationSVM, x);
trainedClassifier.predictFcn = @(x) svmPredictFcn(predictorExtractionFcn(x));
% Add additional fields to the result struct
trainedClassifier.RequiredVariables = {feature 1, etc....}
trainedClassifier.ClassificationSVM = classificationSVM;
trainedClassifier.About = 'This struct is a trained model exported from Classification Learner R2022a.';
trainedClassifier.HowToPredict = sprintf('To make predictions on a new table, T, use: \n yfit = c.predictFcn(T) \nreplacing ''c'' with the name of the variable that is this struct, e.g. ''trainedModel''. \n \nThe table, T, must contain the variables returned by: \n c.RequiredVariables \nVariable formats (e.g. matrix/vector, datatype) must match the original training data. \nAdditional variables are ignored. \n \nFor more information, see <a href="matlab:helpview(fullfile(docroot, ''stats'', ''stats.map''), ''appclassification_exportmodeltoworkspace'')">How to predict using an exported model</a>.');
% Extract predictors and response
% This code processes the data into the right shape for training the
% model.
inputTable = trainingData;
predictorNames = {feature 1, etc....}
predictors = inputTable(:, predictorNames);
response = inputTable.Group;
isCategoricalPredictor = {false, etc...}
% Perform cross-validation
partitionedModel = crossval(trainedClassifier.ClassificationSVM, 'KFold', 5);
% Compute validation predictions
[validationPredictions, validationScores] = kfoldPredict(partitionedModel);
% Compute validation accuracy
validationAccuracy = 1 - kfoldLoss(partitionedModel, 'LossFun', 'ClassifError');
end
0 comentarios
Respuestas (1)
Image Analyst
el 13 de Nov. de 2022
It looks like that Export Function button exported the training code. So do you want to
If #1 (train on new data) you need to put that function into an m-file called "trainClassifier.m" and then call that function with your new training data:
clear
clc;
% Load a new and different set of training data.
load('training_set.mat');
trainingData = dataset_train;
[trainedClassifier, validationAccuracy] = trainClassifier(trainingData) % This gives the results.
If #2 (classify data not used in training), you need to export the model to the workspace first, then save the variable into a .mat file.
save('trainedClassifier.mat', trainedClassifier);
Then when you call load() on that saved model
s = load('trainedClassifier.mat')
it will give you instructions for how to use the model to classify new data.
0 comentarios
Ver también
Categorías
Más información sobre Classification Ensembles en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!