05

Data Scientist · Step 5 of 8 · 12 min

Your first models

Train and compare a baseline, a regression and a decision tree with scikit-learn.

Split before you fit

A model learns from examples, so you need a fair way to check it on examples it has not seen. Split the data first: typically most rows for training and the rest held back for testing. In scikit-learn, train_test_split does this in one line. Never look at the test set while choosing features or settings, or it stops being a fair exam.

If the data has a time order, split by time: train on the past, test on the most recent period. That is how the model will be used.

Always start with a baseline

A baseline is the simplest possible prediction: always guess the average price, or always guess the most common class. It sounds silly, but it tells you what “good” means. If 95% of customers never cancel, a model that is 95% accurate may have learned nothing at all.

scikit-learn has DummyRegressor and DummyClassifier for exactly this. Report your model next to its baseline, every time.

Regression and decision trees

Linear regression predicts a number as a weighted sum of inputs, such as price from size and location. Logistic regression, despite the name, predicts a category, such as cancel or not, as a probability. Both are fast, well understood and easy to explain, which is why they remain everyday tools.

A decision tree asks a series of yes-or-no questions about the inputs, like a flowchart. It handles mixed data and odd shapes well. Many trees combined, as in random forests or gradient boosting, are among the strongest tools for table data.

Overfitting

Overfitting is when a model memorises the training data, noise included, instead of learning the pattern. The sign is a big gap: excellent on training data, much worse on test data. A very deep decision tree is the textbook example.

The cures are simpler models, more data, fewer noisy features, and settings that limit complexity, such as max_depth for a tree. Every scikit-learn model follows the same pattern: create it, call fit on training data, call predict on new data. Learn that once and you can try many models.

Try it

Baseline versus two real models

  1. 1.In Colab, load the California housing file and choose median_house_value as the target.
  2. 2.Split with train_test_split(X, y, test_size=0.2, random_state=42).
  3. 3.Fit DummyRegressor, LinearRegression and DecisionTreeRegressor, and compare their mean absolute error on the test set.
  4. 4.Change the tree's max_depth from None to 5 and see how training and test error move.
I am a beginner using scikit-learn. Write a short, commented Colab script that loads [DATASET], predicts [TARGET], and compares a dummy baseline, linear regression and a decision tree on a held-out test set. Explain each step in one line and do not use any library beyond pandas and scikit-learn.
Your decision tree scores 99% on training data and 71% on test data. What is the most likely problem?

Check yourself

Your decision tree scores 99% on training data and 71% on test data. What is the most likely problem?

Read alongside