Chapter 6: Classification

Making predictions

face Josiah Wang

Once your model has been fitted, you can perform predictions on unseen test data in just one line, with the predict() method of your classifier!

>>> predictions = knn_classifier.predict(x_test)
>>> print(predictions)
[1 0 2 1 1 0 1 2 1 1 2 0 0 0 0 1 2 1 1 2 0 2 0 2 2 2 2 2 0 0]

You can also obtain the probability of predictions across classes, with the predict_proba() method. This is useful if you need multiple predictions per class, or just want to know the classifier’s confidence towards its predictions.

>>> probs = knn_classifier.predict_proba(x_test)
>>> print(probs)
[[0.  1.  0. ]
 [1.  0.  0. ]
 [0.  0.  1. ]
 [0.  1.  0. ]
 [0.  1.  0. ]
 [1.  0.  0. ]
 [0.  1.  0. ]
 [0.  0.  1. ]
 [0.  0.6 0.4]
 [0.  1.  0. ]
 [0.  0.2 0.8]
 ...
 [0.  0.  1. ]
 [1.  0.  0. ]
 [1.  0.  0. ]]

Scikit-learn’s implementation of classifiers is also a very good example of OOP polymorphism in action. This is also known as duck typing. Scikit-learn does not care what classifier you use, as long as the classifier implements a .fit() and a .predict() method. As you can see, this makes it very flexible as we can simply swap classifiers without having to change a lot of code!