<jupyter_start><jupyter_text>Import Libraries<jupyter_code>import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.svm import LinearSVC
from sklearn.metrics import accuracy_score<jupyter_output><empty_output><jupyter_text>Data Pre-processing<jupyter_code># Load the data set to pandas DataFrame
raw_mail_data = pd.read_csv('C:/Users/HP/Desktop/ML and AI/Perfect_Plan_B/spamham.csv')
# Check the first 10 rows
raw_mail_data.head(10)
# Check the dimensions
raw_mail_data.shape
# Finding if there is missing values
raw_mail_data.isna().sum()
# Encode the target class
raw_mail_data.loc[raw_mail_data['Category'] == 'spam' , 'Category'] = 0
raw_mail_data.loc[raw_mail_data['Category'] == 'ham' , 'Category'] = 1
raw_mail_data.head()
# Seperate the data as text(x) and label(y)
x = raw_mail_data['Message']
y = raw_mail_data['Category']<jupyter_output><empty_output><jupyter_text>Train_test_split<jupyter_code># split the data into train and test data sets
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.20, random_state = 3)<jupyter_output><empty_output><jupyter_text>Feature Extraction<jupyter_code># Transform the text data to feature vectors that can be used as input to SV M model using TfidfVectorizer
# convert text to lowercase letters
feature_extraction = TfidfVectorizer(stop_words = 'english')
x_train_features = feature_extraction.fit_transform(x_train)
x_test_features = feature_extraction.transform(x_test)

# convert y_train and y_test values 
y_train = y_train.astype('int')
y_test = y_test.astype('int')<jupyter_output><empty_output><jupyter_text>Building the Model<jupyter_code># Training the SVM
model = LinearSVC()
model.fit(x_train_features, y_train)<jupyter_output><empty_output><jupyter_text>Evaluation of the model<jupyter_code># prediction on training data
prediction_on_training_data = model.predict(x_train_features)
accuracy_on_training_data =  accuracy_score(y_train, prediction_on_training_data)
print('Accuracy on training data : ',accuracy_on_training_data)
# Prediction on test data
prediction_on_test_data = model.predict(x_test_features)
accuracy_on_test_data =  accuracy_score(y_test, prediction_on_test_data)
print('Accuracy on test data : ',accuracy_on_test_data)
from sklearn.metrics import classification_report
print('Classification report : \n\n',classification_report(y_test, prediction_on_test_data))<jupyter_output>Classification report : 

               precision    recall  f1-score   support

           0       0.99      0.88      0.93       155
           1       0.98      1.00      0.99       960

    accuracy                           0.98      1115
   macro avg       0.99      0.94      0.96      1115
weighted avg       0.98      0.98      0.98      1115
{'fraction_non_alphanumeric': 0.08377331925378388, 'fraction_numerical': 0.024639211545230553, 'mean_word_length': 5.021186440677966, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '299888', 'license': 'MIT', 'n_tokens_mistral': 1003, 'n_tokens_neox': 950, 'n_words': 216, 'path': 'Spam_Mail_Detection_Using_SVM.ipynb', 'repo_name': 'Mohit172423/aCubeIT_Projects', 'size': '2841'}