CodeBlanks Logo Icon CodeBlanks

What is scikit-learn?

scikit-learn is the core library for classic ML algorithms in Python.

The design of scikit-learn

scikit-learn is the standard library of classic machine learning. Its strength comes less from the number of algorithms than from its consistent interface: every model shares the same few methods, so swapping one model for another is one line.

from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
for m in [SVC(), RandomForestClassifier(random_state=0)]:
    m.fit(X_train, y_train)
    print(type(m).__name__, m.score(X_test, y_test))

Modules are split by job: linear_model, tree, ensemble, svm, preprocessing, model_selection, metrics.

Built-in datasets

sklearn.datasets offers small real datasets (load_*) and synthetic data generators (make_*). Generators are ideal for trying out a particular situation (imbalanced classes, noise, a non-linear boundary):

from sklearn.datasets import make_classification, make_moons
X, y = make_classification(n_samples=200, n_features=5, random_state=0)
Xm, ym = make_moons(n_samples=200, noise=0.2, random_state=0)
print(X.shape, Xm.shape)   # (200, 5) (200, 2)

Without random_state, different data is generated on every run.

Practice Three models, same code

Train LogisticRegression(max_iter=1000), SVC() and RandomForestClassifier(random_state=0) in a single loop. On each line print the class name (type(m).__name__) and the test accuracy with round(..., 3).

Starter code

model.py
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0, stratify=y)
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier

# buraya yaz

Sample solution

model.py
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0, stratify=y)
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier

for m in [LogisticRegression(max_iter=1000), SVC(), RandomForestClassifier(random_state=0)]:
    m.fit(X_train, y_train)
    print(type(m).__name__, round(m.score(X_test, y_test), 3))

Expected output

LogisticRegression 1.0
SVC 1.0
RandomForestClassifier 0.978

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Why is replacing one model with another easy in scikit-learn?

  • All models are the same algorithm
  • They take no parameters
  • It isn't easy
  • They all share the same fit/predict/score interface Correct answer

Explanation: Thanks to the shared interface, many models can be tried with the same code in a loop.

2 Generate synthetic classification data.

Question
X, y = ____(n_samples=100, n_features=4, random_state=0)
Answer
X, y = make_classification(n_samples=100, n_features=4, random_state=0)

Explanation: make_classification produces an adjustable classification problem.

3 Match the modules.

Term Meaning
linear_model Linear models
ensemble Ensemble models
model_selection Splitting and search
preprocessing Scaling and encoding

All lessons in Intermediate