scikit-learn Essentials
The library behind most classical ML: the estimator API, splitting, preprocessing, Pipelines and ColumnTransformers, cross-validation, tuning, metrics and saving models.
One consistent API
scikit-learn is the standard Python library for classical machine learning: dozens of models, preprocessing tools, model selection and metrics, all with the same interface. Learn the interface once and every algorithm in this course is three lines away.
- Estimators learn from data with
fit(X, y). Hyperparameters go in the constructor:RandomForestClassifier(n_estimators=300). What was learned is stored in attributes ending in an underscore:coef_,feature_importances_. - Predictors add
predict(X), and classifiers usuallypredict_proba(X);score(X, y)gives a default metric. - Transformers (scalers, encoders, imputers, PCA) have
fitandtransform, orfit_transformon training data.
X is a 2-D table (DataFrame or array, one row per example) and y a 1-D array of targets. random_state makes results reproducible.
Code example: fit, predict, score
A worked example: read it step by step (every part is explained), then run it and experiment.
Interactive: this part runs in your browser.
Pipelines and ColumnTransformer
Real tables mix numeric and categorical columns, with missing values. A ColumnTransformer applies different preprocessing to different columns; a Pipeline chains preprocessing and a model into one estimator. This is the single most important habit in applied ML, for three reasons: preprocessing is fitted on training data only (inside every cross-validation fold too), so nothing leaks; the same steps run at prediction time; and you save and deploy one object.
Try it: build a pipeline
Choose the steps and see the pipeline diagram and the code. Then try the "scale before splitting" mistake to see where leakage creeps in.
Interactive: this part runs in your browser.
Cross-validation, tuning and metrics
Parameters of a step inside a pipeline are addressed as step__parameter (two underscores). RandomizedSearchCV samples combinations when the grid is large. Scoring names include accuracy, balanced_accuracy, f1, roc_auc, average_precision, neg_mean_absolute_error (errors are negated so that higher is always better). In sklearn.metrics you will use confusion_matrix, classification_report, roc_auc_score, average_precision_score, mean_absolute_error and r2_score.
Save the final pipeline with joblib.dump(best, "model.joblib") and load it with joblib.load, using the same scikit-learn version.
Code example: Pipeline + ColumnTransformer + GridSearchCV
A worked example: read it step by step (every part is explained), then run it and experiment.
Interactive: this part runs in your browser.
Mistakes to avoid
- Fitting preprocessing on all the data (
StandardScaler().fit(X)before splitting, orSelectKBestoutside the pipeline): leakage. Put it in the Pipeline. - Tuning on the test set: use cross-validation or a separate validation set; touch the test set once.
- Ignoring
handle_unknowninOneHotEncoder: a new category in production crashes the model. - Using
predictwhen you need a threshold:predictuses 0.5; usepredict_probaand choose the threshold from costs. - Random splits for grouped or time data: use
GroupKFold(same patient never in both) orTimeSeriesSplit. - Forgetting the class balance:
stratify=yin splits,class_weight="balanced"where appropriate, and metrics that reflect it.
Cheat sheet: scikit-learn
The commands you will use most, on one screen. Come back to it while you work.
Interactive: this part runs in your browser.
Quiz
1. Which method do all scikit-learn estimators share?
2. Why put the scaler inside a Pipeline?
3. How do you name the C parameter of a step called "clf" in a GridSearchCV grid?
4. A fitted model's learned coefficients are in…
5. Your classifier should flag cases at probability ≥ 0.3. You should use…
📚 Further reading
Trusted, mostly free sources: official documentation, well-known textbooks and courses. When you want to confirm or go deeper on anything here, start with these.
- scikit-learn: Getting started ↗ The estimator API, pipelines and model selection
- scikit-learn User Guide ↗ Official documentation for every model
- scikit-learn: Common pitfalls ↗ Leakage and other mistakes