Learn scikit-learn

The standard Python toolkit for classical machine learning

33 lessons · 6 hours · 4 levels · covers scikit-learn 1.9

scikit-learn provides consistent, well-tested implementations of classical machine learning: linear models, trees and ensembles, clustering, dimensionality reduction, preprocessing, model selection and evaluation. Every model follows the same fit/predict/transform API, so pipelines, cross-validation and hyperparameter search work with any of them. It is the first library most people use for tabular ML and the reference API that XGBoost, LightGBM and many others imitate.

Before you start

scikit-learn builds on these; the tree links to the exact lessons you need.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics11 lessons · 2 hours in total
2. PracticalUse it productively at work, with the usual idioms22 lessons · 4 hours in total
3. AdvancedThe less common features, performance and trade-offs28 lessons · 5 hours in total
4. ExpertInternals and the edge cases experts know33 lessons · 6 hours in total

The scikit-learn tree

scikit-learnThe estimator API: fit, predict, transformThe estimator API: fit,predict, transformFeatures, samples and the shape of X and yFeatures, samplesand the shape of X…Built-in and synthetic datasetsBuilt-in and syntheticdatasetsTrain/test splitTrain/test splitYour first classifierYour first classifierLinear regressionLinear regressionDecision treesDecision treesAccuracy and the confusion matrixAccuracy and theconfusion matrixRegression metricsRegression metricsFeature scalingFeature scalingEncoding categorical featuresEncoding categoricalfeaturesPipelinesPipelinesHandling missing valuesHandling missingvaluesColumnTransformer for mixed dataColumnTransformerfor mixed dataCross-validationCross-validationPrecision, recall, F1 and ROC AUCPrecision, recall, F1and ROC AUCHyperparameter searchHyperparametersearchRandom forests and gradient boostingRandom forests andgradient boostingRegularised linear modelsRegularised linearmodelsClustering with k-means and friendsClustering withk-means and friendsDimensionality reduction with PCADimensionalityreduction with PCASaving and loading modelsSaving and loadingmodelsThe ndarrayThe ndarrayClasses and instancesClasses andinstancesDataFrameDataFrameShape and reshapeShape and reshapeMissing data basicsMissing data basicsLinear algebra basicsLinear algebra basics
Essentials and Practical shown (22 of 33 lessons); the full list is below. Click a lesson to read it.

All 33 lessons, in order

Foundations

  1. The estimator API: fit, predict, transform · Essentials · 10 min

    Every scikit-learn object is an estimator configured in its constructor and trained with fit().

  2. Features, samples and the shape of X and y · Essentials · 8 min

    Models take a 2-D feature matrix X (samples by features) and a 1-D target y.

  3. Built-in and synthetic datasets · Essentials · 7 min

    Load toy datasets and generate synthetic data to practise and to test ideas quickly.

  4. Train/test split · Essentials · 8 min

    Hold out data the model never sees during training to estimate how it will do on new data.

Core models

  1. Your first classifier · Essentials · 10 min

    Train a logistic regression and a k-nearest-neighbours classifier and compare them on held-out data.

  2. Linear regression · Essentials · 9 min

    Predict a number as a weighted sum of features and read what the coefficients mean.

  3. Decision trees · Essentials · 10 min

    Learn a hierarchy of if/else splits on features, and control its depth to avoid overfitting.

  4. Random forests and gradient boosting · Practical · 12 min

    Combine many trees by averaging (forests) or by sequential correction (boosting) for strong tabular models.

  5. Regularised linear models · Practical · 11 min

    Use Ridge, Lasso and ElasticNet, and the C parameter of classifiers, to keep linear models from overfitting.

Evaluation

  1. Accuracy and the confusion matrix · Essentials · 9 min

    See which classes a model confuses, beyond a single accuracy number.

  2. Regression metrics · Essentials · 8 min

    Measure regression error with MAE, RMSE and R², and know what each one rewards.

  3. Precision, recall, F1 and ROC AUC · Practical · 12 min

    Measure the trade-off between catching positives and raising false alarms, at one threshold and across all.

  4. Imbalanced classes and decision thresholds · Advanced · 12 min

    Handle rare positive classes with the right metric, class weights and a tuned decision threshold.

  5. Probability calibration · Advanced · 10 min

    Check whether predicted probabilities match observed frequencies and fix them with CalibratedClassifierCV.

  6. Model inspection and feature importance · Advanced · 12 min

    Explain a model with permutation importance and partial dependence instead of trusting impurity importances.

Preprocessing and pipelines

  1. Feature scaling · Essentials · 9 min

    Standardise or normalise features so distance- and gradient-based models treat them fairly.

  2. Encoding categorical features · Essentials · 10 min

    Turn text categories into numbers with OneHotEncoder and OrdinalEncoder, and handle unseen values.

  3. Pipelines · Practical · 10 min

    Chain preprocessing steps and a model into one estimator that fits and predicts as a unit.

  4. Handling missing values · Practical · 9 min

    Fill missing values with SimpleImputer, keep a missing-indicator, or use models that accept NaN.

  5. ColumnTransformer for mixed data · Practical · 12 min

    Apply different preprocessing to numeric and categorical columns in one step.

  6. Feature engineering transformers · Advanced · 12 min

    Create features inside pipelines with FunctionTransformer, PolynomialFeatures, SplineTransformer and TargetEncoder.

  7. Pandas output and feature names · Advanced · 8 min

    Make transformers and pipelines return DataFrames with meaningful column names via set_output.

Model selection and tuning

  1. Cross-validation · Practical · 11 min

    Estimate performance by averaging over several train/validation splits instead of trusting one.

  2. Hyperparameter search · Practical · 12 min

    Tune hyperparameters with GridSearchCV and RandomizedSearchCV using cross-validation.

  3. Data leakage, groups and time series CV · Advanced · 12 min

    Choose GroupKFold or TimeSeriesSplit when rows are not independent, and spot leakage before it fools you.

  4. Nested cross-validation and honest evaluation · Expert · 12 min

    Get an unbiased performance estimate for a tuned model by wrapping the search inside an outer CV loop.

Unsupervised learning

  1. Clustering with k-means and friends · Practical · 11 min

    Group unlabelled data with KMeans, DBSCAN and agglomerative clustering, and judge the result.

  2. Dimensionality reduction with PCA · Practical · 10 min

    Compress many correlated features into a few principal components that keep most of the variance.

Production and internals

  1. Saving and loading models · Practical · 8 min

    Persist a fitted pipeline with joblib or skops and load it safely in another process.

  2. Writing custom estimators · Expert · 14 min

    Implement transformers and models that work with pipelines, cloning and search by following the estimator contract.

  3. clone, get_params and fitted state · Expert · 10 min

    Understand how scikit-learn copies, configures and inspects estimators inside meta-estimators.

  4. Metadata routing and sample weights · Expert · 12 min

    Pass sample_weight, groups and other per-sample data through pipelines and searches with metadata routing.

  5. Performance, parallelism and large data · Expert · 12 min

    Speed up training with n_jobs, thread control, pipeline caching, sparse data and out-of-core partial_fit.