Skip to content

Commit 0287d7d

Browse files
committed
Update interpret_predictor to the latest Core alpha 10.7.3-a.0
- drop lever variables - reinstate max_variable_importances - add global vs. individual variable importance computation suppport
1 parent 060fff4 commit 0287d7d

File tree

5 files changed

+33
-43
lines changed

5 files changed

+33
-43
lines changed

doc/samples/samples.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,7 @@ Samples
10641064
dictionary_file_path = os.path.join(accidents_dir, "Accidents.kdic")
10651065
accidents_table_path = os.path.join(accidents_dir, "Accidents.txt")
10661066
vehicles_table_path = os.path.join(accidents_dir, "Vehicles.txt")
1067-
output_dir = os.path.join("kh_samples", "deploy_model_mt")
1067+
output_dir = os.path.join("kh_samples", "deploy_model_mt_with_interpretation")
10681068
report_file_path = os.path.join(output_dir, "AnalysisResults.khj")
10691069
interpretor_file_path = os.path.join(output_dir, "InterpretationModel.kdic")
10701070
output_data_table_path = os.path.join(output_dir, "InterpretedAccidents.txt")
@@ -1088,7 +1088,8 @@ Samples
10881088
model_dictionary_file_path,
10891089
"SNB_Accident",
10901090
interpretor_file_path,
1091-
reinforcement_target_value="NonLethal",
1091+
max_variable_importances=3,
1092+
importance_ranking="Individual",
10921093
)
10931094
10941095
# Deploy the interpretation model on the database

khiops/core/api.py

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -375,16 +375,6 @@ def _preprocess_task_arguments(task_args):
375375
if isinstance(task_args["selection_value"], (int, float)):
376376
task_args["selection_value"] = str(task_args["selection_value"])
377377

