Can I set an rng to classificationlearner app so the cross validation is done based on a specific random number generator to make the result reproducible?

10 views (last 30 days)
I need to reproduce the data using classificationlearner app. I was wondering if there is a way to set a rng to k-fold cross validation?
I know I can generate the code and add the rng over there but for some reason I want to run classifier from the classification learner app.

Answers (1)

Drew
Drew on 16 May 2025
If you want to run training in Classification Learner app, and then get the same results using the command line interface, you can set the desired random seed at the MATLAB commandline just before each relevant operation in the Classification Learner app, then use those same seed settings in the CLI code in the relevant locations in the code. Relevant operations include any that make use of the random number generation. So, this can include: (1) Before partition of the data at session start, (2) Before model training, (3) Before feature ranking (if any random number generator is used in the feature ranking algorithm), (4) Before interpretability plot creation (again, if any random number generation is used in the interpretability algorithm).
There is also another approach: Retrieve and store the random number generator state using something like "state=rng" just before each relevant operation in the Classification Learner app. When reproducing the operations at the command line, you can then set the rng state to the relevant retrieved state, just before each relevant operation, using something like "rng(previously_stored_state)";
Finally, if "UseParallel" is "on", there may be some effect on the random seed variability across the parallel computation. So, for potentially improved reproducability, one could set "UseParallel" to "off". For more details, see https://www.mathworks.com/help/stats/reproducibility-in-parallel-statistical-computations.html

Community Treasure Hunt

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

Start Hunting!