<jupyter_start><jupyter_text>GMAIL SPAM DETECTION  IMPORTING LIBRARIES<jupyter_code>import numpy as np 
import pandas as pd<jupyter_output><empty_output><jupyter_text>LOADING DATASET<jupyter_code>#LOADING DATA SET
dt = pd.read_csv("spam.csv")
dt.head(10)
dt['spam'] = dt['type'].map({'spam' : 1,'ham' : 0}).astype(int)
dt.head(5)
print("COLUMNS IN THE GIVEN DATA")
for col in dt.columns:
    print(col)
t=len(dt['type'])
print("NO. OF ROWS IN TYPE COLUMNS:",t)
t=len(dt['text']) 
print("NO. OF ROWS IN TEXT COLUMNS:",t )<jupyter_output>NO. OF ROWS IN TYPE COLUMNS: 116
NO. OF ROWS IN TEXT COLUMNS: 116<jupyter_text>DATA PRE-PROCESSING   TOKENIZATION<jupyter_code># DATA PRE-PROCESSING
# 1. TOKENIZATION
#before tokenization
dt['text'][1]
#defining tokenization function
def token(text):
    return text.split()
dt['text']= dt['text'].apply(token) #applying tokenization
#after tokenization
dt['text'][1]<jupyter_output><empty_output><jupyter_text>STEMMING<jupyter_code># 2. STEMMING
#before stemming
dt['text'][1]
from nltk.stem.snowball import SnowballStemmer
porter = SnowballStemmer("english", ignore_stopwords=False)
def stem_it(text):
    return[porter.stem(word) for word in text]
dt['text']= dt['text'].apply(stem_it)
#after stemming
dt['text'][1]<jupyter_output><empty_output><jupyter_text>LEMMATIZATION<jupyter_code># 3. LEMMATIZATION
#before lemmatization
dt['text'][10]
from nltk.stem import WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
def lemma_it(text):
    return[lemmatizer.lemmatize(word, pos = "a") for word in text]
dt['text']= dt['text'].apply(lemma_it)
#after lemmatization
dt['text'][10]<jupyter_output><empty_output><jupyter_text>STOPWORD REMOVAL<jupyter_code># 4. STOPWORD REMOVAL
#before stopword removal
dt['text'][22]
#from nltk.corpus import stopwords
#print(stopwords.words('english'))
#set(['a', 't','d','y'])
from nltk.corpus import stopwords
stop_words = stopwords.words('english')
def stop_it(text):
    review = [word for word in text if not word in stop_words]
    return review
dt['text'] = dt['text'].apply(stop_it)
#after stopword removal
dt['text'][22]
dt.head(10)
dt['text']= dt['text'].apply(''.join)
dt.head()<jupyter_output><empty_output><jupyter_text>TRANSFORMING TEXT DATA INTO TDF / TF-IDF VECTORS<jupyter_code># TRANSFORM TEXT DATA INTO TDF / TF-IDF VECTORS
from sklearn.feature_extraction.text import TfidfVectorizer
tfidf = TfidfVectorizer()
y = dt.spam.values
x = tfidf.fit_transform(dt['text'])<jupyter_output><empty_output><jupyter_text>SPLITTING THE DATA<jupyter_code>from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(x,y,random_state=1,test_size=0.2,shuffle=False)<jupyter_output><empty_output><jupyter_text>TRAINING AND EVALUATION    LOGISTIC REGRESSION ALGORITHM<jupyter_code># CLASSIFICATION USING LOGISTIC REGRESSION
from sklearn.linear_model import LogisticRegression
clf = LogisticRegression()
clf.fit(x_train,y_train)
y_pred = clf.predict(x_test)
from sklearn.metrics import accuracy_score
acc_log = accuracy_score(y_pred, y_test)*100
print("accuracy", acc_log)<jupyter_output>accuracy 87.5<jupyter_text>LINEARSVC ACCURACY<jupyter_code># CLASSIFICATION USING LINEARSVC ACCURACY
from sklearn.svm import LinearSVC
linear_svc = LinearSVC(random_state=0)
linear_svc.fit(x_train,y_train)
y_pred = linear_svc.predict(x_test)
from sklearn.metrics import accuracy_score
acc_linear_svc = accuracy_score(y_pred, y_test)*100
print("accuracy", acc_linear_svc)<jupyter_output>accuracy 87.5
{'fraction_non_alphanumeric': 0.130758226037196, 'fraction_numerical': 0.012875536480686695, 'mean_word_length': 8.397849462365592, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '141007', 'license': 'MIT', 'n_tokens_mistral': 1403, 'n_tokens_neox': 1355, 'n_words': 261, 'path': 'SPAM DETECTION.ipynb', 'repo_name': 'NupSha05/Gmail-Spam-Detection', 'size': '3495'}