<jupyter_start><jupyter_text>SpamClassifier<jupyter_code>import pandas as pd 

data = pd.read_csv('SMSSpamCollection.csv', sep='\t',names=["label", "message"])
data.head()
#converting the dependent variable in numeric form 
y=pd.get_dummies(data['label'])
#y=y.iloc[:,1].values
y
#Data cleaning and preprocessing
import re
import nltk
#nltk.download('stopwords')

from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
ps = PorterStemmer()
corpus = []
for i in range(0, len(data)):
    review = re.sub('[^a-zA-Z]', ' ', data['message'][i])
    review = review.lower()
    review = review.split()
    
    review = [ps.stem(word) for word in review if not word in stopwords.words('english')]
    review = ' '.join(review)
    corpus.append(review)
# Creating the Bag of Words model
from sklearn.feature_extraction.text import CountVectorizer
cv = CountVectorizer(max_features=2500)
X = cv.fit_transform(corpus).toarray()
# Train Test Split

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.20, random_state = 0)

# Training model using Naive bayes classifier

from sklearn.naive_bayes import MultinomialNB
spam_detect_model = MultinomialNB().fit(X_train, y_train)

y_pred=spam_detect_model.predict(X_test)
from sklearn.metrics import confusion_matrix
confusion_mat =confusion_matrix(y_test,y_pred)
print("Confusion matric :")
print(confusion_mat)
#printing the performance of model
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y_test,y_pred)
print(accuracy)
# finding the above finding using tfidf vectorizer

#Data cleaning and preprocessing
import re
import nltk
#nltk.download('stopwords')
from nltk.stem import WordNetLemmatizer
#from nltk.stem.porter import PorterStemmer

wordnet = WordNetLemmatizer() #object of WordNetLemmatizer class
corpus1 = []
for i in range(0, len(data)):
    review = re.sub('[^a-zA-Z]', ' ', data['message'][i])
    review = review.lower()
    review = review.split()
    
    review = [wordnet.lemmatize(word) for word in review if not word in stopwords.words('english')]
    review = ' '.join(review)
    corpus1.append(review)
#TFIDf
from sklearn.feature_extraction.text import TfidfVectorizer
clf = TfidfVectorizer()
X_tfidf = clf.fit_transform(corpus1).toarray()
# Train Test Split

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X_tfidf, y, test_size = 0.20, random_state = 0)

# Training model using Naive bayes classifier

from sklearn.naive_bayes import MultinomialNB
spam_detect_model = MultinomialNB().fit(X_train, y_train)

y_pred=spam_detect_model.predict(X_test)
from sklearn.metrics import confusion_matrix
confusion_mat =confusion_matrix(y_test,y_pred)
print("Confusion matric :")
print(confusion_mat)
#printing the performance of model
from sklearn.metrics import accuracy_score
accuracy = accuracy_score(y_test,y_pred)
print(accuracy)<jupyter_output>0.9721973094170404
{'fraction_non_alphanumeric': 0.10543877210543877, 'fraction_numerical': 0.011678345011678345, 'mean_word_length': 6.104265402843602, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '50632', 'license': 'Unlicense', 'n_tokens_mistral': 1083, 'n_tokens_neox': 1032, 'n_words': 249, 'path': 'SpamClassifier.ipynb', 'repo_name': 'rairohit298/SpamClassifier', 'size': '2997'}