# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Introduction to Pycaret

# ## Collecting data

# +
from pycaret.datasets import get_data

data = get_data('credit')
# -

data.shape

# +
test = data.sample(frac=0.1)
train = data.drop(test.index)

test.reset_index(inplace=True, drop=True)
train.reset_index(inplace=True, drop=True)
# -

print(test.shape)
print(train.shape)

test.head()

train.head()

# ## Setting up the Pycaret Environment

from pycaret.classification import setup, compare_models, models, create_model, predict_model
from pycaret.classification import tune_model, plot_model, evaluate_model, finalize_model
from pycaret.classification import save_model, load_model

clf = setup(data=train, target="default")

model = compare_models(sort="Accuracy")

print(model)

models()

ridgeclf = create_model("ridge")

print(ridgeclf)

# ## Hyperparameter tuning

tuned_ridge = tune_model(ridgeclf, optimize="Accuracy")

print(tuned_ridge)

# ## Plotting model

plot_model(tuned_ridge, plot="confusion_matrix")

plot_model(tuned_ridge, plot="feature")

plot_model(tuned_ridge, plot="class_report")

evaluate_model(ridgeclf)

# ## Finalize

predict_model(tuned_ridge)

final_ridge = finalize_model(tuned_ridge)

from pycaret.utils import check_metric

# ## Saving models

save_model(final_ridge, "ridge_model")

saved_model = load_model("ridge_model")

type(saved_model)

predictions = predict_model(saved_model, test)

predictions

check_metric(predictions.default, predictions.Label.astype(int), metric="Accuracy")

StarcoderdataJupyterScriptsDedupFiltered