Index of predicted wrong data in Keras, how to find it?

0 votes

I am doing sentimental analysis and using keras to predict positive/negative of movie reviews. What I want to know is the raw data, those are wrongly predicted by my model. I only can get the accuracy, loss from my model but I want to get the subset of texts in which my model predicted wrong. How to do it?

import pandas as pd
from keras.preprocessing.text import Tokenizer
from keras.layers import Dense
import keras
import numpy as np
import  gc
from sklearn.model_selection import train_test_split
dataset=pd.read_csv('balanced_dataset.csv')
tk=Tokenizer(num_words=2000)
tk.fit_on_texts(dataset.review)
x=tk.texts_to_matrix(dataset.review)
y=dataset.label

x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3,random_state=40)

model=keras.models.Sequential()
model.add(Dense(8,input_dim=2000))
model.add(Dense(1,activation='sigmoid'))

model.compile(loss='binary_crossentropy',optimizer='rmsprop',metrics=['acc'])

del tk
gc.collect()

result=model.fit(x_train,y_train,batch_size=128,epochs=20,validation_split=0.1)

Sep 28, 2018 in Python by bug_seeker
• 14,970 points

edited Oct 12, 2018 by Priyaj 181 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Simply, use: model.predict()

pred = model.predict(x_test)
indices = [i for i,v in enumerate(pred) if pred[i]!=y_test[i]]
subset_of_wrongly_predicted = [x_test[i] for i in indices ]
answered Sep 28, 2018 by Priyaj
• 56,120 points
Don't put the solution if you don't understand the question.
Can you please tell me what's wrong with the solution?

Related Questions In Python

0 votes
1 answer
0 votes
1 answer

How can I find out the index of an element from row and column in Python?

You probably want to use np.ravel_multi_index: [code] import numpy ...READ MORE

answered Apr 16, 2018 in Python by charlie_brown
• 7,710 points
40 views
0 votes
1 answer

How to find index from raw and column in python?

You probably want to use np.ravel_multi_index: import numpy as ...READ MORE

answered Sep 24, 2018 in Python by Priyaj
• 56,120 points
19 views
0 votes
1 answer

How to handle AssertionError in Python and find out which line or statement it occurred on?

Use the traceback module: import sys import traceback try: ...READ MORE

answered Dec 17, 2018 in Python by charlie_brown
• 7,710 points
226 views
0 votes
1 answer

How to get rid of tensorflow verbose messages with Keras?

If you are using TensorFlow 0.12, you ...READ MORE

answered Jan 24 in Python by SDeb
• 9,660 points
88 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
25 views
–1 vote
2 answers

How to find the size of a string in Python?

following way to find length of string  x ...READ MORE

answered Mar 29 in Python by rajesh
• 1,210 points
40 views
0 votes
1 answer

How to find index from raw and column in python?

You probably want to use np.ravel_multi_index: import numpy as ...READ MORE

answered Sep 12, 2018 in Python by Priyaj
• 56,120 points
47 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.