1

I want to save the model comparison data frame from compare_models() in pycaret.

# load dataset
from pycaret.datasets import get_data
diabetes = get_data('diabetes')

# init setup
from pycaret.classification import *
clf1 = setup(data = diabetes, target = 'Class variable')

# compare models
best = compare_models()

enter image description here

i.e. this data frame as shown above.

Does anyone know how to do that?

Johnny Tam
  • 423
  • 4
  • 16

2 Answers2

4

The solution is :

df = pull()

by Goosang Yu from the pycaret slack community.

Johnny Tam
  • 423
  • 4
  • 16
  • I would also add that `pull()` should be called just after `compare_models()` as `pull()` returns the latest displayed table as a dataframe. Additionally, if you have defined an experiment in a variable, like this `exp_1 = ClassificationExperiment()` do not forget to run `exp_1.pull()` instead. Most of the documentation usually do not save the Pycaret experiment in a variable so in the code examples they avoid it as Pycaret infers it from the environment. I prefer to use the Pycaret experiment in a variable and I share this advice if anyone else uses it too. – Álvaro H.G Mar 24 '23 at 08:53
-1

compare_models() returns a pandas dataframe, containing the information of the list of models. Hence, you only need to save a dataframe, which can be for example achieved with best.to_csv(path). If you want to save the object in a different format (pickle, xml, ...), you can refer to pandas i/o documentation.

Benjamin Rio
  • 652
  • 2
  • 17
  • Hi Benjamin. Seems compare_models() returns the best model object but not a pandas df. Therefore this method will give: --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) in () ----> 1 best.to_csv("compare_models.csv") AttributeError: 'LogisticRegression' object has no attribute 'to_csv' – Johnny Tam Jun 13 '22 at 08:40