使用 Scikit-learn 构建机器学习管道

Machine LearningMachine LearningBeginner
立即练习

This tutorial is from open-source community. Access the source code

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

简介

在机器学习中,管道(pipeline)是一系列按顺序执行的步骤,用于转换输入数据,然后构建模型。Scikit-learn 提供了一个管道类,可用于将多个处理步骤链接在一起,从而轻松构建涉及多个预处理和建模步骤的复杂模型。

在本教程中,我们将演示如何使用 Scikit-learn 构建一个包含特征选择和支持向量机(SVM)分类的管道。我们将展示如何在管道中集成特征选择以防止过拟合,以及如何检查管道以更好地理解模型。

虚拟机提示

虚拟机启动完成后,点击左上角切换到“笔记本”标签,以访问 Jupyter Notebook 进行练习。

有时,你可能需要等待几秒钟让 Jupyter Notebook 完成加载。由于 Jupyter Notebook 的限制,操作验证无法自动化。

如果你在学习过程中遇到问题,请随时向 Labby 提问。课程结束后提供反馈,我们将立即为你解决问题。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL sklearn(("Sklearn")) -.-> sklearn/CoreModelsandAlgorithmsGroup(["Core Models and Algorithms"]) sklearn(("Sklearn")) -.-> sklearn/DataPreprocessingandFeatureEngineeringGroup(["Data Preprocessing and Feature Engineering"]) sklearn(("Sklearn")) -.-> sklearn/ModelSelectionandEvaluationGroup(["Model Selection and Evaluation"]) sklearn(("Sklearn")) -.-> sklearn/UtilitiesandDatasetsGroup(["Utilities and Datasets"]) ml(("Machine Learning")) -.-> ml/FrameworkandSoftwareGroup(["Framework and Software"]) sklearn/CoreModelsandAlgorithmsGroup -.-> sklearn/svm("Support Vector Machines") sklearn/DataPreprocessingandFeatureEngineeringGroup -.-> sklearn/feature_selection("Feature Selection") sklearn/DataPreprocessingandFeatureEngineeringGroup -.-> sklearn/pipeline("Pipeline") sklearn/ModelSelectionandEvaluationGroup -.-> sklearn/model_selection("Model Selection") sklearn/ModelSelectionandEvaluationGroup -.-> sklearn/metrics("Metrics") sklearn/UtilitiesandDatasetsGroup -.-> sklearn/datasets("Datasets") ml/FrameworkandSoftwareGroup -.-> ml/sklearn("scikit-learn") subgraph Lab Skills sklearn/svm -.-> lab-49126{{"使用 Scikit-learn 构建机器学习管道"}} sklearn/feature_selection -.-> lab-49126{{"使用 Scikit-learn 构建机器学习管道"}} sklearn/pipeline -.-> lab-49126{{"使用 Scikit-learn 构建机器学习管道"}} sklearn/model_selection -.-> lab-49126{{"使用 Scikit-learn 构建机器学习管道"}} sklearn/metrics -.-> lab-49126{{"使用 Scikit-learn 构建机器学习管道"}} sklearn/datasets -.-> lab-49126{{"使用 Scikit-learn 构建机器学习管道"}} ml/sklearn -.-> lab-49126{{"使用 Scikit-learn 构建机器学习管道"}} end

生成并分割数据集

我们将首先使用 Scikit-learn 的make_classification函数生成一个二元分类数据集。我们还将使用 Scikit-learn 的train_test_split函数将数据集拆分为训练子集和测试子集。

from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split

X, y = make_classification(
    n_features=20,
    n_informative=3,
    n_redundant=0,
    n_classes=2,
    n_clusters_per_class=2,
    random_state=42,
)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)

构建管道

现在我们将构建一个由两个步骤组成的管道:特征选择和支持向量机(SVM)分类。我们将使用 Scikit-learn 的SelectKBest函数进行特征选择,并使用 Scikit-learn 的LinearSVC函数进行 SVM 分类。SelectKBest函数基于f_classif方法选择k个最具信息量的特征,该方法计算每个特征与目标变量之间的方差分析 F 值。在这个例子中,我们将k设置为 3。

from sklearn.feature_selection import SelectKBest, f_classif
from sklearn.pipeline import make_pipeline
from sklearn.svm import LinearSVC

anova_filter = SelectKBest(f_classif, k=3)
clf = LinearSVC(dual="auto")
anova_svm = make_pipeline(anova_filter, clf)

训练管道

现在我们将使用fit方法在训练子集上训练管道。在训练过程中,SelectKBest函数将基于方差分析 F 值选择 3 个最具信息量的特征,而LinearSVC函数将在所选特征上训练一个线性支持向量机分类器。

anova_svm.fit(X_train, y_train)

评估管道

现在我们将使用predict方法在测试子集上评估管道。管道将基于方差分析 F 值选择 3 个最具信息量的特征,并且LinearSVC函数将对所选特征进行预测。

from sklearn.metrics import classification_report

y_pred = anova_svm.predict(X_test)
print(classification_report(y_test, y_pred))

检查管道

我们可以检查管道以更好地理解模型。我们可以使用所选特征的索引来检索原始特征名称。

anova_svm[:-1].inverse_transform(anova_svm[-1].coef_)

总结

在本教程中,我们展示了如何使用 Scikit-learn 构建一个包含特征选择和支持向量机(SVM)分类的管道。我们展示了如何在管道中集成特征选择以防止过拟合,以及如何检查管道以更好地理解模型。管道是一种以模块化和高效的方式构建复杂模型的强大工具。