Thanks to visit codestin.com
Credit goes to www.scribd.com

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

KNN - Jupyter Notebook

The document is a Jupyter Notebook that demonstrates the implementation of the K-Nearest Neighbors (KNN) algorithm using the Iris dataset. It includes data loading, preprocessing, model training, and evaluation metrics such as accuracy and confusion matrix. The notebook provides visualizations and insights into the dataset's characteristics and classification performance.

Uploaded by

subho_b
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)
14 views8 pages

KNN - Jupyter Notebook

The document is a Jupyter Notebook that demonstrates the implementation of the K-Nearest Neighbors (KNN) algorithm using the Iris dataset. It includes data loading, preprocessing, model training, and evaluation metrics such as accuracy and confusion matrix. The notebook provides visualizations and insights into the dataset's characteristics and classification performance.

Uploaded by

subho_b
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

KNN - Jupyter Notebook http://localhost:8890/notebooks/Desktop/Lakshya%2021-CSE-84/KNN...

In [1]: import numpy as np


import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
In [10]: from sklearn.datasets import load_iris
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import confusion_matrix, accuracy_score
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn metrics import f1_score
In [11]: iris_data=load_iris()
iris_data keys()
Out[11]: dict_keys(['data', 'target', 'frame', 'target_names', 'DESCR', 'feature_names
', 'filename', 'data_module'])

1 of 8 14-09-2023, 16:44
KNN - Jupyter Notebook http://localhost:8890/notebooks/Desktop/Lakshya%2021-CSE-84/KNN...

In [14]: print(iris_data['DESCR'])

.. _iris_dataset:

Iris plants dataset


--------------------

**Data Set Characteristics:**

:Number of Instances: 150 (50 in each of three classes)


:Number of Attributes: 4 numeric, predictive attributes and the class
:Attribute Information:
- sepal length in cm
- sepal width in cm
- petal length in cm
- petal width in cm
- class:
- Iris-Setosa
- Iris-Versicolour
- Iris-Virginica

:Summary Statistics:

============== ==== ==== ======= ===== ====================


Min Max Mean SD Class Correlation
============== ==== ==== ======= ===== ====================
sepal length: 4.3 7.9 5.84 0.83 0.7826
sepal width: 2.0 4.4 3.05 0.43 -0.4194
petal length: 1.0 6.9 3.76 1.76 0.9490 (high!)
petal width: 0.1 2.5 1.20 0.76 0.9565 (high!)
============== ==== ==== ======= ===== ====================

:Missing Attribute Values: None


:Class Distribution: 33.3% for each of 3 classes.
:Creator: R.A. Fisher
:Donor: Michael Marshall (MARSHALL%[email protected])
:Date: July, 1988

The famous Iris database, first used by Sir R.A. Fisher. The dataset is taken
from Fisher's paper. Note that it's the same as in R, but not as in the UCI
Machine Learning Repository, which has two wrong data points.

This is perhaps the best known database to be found in the


pattern recognition literature. Fisher's paper is a classic in the field and
is referenced frequently to this day. (See Duda & Hart, for example.) The
data set contains 3 classes of 50 instances each, where each class refers to
a
type of iris plant. One class is linearly separable from the other 2; the
latter are NOT linearly separable from each other.

.. topic:: References

- Fisher, R.A. "The use of multiple measurements in taxonomic problems"


Annual Eugenics, 7, Part II, 179-188 (1936); also in "Contributions to
Mathematical Statistics" (John Wiley, NY, 1950).
- Duda, R.O., & Hart, P.E. (1973) Pattern Classification and Scene Analysi

2 of 8 14-09-2023, 16:44
KNN - Jupyter Notebook http://localhost:8890/notebooks/Desktop/Lakshya%2021-CSE-84/KNN...

s.
(Q327.D83) John Wiley & Sons. ISBN 0-471-22361-1. See page 218.
- Dasarathy, B.V. (1980) "Nosing Around the Neighborhood: A New System
Structure and Classification Rule for Recognition in Partially Exposed
Environments". IEEE Transactions on Pattern Analysis and Machine
Intelligence, Vol. PAMI-2, No. 1, 67-71.
- Gates, G.W. (1972) "The Reduced Nearest Neighbor Rule". IEEE Transactio
ns
on Information Theory, May 1972, 431-433.
- See also: 1988 MLC Proceedings, 54-64. Cheeseman et al"s AUTOCLASS II
conceptual clustering system finds 3 classes in the data.
- Many, many more ...

In [15]: df=pd.DataFrame(iris_data.data,columns=iris_data.feature_names)
df info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 150 entries, 0 to 149
Data columns (total 4 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 sepal length (cm) 150 non-null float64
1 sepal width (cm) 150 non-null float64
2 petal length (cm) 150 non-null float64
3 petal width (cm) 150 non-null float64
dtypes: float64(4)
memory usage: 4.8 KB

In [16]: df head()

Out[16]:
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

In [17]: df['target'] = iris_data.target


df head()
Out[17]:
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

3 of 8 14-09-2023, 16:44
KNN - Jupyter Notebook http://localhost:8890/notebooks/Desktop/Lakshya%2021-CSE-84/KNN...

In [18]: df[df target 1].head()

Out[18]:
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

In [19]: df[df target 2].head()

Out[19]:
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

In [20]: iris_data['target_names']

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

In [21]: df['flower_name'] = df.target.apply(lambda x:iris_data.target_names[x])


df head()
Out[21]:
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

4 of 8 14-09-2023, 16:44
KNN - Jupyter Notebook http://localhost:8890/notebooks/Desktop/Lakshya%2021-CSE-84/KNN...

In [22]: df[45:55]

Out[22]:
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

In [25]: df0 = df[:50]


df1 = df[50:100]
df2 df[100:]
In [28]: 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
Out[28]: <matplotlib.collections.PathCollection at 0x1da65e0bfa0>

5 of 8 14-09-2023, 16:44
KNN - Jupyter Notebook http://localhost:8890/notebooks/Desktop/Lakshya%2021-CSE-84/KNN...

In [29]: 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
Out[29]: <matplotlib.collections.PathCollection at 0x1da65e7bb50>

In [30]: df head()

Out[30]:
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

In [33]: X = df.drop(['target','flower_name'],axis='columns')
y df target
In [34]: X_train X_test y_train y_test train_test_split(X y test_size 0.30 random_state

6 of 8 14-09-2023, 16:44
KNN - Jupyter Notebook http://localhost:8890/notebooks/Desktop/Lakshya%2021-CSE-84/KNN...

In [35]: knn = KNeighborsClassifier(n_neighbors=3)


knn fit(X_train y_train)
Out[35]: ▾ KNeighborsClassifier
KNeighborsClassifier(n_neighbors=3)

In [38]: knn (X_test y_test)

Out[38]: 0.9777777777777777

In [39]: knn (X_train y_train)

Out[39]: 0.9523809523809523

In [41]: knn predict([[5.2 3.1 1.5 0.3]])

C:\Users\DELL\anaconda3\lib\site-packages\sklearn\base.py:420: UserWarning: X
does not have valid feature names, but KNeighborsClassifier was fitted with f
eature names
warnings.warn(

Out[41]: array([0])

In [42]: y_pred = knn.predict(X_test)


cm = confusion_matrix(y_test,y_pred)

Out[42]: array([[14, 0, 0],


[ 0, 18, 0],
[ 0, 1, 12]], dtype=int64)

In [44]: cm1 = pd.DataFrame(data=cm,index=[0,1,2],columns=['setosa','versicolor','virginica'


cm1
Out[44]:
setosa versicolor virginica

0 14 0 0

1 0 18 0

2 0 1 12

In [45]: print(classification_report(y_test y_pred))

precision recall f1-score support

0 1.00 1.00 1.00 14


1 0.95 1.00 0.97 18
2 1.00 0.92 0.96 13

accuracy 0.98 45
macro avg 0.98 0.97 0.98 45
weighted avg 0.98 0.98 0.98 45

7 of 8 14-09-2023, 16:44
KNN - Jupyter Notebook http://localhost:8890/notebooks/Desktop/Lakshya%2021-CSE-84/KNN...

In [ ]:

8 of 8 14-09-2023, 16:44

You might also like