K-Nearest Neighbors (KNN)

             ORIGINAL DATA
                   |
                   |
         train_test_split()
                   |
         ┌─────────┴─────────┐
         ↓                   ↓
    TRAINING DATA        TESTING DATA
         |                   |
   ┌─────┴─────┐       ┌─────┴─────┐
   ↓           ↓       ↓           ↓
X_train     y_train  X_test      y_test
   |           |       |           |
Inputs      Answers  Inputs     Actual Answers
   |           |       |
   └─────┬─────┘       |
         ↓             ↓
      TRAIN         PREDICT
      MODEL            |
         |             ↓
         |          y_pred
         |             |
         └─────────────┘
                ↓
             Compare
         y_pred  vs  y_test
                ↓
            Accuracy

Install Required Library

Before running the program, install scikit-learn if it is not available:

In [1]:
!pip install scikit-learn
Requirement already satisfied: scikit-learn in c:\users\sri\anaconda3\lib\site-packages (1.3.2)
Requirement already satisfied: numpy<2.0,>=1.17.3 in c:\users\sri\anaconda3\lib\site-packages (from scikit-learn) (1.24.4)
Requirement already satisfied: scipy>=1.5.0 in c:\users\sri\anaconda3\lib\site-packages (from scikit-learn) (1.10.1)
Requirement already satisfied: joblib>=1.1.1 in c:\users\sri\anaconda3\lib\site-packages (from scikit-learn) (1.1.1)
Requirement already satisfied: threadpoolctl>=2.0.0 in c:\users\sri\anaconda3\lib\site-packages (from scikit-learn) (3.1.0)
WARNING: Ignoring invalid distribution -umpy (c:\users\sri\anaconda3\lib\site-packages)
WARNING: Error parsing dependencies of pyodbc: Invalid version: '4.0.0-unsupported'
WARNING: Ignoring invalid distribution -umpy (c:\users\sri\anaconda3\lib\site-packages)
ERROR: Exception:
Traceback (most recent call last):
  File "c:\users\sri\anaconda3\lib\site-packages\pip\_internal\cli\base_command.py", line 106, in _run_wrapper
    status = _inner_run()
  File "c:\users\sri\anaconda3\lib\site-packages\pip\_internal\cli\base_command.py", line 97, in _inner_run
    return self.run(options, args)
  File "c:\users\sri\anaconda3\lib\site-packages\pip\_internal\cli\req_command.py", line 67, in wrapper
    return func(self, options, args)
  File "c:\users\sri\anaconda3\lib\site-packages\pip\_internal\commands\install.py", line 484, in run
    installed_versions[distribution.canonical_name] = distribution.version
  File "c:\users\sri\anaconda3\lib\site-packages\pip\_internal\metadata\pkg_resources.py", line 192, in version
    return parse_version(self._dist.version)
  File "c:\users\sri\anaconda3\lib\site-packages\pip\_vendor\packaging\version.py", line 56, in parse
    return Version(version)
  File "c:\users\sri\anaconda3\lib\site-packages\pip\_vendor\packaging\version.py", line 202, in __init__
    raise InvalidVersion(f"Invalid version: {version!r}")
pip._vendor.packaging.version.InvalidVersion: Invalid version: '4.0.0-unsupported'

Step 1: Import Libraries

In [2]:
# Import required libraries

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
  • sklearn is a Python machine learning library.
  • load_iris() loads the famous IRIS flower dataset.
  • Other imported functions:
Function Purpose
train_test_split( ) Divides data into training and testing sets
KNeighborsClassifier( ) Implements k-NN algorithm
accuracy_score( ) Calculates model accuracy

Step 2: Load IRIS Dataset

The IRIS dataset contains information about flowers. Each flower has four features: Sepal length , Sepal width , Petal length , Petal width

The flowers belong to three classes:

  1. Iris Setosa
  2. Iris Versicolor
  3. Iris Virginica
In [3]:
iris = load_iris()

Step 3: Separate Input and Output Data

