**[**** 213 ****]**

**Classifying handwritten digits**

In the previous chapter we used a support vector machine to classify the handwritten

digits in the MNIST dataset. In this section we will classify the images using an

artificial neural network:

from sklearn.datasets import load_digits

from sklearn.cross_validation import train_test_split, cross_val_score

from sklearn.pipeline import Pipeline

from sklearn.preprocessing import StandardScaler

from sklearn.neural_network.multilayer_perceptron import

MultilayerPerceptronClassifier

First we use the load_digits convenience function to load the MNIST dataset. We

will fork additional processes during cross validation, which requires execution from

a main-protected block:

>>> if __name__ == '__main__':

>>> digits = load_digits()

>>> X = digits.data

>>> y = digits.target

Scaling the features is particularly important for artificial neural networks and will

help some learning algorithms to converge more quickly. Next, we create a Pipeline

class that scales the data before fitting a MultilayerPerceptronClassifier. This

network contains an input layer, a hidden layer with 150 units, a hidden layer with

100 units, and an output layer. We also increased the value of the regularization

hyperparameter alpha argument. Finally, we print the accuracies of the three cross

validation folds. The code is as follows:

>>> pipeline = Pipeline([

>>> ('ss', StandardScaler()),

>>> ('mlp', MultilayerPerceptronClassifier(n_hidden=[150,

100], alpha=0.1))

>>> ])

>>> print cross_val_score(pipeline, X, y, n_jobs=-1)

Accuracies [ 0.95681063 0.96494157 0.93791946]

The mean accuracy is comparable to the accuracy of the support vector classifier.

Adding more hidden units or hidden layers and grid searching to tune the

hyperparameters could further improve the accuracy.

Implement Binary Classification with Logistic Regression in Scikit-learn: A Beginner Tutorial[ 72 ] Binary classification with logistic regression Ordinary linear regression assumes that the response variable is normally distributed. The normal distribution, also known as the Gaussian distribution or bell curve, is a function that describes the probability that an observation will have a value between any two real numbers. Normally distributed data is symmetrical. […]