Skip to content

Latest commit

 

History

History
124 lines (78 loc) · 3.23 KB

PyCaret_automl_classification.md

File metadata and controls

124 lines (78 loc) · 3.23 KB



Template request | Bug report | Generate Data Product

Tags: #automl #pandas #snippet #classification #dataframe #visualize #pycaret #operations

Author: Minura Punchihewa

Description: This notebook demonstrates how to use PyCaret to quickly and easily build and evaluate machine learning models for classification tasks.

Input

Import libraries

import pandas as pd
try:
    from pycaret.classification import setup, compare_models, evaluate_model, predict_model, finalize_model, \
     save_model, load_model, create_docker
except:
    ! pip install --user pycaret
    from pycaret.classification import setup, compare_models, evaluate_model, predict_model, finalize_model, \
     save_model, load_model, create_docker
    

Variables

csv_path = "https://raw.githubusercontent.com/MinuraPunchihewa/pycaret-automl/main/data/iris.csv"
target_column = "variety"

Model

Read the CSV from path

df = pd.read_csv(csv_path)

View a sample of the data

df.head()

Setup the dataset

# must be called before executing any other function
# can configure many types of transformation operations
# by default Missing Value Imputation, One-Hot Encoding and Train-Test Split operations will be performed
# press enter to continue
grid = setup(data=df, target=target_column)

Train and compare all supported models

# uses cross-validation
best_model = compare_models()

Report the best model

print(best_model)

Evaluate the model using a number of different plots

# click on the different plot types to exlpore
# some plots may not work depending on the data and the model
evaluate_model(best_model)

Make predictions on new data

# data should be a DataFrame without label
# predict_model(best_model, new_data)

Finalize model

# trains the model on the entire dataset including the hold-out set
# does not change any parameter of the model
final_model = finalize_model(best_model)

Output

Save model as a pickle file

save_model(final_model, "classification_model")

Load saved model from pickle file

model = load_model("classification_model")

Create Dockerfile for model

# also creates a requirements.txt file for dependencies
create_docker("classification_model")