The model in this project is short enough to read in one sitting. Almost everything I actually learned came from the parts around the model: the data, the saved files, and the app that serves predictions.
The repository is here: https://github.com/AGA7474/Email-Spam
The data
83,448 emails, two columns: text and label. The label is an integer, 1 for spam and 0 for not spam, and there are no missing values in the final frame.
df = pd.read_csv('Spam Email Classifier.csv') df.groupby('label').describe() df.isnull().sum() Before training anything I used the dataset to practise missing-value handling, which is the part of the notebook I would keep as a teaching example. I deliberately blanked out ranges of both columns, printed the null counts, then put the original values back:
df.loc[2500:3000, 'label'] = np.nan df.loc[45000:45500, 'label'] = np.nan df.loc[6500:7000, 'text'] = np.nan df.loc[35000:36000, 'text'] = np.nan df.isnull().sum() That is not cleaning real dirt, it is rehearsal: df.loc with a slice on each side, and a check that the frame is whole again afterwards. Worth doing once with a copy of your data before you do it on a dataset you cannot restore.
The split and the pipeline
75% train, 25% test:
X_train, X_test, y_train, y_test = train_test_split(df.text, df.label, test_size=0.25) Then the two-step that makes text classification work at all: turn words into counts, then feed the counts to a classifier that likes counts.
v = CountVectorizer() X_train_count = v.fit_transform(X_train.values) model = MultinomialNB() model.fit(X_train_count, y_train) fit_transform on the training text only, then transform on the test text. Fitting the vectorizer on the test set as well would leak the vocabulary and quietly inflate the score. Multinomial Naive Bayes is the natural partner for raw counts: it assumes each word count contributes independently to the class, which is wrong about language and works surprisingly well on spam.
The numbers
On the 25% holdout:
Confusion Matrix: [[ 9713 168] [ 380 10601]] Accuracy: 0.9737 Error Rate: 0.0263 Precision: 0.9844 Recall (Sensitivity): 0.9654 Specificity: 0.9830 F1 Score: 0.9748 Read it as: of 9,881 real non-spam emails, 168 were flagged as spam (the false positives, the ones that annoy a user most). Of 10,981 real spam emails, 380 got through. Precision of 0.984 means that when the model says spam, it is right almost every time, and recall of 0.965 means it still misses about one spam in thirty.
For a spam filter those two errors are not equal, and that is the number I would tune next: a false positive hides a real email, so I would rather trade some recall for fewer false positives.
Saving the model, and where that bit me
pickle.dump(v, open('count_vect', 'wb')) pickle.dump(model, open('email_class.pkl', 'wb')) Two files, no versioning, no metadata. Pickle stores the object graph, not a portable format, so a model pickled with one scikit-learn version can load badly or refuse to load on another. If I did this again I would use joblib, keep the vectorizer and the model in a single Pipeline so they can never drift apart, and write the scikit-learn version into the filename.
The Streamlit app
app.py wraps the pickled pair in a four-page Streamlit app: a home page, a classification page where you paste an email and get spam or not spam, a visualisation page with word clouds for spam and non-spam plus a label distribution plot, and an about page.
The prediction path is three lines:
email_count = vectorizer.transform([email_text]) prediction = model.predict(email_count) if prediction[0] == 1: st.error("This email is classified as SPAM!") The transform comes from the same vectorizer that was fitted on the training text, which is the whole reason the file was saved.
Five things I would fix before calling it finished
- Hardcoded absolute paths. The app loads its files from
E:/Project/email_class.pklandE:/Project/count_vect. That is my machine's drive letter, so the app cannot run anywhere else. Relative paths next toapp.py, or a small config. - The dataset is not in the repo. The visualisation page reads
Spam Email Classifier.csv, which is not committed, so the app fails on the data page even after the model paths are fixed. Either commit a sample or make the page optional. -
requirements.txtis a full environment freeze. Every transitive package in my environment is pinned, including Jupyter internals the app never imports. A short list of what the app actually needs would install in seconds and break less often. - No text preprocessing. Raw
CountVectorizerwith default settings keeps capitalisation, punctuation and stop words. Lowercasing, stripping the headers that email datasets carry, and tryingTF-IDFinstead of counts are the obvious next experiments. - One split, no cross-validation. A single 75/25 split gives one number.
cross_val_scorewith 5 folds would tell me how much that 97.4% moves, which matters more than the number itself.
What this project gave me
The classifier took an afternoon. The parts that took the rest of the time, and that I still think about, were the ones that decide whether a model is usable: does the app find its files, is the vectorizer the same one the model was trained with, and do the metrics reflect the error I actually care about.

