2010-07-27 17:57:58 +08:00
|
|
|
"""
|
2010-11-02 18:38:06 +08:00
|
|
|
==================
|
2010-07-27 17:57:58 +08:00
|
|
|
Pipeline Anova SVM
|
2010-11-02 18:38:06 +08:00
|
|
|
==================
|
2010-07-27 17:57:58 +08:00
|
|
|
|
2010-11-02 18:38:06 +08:00
|
|
|
Simple usage of Pipeline that runs successively a univariate
|
|
|
|
|
feature selection with anova and then a C-SVM of the selected features.
|
2010-07-27 17:57:58 +08:00
|
|
|
"""
|
2010-11-02 18:38:06 +08:00
|
|
|
print __doc__
|
2010-07-27 17:57:58 +08:00
|
|
|
|
2011-09-02 17:00:02 +08:00
|
|
|
from sklearn import svm
|
|
|
|
|
from sklearn.datasets import samples_generator
|
|
|
|
|
from sklearn.feature_selection import SelectKBest, f_regression
|
|
|
|
|
from sklearn.pipeline import Pipeline
|
2010-07-27 17:57:58 +08:00
|
|
|
|
|
|
|
|
# import some data to play with
|
2011-08-04 21:50:19 +08:00
|
|
|
X, y = samples_generator.make_classification(
|
2011-12-20 01:16:51 +08:00
|
|
|
n_features=20, n_informative=3, n_redundant=0,
|
|
|
|
|
n_classes=4, n_clusters_per_class=2)
|
2010-07-27 17:57:58 +08:00
|
|
|
|
2010-07-27 18:46:31 +08:00
|
|
|
# ANOVA SVM-C
|
2011-08-04 21:50:19 +08:00
|
|
|
# 1) anova filter, take 3 best ranked features
|
|
|
|
|
anova_filter = SelectKBest(f_regression, k=3)
|
2010-07-27 18:46:31 +08:00
|
|
|
# 2) svm
|
2010-07-27 17:57:58 +08:00
|
|
|
clf = svm.SVC(kernel='linear')
|
|
|
|
|
|
2010-11-02 18:38:06 +08:00
|
|
|
anova_svm = Pipeline([('anova', anova_filter), ('svm', clf)])
|
2010-07-30 21:26:57 +08:00
|
|
|
anova_svm.fit(X, y)
|
2010-07-27 18:46:31 +08:00
|
|
|
anova_svm.predict(X)
|