KNN Classification Model

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

KNN Classification Model for Iris and Bill Authentication Dataset

Iris Dataset
Using Library Train_Test_Split

Step 1:
Open anaconda navigator and launch Jupyter Notebook
Save the iris.csv dataset in the same folder as your coding
Create a new phyton file and start doing the coding

Step 2:
Import library pandas in the coding.
This library is used to read the CSV file from the same folder
Load the dataset into the coding

import pandas as pd
iris_data = pd.read_csv('C:/Users/ariny/OneDrive/Documents/iris.csv')
iris_data

Step 3:
Split the dataset into training and testing datasets using the function train_test_split.
This function is used to select training data and testing data randomly.
Import function train_test_split using library sklearn.model_selection
Drop species and random column
Test size is 0.3 because 70% for training and 30% for testing

from sklearn.model_selection import train_test_split


X = iris_data.drop(['RANDOM', 'species'], axis=1)
y = iris_data['species']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

Step 4:
Import library KNN from sklearn.neighbors
Use Classifier to determine false or true
Change n_neighbors according to number of nearest neighbors
Train the model using knn.fit and predict using knn.predict

from sklearn.neighbors import KNeighborsClassifier


knn = KNeighborsClassifier(n_neighbors=1)

knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)
Step 5:
Import metrics to calculate accuracy from sklearn
Calculate accuracy score for knn

from sklearn import metrics


print("Accuracy:", metrics.accuracy_score(y_test, y_pred))

Output:

Step 6:
Import classification_report and confusion_matrix from sklearn.metrics
Print classification_report to generate a report containing precision, recall, f1-score and support
and print confusion_matrix to know the model’s performance for true and false prediction of the
dataset

from sklearn.metrics import classification_report, confusion_matrix


print(confusion_matrix(y_test, y_pred))

Output:

print(classification_report(y_test, y_pred))

Output:
Using Manually Random Ordered

Step 1:
Open anaconda navigator and launch Jupyter Notebook
In the bill_authentication.csv put =RANDBETWEEN(x,y) with x and y is 1 until the last number
Sort and split into two for training data and testing data
Save the datasets in the same folder as your coding

Step 2:
Import library pandas in the coding.
This library is used to read the CSV file from the same folder
Load the dataset into the coding

import pandas as pd
iris_train = pd.read_csv('C:/Users/ariny/OneDrive/Documents/bill_train.csv')
iris_test = pd.read_csv('C:/Users/ariny/OneDrive/Documents/bill_test.csv')

Step 3:
Divide the datasets into input and target variables
Drop class columns in input training and target training

input_training = iris_train.drop(["random","Class"], axis=1)


target_training = iris_train["Class"]
input_testing = iris_test.drop(["random","Class"], axis=1)
target_testing = iris_test["Class"]

Step 4:
Import library KNN from sklearn.neighbors
Use Classifier to determine false or true
Change n_neighbors according to number of nearest neighbors
Train the model using knn.fit and predict using knn.predict

from sklearn.neighbors import KNeighborsClassifier


knn = KNeighborsClassifier(n_neighbors=1)

knn.fit(input_training, target_training)
y_pred = knn.predict(input_testing)

Step 5:
Calculate accuracy using metrics.accuracy_score
Import metrics from sklearn
from sklearn import metrics
print("Accuracy:", metrics.accuracy_score(target_testing, y_pred))

Output:

Step 6:
Import confusion_metrics and classification_report from sklearn.metrics
Print confusion metrics to print out the model’s performance for true and false prediction of the
dataset
Print classification report to generate a report containing precision, recall, f-score and suport of
the model.

from sklearn.metrics import classification_report, confusion_matrix


print(confusion_matrix(target_testing, y_pred))

Output:

print(classification_report(target_testing, y_pred))

Output:
Bill Dataset
Using Library Train_Test_Split

Step 1:
Open anaconda navigator and launch Jupyter Notebook
Save the bill_authentication.csv dataset in the same folder as your coding
Create a new phyton file and start doing the coding

Step 2:
Import library pandas in the coding.
This library is used to read the CSV file from the same folder
Load the dataset into the coding

import pandas as pd
bill_data = pd.read_csv('C:/Users/ariny/OneDrive/Documents/bill_authentication.csv')
bill_data

Step 3:
Split the dataset into training and testing datasets using the function train_test_split.
This function is used to select training data and testing data randomly.
Import function train_test_split using library sklearn.model_selection
Drop class column
Test size is 0.3 because 70% for training and 30% for testing

from sklearn.model_selection import train_test_split


X = bill_data.drop('Class', axis='columns')
y = bill_data['Class']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

Step 4:
Import library KNN from sklearn.neighbors
Use Classifier to determine false or true
Change n_neighbors according to number of nearest neighbors
Train the model using knn.fit and predict using knn.predict

from sklearn.neighbors import KNeighborsClassifier


knn = KNeighborsClassifier(n_neighbors=1)

knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)

Step 5:
Import metrics to calculate accuracy from sklearn
Calculate accuracy score for knn

from sklearn import metrics


print("Accuracy:", metrics.accuracy_score(y_test, y_pred))

Output:

Step 6:
Import classification_report and confusion_matrix from sklearn.metrics
Print classification_report to generate a report containing precision, recall, f1-score and support
and print confusion_matrix to know the model’s performance for true and false prediction of the
dataset

from sklearn.metrics import classification_report, confusion_matrix


print(classification_report(y_test, y_pred))

Output:

print(confusion_matrix(y_test, y_pred))

Output:
Using Manually Random Ordered

Step 1:
Open anaconda navigator and launch Jupyter Notebook
In the bill_authentication.csv put =RANDBETWEEN(x,y) with x and y is 1 until the last number
Sort and split into two for training data and testing data
Save the datasets in the same folder as your coding

Step 2:
Import library pandas in the coding.
This library is used to read the CSV file from the same folder
Load the dataset into the coding

import pandas as pd
bill_train = pd.read_csv('C:/Users/ariny/OneDrive/Documents/bill_train.csv')
bill_test = pd.read_csv('C:/Users/ariny/OneDrive/Documents/bill_test.csv')

Step 3:
Divide the datasets into input and target variables
Drop class columns in input training and target training

input_training = bill_train.drop("Class", axis='columns')


target_training = bill_train["Class"]
input_testing = bill_test.drop("Class", axis='columns')
target_testing = bill_test["Class"]

Step 4:
Import library KNN from sklearn.neighbors
Use Classifier to determine false or true
Change n_neighbors according to number of nearest neighbors
Train the model using knn.fit and predict using knn.predict

from sklearn.neighbors import KNeighborsClassifier


knn = KNeighborsClassifier(n_neighbors=1)

knn.fit(input_training, target_training)
y_pred = knn.predict(input_testing)

Step 5:
Calculate accuracy using metrics.accuracy_score
Import metrics from sklearn
from sklearn import metrics
print("Accuracy:", metrics.accuracy_score(target_testing, y_pred))

Output:

Step 6:
Import confusion_metrics and classification_report from sklearn.metrics
Print confusion metrics to print out the model’s performance for true and false prediction of the
dataset
Print classification report to generate a report containing precision, recall, f-score and suport of
the model.

from sklearn.metrics import classification_report, confusion_matrix


print(confusion_matrix(target_testing, y_pred))

Output:

print(classification_report(target_testing, y_pred))

Output:

You might also like