"""
An example showing feature selection.
"""

import numpy as np
import pylab as pl


################################################################################
# import some data to play with

# The IRIS dataset
from scikits.learn import datasets, svm
iris = datasets.load('iris')

# Some noisy data not correlated
E1, E2 = np.random.normal(size=(2, len(iris.data)))

x = iris.data
y = iris.target

################################################################################
pl.figure(1)
pl.clf()

################################################################################
# Univariate feature selection
from scikits.learn.feature_selection import univ_selection 
selector = univ_selection.UnivSelection(
                score_func=univ_selection.f_classif)

selector.fit(x, y)
scores = -np.log(selector.p_values_)
pl.plot(scores/scores.max(), label='Univariate score (p values)')

################################################################################
# Compare to the weights of an SVM
clf = svm.SVC(kernel='linear')
clf.fit(x, y)

svm_weights = (clf.support_**2).sum(axis=0)
pl.plot(svm_weights/svm_weights.max(), label='SVM weight')

pl.title("Comparing feature selection")
pl.xlabel('Feature number')
pl.yticks(())
pl.legend()
pl.show()
 

@@ -34,16 +34,746 @@
 lection.
+%0A%0ANoisy (non informative) features are added to the iris data and%0Aunivariate feature selection is applied. For each feature, we plot the%0Ap-values for the univariate feature selection and the corresponding%0Aweights of an SVM. We can see that univariate feature selection %0Aselects the informative features and that these have larger SVM weights.%0A%0AIn the total set of features, only the 4 first ones are significant. We%0Acan see that they have the highest score with univariate feature%0Aselection. The SVM attributes small weights to these features, but these%0Aweight are non zero. Applying univariate feature selection before the SVM%0Aincreases the SVM weight attributed to the significant features, and will%0Athus improve classification.
 %0A%22%22%22%0A%0Aim
@@ -1051,13 +1051,8 @@
 ed%0AE
-1, E2
  = n
@@ -1077,11 +1077,8 @@
 ze=(
-2, 
 len(
@@ -1091,16 +1091,80 @@
 ata)
-))%0A%0Ax = 
+, 35))%0A%0A# Add the noisy data to the informative features%0Ax = np.hstack((
 iris
@@ -1168,16 +1168,21 @@
 ris.data
+, E))
 %0Ay = iri
@@ -1295,16 +1295,52 @@
 .clf()%0A%0A
+x_indices = np.arange(x.shape%5B-1%5D)%0A%0A
 ########
@@ -1499,24 +1499,161 @@
 _selection %0A
+# As a scoring function, we use a F test for classification%0A# We use the default selection function: the 10%25 most significant%0A# features%0A
 selector = u
@@ -1795,23 +1795,18 @@
 s_)%0A
-pl.plot(
 scores
-/
+ /= 
 scor
@@ -1813,24 +1813,73 @@
 es.max()
-,
+%0Apl.bar(x_indices-.45, scores, width=.3, %0A       
  label=
+r
 'Univari
@@ -1893,17 +1893,47 @@
 re (
-p values)
+$-%5Clog(p%5C,values)$)', %0A        color='g
 ')%0A%0A
@@ -2142,68 +2142,705 @@
 =0)%0A
-pl.plot(svm_weights/svm_weights.max(), label='SVM weight
+svm_weights /= svm_weights.max()%0Apl.bar(x_indices-.15, svm_weights, width=.3, label='SVM weight',%0A        color='r')%0A%0A################################################################################%0A# Now fit an SVM with added feature selection%0Aselector = univ_selection.UnivSelection(%0A                estimator=clf,%0A                score_func=univ_selection.f_classif)%0A%0Aselector.fit(x, y)%0Asvm_weights = (clf.support_**2).sum(axis=0)%0Asvm_weights /= svm_weights.max()%0Afull_svm_weights = np.zeros(selector.support_.shape)%0Afull_svm_weights%5Bselector.support_%5D = svm_weights%0Apl.bar(x_indices+.15, full_svm_weights, width=.3, %0A        label='SVM weight after univariate selection',%0A        color='b
 ')%0A%0A
+%0A
 pl.t
@@ -2917,16 +2917,33 @@
 cks(())%0A
+pl.axis('tight')%0A
 pl.legen

Improve the feature selection example.