Neural network – multilayer perceptron
Using a neural network in scikit-learn is straightforward and proceeds as follows:
- Load the data.
- Scale the data with a standard scaler.
- Do a hyperparameter search. Begin by varying the alpha parameter.
Getting ready
Load the medium-sized California housing dataset that we used in Chapter 9, Tree Algorithms and Ensembles:
%matplotlib inline import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.datasets import fetch_california_housing cali_housing = fetch_california_housing() X = cali_housing.data y = cali_housing.target
Bin the target variable so that the target train set and target test set are a bit more similar. Then use a stratified train/test split:
bins = np.arange(6) binned_y = np.digitize(y, bins) from sklearn.model_selection import train_test_split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2,stratify=binned_y)
How to do it...
- Begin by scaling the input variables. Train the scaler only on...