From 1782a41d322893cea7436a3deeeec1a1aee1f55e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bin=20Dong=20=E8=91=A3=E6=96=8C?= Date: Fri, 11 Nov 2022 10:25:28 -0800 Subject: [PATCH] Add predict_fn_accept_dense_only Add predict_fn_accept_dense_only to explain_instance. When data_row is scipy.sparse.matrix but the model was not trained with scipy.sparse.matrix, the predict_fn_accept_dense_only convert inverse to dense before call predict_fn (and then back to sparse after call predict_fn) Bin dbin@lbl.gov --- lime/lime_tabular.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lime/lime_tabular.py b/lime/lime_tabular.py index 880f3d391..b9752d15d 100644 --- a/lime/lime_tabular.py +++ b/lime/lime_tabular.py @@ -305,7 +305,8 @@ def explain_instance(self, num_samples=5000, distance_metric='euclidean', model_regressor=None, - sampling_method='gaussian'): + sampling_method='gaussian', + predict_fn_accept_dense_only=False): """Generates explanations for a prediction. First, we generate neighborhood data by randomly perturbing features @@ -358,8 +359,17 @@ def explain_instance(self, metric=distance_metric ).ravel() + is_convert_to_dense = False + if sp.sparse.isspmatrix_csr(inverse) and predict_fn_accept_dense_only: + inverse = inverse.toarray() + is_convert_to_dense = True + yss = predict_fn(inverse) + if is_convert_to_dense: + inverse = sp.sparse.csr_matrix(inverse) + is_convert_to_dense = False + # for classification, the model needs to provide a list of tuples - classes # along with prediction probabilities if self.mode == "classification":