Exploring The Intricacies Of K-Nearest Neighbor Algorithm In Machine Learning

Introduction

The K-Nearest Neighbor (KNN) Algorithm is a simple, but powerful machine learning technique that is often used in various classification, regression, and recommendation systems. It's a lazy learning algorithm that stores all instances correspond to training data points in n-dimensional space. When an unknown discrete data is received, it analyzes a certain number (K) of the nearest neighbors.

How does KNN work?

For a sample with unknown classification, the distance to every other point in the data set is calculated. The K points with the smallest distances are selected and the empirical probability density of the classes conditioned on the input variables of this sample is then calculated from these K points (i.e., the proportions of those K samples of various classes are calculated). The estimated class is the class with the highest conditional empirical probability density.

Importing Libraries and Dataset

In Python, we can use Scikit-Learn library to implement and use the algorithm. Let's import the required libraries and the popular "iris" dataset for this example. The iris dataset is a classic and straightforward dataset often used as a beginner's dataset in machine learning and statistics.

from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.neighbors import KNeighborsClassifier from sklearn.metrics import confusion_matrix from sklearn.metrics import accuracy_score from sklearn.datasets import load_iris import pandas as pd iris = load_iris() iris_df = pd.DataFrame(iris.data, columns = iris.feature_names) iris_df['class'] = iris.target

Data Preprocessing

Once we've the data, next step is data preprocessing. This includes splitting the dataset into training and testing datasets, and feature scaling.

X = iris_df.iloc[:, :-1].values y = iris_df.iloc[:, 4].values X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2) scaler = StandardScaler() X_train = scaler.fit_transform(X_train) X_test = scaler.transform(X_test)

Implementing KNN Classifier

Next, we'll define the model, and fit (i.e., train) the model with the training data.

knn = KNeighborsClassifier(n_neighbors=5) knn.fit(X_train, y_train)

Making Predictions and Evaluating the Model

Having trained our K-Nearest Neighbor classifier, let's see how well it does on our testing data.

y_pred = knn.predict(X_test) print(confusion_matrix(y_test, y_pred)) print(accuracy_score(y_test, y_pred))

The confusion matrix shows the true positive, true negative, false positive, and false negative predictions made by the model. The accuracy score will give us a single accuracy metric, indicating the proportion of correct predictions made by the model.

That's a quick primer on the K-Nearest Neighbor algorithm in machine learning. The power of KNN can be utilized in many applications and continues to be a staple method for numerous machine learning problems.