I know this is probably a really easy fix, but I've been stuck on this for a while. I'm a noobie to both machine learning and programming in general. My data is from this dataset. https://www.kaggle.com/ozlerhakan/spam-or-not-spam-dataset

I made a model that can predict whether or not an email is spam email, and it does so reasonably well, getting an 84% accuracy with the validation data. But when I try to make it predict on a new string, it seems to be predicting on a character-by-character basis. Below is the code that I used to preprocess the training(and testing) data. I would've copy-pasted the required code, but when I look at the draft, it looks terrible. So here's the link to my notebook. https://colab.research.google.com/drive/1EnoNFR5CFi85hOrW8ihx66QWb6sToKAL?usp=sharing I tried to debug the function by putting various print statements all over. I've come to the conclusion that the problem spot is when I try to one-hot encode the words. I need those words to be one-hot encoded because that is how I trained the model. But I don't know how to fix that. I would really appreciate some help with this. Thank you in advance.

  • $\begingroup$ Welcome to DataScienceSE. I'm not at all familiar with keras but I think you're right: from a quick look at your code I saw that you redo a one-hot encoding when you test. The general principle with one-hot encoding is that the encoding is determined during training and then the same encoding must be applied when applying the model, otherwise the indexes are all messed up. I can't tell you how to do it with keras, maybe this explanation can help. $\endgroup$
    – Erwan
    Dec 27, 2020 at 23:06
  • $\begingroup$ First of all, thanks for taking the time to respond! I forgot that since one-hot can't be instantiated, it wouldn't have a dictionary of any sort. But I still don't understand why it's getting encoded on a character-by-character basis. From what I can tell, the only difference between a training string and that sample string was that the sample string had 3 quotes. I'm not too familiar with raw TensorFlow code so I couldn't really understand the article. I might just decide to use the Tokenizer instead. $\endgroup$ Dec 28, 2020 at 2:15

1 Answer 1


You are using multiple frameworks (pandas, keras / tensorflow, nltk, reg, scikit-learn) and doing a lot of manually processing. It would be better if you used a single framework. A single framework would automatic many of the steps and appropriately handle training and prediction. The result would be creating the same one-hot encoding scheme at the token level.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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