typestar

Machine learning with scikit-learn

25 steps in 8 sets of Python.

scikit-learn's real contribution is the estimator API: fit, predict, transform, and the fact that every model in the library obeys it. Learn that shape once and the rest of the library is a catalog.

So the tour starts there, then preprocessing and the pipelines that stop you leaking test data into training. Validation, a run through the models, scoring that goes past accuracy, unsupervised methods, and a final set on the things you reach for when the basics stop being enough.

Start this tour

The estimator API

Preprocessing

Validation

  • Cross validationOne split is a coin toss; cross validation scores every fold and averages.
  • Choosing the foldsKFold and StratifiedKFold decide how the data is carved up.
  • Grid searchGridSearchCV tries every combination with cross validation and keeps the best.
  • Does more data help?A learning curve answers whether to gather data or change the model.

Models

  • Random forestsAn ensemble of trees, and the feature importances that come with it.
  • Gradient boostingTrees fitted one after another, each correcting what the last got wrong.
  • Nearest neighboursThe model that does no work until you ask it a question.
  • Decision treesA model you can read: depth, splits, and the rules it learned.

Scoring

Unsupervised

  • KMeans clusteringUnsupervised: no labels, just centroids and the points nearest each one.
  • PCARotating the data so the first axes carry the most variance.

Beyond the basics

Encore

  • sk_churn_model.pyAn end-to-end model: split, preprocess by column type, grid search, report.

The other Python tours