Output is not as expected in the prediction

Viewed 67

I have train and test datasets like this:

train: 2000 files
hit.txt
nohit.txt
hit.txt

Test: 1500 
hit.txt
nohit.txt
hit.txt

I trained a model and got accuracy of 74 percent; here is the code below:

But when i do my prediction on test data set, the scores i get is an array which i do not want.

test_dir = 'test/'
dictionary = make_dic(test_dir)

features_, labels_ = make_dataset(dictionary)

calibrated_pred_final = calibrated_clf_pipe.predict(features_)

calibrated_pred_final
array([1, 1, 1, ..., 1, 1, 0])

test_pred_final = calibrated_clf_pipe.predict_proba(features_)
import numpy as np
batch_y = np.array(test_pred_final).flatten()

f = open('scores.txt', 'w')
for i in range(len(batch_y)):
    f.write(str(batch_y))    

score.txt file looks like this,

[0.38636364 0.61363636 0.05147059 ... 0.61363636 0.86734694 0.13265306][0.38636364 0.61363636 0.05147059 ... 0.61363636 0.86734694 0.13265306][0.38636364 0.61363636 0.05147059 ... 0.61363636 0.86734694 0.13265306][0.38636364 0.61363636 0.05147059 ... 0.61363636 0.86734694 0.13265306][0.38636364 0.61363636 0.05147059 ... 0.61363636 0.86734694 0.13265306][0.38636364 0.61363636 0.05147059 ... 0.61363636 0.86734694 0.13265306][0.38636364 0.61363636 0.05147059 ... 0.61363636 0.86734694 0.13265306][0.38636364 0.61363636 0.05147059 ... 0.61363636 0.86734694 0.13265306][0.38636364 0.61363636 0.05147059 ... 0.61363636 0.86734694 0.13265306][0.38636364 0.61363636 0.05147059 ... 0.61363636 0.86734694 0.13265306][0.38636364 0.61363636 0.05147059 ... 0.61363636 0.86734694 0.13265306][0.38636364 0.61363636 0.05147059 ... 0.61363636 0.86734694 0.13265306][0.38636364 0.61363636 0.05147059 ... 0.61363636 0.86734694 0.13265306] this goes forever.

What I expect is a score per test mail as shown below.

 0.38636364     
 0.61363636 
 0.05147059
    ...
    ...

I am not sure what is going wrong. Why I am getting this score in an array which repeats 1500 times? I assume that this array of scores represents score per mail but how do I remove the entire list and just have one score per mail like my expected results?

1 Answers

The predictions are as expected. The issue is in the way you write to score.txt. You missed indexing into batch_y, so it is adding the entire array for every element in the array. Here is the updated code:

test_dir = 'test/'
dictionary = make_dic(test_dir)

features_, labels_ = make_dataset(dictionary)

calibrated_pred_final = calibrated_clf_pipe.predict(features_)

calibrated_pred_final
array([1, 1, 1, ..., 1, 1, 0])

# line changed below.
test_pred_final = calibrated_clf_pipe.predict_proba(features_)

import numpy as np
batch_y = np.array(test_pred_final).flatten()

f = open('scores.txt', 'w')
for i in range(len(batch_y)):
    f.write(str(batch_y[i]) + '\n') # You missed indexing into batch_y here

You can continue using predict_proba if you want probabilities. If you want 0 1 predictions, use predict.

Related