How to do it...

Let's take a look at how to build a k-nearest neighbors classifier:

  1. 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 
  1. 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 ...

Get Python Machine Learning Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.