PyCaret Official
Searchโ€ฆ
LEARN PYCARET
๐Ÿ“–
Official Blog
Machine Learning in Alteryx with PyCaret

Machine Learning in Alteryx with PyCaret

A step-by-step tutorial on training and deploying machine learning models in Alteryx Designer using PyCaret

Introduction

In this tutorial, I will show you how you can train and deploy machine learning pipelines in a very popular ETL tool Alteryx using PyCaret โ€” an open-source, low-code machine learning library in Python. The Learning Goals of this tutorial are:
๐Ÿ‘‰ What is PyCaret and how to get started?
๐Ÿ‘‰ What is Alteryx Designer and how to set it up?
๐Ÿ‘‰ Train end-to-end machine learning pipeline in Alteryx Designer including data preparation such as missing value imputation, one-hot-encoding, scaling, transformations, etc.
๐Ÿ‘‰ Deploy trained pipeline and generate inference during ETL.

PyCaret

โ€‹PyCaret is an open-source, low-code machine learning library and end-to-end model management tool built-in Python for automating machine learning workflows. PyCaret is known for its ease of use, simplicity, and ability to quickly and efficiently build and deploy end-to-end machine learning pipelines. To learn more about PyCaret, check out their GitHub.

Alteryx Designer

โ€‹Alteryx Designer is a proprietary tool developed by **Alteryx** and is used for automating every step of analytics, including data preparation, blending, reporting, predictive analytics, and data science. You can access any data source, file, application, or data type, and experience the simplicity and power of a self-service platform with 260+ drag-and-drop building blocks. You can download the one-month free trial version of Alteryx Designer from here.
https://www.alteryx.com

Tutorial Pre-Requisites:

For this tutorial, you will need two things. The first one being the Alteryx Designer which is a desktop software that you can download from here. Second, you need Python. The easiest way to get Python is to download Anaconda Distribution. To download that, click here.

๐Ÿ‘‰We are ready now

Open Alteryx Designer and click on File โ†’ New Workflow
New Workflow in Alteryx Designer
On the top, there are tools that you can drag and drop on the canvas and execute the workflow by connecting each component to one another.

Dataset

For this tutorial, I am using a regression dataset from PyCaretโ€™s repository called insurance. You can download the data from here.
Sample Dataset
I will create two separate Alteryx workflows. First one for model training and selection and the second one for scoring the new data using the trained pipeline.

๐Ÿ‘‰ Model Training & Selection

Letโ€™s first read the CSV file from the **Input Data **tool followed by a **Python Script. **Inside the Python script execute the following code:
1
**# install pycaret
2
**from ayx import Package
3
Package.installPackages('pycaret')
4
โ€‹
5
**# read data from input data tool**
6
from ayx import Alteryx
7
data = Alteryx.read("#1")
8
โ€‹
9
**# init setup, prepare data**
10
from pycaret.regression import *
11
s = setup(data, target = 'charges', silent=True)
12
โ€‹
13
**# model training and selection
14
**best = compare_models()
15
โ€‹
16
**# store the results, print and save**
17
results = pull()
18
results.to_csv('c:/users/moezs/pycaret-demo-alteryx/results.csv', index = False)
19
Alteryx.write(results, 1)
20
โ€‹
21
**# finalize best model and save**
22
best_final = finalize_model(best)
23
save_model(best_final, 'c:/users/moezs/pycaret-demo-alteryx/pipeline')
Copied!
This script is importing the regression module from pycaret, then initializing the setup function which automatically handles train_test_split and all the data preparation tasks such as missing value imputation, scaling, feature engineering, etc. compare_models trains and evaluates all the estimators using kfold cross-validation and returns the best model.
pull function calls the model performance metric as a Dataframe which is then saved as results.csv on a drive and also written to the first anchor of Python tool in Alteryx (so that you can view results on screen).
Finally, save_model saves the entire transformation pipeline including the best model as a pickle file.
Training Workflow
When you successfully execute this workflow, you will generate pipeline.pkl and results.csv file. You can see the output of the best models and their cross-validated metrics on-screen as well.
This is what results.csv contains:
These are the cross-validated metrics for all the models. The best model, in this case, is Gradient Boosting Regressor.

๐Ÿ‘‰ Model Scoring

We can now use our pipeline.pkl to score on the new dataset. Since I do not have a separate dataset for ***insurance.csv without the label, ***what I will do is drop the target column i.e. charges, and then generate predictions using the trained pipeline.
Scoring Workflow
I have used the **Select Tool **to remove the target column i.e. charges . In the Python script execute the following code:
1
**# read data from the input tool**
2
from ayx import Alteryx**
3
**data = Alteryx.read("#1")
4
โ€‹
5
**# load pipeline
6
**from pycaret.regression import load_model, predict_model
7
pipeline = load_model('c:/users/moezs/pycaret-demo-alteryx/pipeline')
8
โ€‹
9
**# generate predictions and save to csv
10
**predictions = predict_model(pipeline, data)
11
predictions.to_csv('c:/users/moezs/pycaret-demo-alteryx/predictions.csv', index=False)
12
โ€‹
13
**# display in alteryx
14
**Alteryx.write(predictions, 1)
Copied!
When you successfully execute this workflow, it will generate predictions.csv.
predictions.csv

Coming Soon!

Next week I will take a deep dive and focus on more advanced functionalities of PyCaret that you can use within Alteryx to enhance your machine learning workflows. If you would like to be notified automatically, you can follow me on Medium, LinkedIn, and Twitter.
PyCaret โ€” Image by Author
PyCaret โ€” Image by Author
There is no limit to what you can achieve using this lightweight workflow automation library in Python. If you find this useful, please do not forget to give us โญ๏ธ on our GitHub repository.
To hear more about PyCaret follow us on LinkedIn and Youtube.
Join us on our slack channel. Invite link here.