Sefism is free for learners. We are here to help you grow, one step at a time.Join for free
Sefism.

Best practices · Data and ML

Machine Learning Best Practices

Opens with credits

Best practices for building and evaluating a machine learning model before it goes anywhere near production, checked against the scikit-learn and PyTorch documentation, Google's Rules of Machine Learning and ML Crash Course, MLflow and Fairlearn. It covers problem framing and baselines, data splits and leakage, features, training and tuning, metrics and error analysis, reproducibility and experiment tracking, and fairness checks.

Written by Tauseef Fayyaz7 chapters, 30 practicesAbout 18 min25 quiz questionsReviewed 13 October 2026

What you will learn

7 chapters · about 18 min

  1. 1Problem framing and baselinesDecide what the model predicts, what decision it supports and what it has to beat before you train anything. These practices set the bar every later chapter measures against.4 practices · 3 min
  2. 2Data splits and leakageLeakage makes a weak model look strong. These practices, mostly from scikit-learn's common pitfalls guide, keep information from the evaluation data out of training.5 practices · 3 min
  3. FeaturesGood features matter more than clever models, especially early on. These practices come from Google's Rules of ML and scikit-learn's preprocessing tools.4 practices · 2 minOpens with credits
  4. Training and tuningTuning is where leakage most often sneaks back in. These practices keep hyperparameter search honest and training runs comparable.4 practices · 3 minOpens with credits
  5. Evaluation metrics and error analysisA single headline number rarely tells you whether a model is good enough. These practices pick the right metric and look past the average.5 practices · 3 minOpens with credits
  6. Reproducibility and experiment trackingIf you cannot rerun an experiment, you cannot tell whether a change helped. These practices control randomness and record what each run did.4 practices · 2 minOpens with credits
  7. Fairness checksA model can perform well on average and still fail some groups of people. These practices follow Fairlearn's assessment guide.4 practices · 2 minOpens with credits

About this guide

Most machine learning projects that disappoint do not fail because the model was too simple. They fail because the evaluation was wrong. A scaler fitted on the whole dataset, a customer who appears in both the training and the test set, a test set that was looked at fifty times during tuning, or an accuracy score on data where 99 out of 100 rows belong to one class: each of these produces a number that looks great and means little. These machine learning best practices are about building a model whose numbers you can believe.

The guide follows the order of real work. It starts with framing the problem and setting a baseline, because a model is only good relative to something simpler. It then covers splitting data without leakage, building features, training and tuning, choosing metrics and reading errors, keeping experiments reproducible, and checking how the model behaves for different groups of people. Examples use scikit-learn for classic models and PyTorch for deep learning, with MLflow for tracking and Fairlearn for group metrics, but the ideas apply to any library.

This guide stops where deployment starts. Serving, canary rollouts, drift monitoring and retraining are covered in the ML model deployment checklist. Everything here happens before that: on your laptop, in a notebook or in a training pipeline, while the model is still cheap to change.

Start with the first chapter even if you are eager to train. Google's Rules of Machine Learning open with a reminder that a simple heuristic may get you most of the way, and that you should design your metrics first. A clear problem and a baseline make every later decision easier to judge.

Common questions

What is data leakage in machine learning?

Data leakage is using information during training that would not be available when the model makes real predictions, which makes evaluation scores look better than they are. Common causes are fitting preprocessing on the whole dataset before splitting, letting the same customer or patient appear in both training and test data, and features that are only known after the outcome.

How should I split data for a machine learning model?

Use a training set, a validation set or cross-validation for decisions, and a test set you use once at the end. Split by group when rows share an entity such as a patient or user, and by time when the model will predict the future. Remove duplicates across splits before you evaluate.

Which metric should I use for an imbalanced classification problem?

Not accuracy, since a model that always predicts the majority class can score very high. Use recall when missed positives are costly, precision when false alarms are costly, or F1, balanced accuracy or average precision when you need one summary. Compare against a dummy baseline on the same metric.

Why use a scikit-learn Pipeline?

A Pipeline chains preprocessing and the model so that transforms are learned from training data only and applied the same way at prediction time. It prevents leakage during cross-validation and hyperparameter search, and it means you save and load one object instead of several steps you might forget.

How do I make a PyTorch training run reproducible?

Seed torch, Python's random module and NumPy, turn off cuDNN benchmarking, enable deterministic algorithms, and give DataLoader a seeded generator and a worker_init_fn. Record the library versions too, because PyTorch does not promise identical results across releases or platforms.

How do I check a model for fairness?

Name the harms and groups first, then compute your metrics per group with a tool such as Fairlearn's MetricFrame and look at the gaps and the group sizes. Choose fairness criteria that fit the context, since common criteria can conflict, and record the decision.

Sources (8)

Checked against these, last on 13 October 2026.

  1. Common pitfalls and recommended practices, scikit-learn docsscikit-learn.org/stable/common_pitfalls.html
  2. Cross-validation: evaluating estimator performance, scikit-learn docsscikit-learn.org/stable/modules/cross_validation.html
  3. Metrics and scoring: quantifying the quality of predictions, scikit-learn docsscikit-learn.org/stable/modules/model_evaluation.html
  4. Reproducibility, PyTorch docsdocs.pytorch.org/docs/stable/notes/randomness.html
  5. Rules of Machine Learning: Best Practices for ML Engineering, Google for Developersdevelopers.google.com/machine-learning/guides/rules-of-ml
  6. Datasets, generalization, and overfitting: Dividing the original dataset, Google ML Crash Coursedevelopers.google.com/machine-learning/crash-course/overfitting/dividing-datasets
  7. MLflow Tracking, MLflow docsmlflow.org/docs/latest/ml/tracking/
  8. Performing a Fairness Assessment, Fairlearnfairlearn.org/main/user_guide/assessment/perform_fairness_assessment.html

Picked from the other libraries by shared topics.

Work with me

Stuck on something specific?

Writing only gets you so far. If you want an answer to your situation rather than the general case, book a session and we will work through it together. Sessions are free for approved Sefism members, and a few slots open each week.

  • 1:1 Mentorship
  • Fresh Graduate Mentorship
  • Career Guidance

Brand deals, promos and partnerships: tauseeffayyaz@gmail.com