Learn these and the whole library becomes predictable.
The three interfaces
Learn these and the whole library becomes predictable.
Almost every object in scikit-learn is one of three shapes. This uniformity is the library's central design decision and the reason it has lasted.
**Estimator** — learns from data, then predicts.
model.fit(X, y) learn
model.predict(X) apply
model.predict_proba(X) apply, with probabilities (classifiers)
model.score(X, y) a quick default metric
**Transformer** — changes data rather than predicting it.
scaler.fit(X) learn the parameters, e.g. mean and std
scaler.transform(X) apply them
scaler.fit_transform(X) both at once - TRAINING DATA ONLY
**Meta-estimator** — wraps other estimators. `Pipeline`, `GridSearchCV`, `ColumnTransformer`, `VotingClassifier`. These expose `fit` and `predict` themselves, so they can go anywhere a plain model can, including inside one another.
Because the interface is uniform, swapping algorithms is a one-line change, and tooling written against it works with anything that follows it — which is why XGBoost, LightGBM and CatBoost all adopted the same method names despite being separate projects by different organisations.
The three interfaces
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from xgboost import XGBClassifier # not part of sklearn
# Identical calls. GridSearchCV and Pipeline neither know nor care
# which library a model came from.
for m in [LogisticRegression(max_iter=1000),
RandomForestClassifier(n_jobs=-1),
XGBClassifier()]:
m.fit(X_train, y_train)
print(type(m).__name__, m.score(X_test, y_test))