KNN
KNN
KNN
In [17]:
We are going to use a very famous dataset called Iris. Attributes: sepal length in cm sepal width in cm petal
length in cm petal width in cm We will just use two features for easier visualization, sepal length and width. Class:
Iris Setosa Iris Versicolour Iris Virginica #Load the Dataset
In [3]:
Out[3]:
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target
file:///C:/Users/pc/Downloads/KNN.html 1/4
10/04/2022 07:51 KNN
In [4]:
iris_df.describe()
Out[4]:
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) target
In [7]:
x=pd.DataFrame(iris.data)
y=pd.DataFrame(iris.target)
x.columns=['Sepal_Length','Sepal_width','Petal_Length','Petal_width']
Out[7]:
file:///C:/Users/pc/Downloads/KNN.html 2/4
10/04/2022 07:51 KNN
In [22]:
--------------------------------------------------------------------------
-
NameError Traceback (most recent call las
t)
<ipython-input-22-668994d18e71> in <module>
----> 1 X = dataset.iloc[:, :-1].values
2 y = dataset.iloc[:, 4].values
In [8]:
y.columns=['Targets']
y
Out[8]:
Targets
0 0
1 0
2 0
3 0
4 0
... ...
145 2
146 2
147 2
148 2
149 2
In [20]:
Feature Scaling Before making any actual predictions, it is always a good practice to scale the features so that all
of them can be uniformly evaluated.
In [21]:
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
file:///C:/Users/pc/Downloads/KNN.html 3/4
10/04/2022 07:51 KNN
Training and Predictions It is extremely straight forward to train the KNN algorithm and make predictions with it,
especially when using Scikit-Learn.
In [23]:
C:\ProgramData\Anaconda3\lib\site-packages\ipykernel_launcher.py:3: DataCo
nversionWarning: A column-vector y was passed when a 1d array was expecte
d. Please change the shape of y to (n_samples, ), for example using ravel
().
This is separate from the ipykernel package so we can avoid doing import
s until
Out[23]:
In [24]:
y_pred = classifier.predict(X_test)
Evaluating the Algorithm For evaluating an algorithm, confusion matrix, precision, recall and f1 score are the
most commonly used metrics. The confusion_matrix and classification_report methods of the sklearn.metrics can
be used to calculate these metrics.
In [25]:
[[11 0 0]
[ 0 10 0]
[ 0 0 9]]
precision recall f1-score support
accuracy 1.00 30
macro avg 1.00 1.00 1.00 30
weighted avg 1.00 1.00 1.00 30
In [ ]:
In [ ]:
file:///C:/Users/pc/Downloads/KNN.html 4/4