<jupyter_start><jupyter_text>Text Preprocessing<jupyter_code>ps = PorterStemmer()
wordnet = WordNetLemmatizer()
stopwords = stopwords.words('English')
corpus = []
for i in range(messages.shape[0]):
    review = re.sub(r'[^a-zA-Z]',' ',messages.iloc[i,1])
    review = review.lower().split()
    review = [ps.stem(word) for word in review if word not in stopwords]
    review = ' '.join(review)
    corpus.append(review)
messages['Preprocessed_message'] = corpus
messages.head()
len(corpus)
from sklearn.feature_extraction.text import  TfidfVectorizer
cv = TfidfVectorizer(max_features=5000)
X = cv.fit_transform(corpus).toarray()
X.shape
y = pd.get_dummies(messages.label)
y = y.spam.values
y.shape
from sklearn.model_selection import  train_test_split 
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=0,stratify=y)
print('Shape of training data:',X_train.shape)
print('Shape of testing data:',X_test.shape)
from sklearn.naive_bayes import  MultinomialNB
spam_detect_model = MultinomialNB()
spam_detect_model.fit(X_train,y_train)
y_pred = spam_detect_model.predict(X_test)
spam_detect_model.score(X_test,y_test)
from sklearn.metrics import  classification_report,confusion_matrix
confusion_matrix(y_test,y_pred)
print(classification_report(y_test,y_pred))<jupyter_output>precision    recall  f1-score   support

           0       0.97      1.00      0.98       966
           1       1.00      0.78      0.88       149

    accuracy                           0.97      1115
   macro avg       0.98      0.89      0.93      1115
weighted avg       0.97      0.97      0.97      1115
{'fraction_non_alphanumeric': 0.10656753407682776, 'fraction_numerical': 0.04275092936802974, 'mean_word_length': 3.627507163323782, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '303561', 'license': 'Unlicense', 'n_tokens_mistral': 621, 'n_tokens_neox': 578, 'n_words': 107, 'path': 'Notebook.ipynb', 'repo_name': 'nishantsinha00/Spam-Classification', 'size': '1614'}