In [4]:
X_iris_features = iris.data
y_iris_labels = iris.target
In [5]:
X_iris_features
Out[5]:
array([[5.1, 3.5, 1.4, 0.2],
       [4.9, 3. , 1.4, 0.2],
       [4.7, 3.2, 1.3, 0.2],
       [4.6, 3.1, 1.5, 0.2],
       [5. , 3.6, 1.4, 0.2],
       [5.4, 3.9, 1.7, 0.4],
       [4.6, 3.4, 1.4, 0.3],
       [5. , 3.4, 1.5, 0.2],
       [4.4, 2.9, 1.4, 0.2],
       [4.9, 3.1, 1.5, 0.1],
       [5.4, 3.7, 1.5, 0.2],
       [4.8, 3.4, 1.6, 0.2],
       [4.8, 3. , 1.4, 0.1],
       [4.3, 3. , 1.1, 0.1],
       [5.8, 4. , 1.2, 0.2],
       [5.7, 4.4, 1.5, 0.4],
       [5.4, 3.9, 1.3, 0.4],
       [5.1, 3.5, 1.4, 0.3],
       [5.7, 3.8, 1.7, 0.3],
       [5.1, 3.8, 1.5, 0.3],
       [5.4, 3.4, 1.7, 0.2],
       [5.1, 3.7, 1.5, 0.4],
       [4.6, 3.6, 1. , 0.2],
       [5.1, 3.3, 1.7, 0.5],
       [4.8, 3.4, 1.9, 0.2],
       [5. , 3. , 1.6, 0.2],
       [5. , 3.4, 1.6, 0.4],
       [5.2, 3.5, 1.5, 0.2],
       [5.2, 3.4, 1.4, 0.2],
       [4.7, 3.2, 1.6, 0.2],
       [4.8, 3.1, 1.6, 0.2],
       [5.4, 3.4, 1.5, 0.4],
       [5.2, 4.1, 1.5, 0.1],
       [5.5, 4.2, 1.4, 0.2],
       [4.9, 3.1, 1.5, 0.2],
       [5. , 3.2, 1.2, 0.2],
       [5.5, 3.5, 1.3, 0.2],
       [4.9, 3.6, 1.4, 0.1],
       [4.4, 3. , 1.3, 0.2],
       [5.1, 3.4, 1.5, 0.2],
       [5. , 3.5, 1.3, 0.3],
       [4.5, 2.3, 1.3, 0.3],
       [4.4, 3.2, 1.3, 0.2],
       [5. , 3.5, 1.6, 0.6],
       [5.1, 3.8, 1.9, 0.4],
       [4.8, 3. , 1.4, 0.3],
       [5.1, 3.8, 1.6, 0.2],
       [4.6, 3.2, 1.4, 0.2],
       [5.3, 3.7, 1.5, 0.2],
       [5. , 3.3, 1.4, 0.2],
       [7. , 3.2, 4.7, 1.4],
       [6.4, 3.2, 4.5, 1.5],
       [6.9, 3.1, 4.9, 1.5],
       [5.5, 2.3, 4. , 1.3],
       [6.5, 2.8, 4.6, 1.5],
       [5.7, 2.8, 4.5, 1.3],
       [6.3, 3.3, 4.7, 1.6],
       [4.9, 2.4, 3.3, 1. ],
       [6.6, 2.9, 4.6, 1.3],
       [5.2, 2.7, 3.9, 1.4],
       [5. , 2. , 3.5, 1. ],
       [5.9, 3. , 4.2, 1.5],
       [6. , 2.2, 4. , 1. ],
       [6.1, 2.9, 4.7, 1.4],
       [5.6, 2.9, 3.6, 1.3],
       [6.7, 3.1, 4.4, 1.4],
       [5.6, 3. , 4.5, 1.5],
       [5.8, 2.7, 4.1, 1. ],
       [6.2, 2.2, 4.5, 1.5],
       [5.6, 2.5, 3.9, 1.1],
       [5.9, 3.2, 4.8, 1.8],
       [6.1, 2.8, 4. , 1.3],
       [6.3, 2.5, 4.9, 1.5],
       [6.1, 2.8, 4.7, 1.2],
       [6.4, 2.9, 4.3, 1.3],
       [6.6, 3. , 4.4, 1.4],
       [6.8, 2.8, 4.8, 1.4],
       [6.7, 3. , 5. , 1.7],
       [6. , 2.9, 4.5, 1.5],
       [5.7, 2.6, 3.5, 1. ],
       [5.5, 2.4, 3.8, 1.1],
       [5.5, 2.4, 3.7, 1. ],
       [5.8, 2.7, 3.9, 1.2],
       [6. , 2.7, 5.1, 1.6],
       [5.4, 3. , 4.5, 1.5],
       [6. , 3.4, 4.5, 1.6],
       [6.7, 3.1, 4.7, 1.5],
       [6.3, 2.3, 4.4, 1.3],
       [5.6, 3. , 4.1, 1.3],
       [5.5, 2.5, 4. , 1.3],
       [5.5, 2.6, 4.4, 1.2],
       [6.1, 3. , 4.6, 1.4],
       [5.8, 2.6, 4. , 1.2],
       [5. , 2.3, 3.3, 1. ],
       [5.6, 2.7, 4.2, 1.3],
       [5.7, 3. , 4.2, 1.2],
       [5.7, 2.9, 4.2, 1.3],
       [6.2, 2.9, 4.3, 1.3],
       [5.1, 2.5, 3. , 1.1],
       [5.7, 2.8, 4.1, 1.3],
       [6.3, 3.3, 6. , 2.5],
       [5.8, 2.7, 5.1, 1.9],
       [7.1, 3. , 5.9, 2.1],
       [6.3, 2.9, 5.6, 1.8],
       [6.5, 3. , 5.8, 2.2],
       [7.6, 3. , 6.6, 2.1],
       [4.9, 2.5, 4.5, 1.7],
       [7.3, 2.9, 6.3, 1.8],
       [6.7, 2.5, 5.8, 1.8],
       [7.2, 3.6, 6.1, 2.5],
       [6.5, 3.2, 5.1, 2. ],
       [6.4, 2.7, 5.3, 1.9],
       [6.8, 3. , 5.5, 2.1],
       [5.7, 2.5, 5. , 2. ],
       [5.8, 2.8, 5.1, 2.4],
       [6.4, 3.2, 5.3, 2.3],
       [6.5, 3. , 5.5, 1.8],
       [7.7, 3.8, 6.7, 2.2],
       [7.7, 2.6, 6.9, 2.3],
       [6. , 2.2, 5. , 1.5],
       [6.9, 3.2, 5.7, 2.3],
       [5.6, 2.8, 4.9, 2. ],
       [7.7, 2.8, 6.7, 2. ],
       [6.3, 2.7, 4.9, 1.8],
       [6.7, 3.3, 5.7, 2.1],
       [7.2, 3.2, 6. , 1.8],
       [6.2, 2.8, 4.8, 1.8],
       [6.1, 3. , 4.9, 1.8],
       [6.4, 2.8, 5.6, 2.1],
       [7.2, 3. , 5.8, 1.6],
       [7.4, 2.8, 6.1, 1.9],
       [7.9, 3.8, 6.4, 2. ],
       [6.4, 2.8, 5.6, 2.2],
       [6.3, 2.8, 5.1, 1.5],
       [6.1, 2.6, 5.6, 1.4],
       [7.7, 3. , 6.1, 2.3],
       [6.3, 3.4, 5.6, 2.4],
       [6.4, 3.1, 5.5, 1.8],
       [6. , 3. , 4.8, 1.8],
       [6.9, 3.1, 5.4, 2.1],
       [6.7, 3.1, 5.6, 2.4],
       [6.9, 3.1, 5.1, 2.3],
       [5.8, 2.7, 5.1, 1.9],
       [6.8, 3.2, 5.9, 2.3],
       [6.7, 3.3, 5.7, 2.5],
       [6.7, 3. , 5.2, 2.3],
       [6.3, 2.5, 5. , 1.9],
       [6.5, 3. , 5.2, 2. ],
       [6.2, 3.4, 5.4, 2.3],
       [5.9, 3. , 5.1, 1.8]])
