Linked Questions

Popular Questions

How to loop when predicting in keras text classification model

Asked by At

I am new to keras. I wrote a text classification model and when making predictions for one input ,I am geting the right predictions as shown below:

text=["Cancelling insurance cover that is in excess of your needs"]
one_test = tokenize.texts_to_matrix(text)
text_array=np.array([one_test[0]])
preds = model.predict(text_array)
yhat1 = model.predict_classes(text_array)
yhat2 = model.predict_proba(text_array)
print(preds)
print(yhat1)
print(yhat2)
prediction1=np.argmax(preds)
print(prediction1)

Output: [[0.21625464 0.17296328 0.17964244 0.27282426 0.15831545]]

[3]

[[0.21625464 0.17296328 0.17964244 0.27282426 0.15831545]]

3

However,the want to send a list of inputs to make the predictions

prediction_list=[]
Actionlist= ["Cancelling insurance cover that is in excess of your 
needs","Decrease loan payment","use your surplus cash reserves to pay for 
holiday expense or travel"]
for text in Actionlist:
    print(text)
    one_test = tokenize.texts_to_matrix(text)
    text_array=np.array([one_test[0]])
    preds = model.predict(text_array)
    print(preds)
    yhat1 = model.predict_classes(text_array)
    print(yhat1)
    prediction=np.argmax(preds)
    print(prediction)
    prediction_list.append(prediction)
print(prediction_list)

I am getting the following output instead of getting three predictions.

Cancelling insurance cover that is in excess of your needs

[[0.20537896 0.20620751 0.1970055 0.1982517 0.19315639]]

[1]

1

Decrease loan payment

[[0.20537896 0.20620751 0.1970055 0.1982517 0.19315639]]

[1]

1

use your surplus cash reserves to pay for holiday expense or travel

[[0.20537896 0.20620751 0.1970055 0.1982517 0.19315639]]

[1]

1

[1, 1, 1]

Please help Thanks in advance

Related Questions