378-
# Discard the max_variable_importances interpretation parameters
379-
if "max_variable_importances" in task_args:
380-
if task_args["max_variable_importances"] is not None:
381-
warnings.warn(
382-
"The 'max_variable_importances' parameter of the "
383-
"'khiops.core.api.interpret_predictor' function is not supported "
384-
" yet. All model variables' importances are computed."
385-
)
386-
del task_args["max_variable_importances"]
387-
388378
# Detect and replace deprecated data-path syntax on additional_data_tables
389379
# Mutate task_args in the process
390380
for data_path_task_arg_name in (
@@ -880,9 +870,8 @@ def interpret_predictor(
880870
dictionary_file_path_or_domain,
881871
predictor_dictionary_name,
882872
interpretor_file_path,
883-
max_variable_importances=None,
884-
reinforcement_target_value="",
885-
reinforcement_lever_variables=None,
873+
max_variable_importances=100,
874+
importance_ranking="Global",
886875
log_file_path=None,
887876
output_scenario_path=None,
888877
task_file_path=None,
@@ -905,18 +894,17 @@ def interpret_predictor(
905894
Name of the predictor dictionary used while building the interpretation model.
906895
interpretor_file_path : str
907896
Path to the interpretor dictionary file.
908-
max_variable_importances : int, optional
897+
max_variable_importances : int, default 100
909898
Maximum number of variable importances to be selected in the interpretation
910-
model. If not set, then all the variables in the prediction model are
911-
considered.
912-
..note:: Not currently supported; not taken into account if set.
913-
reinforcement_target_value : str, default ""
914-
If this target value is specified, then its probability of occurrence is
915-
tentatively increased.
916-
reinforcement_lever_variables : list of str, optional
917-
The names of variables to use as lever variables while building the
918-
interpretation model. Min length: 0. Max length: the total number of variables
919-
in the prediction model. If not specified, all variables are used.
899+
model. If the predictor contains fewer variables than this number, then
900+
all the variables of the predictor are considered.
901+
importance_ranking : str, default "Global"
902+
Ranking of the Shapley values produced by the interpretor. Ca be one of:
903+
904+
- "Global": predictor variables are ranked by decreasing global importance.
905+
906+
- "Individual": predictor variables are ranked by decreasing individual
907+
Shapley value.
920908
... :
921909
See :ref:`core-api-common-params`.
922910
@@ -931,6 +919,7 @@ def interpret_predictor(
931919
--------
932920
See the following functions of the ``samples.py`` documentation script:
933921
- `samples.interpret_predictor()`
922+
- `samples.deploy_model_mt_with_interpretation()`
934923
"""
935924
# Save the task arguments
936925
# WARNING: Do not move this line, see the top of the "tasks" section for details

khiops/core/internals/tasks/interpret_predictor.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
######################################################################################
77
"""interpret_predictor task family"""
88
from khiops.core.internals import task as tm
9-
from khiops.core.internals.types import ListType, StringLikeType
9+
from khiops.core.internals.types import IntType, StringLikeType
1010

1111
# Disable long lines to have readable scenarios
1212
# pylint: disable=line-too-long
@@ -21,8 +21,8 @@
2121
("interpretor_file_path", StringLikeType),
2222
],
2323
[
24-
("reinforcement_target_value", StringLikeType, ""),
25-
("reinforcement_lever_variables", ListType(StringLikeType), None),
24+
("max_variable_importances", IntType, 100),
25+
("importance_ranking", StringLikeType, "Global"),
2626
],
2727
["dictionary_file_path", "interpretor_file_path"],
2828
# pylint: disable=line-too-long
@@ -38,14 +38,12 @@
3838
3939
// Interpret model
4040
LearningTools.InterpretPredictor
41-
HowParameter.HowClass __reinforcement_target_value__
4241
43-
__DICT__
44-
__reinforcement_lever_variables__
45-
HowParameter.leverVariablesSpecView.UnselectAll
46-
HowParameter.leverVariablesSpecView.AttributeSpecs.List.Key
47-
HowParameter.leverVariablesSpecView.AttributeSpecs.Used
48-
__END_DICT__
42+
// Number of predictor variables exploited in the interpretation model
43+
ContributionAttributeNumber __max_variable_importances__
44+
45+
// Ranking of the Shapley value produced by the interpretation model
46+
ShapleyValueRanking __importance_ranking__
4947
5048
// Build interpretation dictionary
5149
BuildInterpretationClass

khiops/samples/samples.ipynb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,7 +1396,7 @@
13961396
"metadata": {},
13971397
"source": [
13981398
"### `deploy_model_mt_with_interpretation()`\n\n",
1399-
"Deploys a multi-table interpretor in the simplest way possible\n\n It is a call to `~.api.deploy_model` with additional parameters to handle\n multi-table deployment.\n\n In this example, a Selective Naive Bayes (SNB) interpretation model is\n deployed by applying its associated dictionary to the input database.\n The model variable importances are written to the output data table.\n \n"
1399+
"Deploys a multi-table interpretor in the simplest way possible\n\n It is a call to `~.api.deploy_model` with additional parameters related to\n the variable importances.\n\n In this example, a Selective Naive Bayes (SNB) interpretation model is\n deployed by applying its associated dictionary to the input database.\n The model variable importances are written to the output data table.\n \n"
14001400
]
14011401
},
14021402
{
@@ -1414,7 +1414,7 @@
14141414
"dictionary_file_path = os.path.join(accidents_dir, \"Accidents.kdic\")\n",
14151415
"accidents_table_path = os.path.join(accidents_dir, \"Accidents.txt\")\n",
14161416
"vehicles_table_path = os.path.join(accidents_dir, \"Vehicles.txt\")\n",
1417-
"output_dir = os.path.join(\"kh_samples\", \"deploy_model_mt\")\n",
1417+
"output_dir = os.path.join(\"kh_samples\", \"deploy_model_mt_with_interpretation\")\n",
14181418
"report_file_path = os.path.join(output_dir, \"AnalysisResults.khj\")\n",
14191419
"interpretor_file_path = os.path.join(output_dir, \"InterpretationModel.kdic\")\n",
14201420
"output_data_table_path = os.path.join(output_dir, \"InterpretedAccidents.txt\")\n",
@@ -1438,7 +1438,8 @@
14381438
" model_dictionary_file_path,\n",
14391439
" \"SNB_Accident\",\n",
14401440
" interpretor_file_path,\n",
1441-
" reinforcement_target_value=\"NonLethal\",\n",
1441+
" max_variable_importances=3,\n",
1442+
" importance_ranking=\"Individual\",\n",
14421443
")\n",
14431444
"\n",
14441445
"# Deploy the interpretation model on the database\n",

khiops/samples/samples.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,8 +1184,8 @@ def deploy_model_mt():
11841184
def deploy_model_mt_with_interpretation():
11851185
"""Deploys a multi-table interpretor in the simplest way possible
11861186
1187-
It is a call to `~.api.deploy_model` with additional parameters to handle
1188-
multi-table deployment.
1187+
It is a call to `~.api.deploy_model` with additional parameters related to
1188+
the variable importances.
11891189
11901190
In this example, a Selective Naive Bayes (SNB) interpretation model is
11911191
deployed by applying its associated dictionary to the input database.
@@ -1200,7 +1200,7 @@ def deploy_model_mt_with_interpretation():
12001200
dictionary_file_path = os.path.join(accidents_dir, "Accidents.kdic")
12011201
accidents_table_path = os.path.join(accidents_dir, "Accidents.txt")
12021202
vehicles_table_path = os.path.join(accidents_dir, "Vehicles.txt")
1203-
output_dir = os.path.join("kh_samples", "deploy_model_mt")
1203+
output_dir = os.path.join("kh_samples", "deploy_model_mt_with_interpretation")
12041204
report_file_path = os.path.join(output_dir, "AnalysisResults.khj")
12051205
interpretor_file_path = os.path.join(output_dir, "InterpretationModel.kdic")
12061206
output_data_table_path = os.path.join(output_dir, "InterpretedAccidents.txt")
@@ -1224,7 +1224,8 @@ def deploy_model_mt_with_interpretation():
12241224
model_dictionary_file_path,
12251225
"SNB_Accident",
12261226
interpretor_file_path,
1227-
reinforcement_target_value="NonLethal",
1227+
max_variable_importances=3,
1228+
importance_ranking="Individual",
12281229
)
12291230

12301231
# Deploy the interpretation model on the database

0 commit comments

Comments
 (0)