In [6]:
y_iris_labels # 0- Iris Setosa  1- Iris Versicolor 2-Iris Virginica
Out[6]:
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
       1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2])

0- Iris Setosa

1- Iris Versicolor

2-Iris Virginica

In [7]:
# Names of flower classes
class_names = iris.target_names
class_names
Out[7]:
array(['setosa', 'versicolor', 'virginica'], dtype='<U10')

Step 4: Split Dataset

In [8]:
# Split the dataset into training and testing data

# 80% data is used for training
# 20% data is used for testing

X_train, X_test, y_train, y_test = train_test_split(
    X_iris_features, 
    y_iris_labels, 
    test_size=0.2, 
    random_state=42
)

### X_train Training input data
### y_train Training output/target data
### X_test  Testing input data
### y_test  contains the actual correct answers for X_test

Step 5: Create k-NN Model

In [9]:
# Here k = 3 means the algorithm checks the
# 3 nearest neighbours to classify a new data point

knn = KNeighborsClassifier(n_neighbors=3)

When a new flower is given, the algorithm checks the three closest flowers in the training data.

New Flower

Nearest neighbours:

  1. Setosa
  2. Setosa
  3. Versicolor

Majority = Setosa

Prediction = Setosa

Step 6: Train the Model

The algorithm studies the training examples and learns the relationship between:

