Feature Selection für SVC auf Iris-Datensatz

Machine LearningMachine LearningBeginner
Jetzt üben

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

💡 Dieser Artikel wurde von AI-Assistenten übersetzt. Um die englische Version anzuzeigen, können Sie hier klicken

Einführung

In diesem Lab wird gezeigt, wie die univariate Feature-Selektion durchgeführt werden kann, bevor ein Support Vector Classifier (SVC) ausgeführt wird, um die Klassifizierungsergebnisse zu verbessern. Wir werden den Iris-Datensatz (4 Features) verwenden und 36 nicht-informative Features hinzufügen. Wir werden feststellen, dass unser Modell die beste Leistung erzielt, wenn wir etwa 10% der Features auswählen.

Tipps für die VM

Nachdem der VM-Start abgeschlossen ist, klicken Sie in der oberen linken Ecke, um zur Registerkarte Notebook zu wechseln und Jupyter Notebook für die Übung zu öffnen.

Manchmal müssen Sie einige Sekunden warten, bis Jupyter Notebook vollständig geladen ist. Die Validierung von Vorgängen kann aufgrund von Einschränkungen in Jupyter Notebook nicht automatisiert werden.

Wenn Sie bei der Lernphase Probleme haben, können Sie Labby gerne fragen. Geben Sie nach der Sitzung Feedback ab, und wir werden das Problem für Sie prompt beheben.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL 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(("Sklearn")) -.-> sklearn/ModelSelectionandEvaluationGroup(["Model Selection and Evaluation"]) sklearn(("Sklearn")) -.-> sklearn/UtilitiesandDatasetsGroup(["Utilities and Datasets"]) 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{{"Feature Selection für SVC auf Iris-Datensatz"}} sklearn/preprocessing -.-> lab-49306{{"Feature Selection für SVC auf Iris-Datensatz"}} sklearn/feature_selection -.-> lab-49306{{"Feature Selection für SVC auf Iris-Datensatz"}} sklearn/pipeline -.-> lab-49306{{"Feature Selection für SVC auf Iris-Datensatz"}} sklearn/model_selection -.-> lab-49306{{"Feature Selection für SVC auf Iris-Datensatz"}} sklearn/datasets -.-> lab-49306{{"Feature Selection für SVC auf Iris-Datensatz"}} ml/sklearn -.-> lab-49306{{"Feature Selection für SVC auf Iris-Datensatz"}} end

Daten laden

Wir beginnen, indem wir den Iris-Datensatz laden und 36 nicht-informative Features hinzufügen.

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))))

Pipeline erstellen

Als nächstes erstellen wir eine Pipeline, die aus einem Feature-Selektions-Transformator, einem Skalierer und einer Instanz von SVM besteht, die wir zusammen kombinieren, um einen vollwertigen Schätzer zu erhalten.

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")),
    ]
)

Plotten der Kreuzvalidierungsscore

Wir plotten den Kreuzvalidierungsscore als Funktion des Prozentils der Features.

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()

Zusammenfassung

In diesem Lab haben wir gelernt, wie die univariate Feature-Selektion durchgeführt werden kann, bevor ein Support Vector Classifier (SVC) ausgeführt wird, um die Klassifizierungsergebnisse zu verbessern. Wir haben den Iris-Datensatz (4 Features) verwendet und 36 nicht-informative Features hinzugefügt. Wir haben festgestellt, dass unser Modell die beste Leistung erzielt, wenn wir etwa 10% der Features ausgewählt haben.