text_plain_output_1.png
from sklearn.feature_extraction.text import TfidfVectorizer, CountVectorizer

from sklearn.metrics import accuracy_score

from sklearn.svm import SVC

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

df = pd.read_csv('/kaggle/input/email-classification-nlp/SMS_train.csv', encoding='cp1252')
df2 = pd.read_csv('/kaggle/input/email-classification-nlp/SMS_test.csv', encoding='cp1252')

df.isnull().sum()

df2.isnull().sum()

df = df.replace(to_replace='[^0-9a-zA-Z ]+', value='', regex=True)

df2 = df2.replace(to_replace='[^0-9a-zA-Z ]+', value='', regex=True)

df = df.applymap(lambda s: s.lower() if isinstance(s, str) else s)

df2 = df2.applymap(lambda s: s.lower() if isinstance(s, str) else s)

count_vectorizer = CountVectorizer()
tfidf_vectorizer = TfidfVectorizer()

X_train_count = count_vectorizer.fit_transform(df['clean_text'])
X_train_tfidf = tfidf_vectorizer.fit_transform(df['clean_text'])

X_train_combined = pd.concat([pd.DataFrame(X_train_count.toarray()), pd.DataFrame(X_train_tfidf.toarray())], axis=1)

svm_model = SVC(kernel='linear')
svm_model.fit(X_train_combined, df['Label'])

y_pred = svm_model.predict(X_train_combined)
accuracy = accuracy_score(df['Label'], y_pred)

X_test_count = count_vectorizer.transform(df2['clean_text'])
X_test_tfidf = tfidf_vectorizer.transform(df2['clean_text'])
X_test_combined = pd.concat([pd.DataFrame(X_test_count.toarray()), pd.DataFrame(X_test_tfidf.toarray())], axis=1)
y_pred = svm_model.predict(X_test_combined)
accuracy = accuracy_score(df2['Label'], y_pred)
print('Accuracy:', accuracy)