Constructing a k-nearest neighbors classifier
The k-nearest neighbors is an algorithm that uses k-nearest neighbors in the training dataset to find the category of an unknown object. When we want to find the class to which an unknown point belongs to, we find the k-nearest neighbors and take a majority vote. Let's take a look at how to construct this.
How to do it…
Create a new Python file, and import the following packages:
import numpy as np import matplotlib.pyplot as plt import matplotlib.cm as cm from sklearn import neighbors, datasets from utilities import load_data
We will use the
data_nn_classifier.txt
file for input data. Let's load this input data:# Load input data input_file = 'data_nn_classifier.txt' data = load_data(input_file) X, y = data[:,:-1], data[:,-1].astype(np.int)
The first two columns contain input data and the last column contains the labels. Hence, we separated them into
X
andy
, as shown in the preceding code.Let's visualize the input data:
# Plot input data plt.figure...