Start

In this guide, we’ll show how you can use EvalML to automatically find the best pipeline for predicting whether a patient has breast cancer. Along the way, we’ll highlight EvalML’s built-in tools and features for understanding and interacting with the search process.

[1]:
import evalml
from evalml import AutoMLSearch
2020-08-06 20:14:53,855 featuretools - WARNING    Featuretools failed to load plugin nlp_primitives from library nlp_primitives. For a full stack trace, set logging to debug.
/home/docs/checkouts/readthedocs.org/user_builds/feature-labs-inc-evalml/envs/v0.12.2/lib/python3.7/site-packages/evalml/pipelines/components/transformers/preprocessing/text_featurizer.py:31: RuntimeWarning: No text columns were given to TextFeaturizer, component will have no effect
  warnings.warn("No text columns were given to TextFeaturizer, component will have no effect", RuntimeWarning)

First, we load in the features and outcomes we want to use to train our model.

[2]:
X, y = evalml.demos.load_breast_cancer()

EvalML has many options to configure the pipeline search. At the minimum, we need to define an objective function. For simplicity, we will use the F1 score in this example. However, the real power of EvalML is in using domain-specific objective functions or building your own.

Below EvalML utilizes Bayesian optimization (EvalML’s default optimizer) to search and find the best pipeline defined by the given objective.

[3]:
automl = AutoMLSearch(problem_type="binary", objective="f1", max_pipelines=5)

In order to validate the results of the pipeline creation and optimization process, we will save some of our data as a holdout set.

[4]:
X_train, X_holdout, y_train, y_holdout = evalml.preprocessing.split_data(X, y, test_size=.2)

When we call search(), the search for the best pipeline will begin. There is no need to wrangle with missing data or categorical variables as EvalML includes various preprocessing steps (like imputation, one-hot encoding, feature selection) to ensure you’re getting the best results. As long as your data is in a single table, EvalML can handle it. If not, you can reduce your data to a single table by utilizing Featuretools and its Entity Sets.

You can find more information on pipeline components and how to integrate your own custom pipelines into EvalML here.

[5]:
automl.search(X_train, y_train)
Generating pipelines to search over...
*****************************
* Beginning pipeline search *
*****************************

Optimizing for F1.
Greater score is better.

Searching up to 5 pipelines.
Allowed model families: linear_model, catboost, xgboost, random_forest

(1/5) Mode Baseline Binary Classification P... Elapsed:00:00
        Starting cross validation
        Finished cross validation - mean F1: 0.000
(2/5) CatBoost Classifier w/ Imputer           Elapsed:00:00
        Starting cross validation
        Finished cross validation - mean F1: 0.902
(3/5) XGBoost Classifier w/ Imputer            Elapsed:00:00
        Starting cross validation
        Finished cross validation - mean F1: 0.937
(4/5) Random Forest Classifier w/ Imputer      Elapsed:00:32
        Starting cross validation
        Finished cross validation - mean F1: 0.926
(5/5) Logistic Regression Classifier w/ Imp... Elapsed:00:34
        Starting cross validation
        Finished cross validation - mean F1: 0.964

Search finished after 00:36
Best pipeline: Logistic Regression Classifier w/ Imputer + Standard Scaler
Best pipeline F1: 0.964381

After the search is finished we can view all of the pipelines searched, ranked by score. Internally, EvalML performs cross validation to score the pipelines. If it notices a high variance across cross validation folds, it will warn you. EvalML also provides additional data checks to analyze your data to assist you in producing the best performing pipeline.

[6]:
automl.rankings
[6]:
id pipeline_name score high_variance_cv parameters
0 4 Logistic Regression Classifier w/ Imputer + St... 0.964381 False {'Imputer': {'categorical_impute_strategy': 'm...
1 2 XGBoost Classifier w/ Imputer 0.937138 False {'Imputer': {'categorical_impute_strategy': 'm...
2 3 Random Forest Classifier w/ Imputer 0.925641 False {'Imputer': {'categorical_impute_strategy': 'm...
3 1 CatBoost Classifier w/ Imputer 0.902283 False {'Imputer': {'categorical_impute_strategy': 'm...
4 0 Mode Baseline Binary Classification Pipeline 0.000000 False {'Baseline Classifier': {'strategy': 'mode'}}

If we are interested in see more details about the pipeline, we can view a summary description using the id from the rankings table:

[7]:
automl.describe_pipeline(3)
***************************************
* Random Forest Classifier w/ Imputer *
***************************************

Problem Type: Binary Classification
Model Family: Random Forest

Pipeline Steps
==============
1. Imputer
         * categorical_impute_strategy : most_frequent
         * numeric_impute_strategy : mean
         * fill_value : None
2. Random Forest Classifier
         * n_estimators : 100
         * max_depth : 6
         * n_jobs : -1

Training
========
Training for Binary Classification problems.
Total training time (including CV): 1.6 seconds

Cross Validation
----------------
               F1  Accuracy Binary  Balanced Accuracy Binary  Precision   AUC  Log Loss Binary  MCC Binary # Training # Testing
0           0.944            0.961                     0.947      1.000 0.993            0.115       0.917    303.000   152.000
1           0.903            0.928                     0.921      0.911 0.989            0.140       0.845    303.000   152.000
2           0.930            0.947                     0.947      0.914 0.991            0.122       0.888    304.000   151.000
mean        0.926            0.945                     0.938      0.942 0.991            0.125       0.883          -         -
std         0.021            0.017                     0.015      0.051 0.002            0.013       0.036          -         -
coef of var 0.023            0.017                     0.016      0.054 0.002            0.104       0.041          -         -

We can also view the pipeline parameters directly:

[8]:
pipeline = automl.get_pipeline(3)
print(pipeline.parameters)
{'Imputer': {'categorical_impute_strategy': 'most_frequent', 'numeric_impute_strategy': 'mean', 'fill_value': None}, 'Random Forest Classifier': {'n_estimators': 100, 'max_depth': 6, 'n_jobs': -1}}

We can now select the best pipeline and score it on our holdout data:

[9]:
pipeline = automl.best_pipeline
pipeline.fit(X_train, y_train)
pipeline.score(X_holdout, y_holdout, ["f1"])
[9]:
OrderedDict([('F1', 0.9879518072289156)])

We can also visualize the structure of the components contained by the pipeline:

[10]:
pipeline.graph()
[10]:
_images/start_20_0.svg