0% found this document useful (0 votes)
7 views

LAB # 07 KNN_Iris Dataset.ipynb - Colab

Uploaded by

Abdullah khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

LAB # 07 KNN_Iris Dataset.ipynb - Colab

Uploaded by

Abdullah khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

09/12/2024, 22:49 LAB # 07 KNN_Iris Dataset.

ipynb - Colab

import pandas as pd

from sklearn.datasets import load_iris

iris = load_iris()

iris.feature_names

['sepal length (cm)',


'sepal width (cm)',
'petal length (cm)',
'petal width (cm)']

Double-click (or enter) to edit

iris.target_names

array(['setosa', 'versicolor', 'virginica'], dtype='<U10')

df = pd.DataFrame(iris.data, columns=iris.feature_names)
df.head()

https://colab.research.google.com/drive/1Rql8Pw86rMXQ20F3wSkyUjHGyECJtjPv#printMode=true 1/8
09/12/2024, 22:49 LAB # 07 KNN_Iris Dataset.ipynb - Colab

sepal length (cm) sepal width (cm) petal length (cm) petal width (cm)

0 5.1 3.5 1.4 0.2

1 4.9 3.0 1.4 0.2

2 4.7 3.2 1.3 0.2

3 4.6 3.1 1.5 0.2

4 5.0 3.6 1.4 0.2

df.shape

(150, 4)

df['target'] = iris.target
df.head()

sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target

0 5.1 3.5 1.4 0.2 0

1 4.9 3.0 1.4 0.2 0

2 4.7 3.2 1.3 0.2 0

3 4.6 3.1 1.5 0.2 0

4 5.0 3.6 1.4 0.2 0

df[df.target == 1].head()

sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target

50 7.0 3.2 4.7 1.4 1

51 6.4 3.2 4.5 1.5 1

52 6.9 3.1 4.9 1.5 1

53 5.5 2.3 4.0 1.3 1

54 6.5 2.8 4.6 1.5 1

df[df.target == 2].head()

https://colab.research.google.com/drive/1Rql8Pw86rMXQ20F3wSkyUjHGyECJtjPv#printMode=true 2/8
09/12/2024, 22:49 LAB # 07 KNN_Iris Dataset.ipynb - Colab

sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target

100 6.3 3.3 6.0 2.5 2

101 5.8 2.7 5.1 1.9 2

102 7.1 3.0 5.9 2.1 2

103 6.3 2.9 5.6 1.8 2

104 6.5 3.0 5.8 2.2 2

df['flower_name'] = df.target.apply(lambda x: iris.target_names[x])


df.head()

sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target flower_name

0 5.1 3.5 1.4 0.2 0 setosa

1 4.9 3.0 1.4 0.2 0 setosa

2 4.7 3.2 1.3 0.2 0 setosa

3 4.6 3.1 1.5 0.2 0 setosa

4 5.0 3.6 1.4 0.2 0 setosa

df[45:55]

sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target flower_name

45 4.8 3.0 1.4 0.3 0 setosa

46 5.1 3.8 1.6 0.2 0 setosa

47 4.6 3.2 1.4 0.2 0 setosa

48 5.3 3.7 1.5 0.2 0 setosa

49 5.0 3.3 1.4 0.2 0 setosa

50 7.0 3.2 4.7 1.4 1 versicolor

51 6.4 3.2 4.5 1.5 1 versicolor

52 6.9 3.1 4.9 1.5 1 versicolor

53 5.5 2.3 4.0 1.3 1 versicolor

54 6.5 2.8 4.6 1.5 1 versicolor

df0 = df[:50]
df1 = df[50:100]
df2 = df[100:]

import matplotlib.pyplot as plt


%matplotlib inline

https://colab.research.google.com/drive/1Rql8Pw86rMXQ20F3wSkyUjHGyECJtjPv#printMode=true 3/8
09/12/2024, 22:49 LAB # 07 KNN_Iris Dataset.ipynb - Colab

plt.xlabel('Sepal Length')
plt.ylabel('Sepal Width')
plt.scatter(df0['sepal length (cm)'], df0['sepal width (cm)'],color="green",marker='+')
plt.scatter(df1['sepal length (cm)'], df1['sepal width (cm)'],color="blue",marker='*')

