Let's take a look at how to build a k-nearest neighbors classifier:
- Create a new Python file and import the following packages (the full code is in the nn_classification.py file that's already provided for you):
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 and y, as shown in the preceding ...