Start learning free

LESSON 35 OF 165 · MACHINE LEARNING: YOUR FIRST MODEL

Beat the Baseline First

Practice

An accuracy of 0.627 sounds fine: the tree is right more often than wrong. But in this shop most customers come back anyway, so a "model" that ignores every feature and always says 1 is right a lot too. That lazy guess is the baseline. A model is only useful if it beats it.

DummyClassifier(strategy="most_frequent") from sklearn.dummy builds that lazy guesser. It works like any model: fit counts the answers in the training labels, predict gives back the most frequent one for every row, and score gives back its accuracy.

guesser = DummyClassifier(strategy="most_frequent")
guesser.fit([[0], [0], [0], [0], [0]], ["yes", "yes", "no", "yes", "no"])
print(guesser.predict([[0], [0]]))  # ['yes' 'yes']
print(guesser.score([[0], [0], [0], [0]], ["yes", "no", "yes", "yes"]))  # 0.75

Here the depth-3 tree loses to the baseline. A tree with max_depth=5 may ask up to 5 questions in a row instead of 3, so it can learn finer rules. Its score only means something next to the baseline's.

The starting code fits the depth-3 tree and prints its test score. When you are done, your code prints:

Tree with depth 3: 0.627
Baseline: 0.653
Tree with depth 5: 0.72

After your lines, the check prints what the baseline says to the test customers and the share of 1s in y_train.

Your task: make a DummyClassifier with strategy="most_frequent" in baseline, fit it on the training rows and print its test score. Then make, fit and score a second tree, deeper, with max_depth=5 and random_state=0.


Assignment
Follow the Coding Tutorial to score a most-frequent baseline and a deeper tree next to the depth-3 tree.


Hint
The baseline is used exactly like the tree: make it, fit(X_train, y_train), then score(X_test, y_test). Round each score with round(x, 3).


STEP 1 OF 6
    Ctrl + Enter to check