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

edited Oct 12, 2018 by Priyaj 396 views

1 answer to this question.

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,940 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

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

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

answered Apr 8 in Python by Esha
840 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
98 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,940 points
42 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,720 points
629 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
• 13,190 points
480 views
+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 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 349 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
108 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,940 points
75 views