Flower measurements → Flower type

In [10]:
knn.fit(X_train, y_train)
Out[10]:
KNeighborsClassifier(n_neighbors=3)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.

Step 7: Predict Test Data

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

Step 8: Display Correct and Wrong Predictions

In [12]:
print("----- Prediction Results -----")

for i in range(len(y_test)):

    actual = class_names[y_test[i]]
    predicted = class_names[y_pred[i]]

    print("\nTest Data:", X_test[i])
    print("Actual Class   :", actual)
    print("Predicted Class:", predicted)

    if actual == predicted:
        print("Result: Correct Prediction")
    else:
        print("Result: Wrong Prediction")
----- Prediction Results -----

Test Data: [6.1 2.8 4.7 1.2]
Actual Class   : versicolor
Predicted Class: versicolor
Result: Correct Prediction

Test Data: [5.7 3.8 1.7 0.3]
Actual Class   : setosa
Predicted Class: setosa
Result: Correct Prediction

Test Data: [7.7 2.6 6.9 2.3]
Actual Class   : virginica
Predicted Class: virginica
Result: Correct Prediction

Test Data: [6.  2.9 4.5 1.5]
Actual Class   : versicolor
Predicted Class: versicolor
Result: Correct Prediction

Test Data: [6.8 2.8 4.8 1.4]
Actual Class   : versicolor
Predicted Class: versicolor
Result: Correct Prediction

Test Data: [5.4 3.4 1.5 0.4]
Actual Class   : setosa
Predicted Class: setosa
Result: Correct Prediction

Test Data: [5.6 2.9 3.6 1.3]
Actual Class   : versicolor
Predicted Class: versicolor
Result: Correct Prediction

Test Data: [6.9 3.1 5.1 2.3]
Actual Class   : virginica
Predicted Class: virginica
Result: Correct Prediction

Test Data: [6.2 2.2 4.5 1.5]
Actual Class   : versicolor
Predicted Class: versicolor
Result: Correct Prediction

Test Data: [5.8 2.7 3.9 1.2]
Actual Class   : versicolor
Predicted Class: versicolor
Result: Correct Prediction

Test Data: [6.5 3.2 5.1 2. ]
Actual Class   : virginica
Predicted Class: virginica
Result: Correct Prediction

Test Data: [4.8 3.  1.4 0.1]
Actual Class   : setosa
Predicted Class: setosa
Result: Correct Prediction

Test Data: [5.5 3.5 1.3 0.2]
Actual Class   : setosa
Predicted Class: setosa
Result: Correct Prediction

Test Data: [4.9 3.1 1.5 0.1]
Actual Class   : setosa
Predicted Class: setosa
Result: Correct Prediction

Test Data: [5.1 3.8 1.5 0.3]
Actual Class   : setosa
Predicted Class: setosa
Result: Correct Prediction

Test Data: [6.3 3.3 4.7 1.6]
Actual Class   : versicolor
Predicted Class: versicolor
Result: Correct Prediction

Test Data: [6.5 3.  5.8 2.2]
Actual Class   : virginica
Predicted Class: virginica
Result: Correct Prediction

Test Data: [5.6 2.5 3.9 1.1]
Actual Class   : versicolor
Predicted Class: versicolor
Result: Correct Prediction

Test Data: [5.7 2.8 4.5 1.3]
Actual Class   : versicolor
Predicted Class: versicolor
Result: Correct Prediction

