<jupyter_start><jupyter_text>**Handling Imbalanced Dataset**<jupyter_code>from imblearn.over_sampling import RandomOverSampler # used oversampling
os =  RandomOverSampler()
X, y = os.fit_resample(X, y)
from collections import Counter
Counter(y) # checking value counts of labels
X=pd.DataFrame(X,columns=['Messages']) # after using overfit the X data was in numpy so to make in a proper dataframe we used pandas DataFrame
X.info()
X.head()<jupyter_output><empty_output><jupyter_text>**Data Preprocessing**Removing the punctuation cleaning the text using stemming or lemmatization in which stemming is used to remove the suffix and lemmatization means to have a proper meaning word.<jupyter_code>import nltk
import re 
from nltk.corpus import stopwords
import string
from nltk.stem import WordNetLemmatizer
from nltk.stem import PorterStemmer
lemmatize=WordNetLemmatizer()
stemming=PorterStemmer()
# nltk.download('stopwords')
# nltk.download('wordnet')
corpus=[]
for i in range(len(X)):
  url=re.sub(r'https?://\S+|www\.\S+',' ',X['Messages'][i]) # fixing url
  html=re.sub(r'<.*?>',' ',url) #fixing html
  word=re.sub(r'\W',' ',html) # removing special characters
  remove_pun="".join([char for char in word if char not in string.punctuation]) # removing punctuation
  lower=remove_pun.lower() # lowercase letters
  tokens=lower.split() # tokenization
  # lemmatize=[lemmatize.lemmatize(word) for word in tokens if word not in set(stopwords.words('english'))] # lemmatization
  S_stem=[stemming.stem(word) for word in tokens if word not in set(stopwords.words('english'))] #stemming
  replace=' '.join(S_stem)
  corpus.append(replace)
print(corpus)
len(corpus)
len(y)<jupyter_output><empty_output><jupyter_text>**Train-Test split**<jupyter_code># train-test split for training the model and to avoid data lekage

from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(corpus,y,test_size=0.25,random_state=42,stratify=y)
Counter(y_train),Counter(y_test)
X_train[0:5]<jupyter_output><empty_output><jupyter_text>**Using Term Frequency Inverse Document Frequency**TF = No of repeated words in sentences / No of words in a sentenceIDF= No of sentences / Word containing in the number of sentence<jupyter_code># As our data is in text data so to convert the data into numerical is really important for creating our machine learning model 
# We are using Term Frequency Inverse Document Frequency
from sklearn.feature_extraction.text import TfidfVectorizer
tfidf=TfidfVectorizer(lowercase=True,ngram_range=(1, 2), max_features = 5000)  # Use unigram & bi-gram 
# applying tfidf on X_train and X_test

X_train_tfidf = tfidf.fit_transform(X_train).toarray()
X_test_tfidf = tfidf.transform(X_test).toarray()
X_train_tfidf.shape,X_test_tfidf.shape<jupyter_output><empty_output><jupyter_text>**Using Support Vector Classifier**<jupyter_code># using Support Vector Classifier
from sklearn.svm import SVC
support_vector_classifier=SVC()<jupyter_output><empty_output><jupyter_text>**Evaluating the model**<jupyter_code>from sklearn.metrics import accuracy_score,confusion_matrix,classification_report
support_vector_classifier.fit(X_train_tfidf,y_train)
predictions_svc_training = support_vector_classifier.predict(X_train_tfidf)
accuracy_svc_training=accuracy_score(y_train,predictions_svc_training)

predictions_svc_testing = support_vector_classifier.predict(X_test_tfidf)
accuracy_svc_testing=accuracy_score(y_test,predictions_svc_testing)

print('SVC on training: ',accuracy_svc_training)
print('SVC on testing: ',accuracy_svc_testing)
confusion_matrix_svc=confusion_matrix(y_test,predictions_svc_testing)
print(confusion_matrix_svc)
classification_report_svc=classification_report(y_test,predictions_svc_testing)
print(classification_report_svc)<jupyter_output>[[1205    2]
 [   2 1204]]
              precision    recall  f1-score   support

           0       1.00      1.00      1.00      1207
           1       1.00      1.00      1.00      1206

    accuracy                           1.00      2413
   macro avg       1.00      1.00      1.00      2413
