Using scikit-learn Without Panic

Create, fit, predict, check

Advanced 14 min

In this lesson

Everything so far you built by hand, which was the point — none of it was magic. Now meet the library that does it properly.

scikit-learn is the standard Python machine-learning toolkit, and its best feature is consistency. Every model, from a straight line to a forest of decision trees, is driven by the same four steps. Learn them once and you can use hundreds of algorithms.

Explain it like I’m 5

scikit-learn gives you a predictable set of buttons: create the model, fit it to your examples, predict new answers, check how it did. Every model has the same buttons.

X and y, and the four steps

Two conventions first, because every example you will ever read uses them. X (capital, because it is a table) is the feature matrix: one row per example, one column per feature. y (lowercase, because it is a single column) is the target.

Then the workflow: create the model, fit() it to your data, predict() for new rows, score() to evaluate. fit() is where the learning happens; everything else is bookkeeping.

Example
from sklearn.linear_model import LinearRegression
import numpy as np

# X is the feature matrix: one row per example, one column per feature.
X = np.array([[1], [2], [3], [4], [5], [6]])
# y is the target: one value per row of X.
y = np.array([52, 55, 61, 64, 70, 72])

model = LinearRegression()     # 1. create
model.fit(X, y)                # 2. fit - this is the learning
prediction = model.predict([[7]])   # 3. predict
score = model.score(X, y)      # 4. check

print("learned slope:", round(float(model.coef_[0]), 2))
print("predict 7 hours:", round(float(prediction[0]), 1))
print("r-squared:", round(float(score), 3))
Output
learned slope: 4.23
predict 7 hours: 77.1
r-squared: 0.986
The same revision data as the regression lesson, and the same answer.

The shape error everyone hits

scikit-learn insists that X is two-dimensional (a table) even when there is only one feature and one row. Passing a bare list of numbers produces an error that is genuinely helpful once you know how to read it.

Example
from sklearn.linear_model import LinearRegression
import numpy as np

X = np.array([[1], [2], [3]])
y = np.array([10, 20, 30])
model = LinearRegression().fit(X, y)

try:
    model.predict([4])          # one bare number, not a row
except ValueError as err:
    print("ValueError:", str(err).split(".")[0])

print("correct way:", round(float(model.predict([[4]])[0]), 1))
Output
ValueError: Expected 2D array, got 1D array instead:
array=[4]
correct way: 40.0
“Expected 2D array” means you need another pair of brackets.

Run the four-step workflow yourself on house data. Create a LinearRegression, fit it to X and y, predict the price of a 90 m² house, and report the model's score. Watch the brackets on the prediction.

from sklearn.linear_model import LinearRegression
import numpy as np

X = np.array([[50], [75], [110], [135]])
y = np.array([150, 205, 295, 355])

# TODO: 1. create the model
model = None

# TODO: 2. fit it to X and y

# TODO: 3. predict for a 90 m2 house, and 4. score the model
print("predict 90 m2:", 0)
print("score:", 0)

Swapping the model is one line

Here is the payoff for the consistency. Every scikit-learn model exposes the same methods, so changing algorithm means changing the line that creates it and nothing else.

LinearRegression() becomes DecisionTreeRegressor() or RandomForestRegressor(), and the fit/predict/score calls are untouched. The same applies across classifiers.

That is why the workflow is worth memorizing rather than any individual algorithm. It also makes comparing models genuinely easy: a loop over a list of models is a completely reasonable thing to write.

Common mistake: Mixing up X and y

Why it happens:

They are both just arrays passed to the same call.

How to fix it:

X is the features you know, y is the answer you want. fit(X, y) is always in that order.

Common mistake: Passing one row with the wrong shape

Why it happens:

model.predict([4]) looks like the obvious way to ask about a single value.

How to fix it:

X must be two-dimensional. Use [[4]], or .reshape(-1, 1) on a flat array.

Common mistake: Calling predict() before fit()

Why it happens:

Creating the model looks like it has already done something.

How to fix it:

Creating only sets up the algorithm. fit() is where learning happens, and scikit-learn raises NotFittedError if you skip it.

Common mistake: Copying a workflow without knowing which step does what

Why it happens:

The four lines are short and work when pasted.

How to fix it:

Learn what each step does once. Everything else in scikit-learn is a variation on these four calls.

What does fit() do?

What shape must X be?

Why swap LinearRegression for another model so easily?

Mini exercise (hard)

Compare two models on the same data with a loop, the payoff of the shared interface. Fit a LinearRegression and a DecisionTreeRegressor (with random_state=0) to the same house data, print each one's score, and predict a 90 m² house with both. The predictions will differ noticeably.

Take the wheel. Complete the code, hit Run, and check your output right here.

import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeRegressor

X = np.array([[50], [75], [110], [135]])
y = np.array([150, 205, 295, 355])

models = [
    ("linear", LinearRegression()),
    ("tree  ", DecisionTreeRegressor(random_state=0)),
]

# TODO: fit each model, then print its score and its prediction for 90 m2.
#       The code inside the loop is identical for both - that is the point.
for name, model in models:
    pass

What to learn next

You learned the four buttons every scikit-learn model shares (create, fit(), predict(), score()), plus the X/y shape rules and the “Expected 2D array” error everyone meets once. And you confirmed it: the slope scikit-learn learned was identical to the one you fitted by hand.

Time to run a whole project properly. The Unit 13 Project takes a dataset from features and labels to an honest, hedged conclusion.