预报名 | 结构模型、Stata 实证前沿、Python 数据挖掘国庆工作坊 (opens new window)
机器学习是我们使用一组算法解决来解决生活中问题的过程。创建机器学习模型很容易,但选择在泛化和性能方面都最适合的模型是一项艰巨的任务。
有多种机器学习算法可用于回归和分类,可根据我们要解决的问题来选择,但选择合适的模型是一个需要高计算成本、时间和精力的过程。
为解决上述问题,今天我给大家分享一款非常棒的工具包:FLAML,它是一个由微软开源的轻量级 Python 库,有助于自动、高效地找出最佳机器学习模型,不仅速度快,节省时间,而且设计轻巧。
让我们详细的介绍一下它吧…
安装所需的库
我们将首先使用 pip 安装来安装 FLAML。下面给出的命令将使用 pip 安装。
pip install flaml
导入所需的库
在这一步中,我们将导入创建机器学习模型和下载数据集所需的所有库。
from flaml import AutoML
解决分类问题
现在我们将从解决分类问题开始。我们将在这里使用的数据是著名的 Iris 数据集,可以从 Seaborn 库轻松加载。让我们开始创建模型。
#Loading the Dataset
from sklearn.datasets import load_iris
2
为 Automl 创建实例很重要,同时也定义 Automl 设置,因此在这一步中,我们还将创建 Automl 实例并定义设置。
automl = AutoML()
automl_settings = {
"time_budget": 10, # in seconds
"metric": 'accuracy',
"task": 'classification'
}
2
3
4
5
6
接下来,我们将拆分数据并将其拟合到模型中。最后,我们还将使用模型进行预测并找到最佳模型。
X_train, y_train = load_iris(return_X_y=True)
# Train with labeled input data
automl.fit(X_train=X_train, y_train=y_train,
**automl_settings)
print(automl.predict_proba(X_train).shape)
# Export the best model
print(automl.model)
2
3
4
5
6
7
在这里,我们可以清楚地看到 ExtraTreeEstimator 是此数据的最佳模型。现在让我们打印模型的最佳超参数和准确性。
print('Best ML leaner:', automl.best_estimator)
print('Best hyperparmeter config:', automl.best_config)
print('Best accuracy on validation data: {0:.4g}'.format(1-automl.best_loss))
print('Training duration of best run: {0:.4g} s'.format(automl.best_config_train_time))
2
3
4
同样,对于回归问题,我们也将遵循相同的过程。
解决回归问题
现在将解决一个回归问题。我们将在这里使用的数据是著名的波士顿数据集,可以从 Seaborn 库轻松加载。我们可以遵循与分类问题完全相同的过程。
from sklearn.datasets import load_boston
automl = AutoML()
automl_settings = {
"time_budget": 10, # in seconds
"metric": 'r2',
"task": 'regression'
}
X_train, y_train = load_boston(return_X_y=True)
# Train with labeled input data
automl.fit(X_train=X_train, y_train=y_train,
**automl_settings)
# Predict
print(automl.predict(X_train).shape)
# Export the best model
print(automl.model)
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
print('Best ML leaner:', automl.best_estimator)
print('Best hyperparmeter config:', automl.best_config)
print('Best accuracy on validation data: {0:.4g}'.format(1-automl.best_loss))
print('Training duration of best run: {0:.4g} s'.format(automl.best_config_train_time))
2
3
4
在这里,我们也可以清楚地看到回归问题的最佳模型和超参数。同样,你可以对你关注的数据集执行此过程,并找到最佳模型和超参数。
# 近期文章
腾讯课堂 | Python 网络爬虫与文本数据分析 (opens new window)
读完本文你就了解什么是文本分析 (opens new window)
文本分析在经管领域中的应用概述 (opens new window)
综述: 文本分析在市场营销研究中的应用 (opens new window)
文本分析方法在《管理世界》(2021.5)中的应用 (opens new window)
B 站视频 | Python 自动化办公 (opens new window)
wordexpansion 包 | 新增词向量法构建领域词典 (opens new window)
语法最简单的微博通用爬虫 weibo_crawler (opens new window)
hiResearch 定义自己的科研首页 (opens new window)
大邓 github 汇总, 觉得有用记得 star (opens new window)
whatlies 包 | 简单玩转词向量可视化 (opens new window)
multistop ~ 多语言停用词库 (opens new window)
Jaal 库 轻松绘制动态社交网络关系图 (opens new window)
SciencePlots | 科研样式绘图库 (opens new window)
使用 streamlit 上线中文文本分析网站 (opens new window)
爬虫实战 | 采集 & 可视化知乎问题的回答 (opens new window)
Clumper | dplyr 式的 Python 数据操作包 (opens new window)
Clumper 库 | 常用的数据操作函数 (opens new window)
Clumper 库 | Groupby 具体案例用法 (opens new window)
Clumper 库 | 其他数据分析 (opens new window)
plydata 库 | 数据操作管道操作符 >> (opens new window)
plotnine: Python 版的 ggplot2 作图库 (opens new window)
Wow~70G 上市公司定期报告数据集 (opens new window)
漂亮~ pandas 可以无缝衔接 Bokeh (opens new window)
YelpDaset: 酒店管理类数据集 10+G (opens new window)
全文完
本文由 简悦 SimpRead (opens new window) 优化,用以提升阅读体验
使用了 全新的简悦词法分析引擎 beta,点击查看 (opens new window)详细说明