Chapters
Python114 chapters

Exercises

Neural Networks

3 tasks. Write the code, press Check, and the page runs it against a real Python interpreter.

Exercise 1

The network scores badly because the features are not scaled. Fix it with a pipeline.

Python needs scikit-learn, downloaded on first run
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier

X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=0, stratify=y
)
model = MLPClassifier(hidden_layer_sizes=(16,), max_iter=2000, random_state=0)
model.fit(X_train, y_train)
print(round(model.score(X_test, y_test), 2))
Exercise 2

Print the shape of each weight matrix, to see the layers.

Python needs scikit-learn, downloaded on first run
from sklearn.datasets import load_breast_cancer
from sklearn.neural_network import MLPClassifier
from sklearn.preprocessing import StandardScaler

X, y = load_breast_cancer(return_X_y=True)
X = StandardScaler().fit_transform(X)
model = MLPClassifier(hidden_layer_sizes=(16, 8), max_iter=2000, random_state=0).fit(X, y)
# print the shapes
Exercise 3

Logistic regression cannot solve XOR. Show that a network with a hidden layer can.

Python needs scikit-learn, downloaded on first run
import numpy as np
from sklearn.linear_model import LogisticRegression

X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([0, 1, 1, 0])

print(LogisticRegression().fit(X, y).score(X, y))