ordinalgbt.loss

Functions

dec_clip_y_pred(fun)

stack_zeros_ones(→ numpy.ndarray)

Stacks zeroes and ones on the left and rights part of the array

sigmoid(→ numpy.ndarray)

Sigmoid

grad_sigmoid(→ numpy.ndarray)

Gradient

hess_sigmoid(→ numpy.ndarray)

Hessian

alpha2theta(alpha)

theta2alpha(theta)

probas_from_y_pred(y_preds, theta)

convers y_preds to probabilities

ordinal_logistic_nll(y_true, y_preds, theta)

Ordinal Negative log lilelihood

gradient_ordinal_logistic_nll(→ numpy.ndarray)

Gradient of ordinal nll

hessian_ordinal_logistic_nll(→ numpy.ndarray)

Hessian of ordinal nll

lgb_ordinal_loss(y_true, y_pred, theta)

Ordinal loss for lightgbm use

Module Contents

ordinalgbt.loss.dec_clip_y_pred(fun)
ordinalgbt.loss.stack_zeros_ones(a: numpy.ndarray, only_zeros=False) numpy.ndarray

Stacks zeroes and ones on the left and rights part of the array Stacks horizontally zeros and ones on the left and right hand side of the array respectively. If only_zeros is true then it only stacks zeros. This is for the gradient which is zero at both ends of the sigmoid. e.g.:

a = [[1,2],
    [3,4],
    [5,6]]
returns
    [[0,1,2,1],
    [0,3,4,1],
    [0,5,6,1]]
Parameters:
  • a (np.ndarray) – A 2D array to pad with zeroes and ones

  • only_zeros (bool, default False) – If true, then it pads with only zeroes

Return type:

np.ndarray

ordinalgbt.loss.sigmoid(z) numpy.ndarray

Sigmoid Sigmoid implementation in numpy

Parameters:

z (np.ndarray)

Returns:

Sigmoid

Return type:

np.ndarray

ordinalgbt.loss.grad_sigmoid(z) numpy.ndarray

Gradient Gradient of the sigmoid

Parameters:

z (np.ndarray)

Returns:

Gradient of Sigmoid

Return type:

np.ndarray

ordinalgbt.loss.hess_sigmoid(z) numpy.ndarray

Hessian Hessian of the sigmoid Sigmoid implementation in numpy

Parameters:

z (np.ndarray)

Returns:

Hessian of sigmoid

Return type:

np.ndarray

ordinalgbt.loss.alpha2theta(alpha)
ordinalgbt.loss.theta2alpha(theta)
ordinalgbt.loss.probas_from_y_pred(y_preds, theta)

convers y_preds to probabilities

ordinalgbt.loss.ordinal_logistic_nll(y_true: numpy.ndarray, y_preds: numpy.ndarray, theta: numpy.ndarray)

Ordinal Negative log lilelihood

Parameters:
  • y_true (np.ndarray) – 1-D array with correct labels, starts from 0 and goes up to the number of unique classes minus one (so unique values are 0,1,2 when dealing with three classes)

  • y_preds (np.ndarray) – 1-D array with predictions in latent space

  • theta (np.ndarray) – thresholds, 1-D array, size is the number of classes minus one.

Returns:

logistic ordinal negative log likelihood

Return type:

np.ndarray

ordinalgbt.loss.gradient_ordinal_logistic_nll(y_true: numpy.ndarray, y_preds: numpy.ndarray, theta: numpy.ndarray) numpy.ndarray

Gradient of ordinal nll Gradient of the ordinal logistic regression with respect to the predictions

Parameters:
  • y_true (np.ndarray) – 1-D array with correct labels, starts from 0 and goes up to the number of unique classes minus one (so unique values are 0,1,2 when dealing with three classes)

  • y_preds (np.ndarray) – 1-D array with predictions in latent space

  • theta (np.ndarray) – thresholds, 1-D array, size is the number of classes minus one.

Returns:

Gradient of logistic ordinal negative log likelihood

Return type:

np.ndarray

ordinalgbt.loss.hessian_ordinal_logistic_nll(y_true: numpy.ndarray, y_preds: numpy.ndarray, theta: numpy.ndarray) numpy.ndarray

Hessian of ordinal nll Hessian of the ordinal logistic regression with respect to the predictions

Parameters:
  • y_true (np.ndarray) – 1-D array with correct labels, starts from 0 and goes up to the number of unique classes minus one (so unique values are 0,1,2 when dealing with three classes)

  • y_preds (np.ndarray) – 1-D array with predictions in latent space

  • theta (np.ndarray) – thresholds, 1-D array, size is the number of classes minus one.

Returns:

Hessian of logistic ordinal negative log likelihood

Return type:

np.ndarray

ordinalgbt.loss.lgb_ordinal_loss(y_true: numpy.ndarray, y_pred: numpy.ndarray, theta: numpy.ndarray)

Ordinal loss for lightgbm use The ordinal loss used in the lightgbm framework. Returns the gradient and hessian of the loss.

Parameters:
  • y_true (np.ndarray) – 1-D array with correct labels, starts from 0 and goes up to the number of unique classes minus one (so unique values are 0,1,2 when dealing with three classes)

  • y_preds (np.ndarray) – 1-D array with predictions in latent space

  • theta (np.ndarray) – thresholds, 1-D array, size is the number of classes minus one.

Returns:

Gradient and Hessian of logistic ordinal negative log likelihood

Return type:

(np.ndarray, np.ndarray)