I applied the random forest classifier to my csv file to classify the tweets as spam or not spam and after an accuracy of 93%, when I printed the confusion matrix I got [[1068 105] [ 65 1262]]. Now I would like to print the 65 false negative tweets and the 105 false positive tweets but I am unable to do that. I tried to print the y_test and y_predict but it is difficult to map the incorrectly identified tweets. Has anyone please got any advice on this?

RandomForest = RandomForestClassifier() RandomForest.fit(x_train, y_train) y_predict = RandomForest.predict(x_test) print(confusion_matrix(y_test,y_predictRF))

Thank you


Assuming that you have a column called „target“ and „predicted“:

FN = df[(df[„predicted“]==0) & (df[„target“] == 1)]

and vice versa for FP.

  • $\begingroup$ i don't have one. I tried def predicted(x): x = data.Target for x in (1, 1000): x = y_predict return x then data['predicted'] = data['Target'].apply(lambda x: predicted(x)) but I am getting an error. $\endgroup$
    – tamilini
    Feb 1 at 11:41
  • $\begingroup$ I have a column called „target“ but not „predicted“. $\endgroup$
    – tamilini
    Feb 1 at 11:42
  • $\begingroup$ I also tried data['pred'] = y_predict but it gave me the error "Length of values (2500) does not match length of index (8000)" $\endgroup$
    – tamilini
    Feb 1 at 11:55
  • $\begingroup$ It doesn’t matter how you named your columns, you would just need one binary scaled column with your actual values in it and another binary scaled column with the predictions in it. You have these column because otherwise you wouldn’t be able to print the confusion matrix which you showed to us .. $\endgroup$ Feb 1 at 12:23
  • $\begingroup$ can you please tell me how to add the predicted results into a csv file. when i do it, all 2500 results get added to 1 row. $\endgroup$
    – tamilini
    Feb 1 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.