SproutStackSproutStack home

Your ML Setup: scikit-learn in 10 Minutes

6 min readBeginner-friendlyFree to read

Read a little, play a little. No scary maths, and no rush.

Share:

One library, one pattern, everything after

scikit-learn is the standard toolkit for classical ML: datasets, dozens of models, and evaluation metrics behind a single consistent API. Learn its rhythm once — fit to train, predict to guess, score to grade — and every model in this track (and most you'll meet in jobs) works the same way.

Install and run your first model (copy this)

pip install scikit-learn pandas
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier

X, y = load_iris(return_X_y=True)   # 150 flowers, 4 measurements each
Xtr, Xte, ytr, yte = train_test_split(X, y, test_size=0.2, random_state=7)
model = RandomForestClassifier(n_estimators=50, random_state=7)
model.fit(Xtr, ytr)
print("accuracy:", model.score(Xte, yte))  # expect ~0.95+

That loop — load → split → fit → score — is 90% of this whole track. Run it now, in our playground or your terminal, before reading further. A running model in minute 10 beats a perfect mental model in week 3.

What each line does

  • load_iris(return_X_y=True) returns X, a table of 150 rows with 4 measurement columns, and y, the 150 species labels.
  • train_test_split(..., test_size=0.2, random_state=7) shuffles the rows and keeps 20% aside for testing. The seed makes the shuffle identical every run.
  • RandomForestClassifier(n_estimators=50) sets up a model made of 50 decision trees that vote.
  • fit(Xtr, ytr) is where learning happens, using only the training rows.
  • score(Xte, yte) predicts the test rows and compares against the true labels. For classifiers the default score is accuracy; for regressors it is R², a measure of how much of the variation the model explains.

Why toy datasets first

Iris (150 flowers) and California housing are tiny, clean, and documented — mistakes surface as wrong numbers, not as mysterious data bugs. Graduate to messy real data only after the loop feels boring. And random_state=7 everywhere: fixed seeds make every run reproducible, so "it worked yesterday" stays true.

Predicting one new example

flower = [[5.1, 3.5, 1.4, 0.2]]       # one row, four measurements
print(model.predict(flower))          # the predicted class
print(model.predict_proba(flower))    # its probability for each class

The double brackets matter: scikit-learn always expects a table, even for a single row. Passing a flat list of four numbers is the most common beginner error with predict. predict_proba is often more useful than predict, because a 51% call and a 99% call deserve different treatment.

Swapping the model takes one line

Replace RandomForestClassifier(...) with LogisticRegression(max_iter=1000) or KNeighborsClassifier(), and leave every other line untouched. Comparing several models on the same split is a few minutes of work, which is the real value of the consistent API.

Pipelines keep preprocessing honest

Some models need their inputs scaled to similar ranges. make_pipeline(StandardScaler(), LogisticRegression()) bundles the scaling and the model into one object. When you call fit, the scaler learns its averages from the training rows only, and the same transformation is applied to new data at prediction time. That prevents the subtle leakage of computing statistics on the full dataset, which the data chapters warn about.

Reading the score honestly

An accuracy of 0.97 on iris's 30 test flowers means one wrong answer. With so few test rows, one lucky or unlucky split moves the score noticeably, so do not celebrate a 0.01 difference between models. Always compare against a baseline too: guessing the most common class scores about 0.33 on iris's three balanced classes. A model's score only means something next to what a trivial guess would get.

Remember this

  • fit trains, predict guesses, score grades. Same verbs, every model.
  • Toy data first, seeds fixed, loop running by minute 10.

Check your understanding

5 questions · first-try answers earn the most XP

1. Why does scikit-learn's consistency matter more than any individual model?
2. Why start with a small built-in dataset like iris or California housing?
3. What does calling `model.score(Xte, yte)` actually return in scikit-learn?
4. What does the loop load → split → fit → score represent?
5. What happens to your results if you omit `random_state`?

My notes

Saved in this browser. Highlight a line above and save it, or write it in your own words.

Nothing saved yet. Your highlights will live here.

References

Finished reading?

Ticking it here also ticks the chapter in the sidebar, the section count and your streak — it is all one number.

End of Start Here

Take the section checkpoint

A short mixed quiz on all 2 chapters, to check you are ready to move on.

Related chapters

Spotted a mistake or want a topic covered? Report an issue

📬 An occasional email when new lessons land

New lessons and showdowns, no more than a few times a month. No spam, unsubscribe anytime. We store your email only to send the newsletter.