Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 01

What Is Scikit-learn

Meet scikit-learn, the Python library for machine learning: every model is an estimator you create, fit on examples, and then ask to predict.

Intermediate25 min
What Is Scikit-learn hero Module 27 Lesson 1What Is Scikit-learn hero Module 27 Lesson 1

Machine learning means letting a program find a rule from examples instead of writing the rule yourself. Give it past learners' study hours and their scores, and it works out how hours relate to scores, then guesses the score of someone new. Scikit-learn is the standard Python library for this. Every algorithm in it, from a straight line to a forest of trees, is used in the same three steps: create the model, fit it on examples, and predict new answers.

The scikit-learn workflow from a table of examples to fit and predictThe scikit-learn workflow from a table of examples to fit and predict

Import a model from sklearn

The package is installed as scikit-learn but imported as sklearn. Models live in submodules grouped by family, such as sklearn.linear_model or sklearn.tree. Creating one gives you an empty model that has not learned anything yet.

python
from sklearn.linear_model import LinearRegression

model = LinearRegression()
print(model)
print(hasattr(model, "fit"), hasattr(model, "predict"))

Output:

text
LinearRegression()
True True

Scikit-learn is not preloaded in this page's playground, so the first run that imports it downloads the library. That takes a few seconds; later runs are quick.

Learning from five examples

Here are five learners: hours studied and the score each one got. fit finds the line that best matches them. predict then uses that line for hours nobody in the examples studied. Inputs are a list of rows, which is why each hour value sits inside its own brackets.

python
from sklearn.linear_model import LinearRegression

hours = [[1], [2], [3], [4], [5]]
scores = [52, 57, 61, 66, 70]

model = LinearRegression()
model.fit(hours, scores)

print(model.predict([[6]]).round(1))
print(model.predict([[8], [10]]).round(1))

Output:

text
[74.7]
[83.7 92.7]

Same steps, different model

Swap in a decision tree and nothing else changes: create, fit, predict. That shared shape is scikit-learn's biggest idea. The answers differ because each algorithm learns a different kind of rule.

python
from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeRegressor

hours = [[1], [2], [3], [4], [5]]
scores = [52, 57, 61, 66, 70]

for model in [LinearRegression(), DecisionTreeRegressor(random_state=0)]:
    model.fit(hours, scores)
    print(type(model).__name__, model.predict([[8]]))

Output:

text
LinearRegression [83.7]
DecisionTreeRegressor [70.]

The line keeps rising past the examples. The tree answers 70 because it can only repeat values it has seen, so it never guesses beyond the highest score in its training rows.

Supervised and unsupervised

Both models above are supervised: every example came with the right answer. Unsupervised models get no answers and look for structure on their own. KMeans below splits six session lengths into two groups without being told what the groups mean.

python
from sklearn.cluster import KMeans

minutes = [[5], [7], [6], [48], [52], [50]]
groups = KMeans(n_clusters=2, n_init=10, random_state=0).fit_predict(minutes)
print(groups)

Output:

text
[1 1 1 0 0 0]

The table used in this module

Most lessons learn from the same invented table of 200 learners. hours is study time per week, practice is problems solved, video_minutes is time spent watching videos, track is the course, score is the final mark, and passed is 1 for a score of 60 or more. The fixed seed means you get exactly these rows.

python
import numpy as np
import pandas as pd

rng = np.random.default_rng(27)
learners = pd.DataFrame({
    "hours": rng.uniform(1, 12, 200).round(1),
    "practice": rng.integers(0, 41, 200),
    "video_minutes": rng.integers(0, 3001, 200),
    "track": rng.choice(["Python", "Data", "Web"], 200),
})
bonus = learners["track"].map({"Python": 0, "Data": 6, "Web": -5})
noise = rng.normal(0, 6, 200)
learners["score"] = (
    22 + learners["hours"] * 3.4 + learners["practice"] * 0.55 + bonus + noise
).clip(0, 100).round()
learners["passed"] = (learners["score"] >= 60).astype(int)

print(learners.head())
print(learners.shape)
print("pass rate:", learners["passed"].mean())

Output:

text
   hours  practice  video_minutes   track  score  passed
0    8.7         4           2751     Web   56.0       0
1    4.5         1            662    Data   30.0       0
2    2.3        24           2371  Python   52.0       0
3    4.6        20           2225    Data   61.0       1
4   11.2         9           2701     Web   67.0       1
(200, 6)
pass rate: 0.48

Key takeaways

  • Scikit-learn is imported as sklearn; models live in family submodules

  • Every model is used the same way: create it, fit(X, y), then predict(new)

  • Supervised models learn from examples with answers; unsupervised ones find groups

  • A model only knows what its training examples showed it

  • Next: Features and Labels

  • Previous: Customising Seaborn Plots

Loading playground…

Check your understanding

What Is Scikit-learn Quiz

How is scikit-learn imported?

1 / 10