neurosynth/analysis/classify.py
def cross_val_fit(self, X, y, cross_val='4-Fold', scoring='accuracy',
                      feat_select=None, class_weight='auto'):
        """ Fits X to outcomes y, using clf and cv_method """

        from sklearn import cross_validation

        self.X = X
        self.y = y

        self.set_class_weight(class_weight=class_weight, y=y)

        # Set cross validator
        if isinstance(cross_val, string_types):
            if re.match('.*-Fold', cross_val) is not None:
                n = int(cross_val.split('-')[0])
                self.cver = cross_validation.StratifiedKFold(self.y, n)
            else:
                raise Exception('Unrecognized cross validation method')
        else:
            self.cver = cross_val

        if feat_select is not None:
            self.features_selected = []

        # Perform cross-validated classification
        from sklearn.grid_search import GridSearchCV
        if isinstance(self.clf, GridSearchCV):
            import warnings

            if feat_select is not None:
                warnings.warn(
                    "Cross-validated feature selection not supported with "
                    "GridSearchCV")
            self.clf.set_params(cv=self.cver, scoring=scoring)

            with warnings.catch_warnings():
                warnings.simplefilter('ignore', category=UserWarning)
                self.clf = self.clf.fit(X, y)

            self.cvs = self.clf.best_score_
        else:
            self.cvs = self.feat_select_cvs(
                feat_select=feat_select, scoring=scoring)

        if feat_select is not None:
            fs = feature_selection(
                feat_select, X, y)
            self.features_selected.append(fs)

            X = X[:, fs]

        self.clf.fit(X, y)

        return self.cvs.mean()
def cross_val_fit(self, X, y, cross_val='4-Fold', scoring='accuracy',
                      feat_select=None, class_weight='auto'):
        from sklearn import cross_validation

        self.X = X
        self.y = y

        self.set_class_weight(class_weight=class_weight, y=y)

        # Set cross validator
        if isinstance(cross_val, string_types):
            if re.match('.*-Fold', cross_val) is not None:
                n = int(cross_val.split('-')[0])
                self.cver = cross_validation.StratifiedKFold(self.y, n)
            else:
                raise Exception('Unrecognized cross validation method')
        else:
            self.cver = cross_val

        if feat_select is not None:
            self.features_selected = []

        # Perform cross-validated classification
        from sklearn.grid_search import GridSearchCV
        if isinstance(self.clf, GridSearchCV):
            import warnings

            if feat_select is not None:
                warnings.warn(
                    "Cross-validated feature selection not supported with "
                    "GridSearchCV")
            self.clf.set_params(cv=self.cver, scoring=scoring)

            with warnings.catch_warnings():
                warnings.simplefilter('ignore', category=UserWarning)
                self.clf = self.clf.fit(X, y)

            self.cvs = self.clf.best_score_
        else:
            self.cvs = self.feat_select_cvs(
                feat_select=feat_select, scoring=scoring)

        if feat_select is not None:
            fs = feature_selection(
                feat_select, X, y)
            self.features_selected.append(fs)

            X = X[:, fs]

        self.clf.fit(X, y)

        return self.cvs.mean()
def cross_val_fit ( self , X , y , cross_val = '4-Fold' , scoring = 'accuracy' , feat_select = None , class_weight = 'auto' ) : from sklearn import cross_validation self . X = X self . y = y self . set_class_weight ( class_weight = class_weight , y = y ) # Set cross validator if isinstance ( cross_val , string_types ) : if re . match ( '.*-Fold' , cross_val ) is not None : n = int ( cross_val . split ( '-' ) [ 0 ] ) self . cver = cross_validation . StratifiedKFold ( self . y , n ) else : raise Exception ( 'Unrecognized cross validation method' ) else : self . cver = cross_val if feat_select is not None : self . features_selected = [ ] # Perform cross-validated classification from sklearn . grid_search import GridSearchCV if isinstance ( self . clf , GridSearchCV ) : import warnings if feat_select is not None : warnings . warn ( "Cross-validated feature selection not supported with " "GridSearchCV" ) self . clf . set_params ( cv = self . cver , scoring = scoring ) with warnings . catch_warnings ( ) : warnings . simplefilter ( 'ignore' , category = UserWarning ) self . clf = self . clf . fit ( X , y ) self . cvs = self . clf . best_score_ else : self . cvs = self . feat_select_cvs ( feat_select = feat_select , scoring = scoring ) if feat_select is not None : fs = feature_selection ( feat_select , X , y ) self . features_selected . append ( fs ) X = X [ : , fs ] self . clf . fit ( X , y ) return self . cvs . mean ( )
Fits X to outcomes y, using clf and cv_method
Fits X to outcomes y using clf and cv_method