<matplotlib.collections.PathCollection at 0x78d7e90c9270>

plt.xlabel('Petal Length')
plt.ylabel('Petal Width')
plt.scatter(df0['petal length (cm)'], df0['petal width (cm)'],color="green",marker='+')
plt.scatter(df1['petal length (cm)'], df1['petal width (cm)'],color="blue",marker='*')

https://colab.research.google.com/drive/1Rql8Pw86rMXQ20F3wSkyUjHGyECJtjPv#printMode=true 4/8
09/12/2024, 22:49 LAB # 07 KNN_Iris Dataset.ipynb - Colab
<matplotlib.collections.PathCollection at 0x78d7e869e710>

from sklearn.model_selection import train_test_split

x = df.drop(['target','flower_name'], axis='columns')
y = df.target

x_train, x_test, y_train, y_test = train_test_split(x,y, test_size=0.4, random_state=1)

len(x_train)

90

len(x_test)

60

from sklearn.neighbors import KNeighborsClassifier


knn = KNeighborsClassifier (n_neighbors=55)

knn.fit(x_train, y_train)

▾ KNeighborsClassifier i ?

KNeighborsClassifier(n_neighbors=55)

knn.score(x_test, y_test)

https://colab.research.google.com/drive/1Rql8Pw86rMXQ20F3wSkyUjHGyECJtjPv#printMode=true 5/8
09/12/2024, 22:49 LAB # 07 KNN_Iris Dataset.ipynb - Colab
0.8833333333333333

from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay

y_pred = knn.predict(x_test)

# Compute confusion matrix


cm = confusion_matrix(y_test, y_pred)

# Display confusion matrix in text form


print("Confusion Matrix:\n", cm)

# Plot confusion matrix


disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=knn.classes_)
disp.plot(cmap="Blues")

Confusion Matrix:
[[19 0 0]
[ 1 14 6]
[ 0 0 20]]
<sklearn.metrics._plot.confusion_matrix.ConfusionMatrixDisplay at 0x78d7e7e40bb0>

print(f"\nTotal number of data points in test set: {len(y_test)}")


print(f"Total number of predictions made: {total_predictions}")

Total number of data points in test set: 60


Total number of predictions made: 60

https://colab.research.google.com/drive/1Rql8Pw86rMXQ20F3wSkyUjHGyECJtjPv#printMode=true 6/8
09/12/2024, 22:49 LAB # 07 KNN_Iris Dataset.ipynb - Colab
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay

# Compute confusion matrix


cm = confusion_matrix(y_test, y_pred)

# Calculate total test set predictions


total_predictions = cm.sum()

# Display confusion matrix in text form


print("Confusion Matrix:")
print(cm)

# Display class labels and confusion matrix summary


class_labels = knn.classes_
print("\nClass Labels:")
for i, label in enumerate(class_labels):
print(f"Class {i}: {label}")

print(f"\nTotal number of data points in test set: {len(y_test)}")


print(f"Total number of predictions made: {total_predictions}")

# Plot confusion matrix with class labels


disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=class_labels)
disp.plot(cmap="Blues")

# Show the plot


import matplotlib.pyplot as plt
plt.show()

https://colab.research.google.com/drive/1Rql8Pw86rMXQ20F3wSkyUjHGyECJtjPv#printMode=true 7/8
09/12/2024, 22:49 LAB # 07 KNN_Iris Dataset.ipynb - Colab
Confusion Matrix:
[[11 0 0]
[ 1 10 2]
[ 0 0 6]]

Class Labels:
Class 0: 0

keyboard_arrow_down Load digits dataset from sklearn, and use knn classifier for classification of digits
Class 1: 1
Class 2: 2

Total number of data points in test set: 30


Total number of predictions made: 30
from sklearn.datasets import load_digits
digits = load_digits()
print(digits.data.shape)
(1797, 64)
import matplotlib.pyplot as plt
plt.gray()
plt.matshow(digits.images[2])
plt.show()

(1797, 64)
<Figure size 640x480 with 0 Axes>

https://colab.research.google.com/drive/1Rql8Pw86rMXQ20F3wSkyUjHGyECJtjPv#printMode=true 8/8

You might also like