Test Data: [6.4 2.8 5.6 2.2]
Actual Class   : virginica
Predicted Class: virginica
Result: Correct Prediction

Test Data: [4.7 3.2 1.6 0.2]
Actual Class   : setosa
Predicted Class: setosa
Result: Correct Prediction

Test Data: [6.1 3.  4.9 1.8]
Actual Class   : virginica
Predicted Class: virginica
Result: Correct Prediction

Test Data: [5.  3.4 1.6 0.4]
Actual Class   : setosa
Predicted Class: setosa
Result: Correct Prediction

Test Data: [6.4 2.8 5.6 2.1]
Actual Class   : virginica
Predicted Class: virginica
Result: Correct Prediction

Test Data: [7.9 3.8 6.4 2. ]
Actual Class   : virginica
Predicted Class: virginica
Result: Correct Prediction

Test Data: [6.7 3.  5.2 2.3]
Actual Class   : virginica
Predicted Class: virginica
Result: Correct Prediction

Test Data: [6.7 2.5 5.8 1.8]
Actual Class   : virginica
Predicted Class: virginica
Result: Correct Prediction

Test Data: [6.8 3.2 5.9 2.3]
Actual Class   : virginica
Predicted Class: virginica
Result: Correct Prediction

Test Data: [4.8 3.  1.4 0.3]
Actual Class   : setosa
Predicted Class: setosa
Result: Correct Prediction

Test Data: [4.8 3.1 1.6 0.2]
Actual Class   : setosa
Predicted Class: setosa
Result: Correct Prediction

Step 9: Calculate Accuracy

Accuracy = (Correct Predictions / Total Predictions ) * 100

In [13]:
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy of k-NN Model:", accuracy * 100, "%")
Accuracy of k-NN Model: 100.0 %
In [14]:
print("Dimensions of y_test:", y_test.shape)
print("Dimensions of y_pred:", y_pred.shape)
Dimensions of y_test: (30,)
Dimensions of y_pred: (30,)
In [15]:
print(y_test)
print(y_pred)
[1 0 2 1 1 0 1 2 1 1 2 0 0 0 0 1 2 1 1 2 0 2 0 2 2 2 2 2 0 0]
[1 0 2 1 1 0 1 2 1 1 2 0 0 0 0 1 2 1 1 2 0 2 0 2 2 2 2 2 0 0]

Step 10 — Create the confusion matrix

In [16]:
cm = confusion_matrix(
    y_test,
    y_pred
)

print(cm)
[[10  0  0]
 [ 0  9  0]
 [ 0  0 11]]
Predicted
Setosa Versicolor Virginica
Actual
Setosa 10 0 0
Versicolor 0 9 0
Virginica 0 0 11

Complete Code

In [17]:
# Import required libraries

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
In [18]:
# Load the IRIS dataset

iris = load_iris()

# Features of flowers
X = iris.data

# Target classes (species)
y = iris.target

# Names of flower classes
class_names = iris.target_names


# Split the dataset into training and testing data

# 80% data is used for training
# 20% data is used for testing

X_train, X_test, y_train, y_test = train_test_split(
    X, 
    y, 
    test_size=0.2, 
    random_state=42
)


# Create k-NN classifier

# Here k = 3 means the algorithm checks the
# 3 nearest neighbours to classify a new data point

knn = KNeighborsClassifier(n_neighbors=3)


# Train the model

knn.fit(X_train, y_train)


# Make predictions using test data

y_pred = knn.predict(X_test)


# Display correct and wrong predictions

print("----- Prediction Results -----")

for i in range(len(y_test)):

    actual = class_names[y_test[i]]
    predicted = class_names[y_pred[i]]

    print("\nTest Data:", X_test[i])
    print("Actual Class   :", actual)
    print("Predicted Class:", predicted)

    if actual == predicted:
        print("Result: Correct Prediction")
    else:
        print("Result: Wrong Prediction")


# Calculate accuracy

accuracy = accuracy_score(y_test, y_pred)

print("\n------------------------------")
print("Accuracy of k-NN Model:", accuracy * 100, "%")

cm = confusion_matrix(
    y_test,
    y_pred
)

print(cm)
----- Prediction Results -----

Test Data: [6.1 2.8 4.7 1.2]
Actual Class   : versicolor
Predicted Class: versicolor
Result: Correct Prediction

