Skip to content

Commit

Permalink
Update black formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmindset committed May 7, 2024
1 parent b65751a commit e99e729
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 19 deletions.
12 changes: 8 additions & 4 deletions insolver/interpretation/shap.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ def __init__(self, estimator, estimator_type='tree', data=None):
self.estimator_type = estimator_type

if self.estimator_type not in ['tree', 'linear']:
raise NotImplementedError(f'''
estimator_type {estimator_type} is not supported. Supported values are "tree" and "linear".''')
raise NotImplementedError(
f'''
estimator_type {estimator_type} is not supported. Supported values are "tree" and "linear".'''
)

self.explainer = (
TreeExplainer(self.estimator)
Expand Down Expand Up @@ -75,8 +77,10 @@ def logit(x):

# check type of instance
if not isinstance(instance, np.ndarray) and not isinstance(instance, pd.Series):
raise TypeError(f'''
Type {type(instance)} is not supported. Supported types are numpy.ndarray and pandas.Series.''')
raise TypeError(
f'''
Type {type(instance)} is not supported. Supported types are numpy.ndarray and pandas.Series.'''
)
# get feature_names OR check shape and create features names
if isinstance(instance, pd.Series):
feature_names = list(instance.index)
Expand Down
18 changes: 12 additions & 6 deletions insolver/report/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,10 @@ def _calc_metrics(y_true, y_pred, task, metrics_to_calc, x, exposure=None):
elif isinstance(metrics_to_calc, list):
functions_names = metrics_to_calc
else:
raise TypeError(f'''{type(metrics_to_calc)} type of metrics_to_calc is not supported.
Must be "all", "main" or list.''')
raise TypeError(
f'''{type(metrics_to_calc)} type of metrics_to_calc is not supported.
Must be "all", "main" or list.'''
)

result['root_mean_square_error'] = np.sqrt(functions['mean_squared_error'](y_true, y_pred))

Expand All @@ -167,8 +169,10 @@ def _calc_metrics(y_true, y_pred, task, metrics_to_calc, x, exposure=None):
elif isinstance(metrics_to_calc, list):
functions_names = metrics_to_calc
else:
raise TypeError(f'''{type(metrics_to_calc)} type of metrics_to_calc is not supported.
Must be "all", "main" or list.''')
raise TypeError(
f'''{type(metrics_to_calc)} type of metrics_to_calc is not supported.
Must be "all", "main" or list.'''
)

elif type_of_true == 'binary' and type_of_pred == 'continuous':
functions_names = (
Expand All @@ -180,8 +184,10 @@ def _calc_metrics(y_true, y_pred, task, metrics_to_calc, x, exposure=None):

for name in functions_names:
if name not in functions.keys():
raise NotImplementedError(f'''{name} metric name is not supported. Supported names for {task} task:
{functions.keys()}.''')
raise NotImplementedError(
f'''{name} metric name is not supported. Supported names for {task} task:
{functions.keys()}.'''
)
try:
if name == 'gini_coef' and exposure:
result[name] = functions[name](y_true, y_pred, x[exposure])[2]
Expand Down
24 changes: 16 additions & 8 deletions insolver/report/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,20 +186,24 @@ def __init__(
self.y_test = y_test
self.original_dataset = original_dataset
else:
raise TypeError(f"""Wrong types of input data.
raise TypeError(
f"""Wrong types of input data.
\rX_train {type(X_train)} must be pandas.DataFrame
\ry_train {type(y_train)} must be pandas.Series
\rX_test {type(X_test)} must be pandas.DataFrame
\ry_test {type(y_test)} should be pandas.Series
\rpredicted_train {type(self.predicted_train)} must be pandas.Series
\rpredicted_test {type(self.predicted_test)} must be pandas.Series
\rpredicted_test {type(original_dataset)} must be pandas.DataFrame""")
\rpredicted_test {type(original_dataset)} must be pandas.DataFrame"""
)
self._directory = ntpath.dirname(inspect.getfile(Report))

# check columns
if not sorted(X_train.columns.to_list()) == sorted(X_test.columns.to_list()):
raise KeyError(f'''Columns in X_train {sorted(X_train.columns.to_list())}
and X_test {sorted(X_test.columns.to_list())} are not the same.''')
raise KeyError(
f'''Columns in X_train {sorted(X_train.columns.to_list())}
and X_test {sorted(X_test.columns.to_list())} are not the same.'''
)
elif len(set(X_train.columns.to_list()).difference(original_dataset.columns.to_list())) > 0:
s = set(X_train.columns.to_list()).difference(original_dataset.columns.to_list())
raise KeyError(f'''Columns from X_train {s} are missing from original_dataset.''')
Expand Down Expand Up @@ -324,9 +328,11 @@ def _save_model_section(self, path, report_name):
'articles': [
{
'name': 'Coefficients',
'parts': [f'''
'parts': [
f'''
<div class="p-3 m-3 bg-light border rounded-3 fw-light">
{features_importance}{_create_importance_charts()}</div>'''],
{features_importance}{_create_importance_charts()}</div>'''
],
'header': '',
'footer': features_importance_footer,
'icon': '<i class="bi bi-bar-chart-line"></i>',
Expand All @@ -347,9 +353,11 @@ def _save_model_section(self, path, report_name):
},
{
'name': 'Partial Dependence',
'parts': [f'''
'parts': [
f'''
<div class="p-3 m-3 bg-light border rounded-3 text-center fw-light">
{pdp_part}</div>'''],
{pdp_part}</div>'''
],
'header': '',
'footer': pdp_footer,
'icon': '<i class="bi bi-graph-up"></i>',
Expand Down
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
black>=22.6.0
black[jupyter]==24.4.2
mypy>=0.971
pre-commit>=2.19.0
pytest>=6.2.5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1834,6 +1834,7 @@
],
"source": [
"%%time\n",
"\n",
"igbm = show_results(InsolverGBMWrapper(backend='lightgbm', task='class', n_estimators=100), x, y)"
]
},
Expand Down Expand Up @@ -1980,6 +1981,7 @@
],
"source": [
"%%time\n",
"\n",
"igbm3 = show_results(InsolverGBMWrapper(backend='xgboost', task='class', n_estimators=100), x, y)"
]
},
Expand Down
1 change: 1 addition & 0 deletions tutorials/Regression Example - US Accidents.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"outputs": [],
"source": [
"%matplotlib inline\n",
"\n",
"import numpy as np\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
Expand Down

0 comments on commit e99e729

Please sign in to comment.