Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import catboost as cb
- import lightgbm as lg
- from sklearn.datasets import make_regression
- X, Y = make_regression(n_samples=500_000, n_features=100)
- Y = (Y - Y.mean()) / Y.std()
- class MyLoss(object):
- def calc_ders_range(self, approxes, targets, weights):
- ders = np.zeros((len(targets), 2))
- ders[:, 0] = targets - approxes
- ders[:, 1] = -1
- return ders
- def evaluate(self, approxes, target, weight):
- return np.mean((approxes[0] - target)**2), 1
- def is_max_optimal(self):
- True
- def get_final_error(self, error, weight):
- return error
- def lg_loss(self, target, approxes):
- '''for lightgbm test'''
- ders = self.calc_ders_range(approxes, target, None)
- return ders[:, 0], ders[:, 1]
- lg_reg = lg.LGBMRegressor(objective=MyLoss().lg_loss, max_depth=6, n_estimators=10, learning_rate=0.1, verbose=1)
- lg_reg.fit(X, Y)
- reg = cb.CatBoostRegressor(max_depth=6, n_estimators=10, learning_rate=0.1)
- reg.fit(X, Y)
- reg2 = cb.CatBoostRegressor(objective=MyLoss(), eval_metric=MyLoss(), max_depth=6, n_estimators=10, learning_rate=0.1)
- reg2.fit(X, Y)
- # Test
- pred = reg.predict(X)
- pred2 = reg2.predict(X)
- print(f"max_diff = {np.max(np.abs(pred - pred2)) }")
Advertisement
Add Comment
Please, Sign In to add comment