@@ -2058,25 +2058,37 @@ def link_calibrator_methods(self):
20582058 [c_uint64 , POINTER (c_double ), POINTER (c_double ), POINTER (c_double )],
20592059 )
20602060
2061- def fit_platt_transform (self , logits , tgt_prob ):
2061+ def fit_platt_transform (self , logits , targets , clip_tgt_prob = True ):
20622062 """Python to C/C++ interface for platt transfrom fit.
20632063
20642064 Ref: https://www.csie.ntu.edu.tw/~cjlin/papers/plattprob.pdf
20652065
20662066 Args:
20672067 logits (ndarray): 1-d array of logit with length N.
2068- tgt_prob (ndarray): 1-d array of target probability scores within [0, 1] with length N.
2068+ targets (ndarray): 1-d array of target probability scores within [0, 1] with length N.
2069+ clip_tgt_prob (bool): whether to clip the target probability to
2070+ [1/(prior0 + 2), 1 - 1/(prior1 + 2)]
2071+ where prior1 = sum(targets), prior0 = N - prior1
20692072 Returns:
20702073 A, B: coefficients for Platt's scale.
20712074 """
20722075 assert isinstance (logits , np .ndarray )
2073- assert isinstance (tgt_prob , np .ndarray )
2074- assert len (logits ) == len (tgt_prob )
2075- assert logits .dtype == tgt_prob .dtype
2076+ assert isinstance (targets , np .ndarray )
2077+ assert len (logits ) == len (targets )
2078+ assert logits .dtype == targets .dtype
20762079
2077- if tgt_prob .min () < 0 or tgt_prob .max () > 1.0 :
2080+ if targets .min () < 0 or targets .max () > 1.0 :
20782081 raise ValueError ("Target probability out of bound!" )
20792082
2083+ min_prob , max_prob = 0.0 , 1.0
2084+ if clip_tgt_prob :
2085+ prior1 = np .sum (targets )
2086+ prior0 = len (targets ) - prior1
2087+ min_prob = 1.0 / (prior0 + 2.0 )
2088+ max_prob = (prior1 + 1.0 ) / (prior1 + 2.0 )
2089+
2090+ tgt_prob = np .clip (targets , min_prob , max_prob )
2091+
20802092 AB = np .array ([0 , 0 ], dtype = np .float64 )
20812093
20822094 if tgt_prob .dtype == np .float32 :
0 commit comments