Reader small image

You're reading from  scikit-learn Cookbook - Second Edition

Product typeBook
Published inNov 2017
Reading LevelIntermediate
PublisherPackt
ISBN-139781787286382
Edition2nd Edition
Languages
Right arrow
Author (1)
Trent Hauck
Trent Hauck
author image
Trent Hauck

Trent Hauck is a data scientist living and working in the Seattle area. He grew up in Wichita, Kansas and received his undergraduate and graduate degrees from the University of Kansas. He is the author of the book Instant Data Intensive Apps with pandas How-to, Packt Publishing—a book that can get you up to speed quickly with pandas and other associated technologies.
Read more about Trent Hauck

Right arrow

Neural Networks

In this chapter we will cover the following recipes:

  • Perceptron classifier
  • Neural network – multilayer perceptron
  • Stacking with a neural network

Introduction

Neural networks and deep learning have been incredibly popular recently as they have solved tough problems and perhaps have become a significant part of the public face of artificial intelligence. Let's explore the feed-forward neural networks available in scikit-learn.

Perceptron classifier

With scikit-learn, you can explore the perceptron classifier and relate it to other classification procedures within scikit-learn. Additionally, perceptrons are the building blocks of neural networks, which are a very prominent part of machine learning, particularly computer vision.

Getting ready

Let's get started. The process is as follows:

  1. Load the UCI diabetes classification dataset.
  2. Split the dataset into training and test sets.
  3. Import a perceptron.
  4. Instantiate the perceptron.
  5. Then train the perceptron.
  6. Try the perceptron on the testing set or preferably compute cross_val_score.

Load the UCI diabetes dataset:

import numpy as np
import pandas as pd

data_web_address = "https://archive.ics...

Neural network – multilayer perceptron

Using a neural network in scikit-learn is straightforward and proceeds as follows:

  1. Load the data.
  2. Scale the data with a standard scaler.
  3. 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...

Stacking with a neural network

The two most common meta-learning methods are bagging and boosting. Stacking is less widely used; yet it is powerful because one can combine models of different types. All three methods create a stronger estimator from a set of not-so-strong estimators. We tried the stacking procedure in Chapter 9, Tree Algorithms and Ensembles. Here, we try it with a neural network mixed with other models.

The process for stacking is as follows:

  1. Split the dataset into training and testing sets.
  2. Split the training set into two sets.
  1. Train base learners on the first part of the training set.
  2. Make predictions using the base learners on the second part of the training set. Store these prediction vectors.
  3. Take the stored prediction vectors as inputs and the target variable as output. Train a higher level learner (note that we are still on the second part of the training...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
scikit-learn Cookbook - Second Edition
Published in: Nov 2017Publisher: PacktISBN-13: 9781787286382
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime

Author (1)

author image
Trent Hauck

Trent Hauck is a data scientist living and working in the Seattle area. He grew up in Wichita, Kansas and received his undergraduate and graduate degrees from the University of Kansas. He is the author of the book Instant Data Intensive Apps with pandas How-to, Packt Publishing—a book that can get you up to speed quickly with pandas and other associated technologies.
Read more about Trent Hauck