weighted avg       1.00      1.00      1.00      2413<jupyter_text>Using ***StratifiedKFold*** for cross validation<jupyter_code># from sklearn.model_selection import StratifiedKFold
# from sklearn.model_selection import cross_val_score
# skfold=StratifiedKFold(n_splits=5)
# score=cross_val_score(support_vector_classifier,X_train_tfidf,y_train,cv=skfold)
# score<jupyter_output><empty_output><jupyter_text>**Using Naive Bayes**<jupyter_code>from sklearn.naive_bayes import MultinomialNB
naive_bayes_classifier = MultinomialNB()
naive_bayes_classifier.fit(X_train_tfidf,y_train)
predictions_nb_training = naive_bayes_classifier.predict(X_train_tfidf)
accuracy_nb_training=accuracy_score(y_train,predictions_nb_training)

predictions_nb_testing = naive_bayes_classifier.predict(X_test_tfidf)
accuracy_nb_testing=accuracy_score(y_test,predictions_nb_testing)

print('Naive Bayes on training: ',accuracy_nb_training)
print('Naive Bayes on testing: ',accuracy_nb_testing)
confusion_matrix_nb=confusion_matrix(y_test,predictions_nb_testing)
print(confusion_matrix_nb)
classification_report_nb=classification_report(y_test,predictions_nb_testing)
print(classification_report_nb)<jupyter_output>[[1139   68]
 [  21 1185]]
              precision    recall  f1-score   support

           0       0.98      0.94      0.96      1207
           1       0.95      0.98      0.96      1206

    accuracy                           0.96      2413
   macro avg       0.96      0.96      0.96      2413
weighted avg       0.96      0.96      0.96      2413<jupyter_text>**Using Logistic Regression**<jupyter_code>from sklearn.linear_model import LogisticRegression
rfc=LogisticRegression()
rfc.fit(X_train_tfidf,y_train)
predictions_rfc_training = rfc.predict(X_train_tfidf)
accuracy_rfc_training=accuracy_score(y_train,predictions_rfc_training)

predictions_rfc_testing = rfc.predict(X_test_tfidf)
accuracy_rfc_testing=accuracy_score(y_test,predictions_rfc_testing)

print('Logistic Regression on training: ',accuracy_rfc_training)
print('Logistic Regression classifier on testing: ',accuracy_rfc_testing)
confusion_matrix_lr=confusion_matrix(y_test,predictions_rfc_testing)
print(confusion_matrix_lr)
classification_report_lr=classification_report(y_test,predictions_rfc_testing)
print(classification_report_lr)
from sklearn.ensemble import RandomForestClassifier
random_fc=RandomForestClassifier()
random_fc.fit(X_train_tfidf,y_train)
predictions_randomfc_training = random_fc.predict(X_train_tfidf)
accuracy_randomfc_training=accuracy_score(y_train,predictions_randomfc_training)

predictions_randomfc_testing = random_fc.predict(X_test_tfidf)
accuracy_randomfc_testing=accuracy_score(y_test,predictions_randomfc_testing)

print('Random forest classifier on training: ',accuracy_randomfc_training)
print('Random forest classifier on testing: ',accuracy_randomfc_testing)
confusion_matrix_random_fc=confusion_matrix(y_test,predictions_randomfc_testing)
print(confusion_matrix_random_fc)
classification_report_random_fc=classification_report(y_test,predictions_randomfc_testing)
print(classification_report_random_fc)<jupyter_output>[[1205    2]
 [   1 1205]]
              precision    recall  f1-score   support

           0       1.00      1.00      1.00      1207
           1       1.00      1.00      1.00      1206

    accuracy                           1.00      2413
   macro avg       1.00      1.00      1.00      2413
weighted avg       1.00      1.00      1.00      2413<jupyter_text>**Checking on new data whether it is spam or ham**<jupyter_code>text=input()
clean_data=tfidf.transform([text]).toarray()
predict=support_vector_classifier.predict(clean_data)[0]
if predict == 0:
  print('Spam')
else:
  print('Ham')
import pickle
file=open('Tfidf.pkl','wb')
pickle.dump(tfidf,file)
file1=open('svc.pkl','wb')
pickle.dump(support_vector_classifier,file1)<jupyter_output><empty_output>