Exercises
Neural Networks
3 tasks. Write the code, press Check, and the page runs it against a real Python interpreter.
Exercise 1Passed
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))Put a StandardScaler in front of it.
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
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 = make_pipeline(
StandardScaler(),
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 2Passed
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
coefs_ is a list of arrays, one per connection between layers.
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([w.shape for w in model.coefs_])Exercise 3Passed
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))MLPClassifier with hidden_layer_sizes=(8,), max_iter=5000, random_state=1.
import numpy as np
from sklearn.neural_network import MLPClassifier
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([0, 1, 1, 0])
print(MLPClassifier(hidden_layer_sizes=(8,), max_iter=5000, random_state=1).fit(X, y).score(X, y))