官方文档请戳这里????:https://epistasislab.github.io/tpot/
现有的大部分博文都是参考官方文档的,还是直接看最正宗的吧,看英文还是需要些耐心的吧,哈哈哈~
0. 概述
它一种基于遗传算法优化机器学习管道(pipeline)的Python自动机器学习工具。
简单来说,就是TPOT可以智能地探索数千个可能的pipeline,为数据集找到最好的pipeline,从而实现机器学习中最乏味的部分。
由上图可知,TPOT可以自动实现阴影部分的特征工作,包含特征选择、特征预处理、特征构建、同时还可以进行模型选择和参数调优。
最主要的是,一旦TPOT完成搜索,TPOT同时还提供Python代码,方便后续修改。(此处如果有疑惑,可以看下下面的栗子很快就明白了)
1. 以IRIS数据集为例,使用TPOT
TPOT的接口,与scikit-learn很类似,熟练使用过scikit-learn的同学可以很快上手。
直接上代码:
# 直接导入即可,此处使用的是分类器
from tpot import TPOTClassifier
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
import numpy as np
# 导入数据集,构建训练和测试样本
iris = load_iris()
X_train,X_test,y_train,y_test = train_test_split(iris.data,iris.target,train_size=0.75,test_size=0.25)
# 建模,拟合,预测
tpot = TPOTClassifier(generations=5,population_size=20,verbosity=2)
tpot.fit(X_train,y_train)
print(tpot.score(X_test,y_test))
# 此处,对应于 "一旦TPOT完成搜索,TPOT同时还提供Python代码"
tpot.export('tpot_iris_pipeline.py')
输出,红线部分对应与最优pipeline和参数:
TPOT提供的python代码:
import numpy as np
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.pipeline import make_pipeline, make_union
from sklearn.tree import DecisionTreeClassifier
from tpot.builtins import StackingEstimator
# NOTE: Make sure that the class is labeled 'target' in the data file
tpot_data = pd.read_csv('PATH/TO/DATA/FILE', sep='COLUMN_SEPARATOR', dtype=np.float64)
features = tpot_data.drop('target', axis=1).values
training_features, testing_features, training_target, testing_target = \
train_test_split(features, tpot_data['target'].values, random_state=None)
# Average CV score on the training set was:0.9731225296442687
exported_pipeline = make_pipeline(
StackingEstimator(estimator=GaussianNB()),
StackingEstimator(estimator=DecisionTreeClassifier(criterion="gini", max_depth=1, min_samples_leaf=2, min_samples_split=2)),
LogisticRegression(C=20.0, dual=False, penalty="l1")
)
exported_pipeline.fit(training_features, training_target)
results = exported_pipeline.predict(testing_features)
2. TPOT API
参数:
-
generations: int, optional (default=100),运行管道优化过程的迭代次数。一定是正数。一般来说,值越大,性能越好。 -
population_size: int, optional (default=100),在每一代遗传中保留的个体数。一定是正数。一般来说,值越大,性能越好。 -
offspring_size: int, optional (default=100),在每一次遗传过程中产生的后代数量。一定是正数。 -
mutation_rate: float, optional (default=0.9),变异率,采用默认值即可。 -
crossover_rate: float, optional (default=0.1),交叉率,采用默认值即可。 -
scoring: string or callable, optional (default='neg_mean_squared_error'),回归问题中用于评估给定管道的质量的函数。可以使用以下内置评分函数:'neg_median_absolute_error', 'neg_mean_absolute_error', 'neg_mean_squared_error', 'r2' cv: int, cross-validation generator, or an iterable, optional (default=5)-
subsample: float, optional (default=1.0),在TPOT优化过程中使用的训练样本的比例。必须在0到1之间。 n_jobs: integer, optional (default=1)-
max_time_mins: integer or None, optional (default=None),TPOT需要多少分钟来优化管道。 -
max_eval_time_mins: integer, optional (default=5),TPOT需要多少分钟来评估一个管道。 -
random_state: integer or None, optional (default=None),使用这个参数来确保TPOT每次运行时都会有相同的结果。 -
config_dict: Python dictionary, string, or None, optional (default=None),用于定制TPOT在优化过程中搜索的操作符和参数的配置字典。 -
warm_start: boolean, optional (default=False),表明TPOT实例是否会重用以前调用fit()的入口。 early_stop: integer, optional (default: None)-
verbosity: integer, optional (default=0),- 0将不会打印任何东西
- 1将打印很少的信息
- 2打印更多的信息并提供一个进度条
- 3打印所有内容,并提供一个进度条
方法
-
fit(features, target, sample_weight=None, groups=None),在给定的训练数据上运行TPOT优化过程。 -
predict(features),使用优化的管道来预测测试集的目标值。 -
score(testing_features, testing_target),使用用户指定的评分函数在给定的测试数据上返回优化的管道的得分。 -
export(output_file_name),将优化的管道导出为Python代码。
3. TPOT缺点
用过grid-search的同学一定知道,在参数非常多的情况下,是相当耗时的,由此我们可以看出TPOT问题——耗时。
以下给出解释:
搜索整个管道空间是特别耗时的。认识到原因是必要的,在默认的TPOT参数下(100 generations with 100 population size),TPOT将在完成前评估1万个管道配置。考虑一个网格搜索1万个超参数组合用于机器学习算法以及网格搜索需要多长时间。用10倍的交叉验证来评估这1万个模型,这意味着大约有10万个模型在一个网格搜索的训练数据中被匹配和评估。这是一个耗时的过程,即使对于像决策树这样的简单模型也是如此。
典型的TPOT运行将需要数小时到数天才能完成(除非是一个小数据集),但是可以中断运行,并看到目前为止最好的结果。TPOT还提供warm_start参数,可以从中断的地方重新启动之前运行的TPOT。
亲亲,面对这样的问题,这边给出的建议如下:
在做数据挖掘问题,可以尝试在数据清洗之后,抽样小部分数据跑一下TPOT,得到一个baseline,效果可能还不错。