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.

01 · 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.

02 · 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.

03 · 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.

04 · 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.

05 · Try it

06 · Check yourself

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