Library cheatsheet
scikit-learn 1 Cheatsheet
Split data, preprocess features, train and evaluate models, tune hyperparameters and save pipelines with scikit-learn.
Data and splitting
- Install scikit-learn — Install scikit-learn, which needs NumPy and SciPy (pip installs them for you).
- Load a built-in dataset — Load a small practice dataset as a feature matrix X and target vector y.
- Generate a synthetic dataset — Create a random classification problem, good for testing code quickly.
- Split into train and test sets — Hold out 20% of the data for testing. stratify keeps class proportions, and random_state makes it repeatable.
Preprocessing and pipelines
- Scale features — Standardize to zero mean and unit variance. Fit on training data only, then transform both sets.
- Fill missing values — Replace missing values with the column median.
- Encode categories — Turn category labels into one-hot columns, ignoring categories unseen in training.
- Chain steps into a pipeline — Combine preprocessing and a model so the same steps apply in training and prediction, with no data leakage.
- Treat columns differently — Scale numeric columns and one-hot encode category columns in one transformer.
Training models
- Train and predict — Every estimator has fit and predict. Train on the training set, then predict labels for new rows.
- Get class probabilities — Return the predicted probability of each class instead of just the label.
- Fit a linear regression — Fit a least-squares line for a numeric target, then read the learned coefficients.
- Fit a logistic regression — A fast, interpretable baseline for classification.
- Use gradient boosting — A strong, fast model for tabular data that handles missing values natively.
- Cluster data — Group rows into k clusters without labels.
- Reduce dimensions — Project the data onto its two main directions of variance, for plotting or compression.
Evaluation and tuning
- Measure accuracy — The fraction of predictions that are correct. Be careful with imbalanced classes.
- Get precision, recall and F1 — Print per-class precision, recall, F1 and support.
- See where the model is wrong — Rows are true classes and columns are predicted classes.
- Evaluate a regression model — Report error in the target's own units and the share of variance explained.
- Cross-validate — Score a model on five different train and test splits for a more reliable estimate.
- Tune hyperparameters — Try every combination of settings with cross-validation and keep the best.
- See which features matter — Read how much each feature contributed to a random forest.
- Save and load a model — Persist a fitted model or pipeline with joblib. Only load files you trust.
Related cheatsheets
- Hugging Face cheatsheet — The hf command line, and Transformers and Datasets code for downloading, running, training and sharing models.
- Matplotlib and seaborn 3 cheatsheet — Draw line, scatter, bar and histogram charts with Matplotlib, and statistical charts with seaborn.
- pandas and NumPy 3 cheatsheet — Load, clean, reshape, group and analyze tabular data with pandas, and work with arrays in NumPy.
- Polars 2 cheatsheet — Fast DataFrames in Python: expressions, filtering, grouping, joins, lazy queries and file formats.
- Prisma ORM cheatsheet — Model your database in a schema, run migrations, and query with the type-safe Prisma Client. Examples use the prisma-client-js generator; check the docs for newer generators.
- PyTorch 2 cheatsheet — Tensors, autograd, models, training loops, saving and speed-ups in PyTorch 2.