Chapters
Python114 chapters

Exercises

Overfitting and Underfitting

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

Exercise 1

Score the model with five-fold cross-validation and print the mean, rounded to two places.

Python needs scikit-learn, downloaded on first run
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier

X, y = load_iris(return_X_y=True)
# cross-validate and print the mean
Exercise 2

Print the mean and the standard deviation, so the reader can see how much the score varies.

Python needs scikit-learn, downloaded on first run
from sklearn.datasets import load_iris
from sklearn.model_selection import cross_val_score
from sklearn.tree import DecisionTreeClassifier

X, y = load_iris(return_X_y=True)
scores = cross_val_score(DecisionTreeClassifier(random_state=0), X, y, cv=5)
# print mean then std, each rounded to three places
Exercise 3

Limit the tree's depth to 3 and show the training score is no longer a perfect 1.0.

Python needs scikit-learn, downloaded on first run
from sklearn.datasets import make_classification
from sklearn.tree import DecisionTreeClassifier

X, y = make_classification(n_samples=400, n_features=20, n_informative=5,
                           flip_y=0.15, random_state=0)
model = DecisionTreeClassifier(random_state=0).fit(X, y)
print(round(model.score(X, y), 2))