I am trying to implement a lightGBM classifier with a custom objective function. My target data has four classes and my data is divided into natural groups of 12 observations.
The custom objective function achieve two things:
- The predicted model output must be probablistic and the probabilities must sum to one for each observation. This is also known as a softmax objective function and is relatively simple to implement
- The probabilities for each class must sum to 1 within each group. This has been implemented in the binomial classification space and is known as a conditional logit model.
In summary, for each group (in my case 4 observations), the probabilities should sum to 1 in each column and each row. I have written a slightly hacky function to achieve this, but when I try and run my custom objective functions within the xgb framework in python, I get the following error:
TypeError: cannot unpack non-iterable numpy.float64 object
My full code is as follows:
import lightgbm as lgb
import numpy as np
import pandas as pd
def standardiseProbs(preds, groupSize, eta = 0.1, maxIter = 100):
# add groupId to preds dataframe
n = preds.shape[0]
if n % groupSize != 0:
print('The selected group size paramter is not compatible with the data')
preds['groupId'] = np.repeat(np.arange(0, int(n/groupSize)), groupSize)
#initialise variables
error = 10000
i = 0
# perform loop while error exceeds set threshold (subject to maxIter)
while error > eta and i<maxIter:
i += 1
# get sum of probabilities by game
byGroup = preds.groupby('groupId')[0, 1, 2, 3].sum().reset_index()
byGroup.columns = ['groupId', '0G', '1G', '2G', '3G']
if '3G' in list(preds.columns):
preds = preds.drop(['3G', '2G', '1G', '0G'], axis=1)
preds = preds.merge(byGroup, how='inner', on='groupId')
# adjust probs to be consistent across a game
for v in [1, 2, 3]:
preds[v] = preds[v] / preds[str(v) + 'G']
preds[0] = (groupSize-3)* (preds[0] / preds['0G'])
# sum probabilities by player
preds['rowSum'] = preds[3] + preds[2] + preds[1] + preds[0]
# adjust probs to be consistent across a player
for v in [0, 1, 2, 3]:
preds[v] = preds[v] / preds['rowSum']
# get sum of probabilities by game
byGroup = preds.groupby('groupId')[0, 1, 2, 3].sum().reset_index()
byGroup.columns = ['groupId', '0G', '1G', '2G', '3G']
# calc error
errMat = abs(np.subtract(byGroup[['0G', '1G', '2G', '3G']].values, np.array([(groupSize-3), 1, 1, 1])))
error = sum(sum(errMat))
preds = preds[['groupId', 0, 1, 2, 3]]
return preds
def condObjective(preds, train):
labels = train.get_label()
preds = pd.DataFrame(np.reshape(preds, (int(preds.shape[0]/4), 4), order='C'), columns=[0,1,2,3])
n = preds.shape[0]
yy = np.zeros((n, 4))
yy[np.arange(n), labels] = 1
preds['matchId'] = np.repeat(np.arange(0, int(n/4)), 4)
preds = preds[['matchId', 0, 1, 2, 3]]
preds = standardiseProbs(preds, groupSize = 4, eta=0.001, maxIter=500)
preds = preds[[0, 1, 2, 3]].values
grad = (preds - yy).flatten()
hess = (preds * (1. - preds)).flatten()
return grad, hess
def mlogloss(preds, train):
labels = train.get_label()
preds = pd.DataFrame(np.reshape(preds, (int(preds.shape[0]/4), 4), order='C'), columns=[0,1,2,3])
n = preds.shape[0]
yy = np.zeros((n, 4))
yy[np.arange(n), labels] = 1
preds['matchId'] = np.repeat(np.arange(0, int(n/4)), 4)
preds = preds[['matchId', 0, 1, 2, 3]]
preds = standardiseProbs(preds, groupSize = 4, eta=0.001, maxIter=500)
preds = preds[[0, 1, 2, 3]].values
loss = -(np.sum(yy*np.log(preds)+(1-yy)*np.log(1-preds))/n)
return loss
n, k = 880, 5
xtrain = np.random.rand(n, k)
ytrain = np.random.randint(low=0, high=2, size=n)
ltrain = lgb.Dataset(xtrain, label=ytrain)
xtest = np.random.rand(int(n/2), k)
ytest = np.random.randint(low=0, high=2, size=int(n/2))
ltest = lgb.Dataset(xtrain, label=ytrain)
lgbmParams = {'boosting_type': 'gbdt',
'num_leaves': 250,
'max_depth': 3,
'min_data_in_leaf': 10,
'min_gain_to_split': 0.75,
'learning_rate': 0.01,
'subsample_for_bin': 120100,
'min_child_samples': 70,
'reg_alpha': 1.45,
'reg_lambda': 2.5,
'feature_fraction': 0.45,
'bagging_fraction': 0.55,
'is_unbalance': True,
'objective': 'multiclass',
'num_class': 4,
'metric': 'multi_logloss',
'verbose': 1}
lgbmModel = lgb.train(lgbmParams, ltrain, valid_sets=ltest,fobj=condObjective, feval=mlogloss, num_boost_round=5000, early_stopping_rounds=100, verbose_eval=50)
Assuming there isn't a better way to force my predictions to conform to the restrictive conditions I'm putting on them, what do I need to do to make the custom objective work?
from Custom multi-class log-loss function for lightGBM in python returns error
No comments:
Post a Comment