Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
169 views

Write and Train Your Own Custom Machine Learning Models Using PyCaret

The document discusses how to write and train custom machine learning models using the PyCaret library. It demonstrates how to import various pre-existing model types like decision trees, XGBoost, and neural networks into PyCaret and train them on sample insurance data. It also shows how to define a custom model class in scikit-learn format and train that using PyCaret utilities. The goal is to provide an accessible framework for exploring both standard and custom ML algorithms.

Uploaded by

Teto Schedule
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
169 views

Write and Train Your Own Custom Machine Learning Models Using PyCaret

The document discusses how to write and train custom machine learning models using the PyCaret library. It demonstrates how to import various pre-existing model types like decision trees, XGBoost, and neural networks into PyCaret and train them on sample insurance data. It also shows how to define a custom model class in scikit-learn format and train that using PyCaret utilities. The goal is to provide an accessible framework for exploring both standard and custom ML algorithms.

Uploaded by

Teto Schedule
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Write and train your own custom machine learning models us... https://towardsdatascience.com/write-and-train-your-own-cu...

1 of 18 9/28/2021, 1:40 PM
Write and train your own custom machine learning models us... https://towardsdatascience.com/write-and-train-your-own-cu...

2 of 18 9/28/2021, 1:40 PM
Write and train your own custom machine learning models us... https://towardsdatascience.com/write-and-train-your-own-cu...

# install slim version (default)


pip install pycaret

# install the full version


pip install pycaret[full]

# read data from pycaret repo


from pycaret.datasets import get_data
data = get_data('insurance')

3 of 18 9/28/2021, 1:40 PM
Write and train your own custom machine learning models us... https://towardsdatascience.com/write-and-train-your-own-cu...

setup

# initialize setup
from pycaret.regression import *
s = setup(data, target = 'charges')

setup

4 of 18 9/28/2021, 1:40 PM
Write and train your own custom machine learning models us... https://towardsdatascience.com/write-and-train-your-own-cu...

models

# check all the available models


models()

5 of 18 9/28/2021, 1:40 PM
Write and train your own custom machine learning models us... https://towardsdatascience.com/write-and-train-your-own-cu...

create_model

# train decision tree


dt = create_model('dt')

6 of 18 9/28/2021, 1:40 PM
Write and train your own custom machine learning models us... https://towardsdatascience.com/write-and-train-your-own-cu...

scikit-learn

print(dt)

7 of 18 9/28/2021, 1:40 PM
Write and train your own custom machine learning models us... https://towardsdatascience.com/write-and-train-your-own-cu...

# train multiple models


multiple_models = [create_model(i) for i in ['dt', 'lr',
'xgboost']]

# check multiple_models
type(multiple_models), len(multiple_models)
>>> (list, 3)

print(multiple_models)

compare_models

# compare all models


best_model = compare_models()

8 of 18 9/28/2021, 1:40 PM
Write and train your own custom machine learning models us... https://towardsdatascience.com/write-and-train-your-own-cu...

compare_models

# check the best model


print(best_model)

9 of 18 9/28/2021, 1:40 PM
Write and train your own custom machine learning models us... https://towardsdatascience.com/write-and-train-your-own-cu...

best_model

# predict on hold-out
pred_holdout = predict_model(best_model)

predict_model data

# create copy of data drop target column


data2 = data.copy()
data2.drop('charges', axis=1, inplace=True)

# generate predictions
predictions = predict_model(best_model, data = data2)

10 of 18 9/28/2021, 1:40 PM
Write and train your own custom machine learning models us... https://towardsdatascience.com/write-and-train-your-own-cu...

sklearn

gplearn

11 of 18 9/28/2021, 1:40 PM
Write and train your own custom machine learning models us... https://towardsdatascience.com/write-and-train-your-own-cu...

gplearn

# install gplearn
pip install gplearn

create_model

# import untrained estimator


from gplearn.genetic import SymbolicRegressor
sc = SymbolicRegressor()

# train using create_model


sc_trained = create_model(sc)

12 of 18 9/28/2021, 1:40 PM
Write and train your own custom machine learning models us... https://towardsdatascience.com/write-and-train-your-own-cu...

print(sc_trained)

13 of 18 9/28/2021, 1:40 PM
Write and train your own custom machine learning models us... https://towardsdatascience.com/write-and-train-your-own-cu...

# check hold-out score


pred_holdout_sc = predict_model(sc_trained)

# install ngboost
pip install ngboost

create_model

# import untrained estimator


from ngboost import NGBRegressor
ng = NGBRegressor()

# train using create_model


ng_trained = create_model(ng)

14 of 18 9/28/2021, 1:40 PM
Write and train your own custom machine learning models us... https://towardsdatascience.com/write-and-train-your-own-cu...

print(ng_trained)

15 of 18 9/28/2021, 1:40 PM
Write and train your own custom machine learning models us... https://towardsdatascience.com/write-and-train-your-own-cu...

gplearn ngboost

sklearn

target

fit

# create custom estimator


import numpy as np
from sklearn.base import BaseEstimator

class MyOwnModel(BaseEstimator):

def __init__(self):
self.mean = 0

def fit(self, X, y):


self.mean = y.mean()
return self

def predict(self, X):


return np.array(X.shape[0]*[self.mean])

# import MyOwnModel class


mom = MyOwnModel()

# train using create_model


mom_trained = create_model(mom)

16 of 18 9/28/2021, 1:40 PM
Write and train your own custom machine learning models us... https://towardsdatascience.com/write-and-train-your-own-cu...

# generate predictions on data


predictions = predict_model(mom_trained, data=data)

17 of 18 9/28/2021, 1:40 PM
Write and train your own custom machine learning models us... https://towardsdatascience.com/write-and-train-your-own-cu...

Label

18 of 18 9/28/2021, 1:40 PM

You might also like