Skip to content

Commit

Permalink
Merge pull request #281 from alexliap/add_plot_metric
Browse files Browse the repository at this point in the history
Add plot metric
  • Loading branch information
guillermo-navas-palencia committed Dec 6, 2023
2 parents 3941883 + aa95d62 commit f71725c
Showing 1 changed file with 30 additions and 6 deletions.
36 changes: 30 additions & 6 deletions optbinning/binning/binning_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ def __init__(self, name, dtype, special_codes, splits, n_nonevent, n_event,
self._woe = None
self._hhi = None
self._hhi_norm = None
self._iv_values = None
self._iv = None
self._js = None
self._gini = None
Expand Down Expand Up @@ -547,6 +548,7 @@ def build(self, show_digits=2, add_totals=True):
t_iv = iv.sum()
t_js = js.sum()

self._iv_values = iv
self._iv = t_iv
self._js = t_js
self._hellinger = hellinger(p_ev, p_nev, return_sum=True)
Expand Down Expand Up @@ -643,9 +645,9 @@ def plot(self, metric="woe", add_special=True, add_missing=True,
"""
_check_is_built(self)

if metric not in ("event_rate", "woe"):
if metric not in ("event_rate", "woe", "iv"):
raise ValueError('Invalid value for metric. Allowed string '
'values are "event_rate" and "woe".')
'values are "event_rate", "woe" and "iv".')

if not isinstance(add_special, bool):
raise TypeError("add_special must be a boolean; got {}."
Expand Down Expand Up @@ -689,6 +691,9 @@ def plot(self, metric="woe", add_special=True, add_missing=True,
elif metric == "event_rate":
metric_values = self._event_rate
metric_label = "Event rate"
elif metric == "iv":
metric_values = self._iv_values
metric_label = "IV"

fig, ax1 = plt.subplots(figsize=figsize)

Expand Down Expand Up @@ -1580,7 +1585,9 @@ def __init__(self, name, dtype, special_codes, splits, n_records, sums,
self.user_splits = user_splits

self._mean = None
self._iv_values = None
self._iv = None
self._woe_values = None
self._woe = None
self._t_mean = None
self._hhi = None
Expand Down Expand Up @@ -1624,7 +1631,9 @@ def build(self, show_digits=2, add_totals=True):
t_iv = iv.sum()
t_woe = np.absolute(woe).sum()

self._iv_values = iv
self._iv = t_iv
self._woe_values = woe
self._woe = t_woe
self._t_mean = t_mean

Expand Down Expand Up @@ -1679,13 +1688,17 @@ def build(self, show_digits=2, add_totals=True):
return df

def plot(self, add_special=True, add_missing=True, style="bin",
show_bin_labels=False, savefig=None, figsize=None):
show_bin_labels=False, savefig=None, figsize=None, metric='mean'):
"""Plot the binning table.
Visualize records count and mean values.
Parameters
----------
metric : str, optional (default="mean")
Supported metrics are "mean" to show the Mean value of the target variable in each bin,
"iv" to show the IV of each bin and "woe" to show the Weight of Evidence (WoE) of each bin.
add_special : bool (default=True)
Whether to add the special codes bin.
Expand Down Expand Up @@ -1735,6 +1748,10 @@ def plot(self, add_special=True, add_missing=True, style="bin",
if not isinstance(figsize, tuple):
raise TypeError('figsize argument must be a tuple.')

if metric not in ("mean", "iv", "woe"):
raise ValueError('Invalid value for metric. Allowed string '
'values are "mean", "iv" and "woe".')

if style == "actual":
# Hide special and missing bin
add_special = False
Expand All @@ -1746,9 +1763,16 @@ def plot(self, add_special=True, add_missing=True, style="bin",
elif self.min_x is None or self.max_x is None:
raise ValueError('If style="actual", min_x and max_x must be '
'provided.')

metric_values = self._mean
metric_label = "Mean"

if metric == "mean":
metric_values = self._mean
metric_label = "Mean"
elif metric == "woe":
metric_values = self._woe_values
metric_label = "WoE"
elif metric == "iv":
metric_values = self._iv_values
metric_label = "IV"

fig, ax1 = plt.subplots(figsize=figsize)

Expand Down

0 comments on commit f71725c

Please sign in to comment.