LAB # 07 KNN_Iris Dataset.ipynb - Colab
LAB # 07 KNN_Iris Dataset.ipynb - Colab
ipynb - Colab
import pandas as pd
iris = load_iris()
iris.feature_names
iris.target_names
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)
df.shape
(150, 4)
df['target'] = iris.target
df.head()
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target
df[df.target == 1].head()
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target
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
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target flower_name
df[45:55]
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target flower_name
df0 = df[:50]
df1 = df[50:100]
df2 = df[100:]
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>
x = df.drop(['target','flower_name'], axis='columns')
y = df.target
len(x_train)
90
len(x_test)
60
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
y_pred = knn.predict(x_test)
Confusion Matrix:
[[19 0 0]
[ 1 14 6]
[ 0 0 20]]
<sklearn.metrics._plot.confusion_matrix.ConfusionMatrixDisplay at 0x78d7e7e40bb0>
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
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
(1797, 64)
<Figure size 640x480 with 0 Axes>
https://colab.research.google.com/drive/1Rql8Pw86rMXQ20F3wSkyUjHGyECJtjPv#printMode=true 8/8