scikit-learn: A Toolbox of Ready-Made Models
You have already used it three times
In lesson 1.3 you trained a LinearRegression model on flat rents and a KMeans model on supermarket customers. In lesson 1.6 you trained another LinearRegression on the flats table. All three came from scikit-learn.
Look back at them and you will notice something. Completely different models, one predicting rent and one grouping customers, and yet the code looked almost the same. Create the model, call fit, use the result.
That is not an accident. It is the single best idea in scikit-learn, and once you see it, you can use dozens of models you have never met before.
What is inside the toolbox
scikit-learn (you will also see it written sklearn, which is how you import it) is the standard library for classic machine learning. It covers almost everything in this course:
Key Points
- Models for regression: linear regression, decision trees, random forests and more. (Chapter 4)
- Models for classification: logistic regression, KNN, SVM and more. (Chapter 6)
- Models for clustering and unsupervised learning: KMeans, DBSCAN, PCA. (Chapter 7)
- Tools for preparing data: filling in missing values, turning text categories into numbers, scaling. (Chapter 3)
- Tools for testing models: splitting data, measuring error, cross-validation. (Chapters 3 to 6)
What it does not do is deep learning. For neural networks, people use PyTorch or TensorFlow. For everything else in classic ML, scikit-learn is the first place to look.
The pattern every model follows
Every model in scikit-learn is used in the same five steps:
- 1Import
from sklearn... import Model - 2Create
model = Model() - 3Fit
model.fit(X_train, y_train) - 4Predict
model.predict(X_test) - 5Evaluate
compare with y_test
This works like a common plug socket. Any appliance with the right plug fits any socket in the house, whoever made it. In scikit-learn, every model has the same "plug": fit to learn, predict to use. So swapping one model for another is usually a one-line change.
All five steps, for real
Let us predict rent for the Pune flats, doing every step properly this time. First, load the data and pick the features and target, like in lesson 2.4:
1import pandas as pd
2
3df = pd.read_csv("https://cod-e.vercel.app/data/pune-flats.csv")
4X = df[["bhk", "size_sqft", "floor"]]
5y = df["rent"]Next comes a step we have skipped until now. Remember the lifecycle in lesson 1.5? Before training, we hide some data away as a test set, so we can check the model on flats it has never seen. scikit-learn has a tool for exactly that:
1from sklearn.model_selection import train_test_split
2
3X_train, X_test, y_train, y_test = train_test_split(
4 X, y, test_size=0.25, random_state=42
5)
6print(len(X_train), "flats to learn from,", len(X_test), "hidden for testing")15 flats to learn from, 6 hidden for testing
train_test_split shuffles the rows and cuts them into two parts. test_size=0.25 means a quarter goes into the test set. It gives back four things at once: training features, test features, training answers and test answers, always in that order.
Now the model itself: import, create, fit, predict.
1from sklearn.linear_model import LinearRegression
2
3model = LinearRegression() # create
4model.fit(X_train, y_train) # fit: learn from the training flats only
5predictions = model.predict(X_test) # predict: guess rents for the hidden flats
6
7print("Predicted:", predictions.round())
8print("Actual: ", y_test.values)Predicted: [45559. 41070. 25494. 16755. 27001. 42145.] Actual: [26500 27000 21500 14500 20000 35500]
Finally, evaluate. Instead of eyeballing six pairs of numbers, we measure the average size of the mistakes:
1from sklearn.metrics import mean_absolute_error
2
3print("Average error: ₹", round(mean_absolute_error(y_test, predictions)))Average error: ₹ 8837
Mean absolute error means: for each flat, how far off was the prediction, ignoring whether it was too high or too low? Then take the average. Our model is off by about ₹8,837 per flat.
Be honest: that model is not good
Look at the first flat. It actually rents for ₹26,500 and the model guessed over ₹45,000. Being off by nearly ₹9,000 on average is not something you would trust with real money. Why is it so bad?
Key Points
- It cannot see the area or the furnishing. You saw in lessons 2.4 and 2.5 that both matter a lot, but they are text columns, and models only take numbers. So we left them out.
- It learned from only 15 flats. That is tiny. Real datasets have thousands of rows.
Both of these are fixable, and fixing them is what Chapter 3 is about. For now, the lesson is this: a model is only as good as the information you give it. A clever algorithm cannot make up for missing features.
Swapping models in one line
Here is the payoff of the shared pattern. Let us try three completely different models on the same data. Only the model changes. Everything else stays identical:
1from sklearn.tree import DecisionTreeRegressor
2from sklearn.ensemble import RandomForestRegressor
3
4models = [
5 LinearRegression(),
6 DecisionTreeRegressor(random_state=0),
7 RandomForestRegressor(random_state=0),
8]
9
10for model in models:
11 model.fit(X_train, y_train)
12 error = mean_absolute_error(y_test, model.predict(X_test))
13 print(f"{type(model).__name__:<22} average error: ₹{error:,.0f}")LinearRegression average error: ₹8,837 DecisionTreeRegressor average error: ₹9,167 RandomForestRegressor average error: ₹6,471
Three different algorithms, compared in a few lines. The random forest did best here. You do not need to know how decision trees or random forests work yet. Chapter 6 explains both. The point is that you could try them without learning anything new about the code.
Tip:With only 6 test flats, do not read too much into which model won. Move a couple of flats between the training and test sets and the ranking could easily change. Chapter 5 shows a more reliable way to compare models, called cross-validation.
Three small habits of scikit-learn
Settings go in the brackets when you create a model. DecisionTreeRegressor(max_depth=3) creates a tree that is only allowed to ask 3 questions deep. These settings are called hyperparameters. You choose them. The model does not learn them. Chapter 5 covers how to choose them well.
random_state makes randomness repeatable. Some steps involve chance, like shuffling rows in train_test_split or building a random forest. Setting random_state to any fixed number, like 0 or 42, makes the "random" choices the same every time you run the code. That is why your output will match this lesson exactly.
Anything learned ends with an underscore. After fitting, what the model learned is stored in attributes like coef_ and intercept_ (lesson 1.6) or labels_ (KMeans in lesson 1.3). The underscore is scikit-learn's way of saying "this only exists after fit has been run".
Where people get confused
Key Points
- "I can fit the model on all my data and test it on the same data." Then you are testing a student on the exact paper they studied. Always fit on the training set and evaluate on the test set.
- "X can be a single column like df["size_sqft"]." scikit-learn needs X to be a table, even with one feature. Use double brackets, df[["size_sqft"]]. Single brackets give an error saying it expected a 2-dimensional input.
- "New data can have its columns in any order." predict needs the same columns, in the same order, as the data the model was trained on.
- "The best algorithm is always random forest." It won here, on 6 test flats. Which model is best depends on the data. That is why you compare them.
Quick check
What are the five steps for using any scikit-learn model?
Import it, create it, fit it on the training data, predict on new data, and evaluate the predictions against the real answers.
You want to try a different model on the same problem. What do you need to change?
Usually just the line that creates the model, plus its import. fit and predict work the same way for every model.
What does random_state=42 do?
It fixes the random choices, like how rows are shuffled, so you get the same result every time you run the code. The number 42 itself is not special. Any fixed number works.
Recap
Key Points
- scikit-learn is the standard library for classic ML: models, data preparation and evaluation tools.
- Every model follows the same steps: import, create, fit, predict, evaluate.
- train_test_split hides part of the data as a test set. Fit on training data, evaluate on test data.
- Because the pattern is shared, trying a different model is usually a one-line change.
- A model is only as good as its features. Ours was weak because it could not see area or furnishing.
Next up: the last lesson of this chapter, just enough statistics. What does "average" really tell you, why can it be misleading, and what else do you need to describe your data?
Theia
Can you explain this concept?
Practise scikit-learn: A Toolbox of Ready-Made Models with realistic interview questions. Choose spoken or typed answers, get feedback on your reasoning, then return to the material that needs work.
Sign in to practise this conceptThe session stays focused on this lesson. Your selected interview level sets the bar.