2010-04-16 04:45:52 +08:00
|
|
|
"""
|
2019-08-13 00:02:56 +08:00
|
|
|
============================
|
2010-04-22 01:04:36 +08:00
|
|
|
Univariate Feature Selection
|
2019-08-13 00:02:56 +08:00
|
|
|
============================
|
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
|
|
|
|
2019-08-13 00:02:56 +08:00
|
|
|
from sklearn.datasets import load_iris
|
|
|
|
|
from sklearn.model_selection import train_test_split
|
|
|
|
|
from sklearn.preprocessing import MinMaxScaler
|
|
|
|
|
from sklearn.svm import LinearSVC
|
|
|
|
|
from sklearn.pipeline import make_pipeline
|
|
|
|
|
from sklearn.feature_selection import SelectKBest, f_classif
|
2011-12-19 18:48:50 +08:00
|
|
|
|
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
|
2019-08-13 00:02:56 +08:00
|
|
|
X, y = load_iris(return_X_y=True)
|
2010-04-16 04:45:52 +08:00
|
|
|
|
|
|
|
|
# Some noisy data not correlated
|
2019-08-13 00:02:56 +08:00
|
|
|
E = np.random.RandomState(42).uniform(0, 0.1, size=(X.shape[0], 20))
|
2010-04-16 04:45:52 +08:00
|
|
|
|
|
|
|
|
# Add the noisy data to the informative features
|
2019-08-13 00:02:56 +08:00
|
|
|
X = np.hstack((X, E))
|
|
|
|
|
|
|
|
|
|
# Split dataset to select feature and evaluate the classifier
|
|
|
|
|
X_train, X_test, y_train, y_test = train_test_split(
|
|
|
|
|
X, y, stratify=y, random_state=0
|
|
|
|
|
)
|
2010-04-16 04:45:52 +08:00
|
|
|
|
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
|
2019-08-13 00:02:56 +08:00
|
|
|
# We use the default selection function to select the four
|
|
|
|
|
# most significant features
|
|
|
|
|
selector = SelectKBest(f_classif, k=4)
|
|
|
|
|
selector.fit(X_train, y_train)
|
2012-09-18 05:07:58 +08:00
|
|
|
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,
|
2020-04-01 04:52:23 +08:00
|
|
|
label=r'Univariate score ($-Log(p_{value})$)')
|
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
|
2019-08-13 00:02:56 +08:00
|
|
|
clf = make_pipeline(MinMaxScaler(), LinearSVC())
|
|
|
|
|
clf.fit(X_train, y_train)
|
|
|
|
|
print('Classification accuracy without selecting features: {:.3f}'
|
|
|
|
|
.format(clf.score(X_test, y_test)))
|
2010-04-16 04:45:52 +08:00
|
|
|
|
2019-08-13 00:02:56 +08:00
|
|
|
svm_weights = np.abs(clf[-1].coef_).sum(axis=0)
|
|
|
|
|
svm_weights /= svm_weights.sum()
|
2012-09-18 05:07:58 +08:00
|
|
|
|
2020-04-01 04:52:23 +08:00
|
|
|
plt.bar(X_indices - .25, svm_weights, width=.2, label='SVM weight')
|
2010-04-16 04:45:52 +08:00
|
|
|
|
2019-08-13 00:02:56 +08:00
|
|
|
clf_selected = make_pipeline(
|
|
|
|
|
SelectKBest(f_classif, k=4), MinMaxScaler(), LinearSVC()
|
|
|
|
|
)
|
|
|
|
|
clf_selected.fit(X_train, y_train)
|
|
|
|
|
print('Classification accuracy after univariate feature selection: {:.3f}'
|
|
|
|
|
.format(clf_selected.score(X_test, y_test)))
|
2012-09-18 05:07:58 +08:00
|
|
|
|
2019-08-13 00:02:56 +08:00
|
|
|
svm_weights_selected = np.abs(clf_selected[-1].coef_).sum(axis=0)
|
|
|
|
|
svm_weights_selected /= svm_weights_selected.sum()
|
2012-09-18 05:07:58 +08:00
|
|
|
|
2014-05-15 10:35:13 +08:00
|
|
|
plt.bar(X_indices[selector.get_support()] - .05, svm_weights_selected,
|
2020-04-01 04:52:23 +08:00
|
|
|
width=.2, label='SVM weights after selection')
|
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()
|