Start learning free

LESSON 55 OF 165 · MACHINE LEARNING: DOING IT RIGHT

Overfitting in Python

Practice

A model can be perfect on the rows it learned from and still be poor at its real job: guessing for customers it has never seen. That is called overfitting. The model memorized the training rows, odd cases and all, instead of learning what most customers have in common. The only way to catch it is to score the model on rows it never saw.

A DecisionTreeClassifier (from sklearn.tree) predicts by asking yes/no questions about the columns, like "is orders above 2?". With no limit, it keeps asking until it answers every training row right. max_depth caps how many questions in a row it may ask. After fit, model.score(X, y) gives back the share of the rows in X that the model answers right, a number from 0 to 1.

tree = DecisionTreeClassifier(max_depth=2, random_state=0)
tree.fit(X_train, y_train)
print(round(tree.score(X_test, y_test), 3))   # share of new rows it gets right

Your script must print two trees' scores, first on the training rows, then on the test rows:

Full depth | train: 1.0 | test: 0.667
Depth 5 | train: 0.876 | test: 0.72

Your task: the starting code reads customers.csv, puts five columns in X and bought_again in y, and keeps a quarter of the rows apart for testing. Train a tree with no depth limit and a tree with max_depth=5, and print each one's train and test score side by side.


Assignment
Follow the Coding Tutorial to train both trees and print their train and test scores.


Hint
Score on X_train, y_train for the rows the tree learned and on X_test, y_test for the rows it never saw. Round each score with round(..., 3), and give both trees random_state=0 so the output is the same on every run.


STEP 1 OF 6
    Ctrl + Enter to check

    Mlr Overfitting in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free