0% found this document useful (0 votes)
6 views8 pages

Python Spam Filter with k-NN & Random Forest

The document contains Python code implementations for two machine learning tasks: a spam filter using the k Nearest Neighbours algorithm and a feature extraction algorithm using Random Forest. The first part includes data preparation, model training, evaluation, and visualization of results for different values of k. The second part focuses on training a Random Forest classifier, evaluating its accuracy, and visualizing the top 10 important features.

Uploaded by

Sinchana S Kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

Python Spam Filter with k-NN & Random Forest

The document contains Python code implementations for two machine learning tasks: a spam filter using the k Nearest Neighbours algorithm and a feature extraction algorithm using Random Forest. The first part includes data preparation, model training, evaluation, and visualization of results for different values of k. The second part focuses on training a Random Forest classifier, evaluating its accuracy, and visualizing the top 10 important features.

Uploaded by

Sinchana S Kumar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

SINCHANA S KUMAR

1BG22EC101

Open ended programming questions using python

[Link] a Spam Filter using k Nearest Neighbours


Algorithm.
CODE:
import pandas as pd
import numpy as np
import [Link] as plt
import seaborn as sns
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split
from [Link] import KNeighborsClassifier
from [Link] import (
accuracy_score,
classification_report,
confusion_matrix
)

# Sample dataset
data = {
'text': [
'Win money now!!!',
'Hello, how are you?',
'Congratulations, you won a free ticket!',
'Are you coming to the meeting?',
'Free entry in a weekly contest!!!',
'Call me when you are free',
'Urgent! Call now to win cash prize',
'Let’s go for lunch tomorrow',
'Claim your reward now',
'What are you doing today?'
],
'label': [
'spam', 'ham', 'spam', 'ham', 'spam',
'ham', 'spam', 'ham', 'spam', 'ham'
]
}

# Convert to DataFrame
df = [Link](data)
df['label'] = df['label'].map({'ham': 0, 'spam': 1})

# Vectorize text
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(df['text'])
y = df['label']

# Split data
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)

# Function to plot confusion matrix


def plot_confusion_matrix(y_true, y_pred, title):
cm = confusion_matrix(y_true, y_pred)
[Link](cm, annot=True, fmt='d', cmap='Blues',
xticklabels=['Ham', 'Spam'],
yticklabels=['Ham', 'Spam'])
[Link](title)
[Link]('Predicted')
[Link]('Actual')
[Link]()

# Train k-NN and evaluate for different k values


k_values = range(1, 8)
accuracies = []

for k in k_values:
knn = KNeighborsClassifier(n_neighbors=k)
[Link](X_train, y_train)
y_pred = [Link](X_test)
acc = accuracy_score(y_test, y_pred)
[Link](acc)

print(f"\nResults for k={k}:")


print("Accuracy:", acc)
print(classification_report(y_test, y_pred))
plot_confusion_matrix(y_test, y_pred, title=f"Confusion Matrix (k={k})")

# Plot accuracy vs k
[Link](figsize=(8, 5))
[Link](k_values, accuracies, marker='o', linestyle='--', color='purple')
[Link]("Accuracy vs Number of Neighbors (k)")
[Link]("k (Number of Neighbors)")
[Link]("Accuracy")
[Link](k_values)
[Link](True)
[Link]()
OUTPUT:
2. Implement a Feature Extraction Algorithm using Random
Forest.

CODE:
import pandas as pd
import numpy as np
import [Link] as plt
import seaborn as sns

from sklearn.feature_extraction.text import CountVectorizer


from [Link] import RandomForestClassifier
from sklearn.model_selection import train_test_split
from [Link] import classification_report, accuracy_score

# Sample dataset (can be replaced with a larger one)


data = {
'text': [
'Free money now!!!',
'Hey how are you?',
'Win a brand new iPhone!',
'Are we meeting tomorrow?',
'Free entry in contest!!!',
'Let’s catch up today',
'Congratulations you’ve won!',
'What are you doing now?',
'Call this number for reward',
'Let’s have lunch tomorrow'
],
'label': [
'spam', 'ham', 'spam', 'ham', 'spam',
'ham', 'spam', 'ham', 'spam', 'ham'
]
}
# Load data into DataFrame
df = [Link](data)
df['label'] = df['label'].map({'ham': 0, 'spam': 1})
# Vectorize the text using CountVectorizer
vectorizer = CountVectorizer()
X = vectorizer.fit_transform(df['text'])
y = df['label']
# Train/Test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
# Random Forest Classifier
rf = RandomForestClassifier(n_estimators=100, random_state=42)
[Link](X_train, y_train)
# Predict and evaluate
y_pred = [Link](X_test)
print("Accuracy:", accuracy_score(y_test, y_pred))
print("\nClassification Report:\n", classification_report(y_test, y_pred))
# Get feature importances
feature_names = vectorizer.get_feature_names_out()
importances = rf.feature_importances_
# Create a DataFrame for visualization
feat_df = [Link]({'Feature': feature_names, 'Importance': importances})
feat_df = feat_df.sort_values(by='Importance', ascending=False).head(10) # Top 10 features
# Plot the feature importances
[Link](figsize=(10, 6))
[Link](x='Importance', y='Feature', data=feat_df, palette='viridis')
[Link]("Top 10 Important Features from Random Forest")
[Link]("Importance Score")
[Link]("Word (Feature)")
plt.tight_layout()
[Link]()

OUTPUT:

You might also like