Show Sidebar Hide Sidebar

Pipeline Anova SVM in Scikit-learn

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

New to Plotly?

Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!

Version

In [1]:
import sklearn
sklearn.__version__
Out[1]:
'0.18.1'

Imports

This tutorial imports SelectKBest, f_regression and make_pipeline.

In [2]:
print(__doc__)

from sklearn import svm
from sklearn.datasets import samples_generator
from sklearn.feature_selection import SelectKBest, f_regression
from sklearn.pipeline import make_pipeline
Automatically created module for IPython interactive environment

Calculations

In [3]:
# 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)

# 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, y)
anova_svm.predict(X)
Out[3]:
array([0, 0, 3, 2, 1, 0, 3, 3, 2, 2, 1, 0, 3, 3, 3, 0, 2, 3, 1, 1, 0, 2, 1,
       0, 2, 2, 2, 1, 0, 2, 0, 1, 3, 0, 2, 3, 1, 3, 1, 2, 1, 1, 2, 2, 0, 0,
       1, 1, 3, 0, 2, 1, 0, 0, 2, 2, 3, 0, 0, 2, 0, 1, 3, 0, 0, 3, 1, 1, 2,
       1, 0, 2, 1, 2, 3, 2, 1, 0, 3, 3, 0, 1, 1, 1, 2, 1, 3, 1, 3, 1, 3, 3,
       1, 0, 3, 1, 0, 1, 3, 2])
Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.