Start learning free

LESSON 40 OF 165 · MACHINE LEARNING: YOUR FIRST MODEL

Project: Your First Model, End to End in Python

Practice

Time to put the whole module together in one short script. You load the customers, pick the features (what you know) and the label (what you want to predict), hold back some rows as a test set, train a model, and compare it with a baseline that always gives the most common answer. A model is only worth having if it beats that baseline.

Every scikit-learn model works the same way: you make it, fit it on rows whose answers you know, then predict new rows. Here is the baseline on a tiny made-up example, three rows with one feature each:

clf = DummyClassifier(strategy="most_frequent")
clf.fit([[1], [2], [3]], [0, 1, 1])
print(clf.predict([[7], [8]]))   # [1 1]: 1 was the most common answer

The given report(name, m) asks the model m for its answers on the test rows with m.predict(X_test) and prints three numbers: accuracy_score (the share of all answers that are right), precision_score (of the customers it said yes to, the share who came back) and recall_score (of the customers who came back, the share it found).

Your finished script prints this (the last two lines come from the lesson's check):

Baseline
  accuracy: 0.653
  precision: 0.653
  recall: 1.0
Model
  accuracy: 0.72
  precision: 0.833
  recall: 0.714
Trained on 225 rows, tested on 75
The model beats the baseline: True

Your task: below df, make X from the five features and y from bought_again, split them with train_test_split (test_size=0.25, random_state=0), make and fit a DummyClassifier baseline and a DecisionTreeClassifier with max_depth=5, then call report for both.


Assignment
Follow the Coding Tutorial to build the whole pipeline, from the CSV to a report that compares your model with the baseline.


Hint
Fit both models on X_train, y_train only, never on the test rows. The baseline's recall of 1.0 is no win: it says yes to every customer, so it finds everyone who came back for free.


STEP 1 OF 8
    Ctrl + Enter to check

    Ml First Model Project 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