鸢尾花数据集上支持向量分类器的特征选择

Machine LearningMachine LearningBeginner
立即练习

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

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

简介

本实验展示了如何在运行支持向量分类器(SVC)之前执行单变量特征选择,以提高分类分数。我们将使用鸢尾花数据集(4个特征)并添加36个无信息特征。我们会发现,当我们选择大约10%的特征时,我们的模型能达到最佳性能。

虚拟机使用提示

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

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

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


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL sklearn(("Sklearn")) -.-> sklearn/ModelSelectionandEvaluationGroup(["Model Selection and Evaluation"]) sklearn(("Sklearn")) -.-> sklearn/UtilitiesandDatasetsGroup(["Utilities and Datasets"]) ml(("Machine Learning")) -.-> ml/FrameworkandSoftwareGroup(["Framework and Software"]) sklearn(("Sklearn")) -.-> sklearn/CoreModelsandAlgorithmsGroup(["Core Models and Algorithms"]) sklearn(("Sklearn")) -.-> sklearn/DataPreprocessingandFeatureEngineeringGroup(["Data Preprocessing and Feature Engineering"]) sklearn/CoreModelsandAlgorithmsGroup -.-> sklearn/svm("Support Vector Machines") sklearn/DataPreprocessingandFeatureEngineeringGroup -.-> sklearn/preprocessing("Preprocessing and Normalization") sklearn/DataPreprocessingandFeatureEngineeringGroup -.-> sklearn/feature_selection("Feature Selection") sklearn/DataPreprocessingandFeatureEngineeringGroup -.-> sklearn/pipeline("Pipeline") sklearn/ModelSelectionandEvaluationGroup -.-> sklearn/model_selection("Model Selection") sklearn/UtilitiesandDatasetsGroup -.-> sklearn/datasets("Datasets") ml/FrameworkandSoftwareGroup -.-> ml/sklearn("scikit-learn") subgraph Lab Skills sklearn/svm -.-> lab-49306{{"鸢尾花数据集上支持向量分类器的特征选择"}} sklearn/preprocessing -.-> lab-49306{{"鸢尾花数据集上支持向量分类器的特征选择"}} sklearn/feature_selection -.-> lab-49306{{"鸢尾花数据集上支持向量分类器的特征选择"}} sklearn/pipeline -.-> lab-49306{{"鸢尾花数据集上支持向量分类器的特征选择"}} sklearn/model_selection -.-> lab-49306{{"鸢尾花数据集上支持向量分类器的特征选择"}} sklearn/datasets -.-> lab-49306{{"鸢尾花数据集上支持向量分类器的特征选择"}} ml/sklearn -.-> lab-49306{{"鸢尾花数据集上支持向量分类器的特征选择"}} end

加载数据

我们首先加载鸢尾花数据集,并向其中添加36个无信息特征。

import numpy as np
from sklearn.datasets import load_iris

X, y = load_iris(return_X_y=True)

## Add non-informative features
rng = np.random.RandomState(0)
X = np.hstack((X, 2 * rng.random((X.shape[0], 36))))

创建管道

接下来,我们创建一个管道,它由一个特征选择变换、一个缩放器和一个支持向量机(SVM)实例组成,我们将它们组合在一起以形成一个功能完备的估计器。

from sklearn.pipeline import Pipeline
from sklearn.feature_selection import SelectPercentile, f_classif
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC

clf = Pipeline(
    [
        ("anova", SelectPercentile(f_classif)),
        ("scaler", StandardScaler()),
        ("svc", SVC(gamma="auto")),
    ]
)

绘制交叉验证分数

我们绘制交叉验证分数作为所选特征百分比的函数。

import matplotlib.pyplot as plt
from sklearn.model_selection import cross_val_score

score_means = list()
score_stds = list()
percentiles = (1, 3, 6, 10, 15, 20, 30, 40, 60, 80, 100)

for percentile in percentiles:
    clf.set_params(anova__percentile=percentile)
    this_scores = cross_val_score(clf, X, y)
    score_means.append(this_scores.mean())
    score_stds.append(this_scores.std())

plt.errorbar(percentiles, score_means, np.array(score_stds))
plt.title("Performance of the SVM-Anova varying the percentile of features selected")
plt.xticks(np.linspace(0, 100, 11, endpoint=True))
plt.xlabel("Percentile")
plt.ylabel("Accuracy Score")
plt.axis("tight")
plt.show()

总结

在本实验中,我们学习了如何在运行支持向量分类器(SVC)之前执行单变量特征选择,以提高分类分数。我们使用了鸢尾花数据集(4个特征)并添加了36个无信息特征。我们发现,当我们选择大约10%的特征时,我们的模型取得了最佳性能。