/
model_info.py
64 lines (53 loc) · 2.48 KB
/
model_info.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# ----------------------------------------------------------------------------
# Copyright (C) 2021-2023 Deepchecks (https://www.deepchecks.com)
#
# This file is part of Deepchecks.
# Deepchecks is distributed under the terms of the GNU Affero General
# Public License (version 3 or later).
# You should have received a copy of the GNU Affero General Public License
# along with Deepchecks. If not, see <http://www.gnu.org/licenses/>.
# ----------------------------------------------------------------------------
#
"""Module contains model_info check."""
import warnings
import pandas as pd
from deepchecks.core import CheckResult
from deepchecks.tabular import Context, ModelOnlyCheck
from deepchecks.utils.dataframes import hide_index_for_display
from deepchecks.utils.model import get_model_of_pipeline
__all__ = ['ModelInfo']
class ModelInfo(ModelOnlyCheck):
"""Summarize given model parameters."""
def run_logic(self, context: Context) -> CheckResult:
"""Run check.
Returns
-------
CheckResult
value is dictionary in format {type: <model_type>, params: <model_params_dict>}
"""
model = context.model
estimator = get_model_of_pipeline(model)
model_type = type(estimator).__name__
try:
model_params = estimator.get_params()
default_params = type(estimator)().get_params()
except AttributeError:
model_params = {}
default_params = {}
# Create dataframe to show
model_param_df = pd.DataFrame(model_params.items(), columns=['Parameter', 'Value'])
model_param_df['Default'] = model_param_df['Parameter'].map(lambda x: default_params.get(x, ''))
def highlight_not_default(data):
n = len(data)
if data['Value'] != data['Default']:
return n * ['background-color: lightblue']
else:
return n * ['']
with warnings.catch_warnings():
warnings.simplefilter(action='ignore', category=FutureWarning)
model_param_df = model_param_df.style.apply(highlight_not_default, axis=1)
model_param_df = hide_index_for_display(model_param_df)
value = {'type': model_type, 'params': model_params}
footnote = '<p style="font-size:0.7em"><i>Colored rows are parameters with non-default values</i></p>'
display = [f'Model Type: {model_type}', model_param_df, footnote]
return CheckResult(value, header='Model Info', display=display)