I am generally trying to take into account costs in learning. The set-up is as follows: a statistical learning problem with usuall X and y, where y is imbalanced (roughly 1% of ones).
Scikit learn usually offers wights parameters where you can set up weights matching imbalance. So the weights are depending on the target. Assigning weights will transform the log loss into weighted log loss as seen below.
$\text{Log Loss} = -\frac{1}{N} \sum_{i=1}^{N} \left[ y_i \log(\hat{y}_i) + (1 - y_i) \log(1 - \hat{y}_i) \right]$
$\text{Weighted Log Loss} = -\frac{1}{N} \sum_{i=1}^{N} w_{y_i} \left[ y_i \log(\hat{y}_i) + (1 - y_i) \log(1 - \hat{y}_i) \right]$
As you see the weights $w$ is constant on each classes and only depends on $y_i$. I am generally looking for specifying the weights in terms of errors costs. More specifically, I have costs associated with:
- $c_{1,1}$, cost associated with True Positives (correctly identified positives)
- $c_{0,1}$, cost associated with False Positives (Type 1 error)
- $c_{1,0}$, cost associated with False Negatives (Type 2 error)
- $c_{0,0}$, cost associated with True Negatives (correctly identified negatives)
With three sub-cases:
- $c_{y_i,1,1}, c_{y_i,0,1}, c_{y_i,1,0}, c_{y_i,0,0}$ depends only on classes, typically I have classifications costs for each classes (8 parameters in total)
- $c_{i,1,1}, c_{i,0,1}, c_{i,1,0}, c_{i,0,0}$ depends on instances, so I have four values for each instances.
- $c_{i,1,1}(\hat{y}_i), c_{i,0,1}(\hat{y}_i), c_{i,1,0}(\hat{y}_i), c_{i,0,0}(\hat{y}_i)$ depends both on instances and models outputs.
It appears the loss can be rewritten as:
$\text{Cost-Sensitive Log Loss} = -\frac{1}{N} \sum_{i=1}^{N} \left[ y_i \cdot \left( C_{10} \cdot \log(\hat{y}_i) + C_{11} \cdot \log(1 - \hat{y}_i) \right) + (1 - y_i) \cdot \left( C_{01} \cdot \log(\hat{y}_i) + C_{00} \cdot \log(1 - \hat{y}_i) \right) \right]$
So that it would be solved by custom losses. Is there a generic way to handle these cases, ideally within scikit-learn or scikit-learn compatible frameworks ?
.fitmethod etc. $\endgroup$