dl_3
dl_3
2. Objective:
• Understand the concept of linear classifiers and their mathematical formulation.
• Implement a linear classifier for a given dataset and evaluate its performance.
3. Implementation:
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
# Sample data
emails = ["Free money now!!!", "Join Internship", "Earn $1000 per day", "Click on this link
to get $500"]
labels = [1, 0, 0, 0] # 1: Spam, 0: Not Spam
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(emails)
X_train, X_test, y_train, y_test = train_test_split(X, labels, test_size=0.25, random_state=42)
model = LogisticRegression()
model.fit(X_train, y_train)
predictions = model.predict(X_test)
print(classification_report(y_test, predictions))
import numpy as np
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import classification_report
feedback = [
"I love this product, it's amazing!", "Absolutely wonderful experience.",
"This is the worst product I have ever used.", "I am very disappointed.",
"It's okay, not great but not bad either.", "Pretty average, nothing special.",
"Fantastic quality and great value for money!", "Terrible! Will never buy again.",
"Neutral experience, wasn't impressed or dissatisfied.", "I like it, but it's not perfect."
]
labels = [2, 2, 0, 0, 1, 1, 2, 0, 1, 2]
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(feedback)
X_train, X_test, y_train, y_test = train_test_split(X, labels, test_size=0.3, random_state=42)
classifier = MultinomialNB()
classifier.fit(X_train, y_train)
y_pred = classifier.predict(X_test)
print("Classification Report:")
print(classification_report(y_test, y_pred, target_names=["Negative", "Neutral", "Positive"]))
new_feedback = ["This product is just okay.", "I absolutely love it!", "I hate this so much."]
new_feedback_transformed = vectorizer.transform(new_feedback)
predictions = classifier.predict(new_feedback_transformed)
4. Output:
5. Learning Outcomes:
• Accuracy: The classifier's accuracy on the test dataset is displayed (e.g., 97%).
• Confusion Matrix: Shows the number of correct and incorrect predictions.
• Visualization: The decision boundary effectively separates the classes.
• Insight: Logistic Regression is a simple and effective linear classifier for binary
problems.