一种快速、简单的机器学习算法训练方法
mlautomator的Python项目详细描述
ML自动机
作者:Kevin Vecmanis
机器学习自动机(ml automator)是一个自动化项目,它将基于顺序模型的优化(smbo)与python的sci工具包learn library中的主要学习算法集成在一起,以生成一个非常快速的,自动调整机器学习算法的工具。mlautomator利用名为hyperopt的库来实现这一点。在此处阅读有关hyperopt的更多信息
什么是smbo?
smbo是超参数优化的一种形式,类似于网格搜索和随机搜索。然而,与网格和随机搜索相比,smbo使用了贝叶斯优化来建立概率模型,通过反复试验,能够更好地预测哪些超参数可能产生更好的模型。"序贯"的意思是每次都要进行多次试验,通过应用贝叶斯推理和更新现有的概率模型来测试更好的超参数。
这里的折衷是smbo模型在每次迭代之间花费更多的时间"选择"下一次超参数的选择,但这是可以接受的,因为选择下一次超参数所花费的额外时间通常比每次训练迭代都要少得多。换句话说,smbo的结果是:
- 与网格和随机搜索方法相比,缩短了调整超参数的时间。
- 在测试中取得更好的成绩。
安装:
安装很简单-当然可以用pip
替换pip3
或pipenv
(在虚拟环境中)
pip安装mlautomator
主要功能:
- 对数据预处理和功能选择进行优化此外还对超参数进行优化。
- 使用hyperopt快速智能扫描参数空间
- 优化的参数搜索允许在同一时间段内扫描更大的算法截面。
- 一种特殊的抽查算法。
用法
mlautomator接受一个训练数据集x和一个目标y。用户可以为如何生成这些数据集定义自己的函数。请注意,mlautomator被设计为一种高度优化的抽查算法-您应该注意确保您的数据没有错误,并且已处理了任何丢失的值。
mlautomator将找到转换和预处理数据的方法,以生成一个更好的模型。在将数据传递给mlautomator之前,您可以自由地进行自己的转换。
可选数据实用程序
我正在构建一套数据实用函数,可以准备大多数分类和回归数据集。然而,这些是可选的-mlautomator只需要x和y以numpy ndarray的形式输入。
fromdata.utilitiesimportclf_prepx,y=clf_prep('pima-indians-diabetes.csv')
一旦你有了训练和目标数据,这是使用mlautomator的主要调用…
分类示例:2类
frommlautomator.mlautomatorimportMLAutomatorautomator=MLAutomator(x,y,iterations=25)automator.find_best_algorithm()automator.print_best_space()
mlautomator通常可以在网格搜索或随机搜索的一小部分时间内找到~98%的解。在这里,它对6种常见机器学习算法的所有超参数进行了全面扫描,并为经典的Pima Indians糖尿病数据集产生了卓越的模型性能。
Best Algorithm Configuration:
Best algorithm: Logistic Regression
Best accuracy : 77.73239917976761%
C : 0.02341
k_best : 6
penalty : l2
scaler : RobustScaler(
copy=True,
quantile_range=(25.0, 75.0),
with_centering=True,
with_scaling=True)
solver : lbfgs
Found best solution on iteration 132 of 150
Validation used: 10-fold cross-validation
分类示例:多类
下面是经典iris数据集的结果,这是一个具有三个类的多类分类问题
fromdata.utilitiesimportfrom_sklearnfrommlautomator.mlautomatorimportMLAutomatorx,y=from_sklearn('iris')automator=MLAutomator(x,y,iterations=30,algo_type='classifier',score_metric='accuracy')automator.find_best_algorithm()automator.print_best_space()
Best Algorithm Configuration:
Best algorithm: Bag of Support Vector Machine Classifiers
Best accuracy : 96.67%
C : 0.7064
degree : 2
gamma : auto
k_best : 2
kernel : rbf
n_estimators : 9
probability : True
scaler : None
Found best solution on iteration 3 of 30
Validation used: 10-fold cross-validation
回归示例
ml自动机也支持回归问题。在本例中,我们使用一个实用函数从sklearn.datasets中调用波士顿住房数据集。
fromdata.utilitiesimportfrom_sklearnx,y=from_sklearn('boston')
frommlautomator.mlautomatorimportMLAutomatorautomator=MLAutomator(x,y,iterations=30,algo_type='regressor',score_metric='neg_mean_squared_error')automator.find_best_algorithm()automator.print_best_space()
Best Algorithm Configuration:
Best algorithm: K-Neighbor Regressor
Best neg_mean_squared_error : 10.41395782834094
algorithm : kd_tree
k_best : 11
n_neighbors : 2
scaler : StandardScaler(copy=True, with_mean=True, with_std=True)
weights : distance
Found best solution on iteration 24 of 30
Validation used: 10-fold cross-validation
模型持久性
ml automator允许您保存fit、保存和加载由find_best_algorithm()
方法发现的最佳管道。完整的工作流程如下:
fromdata.utilitiesimportclf_prepfrommlautomator.mlautomatorimportMLAutomatorx,y=clf_prep('pima-indians-diabetes.csv')automator=MLAutomator(x,y,iterations=30,algo_type='classifier',score_metric='accuracy')automator.find_best_algorithm()automator.fit_best_pipeline()automator.save_best_pipeline('Path/to/your/directory')# some time later....automator.load_best_pipeline('Path/to/your/directory')
请注意,mlautomator为您存储了整个转换/特征选择/模型管道,因此当您需要对样本外数据进行预测时,无需执行任何先决条件处理。
现有算法支持
mlautomator当前支持以下算法:
分类:
- XGBoost分类器
- 随机森林分类器
- 支持向量机
- 朴素贝叶斯分类器
- 随机梯度下降分类(SGD)
- k近邻分类
- 逻辑回归
回归:
- xgboost回归器
- 随机森林回归器
- 支持向量机回归
- SGD回归
- k近邻回归
除非使用特定算法参数声明,否则mlautomator将扫描所有算法以找到最佳性能。