"""
===================================================
Recursive feature elimination with cross-validation
===================================================
- Need an external estimator that assigns weights to features (e.g. coefs in linear)
- First, the estimator is trained on the full set of features and the .coef_
    or .feature_importances_ attribute is used to judge the importance of the features.
- Then the least importance features are pruned.
- Repeated until the desired number of features to select is reached.
- RFECV does RFE inside a CV loop to auto-tune the best number of features, i.e.
    if the CV results suggest more features, RFECV will take it.

"""
print(__doc__)

import matplotlib.pyplot as plt
from sklearn.svm import SVC
from sklearn.model_selection import StratifiedKFold
from sklearn.feature_selection import RFECV
from sklearn.datasets import make_classification

# Build a classification task using 3 informative features
X, y = make_classification(n_samples=1000, n_features=25, n_informative=3,
                           n_redundant=2, n_repeated=0, n_classes=2,
                           n_clusters_per_class=1, random_state=0)

# Recursive feature elimination == RFE
# Create the RFE object and compute a cross-validated score.
svc = SVC(kernel="linear")
rfecv = RFECV(estimator=svc, step=1, cv=StratifiedKFold(3), scoring='roc_auc')
rfecv.fit(X, y)

rfecv.grid_scores_
rfecv.ranking_       # importance ranking of features
rfecv.estimator_

dir(rfecv)

print("Optimal number of features : %d" % rfecv.n_features_)

# Plot number of features VS. cross-validation scores
plt.figure()
plt.xlabel("Number of features selected")
plt.ylabel("Cross validation score (nb of correct classifications)")
plt.plot(range(1, len(rfecv.grid_scores_) + 1), rfecv.grid_scores_)
plt.show()
