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
|
2019-01-28 09:57:38 +08:00
|
|
|
SVC (support vector classifier) to improve the classification scores. We use
|
|
|
|
|
the iris dataset (4 features) and add 36 non-informative features. We can find
|
|
|
|
|
that our model achieves best performance when we select around 10% of features.
|
2021-10-22 21:33:22 +08:00
|
|
|
|
2010-07-29 00:06:59 +08:00
|
|
|
"""
|
2010-11-02 18:38:06 +08:00
|
|
|
|
2022-03-18 22:47:03 +08:00
|
|
|
# %%
|
|
|
|
|
# Load some data to play with
|
|
|
|
|
# ---------------------------
|
2010-07-29 00:06:59 +08:00
|
|
|
import numpy as np
|
2019-01-28 09:57:38 +08:00
|
|
|
from sklearn.datasets import load_iris
|
2010-07-29 00:06:59 +08:00
|
|
|
|
2019-01-28 09:57:38 +08:00
|
|
|
X, y = load_iris(return_X_y=True)
|
2022-03-18 22:47:03 +08:00
|
|
|
|
2019-01-28 09:57:38 +08:00
|
|
|
# Add non-informative features
|
2022-03-18 22:47:03 +08:00
|
|
|
rng = np.random.RandomState(0)
|
|
|
|
|
X = np.hstack((X, 2 * rng.random((X.shape[0], 36))))
|
|
|
|
|
|
|
|
|
|
# %%
|
|
|
|
|
# Create the pipeline
|
|
|
|
|
# -------------------
|
|
|
|
|
from sklearn.pipeline import Pipeline
|
|
|
|
|
from sklearn.feature_selection import SelectPercentile, chi2
|
|
|
|
|
from sklearn.preprocessing import StandardScaler
|
|
|
|
|
from sklearn.svm import SVC
|
2010-07-29 00:06:59 +08:00
|
|
|
|
2019-01-28 09:57:38 +08:00
|
|
|
# Create a feature-selection transform, a scaler and an instance of SVM that we
|
2021-01-27 16:48:24 +08:00
|
|
|
# combine together to have a full-blown estimator
|
2022-03-18 22:47:03 +08:00
|
|
|
|
2019-01-28 09:57:38 +08:00
|
|
|
clf = Pipeline(
|
2021-10-07 16:13:00 +08:00
|
|
|
[
|
2019-01-28 09:57:38 +08:00
|
|
|
("anova", SelectPercentile(chi2)),
|
|
|
|
|
("scaler", StandardScaler()),
|
|
|
|
|
("svc", SVC(gamma="auto")),
|
2021-10-07 16:13:00 +08:00
|
|
|
]
|
2019-01-28 09:57:38 +08:00
|
|
|
)
|
2010-07-29 00:06:59 +08:00
|
|
|
|
2022-03-18 22:47:03 +08:00
|
|
|
# %%
|
2010-07-29 00:06:59 +08:00
|
|
|
# Plot the cross-validation score as a function of percentile of features
|
2022-03-18 22:47:03 +08:00
|
|
|
# -----------------------------------------------------------------------
|
|
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
|
from sklearn.model_selection import cross_val_score
|
|
|
|
|
|
2010-07-29 00:06:59 +08:00
|
|
|
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)
|
2019-07-14 22:26:41 +08:00
|
|
|
this_scores = cross_val_score(clf, X, y)
|
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
|
|
|
plt.title("Performance of the SVM-Anova varying the percentile of features selected")
|
2019-01-28 09:57:38 +08:00
|
|
|
plt.xticks(np.linspace(0, 100, 11, endpoint=True))
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.xlabel("Percentile")
|
2019-01-28 09:57:38 +08:00
|
|
|
plt.ylabel("Accuracy Score")
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.axis("tight")
|
|
|
|
|
plt.show()
|