def callback(data): if (data.iteration + 1) % 100 != 0: return clf = data.model trn_data = clf.train_set val_data = clf.valid_sets[0] preds = [ ele[2] for ele in clf.eval_train(f1_metric_xgb2) if ele[1] == 'f12' ][0] labels = trn_data.get_label().astype(np.int) res = f1_group_idx(labels, preds, list_idx).astype(np.bool) weight = trn_data.get_weight() if weight is None: weight = np.ones(preds.shape[0]) weight[res] = 0.5 weight[~res] = 2 trn_data.set_weight(weight) preds = [ ele[2] for ele in clf.eval_valid(f1_metric_xgb2) if ele[1] == 'f12' ][0] labels = val_data.get_label().astype(np.int) t = time.time() res = f1_group(labels, preds, list_idx) sc = np.mean(res) logger.info('cal [{}] {} {}'.format(data.iteration + 1, sc, time.time() - t))
def callback(data): if (data.iteration + 1) % 500 != 0: return clf = data.model trn_data = clf.train_set val_data = clf.valid_sets[0] ''' preds = [ele[2] for ele in clf.eval_train(f1_metric_xgb2) if ele[1] == 'f12'][0] labels = trn_data.get_label().astype(np.int) res, sc = f1_group_sc(labels, preds, list_idx) # .astype(np.bool) weight = np.where(res > sc, 1, 1 + (sc - res) * 100) trn_data.set_weight(weight) ''' preds = [ ele[2] for ele in clf.eval_valid(f1_metric_xgb2) if ele[1] == 'f12' ][0] labels = val_data.get_label().astype(np.int) t = time.time() res = f1_group(labels, preds, list_idx) sc = np.mean(res) logger.info('cal [{}] {} {}'.format(data.iteration + 1, sc, time.time() - t))
def f1_metric_xgb(pred, dtrain): label = dtrain.get_label().astype(np.int) pred = pred.astype(np.float64) #res = [f1(label.take(i), pred.take(i)) for i in list_idx] res = f1_group(label, pred, list_idx) sc = np.mean(res) logger.debug('f1: %s' % (sc)) return 'f1', -sc
def f1_metric(label, pred): res = f1_group(label, pred, list_idx) sc = np.mean(res) logger.debug('f1: %s' % (sc)) return 'f1', sc, True