2010-07-29 00:06:59 +08:00
|
|
|
"""
|
|
|
|
|
=================================================
|
|
|
|
|
SVM-Anova: SVM with univariate feature selection
|
|
|
|
|
=================================================
|
|
|
|
|
|
2015-12-16 03:18:19 +08:00
|
|
|
This example shows how to perform univariate feature selection before running a
|
|
|
|
|
SVC (support vector classifier) to improve the classification scores.
|
2010-07-29 00:06:59 +08:00
|
|
|
"""
|
2013-02-01 22:04:03 +08:00
|
|
|
print(__doc__)
|
2010-11-02 18:38:06 +08:00
|
|
|
|
2010-07-29 00:06:59 +08:00
|
|
|
import numpy as np
|
2014-05-15 04:31:03 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2018-07-17 21:06:11 +08:00
|
|
|
from sklearn.datasets import load_digits
|
|
|
|
|
from sklearn.feature_selection import SelectPercentile, chi2
|
2015-09-11 02:26:39 +08:00
|
|
|
from sklearn.model_selection import cross_val_score
|
2011-09-02 17:00:02 +08:00
|
|
|
from sklearn.pipeline import Pipeline
|
2018-07-17 21:06:11 +08:00
|
|
|
from sklearn.svm import SVC
|
|
|
|
|
|
2010-07-29 00:06:59 +08:00
|
|
|
|
2017-06-20 20:48:57 +08:00
|
|
|
# #############################################################################
|
2010-08-07 19:30:07 +08:00
|
|
|
# Import some data to play with
|
2018-07-17 21:06:11 +08:00
|
|
|
X, y = load_digits(return_X_y=True)
|
2010-09-06 05:05:06 +08:00
|
|
|
# Throw away data, to be in the curse of dimension settings
|
2018-07-17 21:06:11 +08:00
|
|
|
X = X[:200]
|
2010-09-06 05:05:06 +08:00
|
|
|
y = y[:200]
|
2010-07-29 00:06:59 +08:00
|
|
|
n_samples = len(y)
|
2010-09-06 05:05:06 +08:00
|
|
|
X = X.reshape((n_samples, -1))
|
|
|
|
|
# add 200 non-informative features
|
2011-12-17 05:55:42 +08:00
|
|
|
X = np.hstack((X, 2 * np.random.random((n_samples, 200))))
|
2010-07-29 00:06:59 +08:00
|
|
|
|
2017-06-20 20:48:57 +08:00
|
|
|
# #############################################################################
|
2010-07-29 00:06:59 +08:00
|
|
|
# Create a feature-selection transform and an instance of SVM that we
|
|
|
|
|
# combine together to have an full-blown estimator
|
|
|
|
|
|
2018-07-17 21:06:11 +08:00
|
|
|
transform = SelectPercentile(chi2)
|
2010-07-29 00:06:59 +08:00
|
|
|
|
2018-07-17 21:06:11 +08:00
|
|
|
clf = Pipeline([('anova', transform), ('svc', SVC(gamma="auto"))])
|
2010-07-29 00:06:59 +08:00
|
|
|
|
2017-06-20 20:48:57 +08:00
|
|
|
# #############################################################################
|
2010-07-29 00:06:59 +08:00
|
|
|
# Plot the cross-validation score as a function of percentile of features
|
|
|
|
|
score_means = list()
|
2011-12-17 05:55:42 +08:00
|
|
|
score_stds = list()
|
2010-09-06 05:05:06 +08:00
|
|
|
percentiles = (1, 3, 6, 10, 15, 20, 30, 40, 60, 80, 100)
|
2010-07-29 00:06:59 +08:00
|
|
|
|
|
|
|
|
for percentile in percentiles:
|
2011-08-10 19:57:21 +08:00
|
|
|
clf.set_params(anova__percentile=percentile)
|
2015-12-03 02:09:56 +08:00
|
|
|
# Compute cross-validation score using 1 CPU
|
2015-09-11 02:26:39 +08:00
|
|
|
this_scores = cross_val_score(clf, X, y, n_jobs=1)
|
2010-07-29 00:06:59 +08:00
|
|
|
score_means.append(this_scores.mean())
|
|
|
|
|
score_stds.append(this_scores.std())
|
|
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.errorbar(percentiles, score_means, np.array(score_stds))
|
2010-07-29 00:06:59 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.title(
|
2010-07-29 00:06:59 +08:00
|
|
|
'Performance of the SVM-Anova varying the percentile of features selected')
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.xlabel('Percentile')
|
|
|
|
|
plt.ylabel('Prediction rate')
|
2010-07-29 00:06:59 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.axis('tight')
|
|
|
|
|
plt.show()
|