.. only:: html
.. note::
:class: sphx-glr-download-link-note
Click :ref:`here ` to download the full example code or to run this example in your browser via Binder
.. rst-class:: sphx-glr-example-title
.. _sphx_glr_auto_examples_feature_selection_plot_feature_selection_pipeline.py:
==================
Pipeline Anova SVM
==================
Simple usage of Pipeline that runs successively a univariate
feature selection with anova and then a SVM of the selected features.
Using a sub-pipeline, the fitted coefficients can be mapped back into
the original feature space.
.. rst-class:: sphx-glr-script-out
Out:
.. code-block:: none
precision recall f1-score support
0 0.75 0.50 0.60 6
1 0.67 1.00 0.80 6
2 0.67 0.80 0.73 5
3 1.00 0.75 0.86 8
accuracy 0.76 25
macro avg 0.77 0.76 0.75 25
weighted avg 0.79 0.76 0.76 25
[[-0.23912051 0. 0. 0. -0.32369992 0.
0. 0. 0. 0. 0. 0.
0.1083669 0. 0. 0. 0. 0.
0. 0. ]
[ 0.43878897 0. 0. 0. -0.514157 0.
0. 0. 0. 0. 0. 0.
0.04845592 0. 0. 0. 0. 0.
0. 0. ]
[-0.65382765 0. 0. 0. 0.57962287 0.
0. 0. 0. 0. 0. 0.
-0.04736736 0. 0. 0. 0. 0.
0. 0. ]
[ 0.544033 0. 0. 0. 0.58478674 0.
0. 0. 0. 0. 0. 0.
-0.11344771 0. 0. 0. 0. 0.
0. 0. ]]
|
.. code-block:: default
from sklearn import svm
from sklearn.datasets import make_classification
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 = 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.LinearSVC()
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))
coef = anova_svm[:-1].inverse_transform(anova_svm['linearsvc'].coef_)
print(coef)
.. rst-class:: sphx-glr-timing
**Total running time of the script:** ( 0 minutes 0.009 seconds)
.. _sphx_glr_download_auto_examples_feature_selection_plot_feature_selection_pipeline.py:
.. only :: html
.. container:: sphx-glr-footer
:class: sphx-glr-footer-example
.. container:: binder-badge
.. image:: https://mybinder.org/badge_logo.svg
:target: https://mybinder.org/v2/gh/scikit-learn/scikit-learn/0.23.X?urlpath=lab/tree/notebooks/auto_examples/feature_selection/plot_feature_selection_pipeline.ipynb
:width: 150 px
.. container:: sphx-glr-download sphx-glr-download-python
:download:`Download Python source code: plot_feature_selection_pipeline.py `
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: plot_feature_selection_pipeline.ipynb `
.. only:: html
.. rst-class:: sphx-glr-signature
`Gallery generated by Sphinx-Gallery `_