Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose monotonic trend analysis to summary table. #297

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions optbinning/binning/binning_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,15 +419,18 @@ def _binning_selection_criteria(self):
"iv": optb.binning_table.iv,
"gini": optb.binning_table.gini,
"js": optb.binning_table.js,
"quality_score": optb.binning_table.quality_score}
"quality_score": optb.binning_table.quality_score,
"monotonic_trend": optb.binning_table.monotonic_trend}
elif self._target_dtype == "multiclass":
metrics = {
"js": optb.binning_table.js,
"quality_score": optb.binning_table.quality_score}
"quality_score": optb.binning_table.quality_score,
"monotonic_trend": optb.binning_table.monotonic_trend}
elif self._target_dtype == "continuous":
metrics = {
"woe": optb.binning_table.woe,
"quality_score": optb.binning_table.quality_score}
"quality_score": optb.binning_table.quality_score,
"monotonic_trend": optb.binning_table.monotonic_trend}

info = {**info, **metrics}
self._variable_stats[name] = info
Expand Down Expand Up @@ -933,7 +936,8 @@ def summary(self):
df_summary.rename(columns={"index": "name"}, inplace=True)
df_summary["selected"] = self._support

columns = ["name", "dtype", "status", "selected", "n_bins"]
columns = ["name", "dtype", "status", "monotonic_trend",
"selected", "n_bins"]
columns += _METRICS[self._target_dtype]["metrics"]

return df_summary[columns]
Expand Down
58 changes: 50 additions & 8 deletions optbinning/binning/binning_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ def __init__(self, name, dtype, special_codes, splits, n_nonevent, n_event,
self._gini = None
self._n_specials = None
self._quality_score = None
self._type_mono = None
self._ks = None

self._bin_str = False
Expand Down Expand Up @@ -962,7 +963,7 @@ def analysis(self, pvalue_test="chi2", n_samples=100, print_output=True):
self._hhi_norm)

# Monotonic trend
type_mono = type_of_monotonic_trend(self._event_rate[:-2])
self._type_mono = type_of_monotonic_trend(self._event_rate[:-2])

report = (
"---------------------------------------------\n"
Expand All @@ -987,7 +988,8 @@ def analysis(self, pvalue_test="chi2", n_samples=100, print_output=True):
" Significance tests\n\n{}\n"
).format(self._gini, self._iv, self._js, self._hellinger,
self._triangular, self._ks, self._hhi, self._hhi_norm,
cramer_v, self._quality_score, type_mono, df_tests_string)
cramer_v, self._quality_score,
self._type_mono, df_tests_string)

if print_output:
print(report)
Expand Down Expand Up @@ -1089,6 +1091,18 @@ def quality_score(self):

return self._quality_score

@property
def monotonic_trend(self):
"""The monotonic trend.

Returns
-------
monotonic_trend : str
"""
_check_is_analyzed(self)

return self._type_mono


class MulticlassBinningTable:
"""Binning table to summarize optimal binning of a numerical variable with
Expand Down Expand Up @@ -1128,6 +1142,7 @@ def __init__(self, name, special_codes, splits, n_event, classes):
self._hhi_norm = None
self._n_specials = None
self._quality_score = None
self._type_mono = None

self._bin_str = False
self._is_built = False
Expand Down Expand Up @@ -1447,8 +1462,8 @@ def analysis(self, print_output=True):
monotonic_string = ""

for i in range(len(self.classes)):
type_mono = type_of_monotonic_trend(self._event_rate[:-2, i])
monotonic_string += mono_string.format(i, type_mono)
self._type_mono = type_of_monotonic_trend(self._event_rate[:-2, i])
monotonic_string += mono_string.format(i, self._type_mono)

report = (
"-------------------------------------------------\n"
Expand Down Expand Up @@ -1503,6 +1518,18 @@ def quality_score(self):

return self._quality_score

@property
def monotonic_trend(self):
"""The monotonic trend.

Returns
-------
monotonic_trend : str
"""
_check_is_analyzed(self)

return self._type_mono


class ContinuousBinningTable:
"""Binning table to summarize optimal binning of a numerical or categorical
Expand Down Expand Up @@ -1566,8 +1593,8 @@ class ContinuousBinningTable:
"""
def __init__(self, name, dtype, special_codes, splits, n_records, sums,
stds, min_target, max_target, n_zeros, min_x=None, max_x=None,
categories=None, cat_others=None, user_splits=None):

correlation=None, categories=None, cat_others=None,
user_splits=None):
self.name = name
self.dtype = dtype
self.special_codes = special_codes
Expand All @@ -1580,6 +1607,7 @@ def __init__(self, name, dtype, special_codes, splits, n_records, sums,
self.n_zeros = n_zeros
self.min_x = min_x
self.max_x = max_x
self.correlation = correlation
self.categories = categories
self.cat_others = cat_others if cat_others is not None else []
self.user_splits = user_splits
Expand Down Expand Up @@ -2011,7 +2039,7 @@ def analysis(self, print_output=True):
rwoe, p_values, self._hhi_norm)

# Monotonic trend
type_mono = type_of_monotonic_trend(self._mean[:-2])
self._type_mono = type_of_monotonic_trend(self._mean[:-2])

report = (
"-------------------------------------------------\n"
Expand All @@ -2028,10 +2056,12 @@ def analysis(self, print_output=True):
" Quality score {:>15.8f}\n"
"\n"
" Monotonic trend {:>15}\n"
" Correlation {:>15f}\n"
"\n"
" Significance tests\n\n{}\n"
).format(self._iv, self._woe, rwoe, self._hhi, self._hhi_norm,
self._quality_score, type_mono, df_tests_string)
self._quality_score, self._type_mono,
self.correlation, df_tests_string)

if print_output:
print(report)
Expand Down Expand Up @@ -2087,3 +2117,15 @@ def quality_score(self):
_check_is_analyzed(self)

return self._quality_score

@property
def monotonic_trend(self):
"""The monotonic trend.

Returns
-------
monotonic_trend : str
"""
_check_is_analyzed(self)

return self._type_mono
9 changes: 8 additions & 1 deletion optbinning/binning/continuous_binning.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import time
import json

from scipy.stats import kendalltau
from sklearn.utils import check_array

import numpy as np
Expand Down Expand Up @@ -715,10 +716,16 @@ def _fit(self, x, y, sample_weight, check_input):
min_x = None
max_x = None

if self.dtype == "numerical":
correlation_kendalltau = kendalltau(x_clean, y_clean)[0]
else:
correlation_kendalltau = None

self._binning_table = ContinuousBinningTable(
self.name, self.dtype, self.special_codes, self._splits_optimal,
self._n_records, self._sums, self._stds, self._min_target,
self._max_target, self._n_zeros, min_x, max_x, self._categories,
self._max_target, self._n_zeros, min_x, max_x,
correlation_kendalltau, self._categories,
self._cat_others, self.user_splits)

self._time_postprocessing = time.perf_counter() - time_postprocessing
Expand Down
Loading