How to do it…

Let's see how to build a simple classifier using some training data:

  1. We will use the simple_classifier.py file, already provided to you as a reference. To start, we import the numpy and matplotlib.pyplot packages, as we did in Chapter 1The Realm of Supervised Learning, and then we create some sample data:
import numpy as np
import matplotlib.pyplot as plt

X = np.array([[3,1], [2,5], [1,8], [6,4], [5,2], [3,5], [4,7], [4,-1]])
  1. Let's assign some labels to these points:
y = [0, 1, 1, 0, 0, 1, 1, 0]
  1. As we have only two classes, the y list contains 0's and 1's. In general, if you have N classes, then the values in y will range from 0 to N-1. Let's separate the data into classes based on the labels:
class_0 = np.array([X[i] for i in range(len(X)) if y[i]==0])
class_1 = np.array([X[i] for i in range(len(X)) if y[i]==1])
  1. To get an idea about our data, let's plot it, as follows:
plt.figure()
plt.scatter(class_0[:,0], class_0[:,1], color='black', marker='s')
plt.scatter(class_1[:,0], class_1[:,1], color='black', marker='x')
plt.show()

This is a scatterplot, where we use squares and crosses to plot the points. In this context, the marker parameter specifies the shape you want to use. We use squares to denote points in class_0 and crosses to denote points in class_1. If you run this code, you will see the following output:

  1. In the preceding two lines, we just use the mapping between X and y to create two lists. If you were asked to inspect the datapoints visually and draw a separating line, what would you do? You would simply draw a line in between them. Let's go ahead and do this:
line_x = range(10)
line_y = line_x
  1. We just created a line with the mathematical equation y = x. Let's plot it, as follows:
plt.figure()
plt.scatter(class_0[:,0], class_0[:,1], color='black', marker='s')
plt.scatter(class_1[:,0], class_1[:,1], color='black', marker='x')
plt.plot(line_x, line_y, color='black', linewidth=3)
plt.show()

  1. If you run this code, you should see the following output:

The preceding shows how that construction of a separation line between the two classes was simple. In this simple example, this operation was easy, but in many cases, building a line of separation between two classes can be very difficult.