Can I export all the trained model from the classification learner app, using a single command?

Dear experts,
I am trying to find out which model performs on a specific dataset. There are 34 models available in the classification learner app. Do we have any option to export all the 34 trained model using a single command? Presently, I'm doing it one at a time.
Thank you in advance.

3 comentarios

Hi Noushin,

The classification learner app does not have a built-in feature to export all models at once with a single command. However, there is alternative method you can consider to streamline this process.You can automate the process using a loop to train and export each model iteratively. This approach saves time and effort compared to exporting models one by one manually.Here is snippet code as an example.

% Load your dataset

data = readtable('your_dataset.csv');

% Define your features and labels

X = data(:, 1:end-1);

Y = data(:, end);

% Loop through each model and export

for i = 1:34

    % Train your model using the ith model
    model = fitctree(X, Y); % Example model, replace with your model
    % Export the model
    save(sprintf('model_%d.mat', i), 'model');

end

So, the above code is basically loading your example dataset into Matlab. Ensure that your dataset is in the correct format and adjust the code accordingly.Extract the features (X) and labels (Y) from the dataset. Modify this part based on your dataset structure, use a loop that iterates from 1 to 34, representing the 34 models. Inside the loop:Train your model using the current iteration number (ith model). Replace fitctree with the appropriate function for your model.Save the trained model using save function with a unique filename based on the iteration number.Each model is saved as a .mat file with a unique name (e.g., model_1.mat, model_2.mat, ...).

By running this code, you can automatically train and export all 34 models without the need for manual intervention, streamlining the process and improving efficiency.

Thank you so much, Umar. I'll follow the instruction and let you know. Thank you, once again.
No problem, Noushin. Glad to help out. Good luck!

Iniciar sesión para comentar.

Respuestas (1)

The attached function extracts all of the models from a saved Classification Learner session file (`.mat`). The models are placed into an exported model structure which includes a predictFcn, just like when the models are exported using the "Export Model to Workspace" option in the app. Note that other model metadata (for all models) can be obtained by opening the app, going to the "Results Table" view, then using the "Export to Workspace" button.
% Extract all models from the saved session file.
allModels = extractModelsAndPredictions("session_36models.mat",IncludeTrainingData=true)
Warning: This session was generated by the Classification Learner version of R2026a.
To resume the session, open the session file in Classification Learner.

