Learning to recognize handwritten digits with a K-nearest neighbors classifier

In this recipe, we will see how to recognize handwritten digits with a K-nearest neighbors (K-NN) classifier. This classifier is a simple but powerful model, well-adapted to complex, highly nonlinear datasets such as images. We will explain how it works later in this recipe.

How to do it...

  1. We import the modules:
    >>> import numpy as np
        import sklearn
        import sklearn.datasets as ds
        import sklearn.model_selection as ms
        import sklearn.neighbors as nb
        import matplotlib.pyplot as plt
        %matplotlib inline
  2. Let's load the digits dataset, part of the datasets module of scikit-learn. This dataset contains handwritten digits that have been manually labeled:
    >>> digits = ds.load_digits() ...

Get IPython Interactive Computing and Visualization Cookbook - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.