Issues on the translation of MATLAB machine learning model to Python

13 views (last 30 days)
Dear community,
I had training a Machine Learning model using the classification learner, that as the following characteristics:
% This code specifies all the classifier options and trains the classifier.
template = templateTree(...
'MaxNumSplits', 5003);
classificationEnsemble = fitcensemble(...
predictors, ...
response, ...
'Method', 'Bag', ...
'NumLearningCycles', 30, ...
'Learners', template, ...
'ClassNames', [1; 2; 3; 4; 5]);
I managed to get an accuracy of around 98%.
But when I tried to translate this code to Python, as follows and with the same training set, but I am not able to find the same accuracy, its a lot lower in Python. I would like to know if and what are the differences between both languages so I can adjust the parameters accordingly.
template = templateTree(...
'MaxNumSplits', 5003);
classificationEnsemble = fitcensemble(...
predictors, ...
response, ...
'Method', 'Bag', ...
'NumLearningCycles', 30, ...
'Learners', template, ...
'ClassNames', [1; 2; 3; 4; 5]);
Thank you in advance.

Answers (1)

Hornett
Hornett on 11 Sep 2024
When translating machine learning models from MATLAB to Python, there are several factors that can lead to differences in performance. These factors include differences in the default parameters, random state initialization, and implementation details of the algorithms in the respective libraries.
In MATLAB, you used the fitcensemble function with a bagging method and a decision tree template. In Python, you can achieve a similar model using the BaggingClassifier from scikit-learn.

Community Treasure Hunt

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

Start Hunting!