Fails to display in notebook - Object of type 'ndarray' is not JSON serializable
See original GitHub issueHello I have a deep learning classifier using Keras (TensorFlow), I am able to get the explanation using basic text but note in the pretty UI What works
from lime.lime_text import LimeTextExplainer
explainer = LimeTextExplainer(class_names=TEXT_LABELS)
def new_predict(texts):
_seq = tk.texts_to_sequences(texts)
_text_data = pad_sequences(_seq, maxlen=MAX_LEN)
return single_channel_kim_model.predict(_text_data)
exp = explainer.explain_instance('forget reservations thank great company i have cancelled flighted flight once again thank you',
new_predict,
num_features=7,
labels=[0, 2]
)
print ('Explanation for class %s' % TEXT_LABELS[0])
print ('\n'.join(map(str, exp.as_list(label=0))))
print ()
print ('Explanation for class %s' % TEXT_LABELS[2])
print ('\n'.join(map(str, exp.as_list(label=2))))
When I use
exp.show_in_notebook(text='forget reservations thank great company i have cancelled flighted flight once again thank you', labels=(0,))
It throws TypeError: Object of type ‘ndarray’ is not JSON serializable
Thanks
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:6 (2 by maintainers)
Top Results From Across the Web
NumPy array is not JSON serializable - python - Stack Overflow
It means that somewhere, something is trying to dump a numpy array using the json module. But numpy.ndarray is not a type that...
Read more >[Solved] Object of type ndarray is not JSON serializable
If you try to serialize a NumPy array to JSON in Python, you'll get the error below. TypeError: Object of type ndarray is...
Read more >ndarray is not json serializable - You.com | The AI Search ...
In this tutorial, we will use an example to show you how to fix TypeError: Object of type 'ndarray' is not JSON serializablein...
Read more >Object of type <Foo> is not JSON serializable (in Array)
[Example code]-TypeError: Object of type <Foo> is not JSON serializable (in Array) ... with python - more specific flask - and stumble from...
Read more >TypeError: Object of type \'int64\' is not JSON serializable ...
NumPy data types don't recognize json. To get rid of “object of type int64 is not json serializable” you need to convert the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I was getting the same error while trying to use LIME in Titanic data set (which had a mix of numeric and categorical variables) LimeTabularExplainer expects all the numeric attributes to be as float64 format in order to be displayed using show_in_notebook method. So convert all the int64 features (including the target variable) to np.float64 type before starting to build and interpret models using LIME. I have used LIME to interpret my models for Titanic data set, entire code (data cleaning, preprocessing, feature engineering, interpreting with LIME) can be found in my github account
I have fixed the issue, I don’t think it was about versions. you just have to make sure your data type before you feed it to your the LimeTabularExplainer. In my case, categorical_names in LimeTabularExplainer was int64 [0, 1], instead of [no, yes]. I realize this parameter should be a Unicode data type. So .astype(‘U’) helped.