Pipeline Anova SVMΒΆ

Simple usage of Pipeline that runs successively a univariate feature selection with anova and then a C-SVM of the selected features.

Out:

precision    recall  f1-score   support

          0       1.00      0.88      0.93         8
          1       1.00      0.83      0.91         6
          2       0.57      0.67      0.62         6
          3       0.67      0.80      0.73         5

avg / total       0.83      0.80      0.81        25

from sklearn import svm
from sklearn.datasets import samples_generator
from sklearn.feature_selection import SelectKBest, f_regression
from sklearn.pipeline import make_pipeline
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report

print(__doc__)

# import some data to play with
X, y = samples_generator.make_classification(
    n_features=20, n_informative=3, n_redundant=0, n_classes=4,
    n_clusters_per_class=2)

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)

# ANOVA SVM-C
# 1) anova filter, take 3 best ranked features
anova_filter = SelectKBest(f_regression, k=3)
# 2) svm
clf = svm.SVC(kernel='linear')

anova_svm = make_pipeline(anova_filter, clf)
anova_svm.fit(X_train, y_train)
y_pred = anova_svm.predict(X_test)
print(classification_report(y_test, y_pred))

Total running time of the script: ( 0 minutes 0.005 seconds)

Generated by Sphinx-Gallery