🛠Mini Project: Spam Classifier with Machine Learning
on
Get link
Facebook
X
Pinterest
Email
Other Apps
🛠Mini Project: Spam Classifier with Machine Learning
In this mini project, we'll build a simple Spam Classifier using Machine Learning techniques.
We’ll use TF-IDF Vectorization for feature extraction and a Naive Bayes Classifier for classification.
🔹 Step 1: Import Required Libraries
We'll need scikit-learn library, which provides all the tools we need.
🔹 Step 2: Prepare the Data
We create a small dataset with labels "spam" and "ham" (not spam).
🔹 Step 3: Build and Train the Model
We will transform the text messages into numerical vectors using TF-IDF and train a Naive Bayes classifier.
# Step 1: Import Libraries
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# Step 2: Create Dataset
texts = [
"Congratulations! You've won a free iPhone!",
"Your loan is approved, call now!",
"Let's meet for lunch tomorrow.",
"Important update about your account.",
"Win cash prizes easily!",
"Are you attending the meeting today?"
]
labels = ["spam", "spam", "ham", "ham", "spam", "ham"]
# Step 3: Preprocess and Split Data
X_train, X_test, y_train, y_test = train_test_split(texts, labels, test_size=0.3, random_state=42)
# Step 4: Feature Extraction
vectorizer = TfidfVectorizer()
X_train_vectors = vectorizer.fit_transform(X_train)
X_test_vectors = vectorizer.transform(X_test)
# Step 5: Train Model
model = MultinomialNB()
model.fit(X_train_vectors, y_train)
# Step 6: Predictions
predictions = model.predict(X_test_vectors)
# Step 7: Accuracy
print("Accuracy:", accuracy_score(y_test, predictions))
# Step 8: Testing on New Message
test_message = ["Win an amazing prize now!"]
test_vector = vectorizer.transform(test_message)
result = model.predict(test_vector)
print("Prediction:", result[0])
Comments
Post a Comment