Spaces:
Sleeping
Sleeping
Update pages/spam_ham_Detection.py
Browse files- pages/spam_ham_Detection.py +59 -59
pages/spam_ham_Detection.py
CHANGED
|
@@ -1,59 +1,59 @@
|
|
| 1 |
-
import pandas as pd
|
| 2 |
-
import numpy as np
|
| 3 |
-
import streamlit as st
|
| 4 |
-
import seaborn as sns
|
| 5 |
-
from sklearn.feature_extraction.text import CountVectorizer
|
| 6 |
-
from sklearn.model_selection import train_test_split
|
| 7 |
-
from sklearn.neighbors import KNeighborsClassifier
|
| 8 |
-
from sklearn.naive_bayes import MultinomialNB
|
| 9 |
-
from sklearn.tree import DecisionTreeClassifier
|
| 10 |
-
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
|
| 11 |
-
from sklearn.svm import SVC
|
| 12 |
-
from sklearn.metrics import accuracy_score
|
| 13 |
-
|
| 14 |
-
st.markdown(
|
| 15 |
-
"<h1 style='text-align: center; color: #FF4B4B;'>π§ Email Spam or Ham Classification</h1>",
|
| 16 |
-
unsafe_allow_html=True
|
| 17 |
-
)
|
| 18 |
-
|
| 19 |
-
df = pd.read_csv(
|
| 20 |
-
|
| 21 |
-
# Text Vectorization
|
| 22 |
-
bow = CountVectorizer(stop_words='english')
|
| 23 |
-
x = df['Message']
|
| 24 |
-
y = df['Category']
|
| 25 |
-
final_data = pd.DataFrame(bow.fit_transform(x).toarray(), columns=bow.get_feature_names_out())
|
| 26 |
-
|
| 27 |
-
# Train-test split
|
| 28 |
-
x_train, x_test, y_train, y_test = train_test_split(final_data, y, test_size=0.25, random_state=20)
|
| 29 |
-
|
| 30 |
-
# Model Selection
|
| 31 |
-
model_choice = st.radio("Select a model to evaluate:", ['KNN', 'Naive Bayes', 'Decision Tree', 'Random Forest', 'SVM'])
|
| 32 |
-
|
| 33 |
-
if model_choice == 'KNN':
|
| 34 |
-
model = KNeighborsClassifier()
|
| 35 |
-
elif model_choice == 'Naive Bayes':
|
| 36 |
-
model = MultinomialNB()
|
| 37 |
-
elif model_choice == 'Decision Tree':
|
| 38 |
-
model = DecisionTreeClassifier()
|
| 39 |
-
elif model_choice == 'Random Forest':
|
| 40 |
-
model = RandomForestClassifier()
|
| 41 |
-
elif model_choice == 'SVM':
|
| 42 |
-
model = SVC()
|
| 43 |
-
|
| 44 |
-
# Train Model
|
| 45 |
-
model.fit(x_train, y_train)
|
| 46 |
-
y_pred = model.predict(x_test)
|
| 47 |
-
accuracy = accuracy_score(y_test, y_pred)
|
| 48 |
-
|
| 49 |
-
# Display Accuracy
|
| 50 |
-
if st.button("π Show Model Accuracy"):
|
| 51 |
-
st.write(f"### {model_choice} Model Accuracy: {accuracy:.2f}")
|
| 52 |
-
|
| 53 |
-
# Text Input for Prediction
|
| 54 |
-
user_input = st.text_area("βοΈ Enter text for prediction:")
|
| 55 |
-
|
| 56 |
-
if st.button("π Predict"):
|
| 57 |
-
data = bow.transform([user_input]).toarray()
|
| 58 |
-
prediction = model.predict(data)[0]
|
| 59 |
-
st.success(f"π Prediction: {prediction}")
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import seaborn as sns
|
| 5 |
+
from sklearn.feature_extraction.text import CountVectorizer
|
| 6 |
+
from sklearn.model_selection import train_test_split
|
| 7 |
+
from sklearn.neighbors import KNeighborsClassifier
|
| 8 |
+
from sklearn.naive_bayes import MultinomialNB
|
| 9 |
+
from sklearn.tree import DecisionTreeClassifier
|
| 10 |
+
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
|
| 11 |
+
from sklearn.svm import SVC
|
| 12 |
+
from sklearn.metrics import accuracy_score
|
| 13 |
+
|
| 14 |
+
st.markdown(
|
| 15 |
+
"<h1 style='text-align: center; color: #FF4B4B;'>π§ Email Spam or Ham Classification</h1>",
|
| 16 |
+
unsafe_allow_html=True
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
df = pd.read_csv('spam.csv')
|
| 20 |
+
|
| 21 |
+
# Text Vectorization
|
| 22 |
+
bow = CountVectorizer(stop_words='english')
|
| 23 |
+
x = df['Message']
|
| 24 |
+
y = df['Category']
|
| 25 |
+
final_data = pd.DataFrame(bow.fit_transform(x).toarray(), columns=bow.get_feature_names_out())
|
| 26 |
+
|
| 27 |
+
# Train-test split
|
| 28 |
+
x_train, x_test, y_train, y_test = train_test_split(final_data, y, test_size=0.25, random_state=20)
|
| 29 |
+
|
| 30 |
+
# Model Selection
|
| 31 |
+
model_choice = st.radio("Select a model to evaluate:", ['KNN', 'Naive Bayes', 'Decision Tree', 'Random Forest', 'SVM'])
|
| 32 |
+
|
| 33 |
+
if model_choice == 'KNN':
|
| 34 |
+
model = KNeighborsClassifier()
|
| 35 |
+
elif model_choice == 'Naive Bayes':
|
| 36 |
+
model = MultinomialNB()
|
| 37 |
+
elif model_choice == 'Decision Tree':
|
| 38 |
+
model = DecisionTreeClassifier()
|
| 39 |
+
elif model_choice == 'Random Forest':
|
| 40 |
+
model = RandomForestClassifier()
|
| 41 |
+
elif model_choice == 'SVM':
|
| 42 |
+
model = SVC()
|
| 43 |
+
|
| 44 |
+
# Train Model
|
| 45 |
+
model.fit(x_train, y_train)
|
| 46 |
+
y_pred = model.predict(x_test)
|
| 47 |
+
accuracy = accuracy_score(y_test, y_pred)
|
| 48 |
+
|
| 49 |
+
# Display Accuracy
|
| 50 |
+
if st.button("π Show Model Accuracy"):
|
| 51 |
+
st.write(f"### {model_choice} Model Accuracy: {accuracy:.2f}")
|
| 52 |
+
|
| 53 |
+
# Text Input for Prediction
|
| 54 |
+
user_input = st.text_area("βοΈ Enter text for prediction:")
|
| 55 |
+
|
| 56 |
+
if st.button("π Predict"):
|
| 57 |
+
data = bow.transform([user_input]).toarray()
|
| 58 |
+
prediction = model.predict(data)[0]
|
| 59 |
+
st.success(f"π Prediction: {prediction}")
|