-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathxgboost_wrapper.py
More file actions
53 lines (38 loc) · 1.46 KB
/
xgboost_wrapper.py
File metadata and controls
53 lines (38 loc) · 1.46 KB
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
53
import sys
import math
import numpy as np
code_path = "/afs/cern.ch/user/j/jpavezse/systematics/xgboost/wrapper/"
sys.path.append(code_path)
import xgboost as xgb
import pdb
def logloss(y_true, Y_pred):
return -1 * sum(math.log(y[int(label)]) if y[int(label)] > 0 else -np.inf for y, label in zip(Y_pred, y_true)) / len(Y_pred)
class XGBoostClassifier():
def __init__(self, num_boost_round=10, **params):
self.clf = None
self.num_boost_round = num_boost_round
self.params = params
self.params.update({'objective': 'multi:softprob'})
def fit(self, X, y, num_boost_round=None):
num_boost_round = num_boost_round or self.num_boost_round
dtrain = xgb.DMatrix(X, label=y)
self.clf = xgb.train(params=self.params, dtrain=dtrain, num_boost_round=num_boost_round)
def predict(self, X):
Y = self.predict_proba(X)
y = np.argmax(Y, axis=1)
return np.array(y)
def predict_proba(self, X):
dtest = xgb.DMatrix(X)
return self.clf.predict(dtest)
def score(self, X, y):
Y = self.predict_proba(X)
return 1 / logloss(y, Y)
def get_params(self, deep=True):
return self.params
def set_params(self, **params):
if 'num_boost_round' in params:
self.num_boost_round = params.pop('num_boost_round')
if 'objective' in params:
del params['objective']
self.params.update(params)
return self