scikit-learn is the standard Python library for classical machine learning — every model, preprocessor and evaluation metric shares the same consistent API, which is exactly what makes it so fast to work with once you learn the pattern once.
The One API Pattern That Runs Through Everything
| Method | What It Does | Used On |
|---|---|---|
.fit(X, y) | Learn parameters from training data | Models and preprocessors |
.predict(X) | Produce predictions for new data | Models |
.transform(X) | Apply a learned transformation | Preprocessors (scalers, encoders) |
.fit_transform(X) | Fit and transform in one call | Preprocessors |
.score(X, y) | Quick built-in performance metric | Models |
Swapping Models Without Rewriting Code
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
for ModelClass in [LogisticRegression, DecisionTreeClassifier, RandomForestClassifier]:
model = ModelClass()
model.fit(X_train, y_train) # identical call for every model
print(ModelClass.__name__, model.score(X_test, y_test))
Expected output: a printed accuracy score per model class — this loop works unchanged because every scikit-learn estimator, regardless of algorithm, implements the exact same .fit()/.score() interface.
Preprocessing Follows the Same Pattern
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train) # learn mean/std from train data, then scale it
X_test_scaled = scaler.transform(X_test) # apply the SAME learned mean/std to test data
Notice fit_transform on train, but only transform on test — fitting the scaler on test data would leak information about the test set into preprocessing. See Data Leakage.
Why This Consistency Matters
- Trying 5 different algorithms takes 5 lines of code change, not 5 different libraries with different conventions
- Preprocessing steps and models chain together cleanly into a Pipeline
- Every model exposes the same evaluation and cross-validation tooling
Common Mistakes
- Calling
.fit_transform()on the test set instead of just.transform()— this silently causes data leakage. - Assuming
.score()always means accuracy — for regressors it returns R², for classifiers it returns accuracy by default; always check what metric a specific.score()call actually returns.
Interview Relevance
Q: "Why does scikit-learn use fit/transform separately instead of always doing both at once?" So that whatever's learned from the training set (mean, std, encoding categories) can be applied identically to new data without re-learning it — critical for consistent behavior between training and inference, and for preventing test-set leakage.
Practice Question
Explain why calling scaler.fit_transform(X_test) instead of scaler.transform(X_test) is a bug, using the fit/transform distinction above.