The following code imports required libraries:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
14- Importing the Dataset
Execute the following command to import the dataset.
banknote_data = pd.read_csv(rD:\Datasets\banknote_data.csv)
The script above reads the dataset and stores it in banknote_data dataframe.
15- Analyzing the Data
The following script returns the data dimensions:
The above script returns (1372, 5) which means that our dataset contains 1372 records and five attributes.
Execute the following script to eyeball the data:
The output looks like this:
16- Data Preprocessing
To following script divides the data into feature and label set.
Finally let’s divide the data into 80 % training and 20% test sets:
from sklearn.model_selection import train_test_split
train_features, test_features, train_labels, test_labels = train_test_split(features, labels, test_size = 0.2, random_state = 0)
17- Scaling the Data
from sklearn.preprocessing import StandardScaler
feature_scaler = StandardScaler()
train_features = feature_scaler.fit_transform(train_features)
test_features = feature_scaler.transform(test_features)
18- Training the Algorithm and making Predictions
To implement KNN Algorithm with Scikit learn we need to use the KNeighborsClassifier class of the sklear.neighbors library. The value of K is specified as value for the n_neighbors parameter as shown in the following script. We use a value of 3 for K. Execute the following script to train the model on train_features and train_labels