Chapter 11 Neural Nets (Python)
Chapter 11 Neural Nets (Python)
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from dmba import classificationSummary
Basic Idea
⚫Combine input information in a complex &
flexible neural net “model”
Rectangles are nodes, wij on arrows are weights, and ϴj are node bias values
Moving Through the Network
The Input Layer
example_df = pd.read_csv('TinyData.csv')
predictors = ['Fat', 'Salt']
outcome = 'Acceptance'
X = example_df[predictors]
y = example_df[outcome]
classes = sorted(y.unique())
Code for Tiny Example
Using MPClassifier in scikit-learn
Intercepts
[array([0.13368045, 4.07247552, 7.00768104]),
array([14.30748676])]
Weights
[array([
[ -1.30656481, -4.20427792, -13.29587332],
[ -0.04399727, -4.91606924, -6.03356987]
]),
array([
[ -0.27348313],
[ -9.01211573],
[-17.63504694]
])]
Predictions
# Prediction
print(pd.concat([
example_df,
pd.DataFrame(clf.predict_proba(X), columns=classes)
], axis=1))
To avoid overfitting:
⚫ Track error in validation data or via cross-
validation
⚫ Limit iterations
⚫ Limit complexity of network
User Inputs
Specify Network Architecture
Number of hidden layers
⚫Most popular – one hidden layer (use
argument hidden_layer_sizes)