4
4
from azureml .train .automl .runtime .automl_explain_utilities import (
5
5
automl_setup_model_explanations ,
6
6
)
7
+ import scipy as sp
7
8
8
9
9
10
def init ():
@@ -20,6 +21,22 @@ def init():
20
21
scoring_explainer = joblib .load (scoring_explainer_path )
21
22
22
23
24
+ def is_multi_dimensional (matrix ):
25
+ if hasattr (matrix , "ndim" ) and matrix .ndim > 1 :
26
+ return True
27
+ if hasattr (matrix , "shape" ) and matrix .shape [1 ]:
28
+ return True
29
+ return False
30
+
31
+
32
+ def convert_matrix (matrix ):
33
+ if sp .sparse .issparse (matrix ):
34
+ matrix = matrix .todense ()
35
+ if is_multi_dimensional (matrix ):
36
+ matrix = matrix .tolist ()
37
+ return matrix
38
+
39
+
23
40
def run (raw_data ):
24
41
# Get predictions and explanations for each data point
25
42
data = pd .read_json (raw_data , orient = "records" )
@@ -33,10 +50,16 @@ def run(raw_data):
33
50
engineered_local_importance_values = scoring_explainer .explain (
34
51
automl_explainer_setup_obj .X_test_transform
35
52
)
53
+ engineered_local_importance_values = convert_matrix (
54
+ engineered_local_importance_values
55
+ )
56
+
36
57
# Retrieve model explanations for raw explanations
37
58
raw_local_importance_values = scoring_explainer .explain (
38
59
automl_explainer_setup_obj .X_test_transform , get_raw = True
39
60
)
61
+ raw_local_importance_values = convert_matrix (raw_local_importance_values )
62
+
40
63
# You can return any data type as long as it is JSON-serializable
41
64
return {
42
65
"predictions" : predictions .tolist (),
0 commit comments