dreamquark-ai / tabnet

PyTorch implementation of TabNet paper : https://arxiv.org/pdf/1908.07442.pdf
https://dreamquark-ai.github.io/tabnet/
MIT License
2.65k stars 488 forks source link

how to add weight for r2_score? #564

Open RainFung opened 5 days ago

RainFung commented 5 days ago
from sklearn.metrics import r2_score  
class R2Score(Metric):  
    def __init__(self):  
        self._name = "r2"  
        self._maximize = True  

    def __call__(self, y_true, y_pred):  
        return r2_score(y_true, y_pred)

how can i add weight for r2_score

Optimox commented 3 days ago

Hello, if the weights you want to apply are based on the labels you could simply do something like that:

from sklearn.metrics import r2_score  
class R2Score(Metric):  
    def __init__(self):  
        self._name = "r2"  
        self._maximize = True  

    def __call__(self, y_true, y_pred):  
        weights_example=y_true**2
        return r2_score(y_true, y_pred, sample_weight=weights_example)

if the weights are just external weights then you won't be able to easily access them inside tabnet .fit unfortunately. You can still monitor a proxy r2_score without weights during training and compute your out of fold weighted r2_score at the end of each fold.