Index of predicted wrong data in Keras how to find it

+1 vote

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
• 15,510 points

edited Oct 12, 2018 by Priyaj 5,893 views

1 answer to this question.

+1 vote

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
• 58,020 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

+1 vote
1 answer

How to find the index of a particular value in a dataframe?

First, use the dataframe to match the ...READ MORE

answered Apr 8, 2019 in Python by Esha
281,919 views
0 votes
1 answer

How to find the index of an item in a list?

One thing that is really helpful in ...READ MORE

answered Dec 3, 2020 in Python by Gitika
• 65,770 points
1,456 views
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,720 points
2,373 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
• 58,020 points
1,308 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, 2019 in Python by SDeb
• 13,300 points
4,693 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,468 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, 2019 in Python by rajesh
• 1,270 points
2,171 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
• 58,020 points
991 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP