Machine learning (ML) is the process of constructing a mathematical model of a system based on a sample dataset collected from that system.
One of the main goals of training an ML model is to teach the model to separate the signal present in the data from the noise inherent in system and in the data collection process. If this is done effectively, the model can then be used to make accurate predictions about the system when presented with new, similar data. Additionally, introspecting on an ML model can reveal key information about the system being modeled, such as which inputs and transformations of the inputs are most useful to the ML model for learning the signal in the data, and are therefore the most predictive.
There are a variety of ML problem types. Supervised learning describes the case where the collected data contains an output value to be modeled and a set of inputs with which to train the model. EvalML focuses on training supervised learning models.
EvalML supports three common supervised ML problem types. The first is regression, where the target value to model is a continuous numeric value. Next are binary and multiclass classification, where the target value to model consists of two or more discrete values or categories. The choice of which supervised ML problem type is most appropriate depends on domain expertise and on how the model will be evaluated and used.
AutoML is the process of automating the construction, training and evaluation of ML models. Given a data and some configuration, AutoML searches for the most effective and accurate ML model or models to fit the dataset. During the search, AutoML will explore different combinations of model type, model parameters and model architecture.
An effective AutoML solution offers several advantages over constructing and tuning ML models by hand. AutoML can assist with many of the difficult aspects of ML, such as avoiding overfitting and underfitting, imbalanced data, detecting data leakage and other potential issues with the problem setup, and automatically applying best-practice data cleaning, feature engineering, feature selection and various modeling techniques. AutoML can also leverage search algorithms to optimally sweep the hyperparameter search space, resulting in model performance which would be difficult to achieve by manual training.
EvalML supports all of the above and more.
In its simplest usage, the AutoML search interface requires only the input data, the target data and a problem_type specifying what kind of supervised ML problem to model.
problem_type
** Graphing methods, like AutoMLSearch, on Jupyter Notebook and Jupyter Lab require ipywidgets to be installed.
** If graphing on Jupyter Lab, jupyterlab-plotly required. To download this, make sure you have npm installed.
[1]:
import evalml X, y = evalml.demos.load_breast_cancer() automl = evalml.automl.AutoMLSearch(problem_type='binary') automl.search(X, y)
Using default limit of max_iterations=5. Generating pipelines to search over... ***************************** * Beginning pipeline search * ***************************** Optimizing for Log Loss Binary. Lower score is better. Searching up to 5 pipelines. Allowed model families: random_forest, lightgbm, xgboost, linear_model, catboost, extra_trees
(1/5) Mode Baseline Binary Classification P... Elapsed:00:00 Starting cross validation Finished cross validation - mean Log Loss Binary: 12.868 (2/5) LightGBM Classifier w/ Imputer Elapsed:00:00 Starting cross validation Finished cross validation - mean Log Loss Binary: 0.138 (3/5) Extra Trees Classifier w/ Imputer Elapsed:00:00 Starting cross validation Finished cross validation - mean Log Loss Binary: 0.146 (4/5) Elastic Net Classifier w/ Imputer + S... Elapsed:00:02 Starting cross validation Finished cross validation - mean Log Loss Binary: 0.504 (5/5) CatBoost Classifier w/ Imputer Elapsed:00:02 Starting cross validation Finished cross validation - mean Log Loss Binary: 0.390 Search finished after 00:02 Best pipeline: LightGBM Classifier w/ Imputer Best pipeline Log Loss Binary: 0.137999
The AutoML search will log its progress, reporting each pipeline and parameter set evaluated during the search.
There are a number of mechanisms to control the AutoML search time. One way is to set the maximum number of candidate models to be evaluated during AutoML using max_iterations. By default, AutoML will search a fixed number of iterations and parameter pairs (max_iterations=5). The first pipeline to be evaluated will always be a baseline model representing a trivial solution.
max_iterations
max_iterations=5
The AutoML interface supports a variety of other parameters. For a comprehensive list, please refer to the API reference.
EvalML includes a simple method, detect_problem_type, to help determine the problem type given the target data.
detect_problem_type
This function can return the predicted problem type as a ProblemType enum, choosing from ProblemType.BINARY, ProblemType.MULTICLASS, and ProblemType.REGRESSION. If the target data is invalid (for instance when there is only 1 unique label), the function will throw an error instead.
[2]:
import pandas as pd from evalml.problem_types import detect_problem_type y = pd.Series([0, 1, 1, 0, 1, 1]) detect_problem_type(y)
<ProblemTypes.BINARY: 'binary'>
AutoMLSearch takes in an objective parameter to determine which objective to optimize for. By default, this parameter is set to auto, which allows AutoML to choose LogLossBinary for binary classification problems, LogLossMulticlass for multiclass classification problems, and R2 for regression problems.
objective
auto
LogLossBinary
LogLossMulticlass
R2
It should be noted that the objective parameter is only used in ranking and helping choose the pipelines to iterate over, but is not used to optimize each individual pipeline during fit-time.
To get the default objective for each problem type, you can use the get_default_primary_search_objective function.
get_default_primary_search_objective
[3]:
from evalml.automl import get_default_primary_search_objective binary_objective = get_default_primary_search_objective("binary") multiclass_objective = get_default_primary_search_objective("multiclass") regression_objective = get_default_primary_search_objective("regression") print(binary_objective.name) print(multiclass_objective.name) print(regression_objective.name)
Log Loss Binary Log Loss Multiclass R2
EvalML’s AutoML algorithm generates a set of pipelines to search with. To provide a custom set instead, set allowed_pipelines to a list of custom pipeline classes. Note: this will prevent AutoML from generating other pipelines to search over.
[4]:
from evalml.pipelines import MulticlassClassificationPipeline class CustomMulticlassClassificationPipeline(MulticlassClassificationPipeline): component_graph = ['Simple Imputer', 'Random Forest Classifier'] automl_custom = evalml.automl.AutoMLSearch(problem_type='multiclass', allowed_pipelines=[CustomMulticlassClassificationPipeline])
Using default limit of max_iterations=5.
To stop the search early, hit Ctrl-C. This will bring up a prompt asking for confirmation. Responding with y will immediately stop the search. Responding with n will continue the search.
Ctrl-C
y
n
A summary of all the pipelines built can be returned as a pandas DataFrame which is sorted by score. The score column contains the average score across all cross-validation folds while the validation_score column is computed from the first cross-validation fold.
[5]:
automl.rankings
Each pipeline is given an id. We can get more information about any particular pipeline using that id. Here, we will get more information about the pipeline with id = 1.
id
id = 1
[6]:
automl.describe_pipeline(1)
********************************** * LightGBM Classifier w/ Imputer * ********************************** Problem Type: binary Model Family: LightGBM Pipeline Steps ============== 1. Imputer * categorical_impute_strategy : most_frequent * numeric_impute_strategy : mean * categorical_fill_value : None * numeric_fill_value : None 2. LightGBM Classifier * boosting_type : gbdt * learning_rate : 0.1 * n_estimators : 100 * max_depth : 0 * num_leaves : 31 * min_child_samples : 20 * n_jobs : -1 Training ======== Training for binary problems. Total training time (including CV): 0.5 seconds Cross Validation ---------------- High variance within cross validation scores. Model may not perform as estimated on unseen data. Log Loss Binary MCC Binary AUC Precision F1 Balanced Accuracy Binary Accuracy Binary # Training # Testing 0 0.177 0.899 0.991 0.943 0.936 0.948 0.953 379.000 190.000 1 0.104 0.955 0.995 1.000 0.971 0.972 0.979 379.000 190.000 2 0.133 0.909 0.986 0.943 0.943 0.955 0.958 380.000 189.000 mean 0.138 0.921 0.991 0.962 0.950 0.958 0.963 - - std 0.036 0.030 0.005 0.033 0.018 0.012 0.014 - - coef of var 0.264 0.033 0.005 0.034 0.019 0.013 0.015 - -
We can get the object of any pipeline via their id as well:
[7]:
pipeline = automl.get_pipeline(1) print(pipeline.name) print(pipeline.parameters)
LightGBM Classifier w/ Imputer {'Imputer': {'categorical_impute_strategy': 'most_frequent', 'numeric_impute_strategy': 'mean', 'categorical_fill_value': None, 'numeric_fill_value': None}, 'LightGBM Classifier': {'boosting_type': 'gbdt', 'learning_rate': 0.1, 'n_estimators': 100, 'max_depth': 0, 'num_leaves': 31, 'min_child_samples': 20, 'n_jobs': -1}}
If we specifically want to get the best pipeline, there is a convenient accessor for that.
[8]:
best_pipeline = automl.best_pipeline print(best_pipeline.name) print(best_pipeline.parameters)
The AutoMLSearch class records detailed results information under the results field, including information about the cross-validation scoring and parameters.
AutoMLSearch
results
[9]:
automl.results
{'pipeline_results': {0: {'id': 0, 'pipeline_name': 'Mode Baseline Binary Classification Pipeline', 'pipeline_class': evalml.pipelines.classification.baseline_binary.ModeBaselineBinaryPipeline, 'pipeline_summary': 'Baseline Classifier', 'parameters': {'Baseline Classifier': {'strategy': 'mode'}}, 'score': 12.868443394958925, 'high_variance_cv': False, 'training_time': 0.05095219612121582, 'cv_data': [{'all_objective_scores': OrderedDict([('Log Loss Binary', 12.906595389677152), ('MCC Binary', 0.0), ('AUC', 0.5), ('Precision', 0.0), ('F1', 0.0), ('Balanced Accuracy Binary', 0.5), ('Accuracy Binary', 0.6263157894736842), ('# Training', 379), ('# Testing', 190)]), 'score': 12.906595389677152, 'binary_classification_threshold': 0.5}, {'all_objective_scores': OrderedDict([('Log Loss Binary', 12.906595389677149), ('MCC Binary', 0.0), ('AUC', 0.5), ('Precision', 0.0), ('F1', 0.0), ('Balanced Accuracy Binary', 0.5), ('Accuracy Binary', 0.6263157894736842), ('# Training', 379), ('# Testing', 190)]), 'score': 12.906595389677149, 'binary_classification_threshold': 0.5}, {'all_objective_scores': OrderedDict([('Log Loss Binary', 12.792139405522475), ('MCC Binary', 0.0), ('AUC', 0.5), ('Precision', 0.0), ('F1', 0.0), ('Balanced Accuracy Binary', 0.5), ('Accuracy Binary', 0.6296296296296297), ('# Training', 380), ('# Testing', 189)]), 'score': 12.792139405522475, 'binary_classification_threshold': 0.5}], 'percent_better_than_baseline': 0, 'validation_score': 12.906595389677152}, 1: {'id': 1, 'pipeline_name': 'LightGBM Classifier w/ Imputer', 'pipeline_class': evalml.pipelines.utils.make_pipeline.<locals>.GeneratedPipeline, 'pipeline_summary': 'LightGBM Classifier w/ Imputer', 'parameters': {'Imputer': {'categorical_impute_strategy': 'most_frequent', 'numeric_impute_strategy': 'mean', 'categorical_fill_value': None, 'numeric_fill_value': None}, 'LightGBM Classifier': {'boosting_type': 'gbdt', 'learning_rate': 0.1, 'n_estimators': 100, 'max_depth': 0, 'num_leaves': 31, 'min_child_samples': 20, 'n_jobs': -1}}, 'score': 0.13799860263727523, 'high_variance_cv': True, 'training_time': 0.5173044204711914, 'cv_data': [{'all_objective_scores': OrderedDict([('Log Loss Binary', 0.17663855005522583), ('MCC Binary', 0.8985734479173947), ('AUC', 0.9911232098473193), ('Precision', 0.9428571428571428), ('F1', 0.9361702127659575), ('Balanced Accuracy Binary', 0.9479820097052906), ('Accuracy Binary', 0.9526315789473684), ('# Training', 379), ('# Testing', 190)]), 'score': 0.17663855005522583, 'binary_classification_threshold': 0.5}, {'all_objective_scores': OrderedDict([('Log Loss Binary', 0.1042038854081209), ('MCC Binary', 0.9554966130892879), ('AUC', 0.9952657119185703), ('Precision', 1.0), ('F1', 0.9710144927536231), ('Balanced Accuracy Binary', 0.971830985915493), ('Accuracy Binary', 0.9789473684210527), ('# Training', 379), ('# Testing', 190)]), 'score': 0.1042038854081209, 'binary_classification_threshold': 0.5}, {'all_objective_scores': OrderedDict([('Log Loss Binary', 0.1331533724484789), ('MCC Binary', 0.9092436974789916), ('AUC', 0.9861944777911165), ('Precision', 0.9428571428571428), ('F1', 0.9428571428571428), ('Balanced Accuracy Binary', 0.9546218487394957), ('Accuracy Binary', 0.9576719576719577), ('# Training', 380), ('# Testing', 189)]), 'score': 0.1331533724484789, 'binary_classification_threshold': 0.5}], 'percent_better_than_baseline': 98.9276200826952, 'validation_score': 0.17663855005522583}, 2: {'id': 2, 'pipeline_name': 'Extra Trees Classifier w/ Imputer', 'pipeline_class': evalml.pipelines.utils.make_pipeline.<locals>.GeneratedPipeline, 'pipeline_summary': 'Extra Trees Classifier w/ Imputer', 'parameters': {'Imputer': {'categorical_impute_strategy': 'most_frequent', 'numeric_impute_strategy': 'mean', 'categorical_fill_value': None, 'numeric_fill_value': None}, 'Extra Trees Classifier': {'n_estimators': 100, 'max_features': 'auto', 'max_depth': 6, 'min_samples_split': 2, 'min_weight_fraction_leaf': 0.0, 'n_jobs': -1}}, 'score': 0.14606590090087035, 'high_variance_cv': False, 'training_time': 1.170452356338501, 'cv_data': [{'all_objective_scores': OrderedDict([('Log Loss Binary', 0.15683251263377887), ('MCC Binary', 0.8778182058245897), ('AUC', 0.989821280624926), ('Precision', 0.9838709677419355), ('F1', 0.9172932330827067), ('Balanced Accuracy Binary', 0.9253757841164635), ('Accuracy Binary', 0.9421052631578948), ('# Training', 379), ('# Testing', 190)]), 'score': 0.15683251263377887, 'binary_classification_threshold': 0.5}, {'all_objective_scores': OrderedDict([('Log Loss Binary', 0.1314416667412096), ('MCC Binary', 0.9010215251215669), ('AUC', 0.995384069120606), ('Precision', 1.0), ('F1', 0.9323308270676691), ('Balanced Accuracy Binary', 0.9366197183098591), ('Accuracy Binary', 0.9526315789473684), ('# Training', 379), ('# Testing', 190)]), 'score': 0.1314416667412096, 'binary_classification_threshold': 0.5}, {'all_objective_scores': OrderedDict([('Log Loss Binary', 0.14992352332762263), ('MCC Binary', 0.9092436974789916), ('AUC', 0.9879951980792318), ('Precision', 0.9428571428571428), ('F1', 0.9428571428571428), ('Balanced Accuracy Binary', 0.9546218487394957), ('Accuracy Binary', 0.9576719576719577), ('# Training', 380), ('# Testing', 189)]), 'score': 0.14992352332762263, 'binary_classification_threshold': 0.5}], 'percent_better_than_baseline': 98.8649295301863, 'validation_score': 0.15683251263377887}, 3: {'id': 3, 'pipeline_name': 'Elastic Net Classifier w/ Imputer + Standard Scaler', 'pipeline_class': evalml.pipelines.utils.make_pipeline.<locals>.GeneratedPipeline, 'pipeline_summary': 'Elastic Net Classifier w/ Imputer + Standard Scaler', 'parameters': {'Imputer': {'categorical_impute_strategy': 'most_frequent', 'numeric_impute_strategy': 'mean', 'categorical_fill_value': None, 'numeric_fill_value': None}, 'Elastic Net Classifier': {'alpha': 0.5, 'l1_ratio': 0.5, 'n_jobs': -1, 'max_iter': 1000, 'penalty': 'elasticnet', 'loss': 'log'}}, 'score': 0.5037404207981783, 'high_variance_cv': False, 'training_time': 0.17138457298278809, 'cv_data': [{'all_objective_scores': OrderedDict([('Log Loss Binary', 0.5106495939683122), ('MCC Binary', 0.5269050806054695), ('AUC', 0.9815362764824239), ('Precision', 1.0), ('F1', 0.5510204081632654), ('Balanced Accuracy Binary', 0.6901408450704225), ('Accuracy Binary', 0.7684210526315789), ('# Training', 379), ('# Testing', 190)]), 'score': 0.5106495939683122, 'binary_classification_threshold': 0.5}, {'all_objective_scores': OrderedDict([('Log Loss Binary', 0.5088061082724736), ('MCC Binary', 0.5269050806054695), ('AUC', 0.9914782814534264), ('Precision', 1.0), ('F1', 0.5510204081632654), ('Balanced Accuracy Binary', 0.6901408450704225), ('Accuracy Binary', 0.7684210526315789), ('# Training', 379), ('# Testing', 190)]), 'score': 0.5088061082724736, 'binary_classification_threshold': 0.5}, {'all_objective_scores': OrderedDict([('Log Loss Binary', 0.49176556015374906), ('MCC Binary', 0.6324555320336759), ('AUC', 0.9854741896758704), ('Precision', 1.0), ('F1', 0.6792452830188679), ('Balanced Accuracy Binary', 0.7571428571428571), ('Accuracy Binary', 0.8201058201058201), ('# Training', 380), ('# Testing', 189)]), 'score': 0.49176556015374906, 'binary_classification_threshold': 0.5}], 'percent_better_than_baseline': 96.0854595591918, 'validation_score': 0.5106495939683122}, 4: {'id': 4, 'pipeline_name': 'CatBoost Classifier w/ Imputer', 'pipeline_class': evalml.pipelines.utils.make_pipeline.<locals>.GeneratedPipeline, 'pipeline_summary': 'CatBoost Classifier w/ Imputer', 'parameters': {'Imputer': {'categorical_impute_strategy': 'most_frequent', 'numeric_impute_strategy': 'mean', 'categorical_fill_value': None, 'numeric_fill_value': None}, 'CatBoost Classifier': {'n_estimators': 10, 'eta': 0.03, 'max_depth': 6, 'bootstrap_type': None, 'silent': True, 'allow_writing_files': False}}, 'score': 0.3901007526814435, 'high_variance_cv': False, 'training_time': 0.44793009757995605, 'cv_data': [{'all_objective_scores': OrderedDict([('Log Loss Binary', 0.3976961654004939), ('MCC Binary', 0.8425009463611858), ('AUC', 0.9835483489170316), ('Precision', 0.9523809523809523), ('F1', 0.8955223880597014), ('Balanced Accuracy Binary', 0.9099301692507988), ('Accuracy Binary', 0.9263157894736842), ('# Training', 379), ('# Testing', 190)]), 'score': 0.3976961654004939, 'binary_classification_threshold': 0.5}, {'all_objective_scores': OrderedDict([('Log Loss Binary', 0.39058702666377215), ('MCC Binary', 0.9216584956231404), ('AUC', 0.9958574979287491), ('Precision', 0.9848484848484849), ('F1', 0.948905109489051), ('Balanced Accuracy Binary', 0.9535447982009706), ('Accuracy Binary', 0.9631578947368421), ('# Training', 379), ('# Testing', 190)]), 'score': 0.39058702666377215, 'binary_classification_threshold': 0.5}, {'all_objective_scores': OrderedDict([('Log Loss Binary', 0.38201906598006447), ('MCC Binary', 0.9218075091290715), ('AUC', 0.9885954381752702), ('Precision', 0.9315068493150684), ('F1', 0.9510489510489512), ('Balanced Accuracy Binary', 0.9647058823529412), ('Accuracy Binary', 0.9629629629629629), ('# Training', 380), ('# Testing', 189)]), 'score': 0.38201906598006447, 'binary_classification_threshold': 0.5}], 'percent_better_than_baseline': 96.9685474714505, 'validation_score': 0.3976961654004939}}, 'search_order': [0, 1, 2, 3, 4]}