#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2015 DataCultureAnalytics Company
#

"""
Wrappers around sklearn's common cross validation methods.
"""

from sklearn.cross_validation import StratifiedKFold


def report_stratifiedKFold_cv(estimator, X, y, n_folds, metric=None):
    """
    Given an estimator and a training dataset, report the performance on a
    StratifiedKFold cross validation.
    """

    scores = []
    print "Estimator: %s" % str(estimator)
    for train_inds, test_inds in StratifiedKFold(y, n_folds=n_folds):
        xTrain, yTrain = X[train_inds, :], y[train_inds]
        xTest, yTest = X[test_inds, :], y[test_inds]
        estimator.fit(xTrain, yTrain)
        if metric is None:
            score = estimator.score(xTest, yTest)
        else:
            pred = estimator.predict(xTest)
            score = metric(yTest, pred)
            scores.append(score)
        print "\t", score
        scores.append((score, (test_inds, train_inds)))
    return scores

@@ -195,46 +195,74 @@
 arn.
-cross_validation import StratifiedKFol
+model_selection import GridSearchCV, StratifiedKFold, ParameterGri
 d%0A%0A%0A
@@ -1028,16 +1028,16 @@
 inds)))%0A
-
     retu
@@ -1046,8 +1046,2101 @@
  scores%0A
+%0A%0Aclass SelectiveGridSearchCV(GridSearchCV):%0A    %22%22%22SelectiveGridSearchCV%0A%0A    By default, the original%0A    %60sklearn.model_selection.GridSearchCV%60 runs through all possible%0A    combinations of hyperparamters through the estimator. This class allows%0A    dropping specific combinations od hyperparameters from the gridsearch.%0A    %22%22%22%0A%0A    def __init__(self, estimator, grid_exceptions=%5B%5D, **kwargs):%0A        %22%22%22__init__%0A%0A        :param estimator: The sklearn estimator to be passed to grid search.%0A        (Same as the first parameter of GridSearchCV).%0A        :param grid_exceptions: List of dictionaries that represent%0A        combinations of parameterst that need to be omitted from the grid%0A        search.%0A        :param **kwargs: Other arguments to be passed to the grid search.%0A        %22%22%22%0A        self.grid_exceptions = grid_exceptions%0A        super(SelectiveGridSearchCV, self).__init__(estimator, **kwargs)%0A        self.restricted_grid = list(ParameterGrid(self.param_grid))%0A        to_remove = %5B%5D%0A        for exception in self.grid_exceptions:%0A            for combination in self.restricted_grid:%0A                if all(item in combination.items() for item in%0A                        exception.items()):%0A                    to_remove.append(combination)%0A        for removal in to_remove:%0A            self.restricted_grid.remove(removal)%0A%0A    def fit(self, X, y=None, groups=None):%0A        %22%22%22Run fit with all sets of parameters.%0A%0A        Parameters%0A        ----------%0A%0A        X : array-like, shape = %5Bn_samples, n_features%5D%0A            Training vector, where n_samples is the number of samples and%0A            n_features is the number of features.%0A%0A        y : array-like, shape = %5Bn_samples%5D or %5Bn_samples, n_output%5D, optional%0A            Target relative to X for classification or regression;%0A            None for unsupervised learning.%0A%0A        groups : array-like, with shape (n_samples,), optional%0A            Group labels for the samples used while splitting the dataset into%0A            train/test set.%0A        %22%22%22%0A        return self._fit(X, y, groups, self.restricted_grid)%0A

Add selective grid search