Skip to content

Commit 73d8eaf

Browse files
Jeffshep/fixcsrmatrixissue (Azure#1072)
* Fix error with csr_matrix is not JSON serializable * Applied black
1 parent 2abb872 commit 73d8eaf

File tree

1 file changed

+23
-0
lines changed
  • python-sdk/tutorials/automl-with-azureml/regression-explanation-featurization

1 file changed

+23
-0
lines changed

python-sdk/tutorials/automl-with-azureml/regression-explanation-featurization/score_explain.py

+23
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from azureml.train.automl.runtime.automl_explain_utilities import (
55
automl_setup_model_explanations,
66
)
7+
import scipy as sp
78

89

910
def init():
@@ -20,6 +21,22 @@ def init():
2021
scoring_explainer = joblib.load(scoring_explainer_path)
2122

2223

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+
2340
def run(raw_data):
2441
# Get predictions and explanations for each data point
2542
data = pd.read_json(raw_data, orient="records")
@@ -33,10 +50,16 @@ def run(raw_data):
3350
engineered_local_importance_values = scoring_explainer.explain(
3451
automl_explainer_setup_obj.X_test_transform
3552
)
53+
engineered_local_importance_values = convert_matrix(
54+
engineered_local_importance_values
55+
)
56+
3657
# Retrieve model explanations for raw explanations
3758
raw_local_importance_values = scoring_explainer.explain(
3859
automl_explainer_setup_obj.X_test_transform, get_raw=True
3960
)
61+
raw_local_importance_values = convert_matrix(raw_local_importance_values)
62+
4063
# You can return any data type as long as it is JSON-serializable
4164
return {
4265
"predictions": predictions.tolist(),

0 commit comments

Comments
 (0)