Iris データセットにおける SVC のための特徴選択

Machine LearningMachine LearningBeginner
今すぐ練習

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

💡 このチュートリアルは英語版からAIによって翻訳されています。原文を確認するには、 ここをクリックしてください

はじめに

この実験では、サポートベクトル分類器(SVC)を実行する前に単変量特徴選択を行い、分類スコアを向上させる方法を示します。irisデータセット(4つの特徴)を使用し、36の非情報的な特徴を追加します。特徴の約10%を選択するときに、モデルが最良の性能を達成することがわかります。

VMのヒント

VMの起動が完了したら、左上隅をクリックしてノートブックタブに切り替え、Jupyter Notebookを使用して練習します。

Jupyter Notebookが読み込み終了するまで数秒待つ必要がある場合があります。Jupyter Notebookの制限により、操作の検証を自動化することはできません。

学習中に問題に遭遇した場合は、Labbyにお問い合わせください。セッション後にフィードバックを提供してください。すぐに問題を解決いたします。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL 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(("Sklearn")) -.-> sklearn/CoreModelsandAlgorithmsGroup(["Core Models and Algorithms"]) 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{{"Iris データセットにおける SVC のための特徴選択"}} sklearn/preprocessing -.-> lab-49306{{"Iris データセットにおける SVC のための特徴選択"}} sklearn/feature_selection -.-> lab-49306{{"Iris データセットにおける SVC のための特徴選択"}} sklearn/pipeline -.-> lab-49306{{"Iris データセットにおける SVC のための特徴選択"}} sklearn/model_selection -.-> lab-49306{{"Iris データセットにおける SVC のための特徴選択"}} sklearn/datasets -.-> lab-49306{{"Iris データセットにおける SVC のための特徴選択"}} ml/sklearn -.-> lab-49306{{"Iris データセットにおける SVC のための特徴選択"}} end

データの読み込み

まず、irisデータセットを読み込み、それに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)を実行する前に単変量特徴選択を行い、分類スコアを向上させる方法を学びました。irisデータセット(4つの特徴)を使用し、36の非情報的な特徴を追加しました。特徴の約10%を選択するときに、モデルが最良の性能を達成することがわかりました。