0% found this document useful (0 votes)
9 views3 pages

22mc3014 - EK - Lab10.ipynb - Colab

Uploaded by

eishaankhatri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
9 views3 pages

22mc3014 - EK - Lab10.ipynb - Colab

Uploaded by

eishaankhatri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 3

10/23/24, 3:41 PM 22mc3014_EK_Lab10.

ipynb - Colab

import numpy as np
import matplotlib.pyplot as plt
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score

# Expanded Training Data (RGB values and Size for Red Apple, Green Grapes, and Blueberry)
# Format: [R, G, B, Size]
red_apple = [
[255, 0, 0, 10.0], [250, 10, 5, 10.5], [245, 20, 15, 9.8],
[248, 15, 8, 10.2], [252, 5, 3, 10.1]
]
green_grapes = [
[0, 255, 0, 2.0], [5, 250, 10, 2.2], [10, 245, 15, 2.1],
[8, 252, 12, 2.3], [3, 248, 7, 2.4]
]
blueberry = [
[0, 0, 255, 1.5], [10, 5, 250, 1.4], [20, 15, 245, 1.6],
[5, 10, 248, 1.55], [3, 8, 253, 1.7]
]

# Labels for the classes: 0 = Red Apple, 1 = Green Grapes, 2 = Blueberry


X_train = np.array(red_apple + green_grapes + blueberry)
y_train = np.array([0] * 5 + [1] * 5 + [2] * 5)

# Modified Test Data (unknown data points to test the classifier)


X_test = np.array([
[240, 30, 20, 10.2], # Should be Red Apple
[0, 250, 5, 2.3], # Should be Green Grapes
[15, 10, 240, 1.6], # Should be Blueberry
[255, 40, 10, 10.1] # Should be Red Apple
])

y_test = np.array([0, 1, 2, 0]) # Ground truth labels for testing

# 1. LDA Classifier
lda = LDA()
lda.fit(X_train, y_train)
y_pred_lda = lda.predict(X_test)
accuracy_lda = accuracy_score(y_test, y_pred_lda)
print(f'LDA Classification Accuracy: {accuracy_lda * 100:.2f}%')

# 2. KNN Classifier
def knn_classification(k):
knn = KNeighborsClassifier(n_neighbors=k)
knn.fit(X_train, y_train)
y_pred_knn = knn.predict(X_test)
accuracy_knn = accuracy_score(y_test, y_pred_knn)
print(f'KNN (k={k}) Classification Accuracy: {accuracy_knn * 100:.2f}%')

# Test KNN with different k values


for k in [1, 3, 5, 14]:
knn_classification(k)

# 3. Naive Bayes Classifier


nb = GaussianNB()
nb.fit(X_train, y_train)
y_pred_nb = nb.predict(X_test)
accuracy_nb = accuracy_score(y_test, y_pred_nb)
print(f'Naive Bayes Classification Accuracy: {accuracy_nb * 100:.2f}%')

# Scatter plot for training data


plt.figure(figsize=(8, 6))
# Plot Red Apples
plt.scatter(X_train[y_train == 0][:, 0], X_train[y_train == 0][:, 1],
color='red', label='Red Apple (Train)', marker='o', s=100)
# Plot Green Grapes
plt.scatter(X_train[y_train == 1][:, 0], X_train[y_train == 1][:, 1],
color='green', label='Green Grapes (Train)', marker='s', s=100)
# Plot Blueberries
plt.scatter(X_train[y_train == 2][:, 0], X_train[y_train == 2][:, 1],
color='blue', label='Blueberry (Train)', marker='^', s=100)

# Plot Test Samples


plt.scatter(X_test[:, 0], X_test[:, 1], color='black', label='Test Samples',
marker='x', s=200, edgecolor='white')

# Adding labels and title


plt.title('Fruit Classification (RGB + Size)')
plt.xlabel('R (Red)')
l l b l(' ( )')
https://colab.research.google.com/drive/1wQfmFOC39IjAthkk_YiLztxkeNM8P221#scrollTo=QqG03L1awxkN&printMode=true 1/3
10/23/24, 3:41 PM 22mc3014_EK_Lab10.ipynb - Colab
plt.ylabel('G (Green)')
plt.legend()
plt.grid(True)
plt.show()

LDA Classification Accuracy: 100.00%


KNN (k=1) Classification Accuracy: 100.00%
KNN (k=3) Classification Accuracy: 100.00%
KNN (k=5) Classification Accuracy: 100.00%
KNN (k=14) Classification Accuracy: 75.00%
Naive Bayes Classification Accuracy: 100.00%
<ipython-input-10-25a4d05255c8>:76: UserWarning: You passed a edgecolor/edgecolors ('white') for an unfilled marker ('x'). Matplotl
plt.scatter(X_test[:, 0], X_test[:, 1], color='black', label='Test Samples',

import numpy as np
from sklearn.naive_bayes import MultinomialNB
from sklearn.feature_extraction.text import CountVectorizer

# Modified dataset with more normal and spam messages


messages = [
"Dear Lunch", # Normal
"Friend Lunch", # Normal
"Dear Friend", # Normal
"Lunch Money", # Normal
"Meeting Schedule", # Normal
"Lunch Tomorrow", # Normal
"Friend Meeting", # Normal
"Schedule Lunch", # Normal
"Winner Offer Free Money", # Spam
"Claim Urgent Offer Money", # Spam
"Dear Winner Free Offer", # Spam
"Urgent Offer Winner", # Spam
"Friend Dear Money Money", # Spam
"Meeting Free Offer Winner", # Spam
"Money Offer Winner Urgent", # Spam
"Offer Urgent Money Winner", # Spam
]

# Labels: 0 = Normal, 1 = Spam


y_train = np.array([0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1])

# Convert the text messages to token counts (Bag-of-Words model)


vectorizer = CountVectorizer()
X_train = vectorizer.fit_transform(messages)

# Initialize and train the Naive Bayes Classifier


nb = MultinomialNB()
nb.fit(X_train, y_train)

# Define the test messages (with new examples)


test_messages = [
"Dear Friend Schedule", # Should be classified as Normal

https://colab.research.google.com/drive/1wQfmFOC39IjAthkk_YiLztxkeNM8P221#scrollTo=QqG03L1awxkN&printMode=true 2/3
10/23/24, 3:41 PM 22mc3014_EK_Lab10.ipynb - Colab
"Urgent Winner Free Money Money", # Should be classified as Spam
"Lunch Tomorrow with Friend", # Should be classified as Normal
"Claim Free Offer Money Now" # Should be classified as Spam
]

# Convert the test messages to token counts using the same vectorizer
X_test = vectorizer.transform(test_messages)

# Predict the classes for the test messages


y_pred = nb.predict(X_test)

# Display the results


for i, message in enumerate(test_messages):
label = 'Spam' if y_pred[i] == 1 else 'Normal'
print(f"Message: '{message}' -> Classified as: {label}")

Message: 'Dear Friend Schedule' -> Classified as: Normal


Message: 'Urgent Winner Free Money Money' -> Classified as: Spam
Message: 'Lunch Tomorrow with Friend' -> Classified as: Normal
Message: 'Claim Free Offer Money Now' -> Classified as: Spam

https://colab.research.google.com/drive/1wQfmFOC39IjAthkk_YiLztxkeNM8P221#scrollTo=QqG03L1awxkN&printMode=true 3/3

You might also like