MATLAB Answers

How to save any trained machine learning model to use it for prediction later?

52 views (last 30 days)
Maaz Ahmad
Maaz Ahmad on 19 Mar 2020
Commented: Maaz Ahmad on 19 Mar 2020
Hi, I am using number of machine learning models which include inbuilt models in MATLAB like fitrsvm, fitrgp, fitrensemble etc. and some models using functions in external toolbox like 'dacefit.m' for kriging model etc.
Is there a common way to save my models which I have trained on a dataset, so that I could use the same trained models on different test data later?
If not, kindly help me with saving a trained fitrensemble (Regression Tree Ensemble) model so that I could just use it for predictions in future.
Thanks in advance!

  0 Comments

Sign in to comment.

Accepted Answer

the cyclist
the cyclist on 19 Mar 2020
Edited: the cyclist on 19 Mar 2020
The standard calling syntax, e.g.
Mdl = fitrensemble(Tbl,ResponseVarName);
stores everything you need in the model object named Mdl.
You can see in the first example in the documentation for fitrensemble how to then make predictions from the model.

  7 Comments

Show 4 older comments
Maaz Ahmad
Maaz Ahmad on 19 Mar 2020
I see the issue now! When I ran your sample code, it worked but was failing for my code. Actually I am using multiple models and was saving each model separately. Hence on loading, I was only able to load the last saved model.
On saving all models using single save command as
save('checks.mat','Mdl1','Mdl2',....);
it works! Thanks though.
the cyclist
the cyclist on 19 Mar 2020
Glad to hear it worked. Rather than saving models as Mdl1, Mdl2, etc, you could consider saving them all in a single cell array:
Mdl{1} = fitrensemble(...);
Mdl{2} = fitrensemble(...);
...
Mdl{n} = fitrensemble(...);
Then you can just save the single cell array Mdl, which has all your models.

Sign in to comment.

More Answers (0)

Sign in to answer this question.


Translated by