<jupyter_start><jupyter_text>Data Preprocessing<jupyter_code>df = pd.read_csv('data/spam.tsv', sep='\t')
df.head()
df.describe()
df.isna().sum()
df['label'].value_counts()
ham = df[df['label'] == 'ham']
ham.shape
spam = df[df['label'] == 'spam']
spam.shape
ham = ham.sample(spam.shape[0])
ham.shape
data = ham.append(spam, ignore_index=True)
data.shape
data
data['label'].value_counts()
plt.hist(data[data['label'] == 'ham']['length'], bins = 100, alpha = 0.7)
plt.hist(data[data['label'] == 'spam']['length'], bins = 100, alpha = 0.7)
plt.show()
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test =  train_test_split(data['message'], data['label'], test_size = 0.3, random_state =0, shuffle = True)
X_train.shape
X_test.shape<jupyter_output><empty_output><jupyter_text>Building Model   Random Forest<jupyter_code>from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.ensemble import RandomForestClassifier
from sklearn.pipeline import Pipeline
classifier = Pipeline([("tfidf", TfidfVectorizer()) , ("classifier", RandomForestClassifier(n_estimators=100))])
classifier.fit(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Model Prediction<jupyter_code>y_pred = classifier.predict(X_test)
y_test, y_pred
from sklearn.metrics import classification_report, accuracy_score, confusion_matrix
accuracy_score(y_test, y_pred)
confusion_matrix(y_test, y_pred)
print(classification_report(y_test, y_pred))<jupyter_output>precision    recall  f1-score   support

         ham       0.93      0.99      0.96       227
        spam       0.99      0.92      0.95       222

    accuracy                           0.96       449
   macro avg       0.96      0.96      0.96       449
weighted avg       0.96      0.96      0.96       449<jupyter_text>Using Support Vector Machine<jupyter_code>from sklearn.svm import SVC
svm = Pipeline([("tfidf", TfidfVectorizer()) , ("classifier", SVC(C = 100, gamma='auto'))])
svm.fit(X_train, y_train)
y_pred = svm.predict(X_test)
accuracy_score(y_test, y_pred)
confusion_matrix(y_test, y_pred)
print(classification_report(y_test, y_pred))
text1 = ['Hello, how are you']
print("RF : " + classifier.predict(text1))
print("SVM : " + svm.predict(text1))
text2 = ['Hello, you won a lottery of 1 Million, to get call 12345']
print("RF : " + classifier.predict(text2))
print("SVM : " + svm.predict(text2))
text3 = ['OTP for transaction at ABCD bank is 345678']
print("RF : " + classifier.predict(text3))
print("SVM : " + svm.predict(text3))<jupyter_output>['RF : ham']
['SVM : ham']
{'fraction_non_alphanumeric': 0.1439157566302652, 'fraction_numerical': 0.0374414976599064, 'mean_word_length': 4.790067720090294, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '146238', 'license': 'MIT', 'n_tokens_mistral': 998, 'n_tokens_neox': 931, 'n_words': 186, 'path': 'Spam Message Classifier.ipynb', 'repo_name': 'jayant2014/MyML', 'size': '2564'}