3
$\begingroup$

I have followed this post and tried to see if there will be any difference in predicted probabilities if I use different one-hot encoding in XGboost.

This is my code with some dummy data, which is actually not ordinal in nature.

from xgboost import XGBClassifier
from sklearn.datasets import load_iris
import pandas as pd

df = load_iris(as_frame=True)['frame']
x = df[[_ for _ in df.columns if _ != 'target']]
y = df['target']

model = XGBClassifier()

y1 = pd.get_dummies(y)
y2 = pd.get_dummies(y)
y2.loc[np.where(y==1)[0], [1,2]] = 1
y2.loc[np.where(y==2)[0], :] = 1

m1 = model.fit(x,y1)
m2 = model.fit(x,y2)

print(m1.predict_proba(x) == m2.predict_proba(x))

The predicted scores are exactly the same, so I am unsure if the difference in encoding does anything in XGBoost.

How can XGBoost handle ordinal classification then?

$\endgroup$

1 Answer 1

2
$\begingroup$

I think you are hitting undetermined behaviour here as XGBoost is not designed to have y be a pandas.DataFrame. I suspect that the sklearn API casts this into some numpy array but after than all bets are off. It appears it does something like one a classification task per column.

Strictly speaking XGBoost does not support ordinal regression, we can roughly approximate it to some degree by using multi-class classification as surrogate. i.e. having mlogloss (multi-class log-loss) as our evaluation metric and our objective as multi:softprob so we predict the probability of each data-point belonging to each class. Then we can directly change the predicted probabilities to ordinal values. The change itself can be done using two main options, without or with thresholding: A. We directly pick the class/label with the highest predicted probability (something akin to np.argmax(predicted_probs, axis=1) in Python) B. We apply thresholds to our predicted probability ranges to map them to ordinal values. (something akin to : np.argmax(predicted_probs > thresholds, axis=1) in Python).

The above being said, if we are dealing with multiple levels (e.g. 10+) and the spacing between is expected roughly equal, it might make sense to simply treat this as a regression problem directly. Similarly, if we are really aching about it, we can code the proportional odds loss (aka cumulative log-odds loss) as our objective manually and use that directly. Just remember we need the loss, the gradient and the Hessian.

$\endgroup$
7
  • $\begingroup$ Ye, so can we you please expand on two points: (1) 'Then we can directly change the predicted probabilities to ordinal values', how exactly? and (2) What I am trying to figure out is how to implement the loss, gradient and Hessian given the proportional odds loss. In the post, I've written what I think is the loss (loglikelihood, cross-entropy) given the proportional odds assumption, but for this, I think you need to treat this as a binary problem, no? Can you please expand? Thanks. $\endgroup$
    – deblue
    Commented Nov 19, 2023 at 9:34
  • $\begingroup$ (1). There are two options, without or with thresholding: A. We directly pick the class/label with the highest predicted probability (something akin to np.argmax(predicted_probs, axis=1) in Python) B. We apply thresholds to our predicted probability ranges to map them to ordinal values. (something akin to : np.argmax(predicted_probs > thresholds, axis=1) in Python). (2) Apologies, I don't see any loss in your post; (in any case) we can see this as a binary problem where we predict the cumulative probability of the j-th ordinal value for the i-th observation. (cont.) $\endgroup$
    – usεr11852
    Commented Nov 19, 2023 at 21:01
  • $\begingroup$ Then our response is an indicator variable that is 1 if the target variable for the i-th observation is less than or equal to the j-th ordinal value, and 0 otherwise. So when we have something like $y_{ij} \log(\hat{p}_{ij})$ in our loss we represent the contribution to the loss from the correct predictions as this part is maximized when the predicted probability is equal to $1$ for the correct ordinal value. ($\hat{p}_{ij}$ being the predicted cumulative probability of the j-th ordinal value for the i-th observation.) $\endgroup$
    – usεr11852
    Commented Nov 19, 2023 at 21:09
  • $\begingroup$ hey, no sorry, it's another post actually this one (I mixed them up): stats.stackexchange.com/questions/631367/…. I describe there what problems I have with implementing the proportional odds assumptions. $\endgroup$
    – deblue
    Commented Nov 20, 2023 at 8:27
  • 1
    $\begingroup$ OK, anyway I will edit this post such it includes the clarifications for point 1. Let's keep them separately so it is clear what is asked and answered. $\endgroup$
    – usεr11852
    Commented Nov 20, 2023 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.