本文总结了常见的AutoML库,可供大家选择。

LightAutoML

项目链接:https://github.com/sberbank-ai-lab/LightAutoML

推荐指数:⭐⭐⭐

LightAutoML是基于Python环境下的结构化自动机器学习库,现在支持的任务有:

  • 二分类
  • 多分类
  • 回归任务

LightAutoML现在只支持单表单记录的形式,即每一行由样本的特征和标签组成。

import pandas as pd
from sklearn.metrics import f1_score

from lightautoml.automl.presets.tabular_presets import TabularAutoML
from lightautoml.tasks import Task

df_train = pd.read_csv('../input/titanic/train.csv')
df_test = pd.read_csv('../input/titanic/test.csv')

automl = TabularAutoML(
    task = Task(
        name = 'binary',
        metric = lambda y_true, y_pred: f1_score(y_true, (y_pred > 0.5)*1))
)
oof_pred = automl.fit_predict(
    df_train,
    roles = {'target': 'Survived', 'drop': ['PassengerId']}
)
test_pred = automl.predict(df_test)

pd.DataFrame({
    'PassengerId':df_test.PassengerId,
    'Survived': (test_pred.data[:, 0] > 0.5)*1
}).to_csv('submit.csv', index = False)

H2O AutoML

项目链接:https://docs.h2o.ai/h2o/latest-stable/h2o-docs/automl.html

推荐指数:⭐⭐⭐⭐

H2O AutoML是基于Python环境和R环境下的结构化自动机器学习库,支持分布式部署,对模型调参、模型选择和特征筛选支持比较完备,但使用起来比较复杂。

import h2o
from h2o.automl import H2OAutoML

h2o.init()
train_hf = h2o.H2OFrame(train_df.copy())
test_hf = h2o.H2OFrame(test_df.copy())

train_hf[target_column] = train_hf[target_column].asfactor()

aml = H2OAutoML(
    seed=2021, 
    max_runtime_secs=100,
    nfolds = 3,
    exclude_algos = ["DeepLearning"]
)

aml.train(
    x=list(feature_columns), 
    y=target_column, 
    training_frame=train_hf
)

preds = aml.predict(h2o.H2OFrame(test_df[feature_columns].copy()))
preds_df = h2o.as_list(preds)
preds_df

submission[['Class_1', 'Class_2', 'Class_3', 'Class_4']] = preds_df[['Class_1', 'Class_2', 'Class_3', 'Class_4']]
submission.to_csv('h2o_automl_300s.csv', index=False)
submission.head()

MLJAR AutoML

项目链接:https://github.com/mljar/mljar-supervised

推荐指数:⭐⭐⭐⭐

MLJAR AutoML是基于Python环境下的结构化自动机器学习库,所支持的机器学习模型非常多,且对模型可视化支持的非常好。

from supervised.automl import AutoML # mljar-supervised
automl = AutoML(
    mode="Compete", 
    eval_metric="f1",
    total_time_limit=300,
    features_selection=False # switch off feature selection
)
automl.fit(
    train[feature_cols], 
    train[target_column]
)

preds = automl.predict(test[feature_cols])

submission['Survived'] = preds
submission.to_csv('mljar_automl_300s_f1_metric.csv', index=False)
submission.head()

PyCaret

项目链接:https://pycaret.org/

推荐指数:⭐⭐⭐⭐⭐

PyCaret是基于Python环境下的结构化自动机器学习库,支持的任务包括:

  • 分类
  • 回归
  • 聚类
  • 异常检测
  • NLP
  • 关联规则

PyCaret支持的模型比较多,项目也比较活跃,但对模型的可视化做的不够。

from pycaret.classification import *
from category_encoders.cat_boost import CatBoostEncoder

cat_train_df = train_df.copy()
cat_test_df = test_df.copy()

ce = CatBoostEncoder()

cols_to_encode = ['name', 'sex', 'ticket', 'cabin', 'embarked']
cat_train_df[pure_cat_cols] = ce.fit_transform(cat_train_df[pure_cat_cols], cat_train_df[target_column])
cat_test_df[pure_cat_cols] = ce.transform(cat_test_df[pure_cat_cols])

setup(
    data = cat_train_df[feature_cols.to_list() + [target_column]], 
    target = target_column,
    fold = 3,
    silent = True,
)

best_models = compare_models(
    sort='F1', 
    n_select=3, 
    budget_time=300,
) # we will use it later

best = automl(optimize = 'F1')

EvalML: AutoML

项目链接:https://evalml.alteryx.com/en/latest/

推荐指数:⭐⭐⭐

EvalML是一款比较模块比较完备的自动机器学习框架,支持分类、回归和时间序列任务。但提出的时间稍晚,所以使用的人很少。

from evalml.automl import AutoMLSearch
X = train_df.drop(columns=[target_column, 'passengerid'])
y = train_df[target_column]

X_train,X_test,y_train,y_test = train_test_split(X, y, test_size=0.2)
automl = AutoMLSearch(
    X_train=X_train, 
    y_train=y_train, 
    problem_type='binary',
    random_seed=2021,
    max_time=300,
)

automl.search()
pipeline = automl.best_pipeline
pipeline.fit(X, y)

TPOT: Genetic Approach

项目链接:http://epistasislab.github.io/tpot/

推荐指数:⭐⭐⭐

TPOT是一款非常轻量级的自动机器学习框架,利用遗传算法可以快读完成特征的构造。但TPOT所支持的功能较少,所以场景有限。

from tpot import TPOTClassifier
from sklearn.model_selection import train_test_split

tpot = TPOTClassifier(generations=5, population_size=50, verbosity=2, random_state=42)
tpot.fit(X_train, y_train)
print(tpot.score(X_test, y_test))
tpot.export('tpot_digits_pipeline.py')

FLAML

项目链接:https://github.com/microsoft/FLAML

推荐指数:⭐⭐⭐⭐

FLAML是由微软提出的自动机器学习库,支持分类和回归任务。FLAML对特征的构造和搜索支持的比较好,非常轻量。

from flaml import AutoML
from sklearn.datasets import load_boston

automl = AutoML()

# Specify automl goal and constraint
automl_settings = {
    "time_budget": 300,  # in seconds
    "metric": 'accuracy',
    "task": 'classification',
}
automl.fit(
    X_train=train_df[feature_cols], 
    y_train=train_df[target_column],
    **automl_settings
)

print(automl.predict_proba(train_df[feature_cols]))