Skip to content

Commit f9f68e3

Browse files
authored
numerical stability fix
1 parent 9ea4aba commit f9f68e3

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

category_encoders/target_encoder.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Target Encoder"""
22
import numpy as np
33
import pandas as pd
4+
from scipy.special import expit
45
from category_encoders.ordinal import OrdinalEncoder
56
import category_encoders.utils as util
67
import warnings
@@ -261,5 +262,6 @@ def target_encode(self, X_in):
261262
return X
262263

263264
def _weighting(self, n):
264-
# monotonically increasing function on n bounded between 0 and 1
265-
return 1 / (1 + np.exp(-(n - self.min_samples_leaf) / self.smoothing))
265+
# monotonically increasing function of n bounded between 0 and 1
266+
# sigmoid in this case, using scipy.expit for numerical stability
267+
return expit((n - self.min_samples_leaf) / self.smoothing)

0 commit comments

Comments
 (0)