| |
| """Email_spam_calsifier |
| |
| Automatically generated by Colab. |
| |
| Original file is located at |
| https://colab.research.google.com/drive/1obsGCm8CluG_jKB59HlJAlyO5movzKD_ |
| |
| # IMPORT LIBRARIES |
| """ |
|
|
| import numpy as np |
| import pandas as pd |
| import matplotlib.pyplot as plt |
| import pickle |
|
|
| from sklearn.model_selection import train_test_split |
| from sklearn.feature_extraction.text import TfidfVectorizer |
| from sklearn.svm import SVC |
| from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score |
| from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay |
|
|
| """#**LOAD DATA**""" |
|
|
| df = pd.read_csv('/content/mail_data.csv') |
|
|
| |
| data = df.where((pd.notnull(df)), '') |
|
|
| |
| data.loc[data['Category'] == 'spam', 'Category'] = 0 |
| data.loc[data['Category'] == 'ham', 'Category'] = 1 |
|
|
| |
| x = data['Message'] |
| y = data['Category'].astype('int') |
|
|
| """#**TRAIN TEST SPLIT** |
| |
| """ |
|
|
| x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=3) |
|
|
| """#**TF-IDF**""" |
|
|
| tfidf = TfidfVectorizer(min_df=1, stop_words='english', lowercase=True) |
|
|
| x_train_features = tfidf.fit_transform(x_train) |
| x_test_features = tfidf.transform(x_test) |
|
|
| """#**SVM MODEL**""" |
|
|
| model = SVC() |
|
|
| model.fit(x_train_features, y_train) |
|
|
| """#**PREDICTIONS**""" |
|
|
| train_pred = model.predict(x_train_features) |
| test_pred = model.predict(x_test_features) |
|
|
| """#**EVALUATION**""" |
|
|
| train_acc = accuracy_score(y_train, train_pred) |
| test_acc = accuracy_score(y_test, test_pred) |
|
|
| precision = precision_score(y_test, test_pred) |
| recall = recall_score(y_test, test_pred) |
| f1 = f1_score(y_test, test_pred) |
|
|
| print("Training Accuracy:", train_acc) |
| print("Testing Accuracy:", test_acc) |
| print("Precision:", precision) |
| print("Recall:", recall) |
| print("F1 Score:", f1) |
|
|
| """#**SAMPLE TEST**""" |
|
|
| sample = ["claim your free gift card today"] |
|
|
| sample_vec = tfidf.transform(sample) |
| result = model.predict(sample_vec) |
|
|
| print("\nPrediction:", "Ham" if result[0] == 1 else "Spam") |
|
|
| """#**SAVE MODEL**""" |
|
|
| pickle.dump(model, open("email_model.pkl", "wb")) |
| pickle.dump(tfidf, open("email_vectorizer.pkl", "wb")) |
|
|
| from google.colab import files |
| files.download("email_model.pkl") |
| files.download("email_vectorizer.pkl") |
|
|
| """#**BAR CHART**""" |
|
|
| plt.figure() |
| data['Category'].value_counts().plot(kind='bar') |
| plt.title("Spam vs Ham Emails") |
| plt.xlabel("Category (0=Spam, 1=Ham)") |
| plt.ylabel("Count") |
| plt.savefig("bar_chart.png") |
| plt.show() |
|
|
| """#**CONFUSION MATRIX**""" |
|
|
| cm = confusion_matrix(y_test, test_pred) |
| disp = ConfusionMatrixDisplay(confusion_matrix=cm) |
| disp.plot() |
| plt.savefig("confusion_matrix.png") |
| plt.show() |
|
|
| """#**ACCURACY COMPARISON**""" |
|
|
| plt.figure() |
| plt.bar(['Train Accuracy', 'Test Accuracy'], [train_acc, test_acc]) |
| plt.title("Train vs Test Accuracy") |
| plt.savefig("accuracy_graph.png") |
| plt.show() |
|
|
| """#**DOWNLOAD GRAPHS**""" |
|
|
| files.download("bar_chart.png") |
| files.download("confusion_matrix.png") |
| files.download("accuracy_graph.png") |
|
|
| !pip install streamlit pyngrok |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| !npm install -g localtunnel |
|
|
| !streamlit run app.py & npx localtunnel --port 8501 |