Coderhouse goes global

🌍

Read more

What Are Decision Trees and Random Forests and How They Work

AI Engineering

Build and ship real applications with LLMs, agents and automation.

8 weeks · Live online · View course →

Data Analytics

Go from zero to job-ready analyst with SQL, Python and BI tools.

10 weeks · Live online · View course →

Dan Patiño

AI Strategy & Innovation at Coderhouse

Artificial Intelligence

What Are Decision Trees and Random Forests and How They Work

What Are Decision Trees and Random Forests and How They Work

A decision tree is a supervised learning model that predicts a class or a number by asking a sequence of if-then questions about the features. Random forests build many such trees on random slices of the data and average their answers, which usually beats a single tree on accuracy and stability.

Trees are popular with beginners because you can visualize the rules, explain a prediction to a stakeholder, and train them with little feature scaling. They are also the backbone of many production tabular models. Understanding how one tree splits—and why a forest of trees reduces overfitting—is a core machine learning skill before you move into boosting or deep learning.

How a decision tree learns

Training starts with the full dataset at the root. At each node the algorithm searches for a feature and threshold that best separate the target—for classification that often means reducing impurity (Gini or entropy); for regression it means reducing variance or squared error. The chosen split creates child nodes; the process repeats until a stopping rule fires (max depth, minimum samples per leaf, or pure leaves).

Prediction is a walk down the tree: at each internal node follow the branch that matches the example until you reach a leaf. Classification leaves vote for a class (or store class probabilities); regression leaves return an average target value. The result is a piecewise constant approximation of the underlying function.

According to the scikit-learn decision trees documentation, trees need little preparation compared with many linear models, handle mixed feature types conceptually, and produce white-box rules you can inspect. The same docs warn that deep trees overfit, that small data changes can rewrite the structure, and that greedy splitting does not guarantee a globally optimal tree.

Why single trees overfit—and how forests help

A deep tree can memorize noise. That is classic overfitting: excellent training score, weak performance on new data. Pruning, limiting depth, and requiring more samples per leaf are the first defenses.

A random forest trains many trees and aggregates them. Two sources of randomness decorrelate the trees:

  • Bootstrap samples: each tree usually sees a resampled draw of the training rows.

  • Random feature subsets: at each split, only a subset of features is considered (controlled by max_features in scikit-learn).

Because individual tree errors are less aligned, averaging (or averaging predicted probabilities) reduces variance. The scikit-learn ensemble guide describes random forests as perturb-and-combine methods designed specifically around trees: inject randomness so errors cancel when you pool predictions, often with only a small bias cost.

Decision tree vs random forest (when to use which)

  • Prefer a single tree when you need maximum interpretability, a small rule set for compliance storytelling, or a quick baseline you can draw on a whiteboard.

  • Prefer a random forest when predictive performance on tabular data matters more than a single readable path, and you can afford the extra compute and less transparent logic.

  • Still validate properly. Forests reduce variance but do not remove the need for holdout sets or cross-validation, especially when tuning depth, min_samples_leaf, or max_features.

Practical tips for beginners

  1. Start with a shallow tree to learn which features drive splits.

  2. Plot feature importances from a forest as a diagnostic, not as causal proof.

  3. Watch class imbalance: trees can bias toward majority classes if you do not balance or weight samples.

  4. For classification, default max_features="sqrt" is a common starting point in scikit-learn; for regression, defaults differ—always check the version docs and validate.

  5. Increase n_estimators until validation gains flatten; past that point you mainly buy compute cost.

Where trees and forests show up in real work

Credit risk scorecards, churn models, fraud flags, demand forecasting features, and many Kaggle-style tabular problems still lean on tree ensembles. Even when your final system is a neural net, trees remain strong baselines and useful for feature understanding. They also pair naturally with solid feature engineering: clear numeric and categorical inputs make splits more meaningful.

FAQ: Are random forests outdated now that gradient boosting exists?

No. Boosting often wins benchmarks, but forests are robust defaults, parallelize easily, and are harder to overfit than a single deep tree. Many teams still ship forests for tabular baselines and production simplicity.

FAQ: Can trees handle missing values and categories?

Some implementations can; scikit-learn’s classic tree API historically required numeric matrices and separate encoding. Always check the library you use. In practice, analysts often impute or encode before fitting.

FAQ: How is this different from logistic regression?

Logistic regression learns a smooth linear boundary in feature space (unless you add interactions manually). Trees learn axis-aligned partitions and capture interactions automatically by stacking splits. Use the simpler model when linearity and coefficients matter; use trees when relationships are nonlinear and messy.

Learn to build and evaluate models with live practice

If you want hands-on practice with data, models, and evaluation, guided by practitioners, Coderhouse offers live online courses with one class per week:

  • Data Analytics Course: 11 weeks for career switchers, analysts, and data-driven professionals, covering metrics, SQL, Python, visualization, experimentation, and AI-assisted analysis.

  • AI Engineering Course: 8 weeks for experienced developers moving into AI engineering, covering LLM applications, retrieval, agents, evaluation, and deployment.

Dan Patiño

I'm Dan Patiño, head of AI Strategy & Innovation at Coderhouse. My day-to-day work involves merging the tactical management of e-commerce (CRO, Email Marketing and SEO) with the development of disruptive solutions. I specialize in building internal AI-powered apps to automate tasks and boost innovation within the team. I firmly believe that technology is strategy's best ally. To dive deeper into my professional journey, I'll be waiting for you on my LinkedIn profile.