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