-
Notifications
You must be signed in to change notification settings - Fork 415
/
Copy pathcross_validation.py
52 lines (37 loc) · 1.64 KB
/
cross_validation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import warnings
from sklearn import cross_validation
from sklearn import grid_search
DEPRECATION_MSG = '''
Custom cross-validation compatibility shims are no longer needed for
scikit-learn>=0.16.0 and will be dropped in sklearn-pandas==2.0.
'''
def cross_val_score(model, X, *args, **kwargs):
warnings.warn(DEPRECATION_MSG, DeprecationWarning)
X = DataWrapper(X)
return cross_validation.cross_val_score(model, X, *args, **kwargs)
class GridSearchCV(grid_search.GridSearchCV):
def __init__(self, *args, **kwargs):
warnings.warn(DEPRECATION_MSG, DeprecationWarning)
super(GridSearchCV, self).__init__(*args, **kwargs)
def fit(self, X, *params, **kwparams):
return super(GridSearchCV, self).fit(DataWrapper(X), *params, **kwparams)
def predict(self, X, *params, **kwparams):
return super(GridSearchCV, self).predict(DataWrapper(X), *params, **kwparams)
try:
class RandomizedSearchCV(grid_search.RandomizedSearchCV):
def __init__(self, *args, **kwargs):
warnings.warn(DEPRECATION_MSG, DeprecationWarning)
super(RandomizedSearchCV, self).__init__(*args, **kwargs)
def fit(self, X, *params, **kwparams):
return super(RandomizedSearchCV, self).fit(DataWrapper(X), *params, **kwparams)
def predict(self, X, *params, **kwparams):
return super(RandomizedSearchCV, self).predict(DataWrapper(X), *params, **kwparams)
except AttributeError:
pass
class DataWrapper(object):
def __init__(self, df):
self.df = df
def __len__(self):
return len(self.df)
def __getitem__(self, key):
return self.df.iloc[key]