Replies: 2 comments
|
Ok, so I think I found the answer, also I am not absolutely sure. I think PyCaret creates a pipeline that is run on each split in the CV phase. On each split, train set is preprocessed with normalization and transformation, then test set is transformed. After that, the model is fitted and the prediction is made using the transformed test set. If I turn off all preprocessing options in PyCaet's setup, the averaged CV results returned from compare_models and sklearn are exactly the same. I coudln't find any confirmation on PyCaret guides, and since the GitHub changed recently due to the 4.0 update, it’s not so easy to explore the scripts anymore. |
|
You have basically diagnosed it correctly, and yes, it is the preprocessing. The difference is where it gets fit. PyCaret builds the transforms and the model as one pipeline and refits that pipeline inside every CV fold: on each split it fits the transforms on that fold's training rows only, applies them to the fold's validation rows, then fits and scores the model. That is the leak-free way to do it. In your sklearn version you passed To replicate PyCaret in sklearn, put the transforms and the model in a from sklearn.pipeline import Pipeline
from sklearn.preprocessing import QuantileTransformer, StandardScaler
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import cross_validate, RepeatedStratifiedKFold
# match PyCaret's transformer settings and the order it applies them (transform, then normalize)
pipe = Pipeline([
("quantile", QuantileTransformer(output_distribution="normal")),
("scale", StandardScaler()),
("rf", RandomForestClassifier(random_state=42, n_jobs=-1)),
])
cv = RepeatedStratifiedKFold(n_splits=5, n_repeats=10, random_state=42)
scores = cross_validate(pipe, xtrain_raw, ytrain, scoring=scoring, cv=cv)Two things can still keep them from matching exactly even after that:
So your own fix (all preprocessing off matches) is the confirmation that it is the per-fold preprocessing. Rebuild it as a Pipeline on raw data, keep outlier removal and GPU in mind, and you can get them to agree. |
Uh oh!
There was an error while loading. Please reload this page.
Hello. I am currently working with PyCaret 3.4.0, since 4.0 lacks some configuration parameters that are useful for my case.
I tried to replicate PyCaret results using scikit-learn.
This is my script, after running Pycaret's setup and obtaining the transformed data:
compare_modelsreturn these results:but I get these results from sklearn:
Just to clarify, I am using the exactly same CV splitter on both PyCaret and sklearn.
cv = RepeatedStratifiedKFold(n_splits=5, n_repeats=10, random_state=42)The setup used was:
Any idea on what might be happening?
All reactions