Test Data: [5.7 3.8 1.7 0.3]
Actual Class   : setosa
Predicted Class: setosa
Result: Correct Prediction

Test Data: [7.7 2.6 6.9 2.3]
Actual Class   : virginica
Predicted Class: virginica
Result: Correct Prediction

Test Data: [6.  2.9 4.5 1.5]
Actual Class   : versicolor
Predicted Class: versicolor
Result: Correct Prediction

Test Data: [6.8 2.8 4.8 1.4]
Actual Class   : versicolor
Predicted Class: versicolor
Result: Correct Prediction

Test Data: [5.4 3.4 1.5 0.4]
Actual Class   : setosa
Predicted Class: setosa
Result: Correct Prediction

Test Data: [5.6 2.9 3.6 1.3]
Actual Class   : versicolor
Predicted Class: versicolor
Result: Correct Prediction

Test Data: [6.9 3.1 5.1 2.3]
Actual Class   : virginica
Predicted Class: virginica
Result: Correct Prediction

Test Data: [6.2 2.2 4.5 1.5]
Actual Class   : versicolor
Predicted Class: versicolor
Result: Correct Prediction

Test Data: [5.8 2.7 3.9 1.2]
Actual Class   : versicolor
Predicted Class: versicolor
Result: Correct Prediction

Test Data: [6.5 3.2 5.1 2. ]
Actual Class   : virginica
Predicted Class: virginica
Result: Correct Prediction

Test Data: [4.8 3.  1.4 0.1]
Actual Class   : setosa
Predicted Class: setosa
Result: Correct Prediction

Test Data: [5.5 3.5 1.3 0.2]
Actual Class   : setosa
Predicted Class: setosa
Result: Correct Prediction

Test Data: [4.9 3.1 1.5 0.1]
Actual Class   : setosa
Predicted Class: setosa
Result: Correct Prediction

Test Data: [5.1 3.8 1.5 0.3]
Actual Class   : setosa
Predicted Class: setosa
Result: Correct Prediction

Test Data: [6.3 3.3 4.7 1.6]
Actual Class   : versicolor
Predicted Class: versicolor
Result: Correct Prediction

Test Data: [6.5 3.  5.8 2.2]
Actual Class   : virginica
Predicted Class: virginica
Result: Correct Prediction

Test Data: [5.6 2.5 3.9 1.1]
Actual Class   : versicolor
Predicted Class: versicolor
Result: Correct Prediction

Test Data: [5.7 2.8 4.5 1.3]
Actual Class   : versicolor
Predicted Class: versicolor
Result: Correct Prediction

Test Data: [6.4 2.8 5.6 2.2]
Actual Class   : virginica
Predicted Class: virginica
Result: Correct Prediction

Test Data: [4.7 3.2 1.6 0.2]
Actual Class   : setosa
Predicted Class: setosa
Result: Correct Prediction

Test Data: [6.1 3.  4.9 1.8]
Actual Class   : virginica
Predicted Class: virginica
Result: Correct Prediction

Test Data: [5.  3.4 1.6 0.4]
Actual Class   : setosa
Predicted Class: setosa
Result: Correct Prediction

Test Data: [6.4 2.8 5.6 2.1]
Actual Class   : virginica
Predicted Class: virginica
Result: Correct Prediction

Test Data: [7.9 3.8 6.4 2. ]
Actual Class   : virginica
Predicted Class: virginica
Result: Correct Prediction

Test Data: [6.7 3.  5.2 2.3]
Actual Class   : virginica
Predicted Class: virginica
Result: Correct Prediction

Test Data: [6.7 2.5 5.8 1.8]
Actual Class   : virginica
Predicted Class: virginica
Result: Correct Prediction

Test Data: [6.8 3.2 5.9 2.3]
Actual Class   : virginica
Predicted Class: virginica
Result: Correct Prediction

Test Data: [4.8 3.  1.4 0.3]
Actual Class   : setosa
Predicted Class: setosa
Result: Correct Prediction

Test Data: [4.8 3.1 1.6 0.2]
Actual Class   : setosa
Predicted Class: setosa
Result: Correct Prediction

------------------------------
Accuracy of k-NN Model: 100.0 %
[[10  0  0]
 [ 0  9  0]
 [ 0  0 11]]