Frank
Frank

Reputation: 31

SHAP Explainer ValueError: Input contains NaN, infinity or a value too large for dtype('float64')

It works fine when I run it on Linux, but the error occurred when I ran it on Windows 10. Please contribute to its successful operation in Windows 10.

Code:

import shap
# print the JS visualization code to the notebook
shap.initjs()

def prob(data):
    return model.forward(Variable(torch.from_numpy(data)).float()).detach().numpy().reshape(-1, 1)

shap_explainer = shap.KernelExplainer(prob, 
                                      X_train.numpy(), 
                                      link="logit")
shap_values = shap_explainer.shap_values(X_test.numpy(), nsamples=100)

Error:

ValueError Traceback (most recent call last) in 2 X_train.numpy(), 3 link="logit") ----> 4 shap_values = shap_explainer.shap_values(X_test.numpy(), nsamples=100)

D:\Software\anaconda3\lib\site-packages\shap\explainers_kernel.py in shap_values(self, X, **kwargs) 188 if self.keep_index: 189 data = convert_to_instance_with_index(data, column_name, index_value[i:i + 1], index_name) --> 190 explanations.append(self.explain(data, **kwargs)) 191 if kwargs.get("gc_collect", False): 192 gc.collect()

D:\Software\anaconda3\lib\site-packages\shap\explainers_kernel.py in explain(self, incoming_instance, **kwargs) 386 phi_var = np.zeros((self.data.groups_size, self.D)) 387 for d in range(self.D): --> 388 vphi, vphi_var = self.solve(self.nsamples / self.max_samples, d) 389 phi[self.varyingInds, d] = vphi 390 phi_var[self.varyingInds, d] = vphi_var

D:\Software\anaconda3\lib\site-packages\shap\explainers_kernel.py in solve(self, fraction_evaluated, dim) 563 elif self.l1_reg == "auto" or self.l1_reg == "bic" or self.l1_reg == "aic": 564 c = "aic" if self.l1_reg == "auto" else self.l1_reg --> 565 nonzero_inds = np.nonzero(LassoLarsIC(criterion=c).fit(mask_aug, eyAdj_aug).coef_)[0] 566 567 # use a fixed regularization coeffcient

D:\Software\anaconda3\lib\site-packages\sklearn\linear_model_least_angle.py in fit(self, X, y, copy_X) 1862 if copy_X is None: 1863 copy_X = self.copy_X -> 1864 X, y = self._validate_data(X, y, y_numeric=True) 1865 1866 X, y, Xmean, ymean, Xstd = LinearModel._preprocess_data(

D:\Software\anaconda3\lib\site-packages\sklearn\base.py in _validate_data(self, X, y, reset, validate_separately, **check_params) 431 y = check_array(y, **check_y_params) 432 else: --> 433 X, y = check_X_y(X, y, **check_params) 434 out = X, y 435

D:\Software\anaconda3\lib\site-packages\sklearn\utils\validation.py in inner_f(*args, **kwargs) 61 extra_args = len(args) - len(all_args) 62 if extra_args <= 0: ---> 63 return f(*args, **kwargs) 64 65 # extra_args > 0

D:\Software\anaconda3\lib\site-packages\sklearn\utils\validation.py in check_X_y(X, y, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, estimator) 825 else: 826 y = column_or_1d(y, warn=True) --> 827 _assert_all_finite(y) 828 if y_numeric and y.dtype.kind == 'O': 829 y = y.astype(np.float64)

D:\Software\anaconda3\lib\site-packages\sklearn\utils\validation.py in _assert_all_finite(X, allow_nan, msg_dtype) 101 not allow_nan and not np.isfinite(X).all()): 102 type_err = 'infinity' if allow_nan else 'NaN, infinity' --> 103 raise ValueError( 104 msg_err.format 105 (type_err,

ValueError: Input contains NaN, infinity or a value too large for dtype('float64').

Upvotes: 0

Views: 337

Answers (1)

Frank
Frank

Reputation: 31

The reason for this error is related to the training of the model, which we need to prevent from overfitting

Upvotes: 0

Related Questions