Warning: Model 2.6 is already compact in the session (training data not saved). Exporting compact model only.
Warning: Model 2.7 is already compact in the session (training data not saved). Exporting compact model only.
Warning: Model 2.22 is already compact in the session (training data not saved). Exporting compact model only.
Warning: Model 2.23 is already compact in the session (training data not saved). Exporting compact model only.
allModels = 36×7 table
ModelNumber Status ExportedModel ValidationPredictions ValidationScores TestPredictions TestScores ___________ ________ _____________ _____________________ ________________ _______________ _____________ "1" "Tested" {1×1 struct} {120×1 cell} {120×3 double} {30×1 cell} {30×3 double} "2.1" "Tested" {1×1 struct} {120×1 cell} {120×3 double} {30×1 cell} {30×3 double} "2.2" "Tested" {1×1 struct} {120×1 cell} {120×3 double} {30×1 cell} {30×3 double} "2.3" "Tested" {1×1 struct} {120×1 cell} {120×3 double} {30×1 cell} {30×3 double} "2.4" "Tested" {1×1 struct} {120×1 cell} {120×3 double} {30×1 cell} {30×3 double} "2.5" "Tested" {1×1 struct} {120×1 cell} {120×3 double} {30×1 cell} {30×3 double} "2.6" "Tested" {1×1 struct} {120×1 cell} {120×3 double} {30×1 cell} {30×3 double} "2.7" "Tested" {1×1 struct} {120×1 cell} {120×3 double} {30×1 cell} {30×3 double} "2.8" "Tested" {1×1 struct} {120×1 cell} {120×3 double} {30×1 cell} {30×3 double} "2.9" "Tested" {1×1 struct} {120×1 cell} {120×3 double} {30×1 cell} {30×3 double} "2.10" "Tested" {1×1 struct} {120×1 cell} {120×3 double} {30×1 cell} {30×3 double} "2.11" "Tested" {1×1 struct} {120×1 cell} {120×3 double} {30×1 cell} {30×3 double} "2.12" "Tested" {1×1 struct} {120×1 cell} {120×3 double} {30×1 cell} {30×3 double} "2.13" "Tested" {1×1 struct} {120×1 cell} {120×3 double} {30×1 cell} {30×3 double} "2.14" "Tested" {1×1 struct} {120×1 cell} {120×3 double} {30×1 cell} {30×3 double} "2.15" "Tested" {1×1 struct} {120×1 cell} {120×3 double} {30×1 cell} {30×3 double}
% Now take a look at the first model
allModels.ExportedModel{1}
ans = struct with fields:
predictFcn: @(x)exportableModel.predictFcn(predictorExtractionFcn(x)) RequiredVariables: {'SepalLength' 'SepalWidth' 'PetalLength' 'PetalWidth'} ClassificationTree: [1×1 ClassificationTree] About: 'This struct is a trained model exported from Classification Learner R2026a.' HowToPredict: 'To make predictions on a new table, T, use: ↵ [yfit,scores] = c.predictFcn(T) ↵replace 'c' with the name of the variable that is this struct, e.g. 'trainedModel'. ↵ ↵The table, T, must contain the variables returned by: ↵ c.RequiredVariables ↵Variable formats (e.g. matrix/vector, datatype) must match the original training data. ↵Additional variables are ignored. ↵ ↵For more information, see How to predict using an exported model.'
allModels.ExportedModel{1}.ClassificationTree
ans =
ClassificationTree PredictorNames: {'SepalLength' 'SepalWidth' 'PetalLength' 'PetalWidth'} ResponseName: 'Y' CategoricalPredictors: [] ClassNames: {'setosa' 'versicolor' 'virginica'} ScoreTransform: 'none' NumObservations: 120
% Now take a look at the last model, in row 36.
allModels.ModelNumber{36}
ans = '2.35'
allModels.ExportedModel{36}
ans = struct with fields:
predictFcn: @(x)exportableModel.predictFcn(predictorExtractionFcn(x)) RequiredVariables: {'SepalLength' 'SepalWidth' 'PetalLength' 'PetalWidth'} ClassificationNeuralNetwork: [1×1 ClassificationNeuralNetwork] About: 'This struct is a trained model exported from Classification Learner R2026a.' HowToPredict: 'To make predictions on a new table, T, use: ↵ [yfit,scores] = c.predictFcn(T) ↵replace 'c' with the name of the variable that is this struct, e.g. 'trainedModel'. ↵ ↵The table, T, must contain the variables returned by: ↵ c.RequiredVariables ↵Variable formats (e.g. matrix/vector, datatype) must match the original training data. ↵Additional variables are ignored. ↵ ↵For more information, see How to predict using an exported model.'
allModels.ExportedModel{36}.ClassificationNeuralNetwork
ans =
ClassificationNeuralNetwork PredictorNames: {'SepalLength' 'SepalWidth' 'PetalLength' 'PetalWidth'} ResponseName: 'Y' CategoricalPredictors: [] ClassNames: {'setosa' 'versicolor' 'virginica'} ScoreTransform: 'none' NumObservations: 120 LayerSizes: [] Activations: '' OutputLayerActivation: '' Solver: 'LBFGS' ConvergenceInfo: [1×1 struct] TrainingHistory: [30×7 table] View network information using dlnetwork.
% This last model is a customizable neural network that has a Deep Learning Toolbox dlnetwork
% object inside of a model built with fitcnet. We can look at the network this way
net = dlnetwork(allModels.ExportedModel{36}.ClassificationNeuralNetwork)
net =
dlnetwork with properties: Layers: [12×1 nnet.cnn.layer.Layer] Connections: [12×2 table] Learnables: [14×3 table] State: [0×3 table] InputNames: {'input'} OutputNames: {'softmax_output'} Initialized: 1 View summary with summary.

Preguntada:

el 16 de Jul. de 2024

Respondida:

el 19 de Jun. de 2026 a las 21:14

Community Treasure Hunt

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

Start Hunting!

Translated by