2010-04-16 04:45:52 +08:00
|
|
|
"""
|
2010-04-22 01:04:36 +08:00
|
|
|
===============================
|
|
|
|
|
Univariate Feature Selection
|
|
|
|
|
===============================
|
2010-04-16 04:45:52 +08:00
|
|
|
|
2010-04-22 01:04:36 +08:00
|
|
|
An example showing univariate feature selection.
|
2010-04-16 04:45:52 +08:00
|
|
|
|
|
|
|
|
Noisy (non informative) features are added to the iris data and
|
|
|
|
|
univariate feature selection is applied. For each feature, we plot the
|
|
|
|
|
p-values for the univariate feature selection and the corresponding
|
2010-07-30 21:26:57 +08:00
|
|
|
weights of an SVM. We can see that univariate feature selection
|
2010-04-16 04:45:52 +08:00
|
|
|
selects the informative features and that these have larger SVM weights.
|
|
|
|
|
|
|
|
|
|
In the total set of features, only the 4 first ones are significant. We
|
|
|
|
|
can see that they have the highest score with univariate feature
|
2012-09-18 05:07:58 +08:00
|
|
|
selection. The SVM assigns a large weight to one of these features, but also
|
|
|
|
|
Selects many of the non-informative features.
|
|
|
|
|
Applying univariate feature selection before the SVM
|
2010-04-16 04:45:52 +08:00
|
|
|
increases the SVM weight attributed to the significant features, and will
|
|
|
|
|
thus improve classification.
|
|
|
|
|
"""
|
2013-02-01 22:04:03 +08:00
|
|
|
print(__doc__)
|
2010-04-16 04:45:52 +08:00
|
|
|
|
|
|
|
|
import numpy as np
|
2014-05-15 04:31:03 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2010-04-16 04:45:52 +08:00
|
|
|
|
2011-12-19 18:48:50 +08:00
|
|
|
from sklearn import datasets, svm
|
|
|
|
|
from sklearn.feature_selection import SelectPercentile, f_classif
|
|
|
|
|
|
2017-06-20 20:48:57 +08:00
|
|
|
# #############################################################################
|
|
|
|
|
# Import some data to play with
|
2010-04-16 04:45:52 +08:00
|
|
|
|
2012-09-18 05:07:58 +08:00
|
|
|
# The iris dataset
|
2010-04-19 22:15:47 +08:00
|
|
|
iris = datasets.load_iris()
|
2010-04-16 04:45:52 +08:00
|
|
|
|
|
|
|
|
# Some noisy data not correlated
|
2012-09-18 05:07:58 +08:00
|
|
|
E = np.random.uniform(0, 0.1, size=(len(iris.data), 20))
|
2010-04-16 04:45:52 +08:00
|
|
|
|
|
|
|
|
# Add the noisy data to the informative features
|
2012-09-18 05:07:58 +08:00
|
|
|
X = np.hstack((iris.data, E))
|
2010-04-16 04:45:52 +08:00
|
|
|
y = iris.target
|
|
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.figure(1)
|
|
|
|
|
plt.clf()
|
2010-04-16 04:45:52 +08:00
|
|
|
|
2012-09-18 05:07:58 +08:00
|
|
|
X_indices = np.arange(X.shape[-1])
|
2010-04-16 04:45:52 +08:00
|
|
|
|
2017-06-20 20:48:57 +08:00
|
|
|
# #############################################################################
|
2011-12-19 18:48:50 +08:00
|
|
|
# Univariate feature selection with F-test for feature scoring
|
|
|
|
|
# We use the default selection function: the 10% most significant features
|
|
|
|
|
selector = SelectPercentile(f_classif, percentile=10)
|
2012-09-18 05:07:58 +08:00
|
|
|
selector.fit(X, y)
|
|
|
|
|
scores = -np.log10(selector.pvalues_)
|
2010-04-16 04:45:52 +08:00
|
|
|
scores /= scores.max()
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.bar(X_indices - .45, scores, width=.2,
|
2017-06-07 19:23:12 +08:00
|
|
|
label=r'Univariate score ($-Log(p_{value})$)', color='darkorange',
|
|
|
|
|
edgecolor='black')
|
2010-04-16 04:45:52 +08:00
|
|
|
|
2017-06-20 20:48:57 +08:00
|
|
|
# #############################################################################
|
2010-04-16 04:45:52 +08:00
|
|
|
# Compare to the weights of an SVM
|
|
|
|
|
clf = svm.SVC(kernel='linear')
|
2012-09-18 05:07:58 +08:00
|
|
|
clf.fit(X, y)
|
2010-04-16 04:45:52 +08:00
|
|
|
|
2011-12-19 18:48:50 +08:00
|
|
|
svm_weights = (clf.coef_ ** 2).sum(axis=0)
|
2010-04-16 04:45:52 +08:00
|
|
|
svm_weights /= svm_weights.max()
|
2012-09-18 05:07:58 +08:00
|
|
|
|
2015-10-23 20:10:22 +08:00
|
|
|
plt.bar(X_indices - .25, svm_weights, width=.2, label='SVM weight',
|
2017-06-07 19:23:12 +08:00
|
|
|
color='navy', edgecolor='black')
|
2010-04-16 04:45:52 +08:00
|
|
|
|
2012-09-18 05:07:58 +08:00
|
|
|
clf_selected = svm.SVC(kernel='linear')
|
|
|
|
|
clf_selected.fit(selector.transform(X), y)
|
|
|
|
|
|
|
|
|
|
svm_weights_selected = (clf_selected.coef_ ** 2).sum(axis=0)
|
|
|
|
|
svm_weights_selected /= svm_weights_selected.max()
|
|
|
|
|
|
2014-05-15 10:35:13 +08:00
|
|
|
plt.bar(X_indices[selector.get_support()] - .05, svm_weights_selected,
|
2017-06-07 19:23:12 +08:00
|
|
|
width=.2, label='SVM weights after selection', color='c',
|
|
|
|
|
edgecolor='black')
|
2012-09-18 05:07:58 +08:00
|
|
|
|
|
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.title("Comparing feature selection")
|
|
|
|
|
plt.xlabel('Feature number')
|
|
|
|
|
plt.yticks(())
|
|
|
|
|
plt.axis('tight')
|
|
|
|
|
plt.legend(loc='upper right')
|
|
|
|
|
plt.show()
|