Coding Now – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 9667708830
Back to Machine Learning Notes
Topic #206

scikit-learn — The Complete Introduction

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

MethodWhat It DoesUsed On
.fit(X, y)Learn parameters from training dataModels and preprocessors
.predict(X)Produce predictions for new dataModels
.transform(X)Apply a learned transformationPreprocessors (scalers, encoders)
.fit_transform(X)Fit and transform in one callPreprocessors
.score(X, y)Quick built-in performance metricModels

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.

Related ML Notes

Want to go beyond the notes?

Join CodingNow's Machine Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →