How dforgeek/reg_tree_from_scratch works
Purpose and entry points
This is an educational, pure-Python regression decision-tree implementation. Its primary API is MyDecisionTreeRegressor.fit() / .predict() in decision_tree.py. The executable workflow is the visualization CLI in visualize.py, which loads tabular data, trains a tree, and exports a Graphviz image. A notebook demonstrates interactive use: decision_tree_usage.ipynb.
Model implementation
decision_tree.py contains the architecture’s core: the regression-tree class, recursive node construction, split selection, and prediction traversal. Training partitions feature rows until stopping rules such as maximum depth, minimum samples per split, or minimum information gain are reached. Leaf nodes produce regression predictions, typically derived from the target values in their partition.
Grounding:
• decision_tree.py
• config.py
Split evaluation and feature handling
The tree supports numeric and categorical features. Candidate splits are evaluated using regression impurity/variance reduction (information gain), allowing training to choose the feature and threshold/category split that most improves target homogeneity. This is the principal algorithmic boundary: pandas-like tabular inputs enter the estimator; internal recursive node/split representations drive training and inference.
Grounding:
• decision_tree.py
• utils.py
Configuration boundary
Default hyperparameters and dataset-related settings are centralized in config.py; callers may also provide estimator arguments or visualization CLI flags. Configuration controls model complexity and search behavior, including depth, minimum split size, information-gain threshold, and categorical handling limits.
Grounding:
• config.py
• decision_tree.py
• visualize.py
Training and inference flow
1. A caller loads and cleans a CSV into feature matrix X and regression target y.
2. MyDecisionTreeRegressor.fit(X, y) recursively searches valid splits and builds the root-to-leaf tree.
3. predict(X) routes each row through stored split decisions to a leaf prediction.
4. Consumers can use predictions directly or render the learned structure.
The included sample dataset is hotel-price-oriented and is used as an example rather than a required runtime service.
Grounding:
• example_data/hotel_data.csv
• decision_tree.py
• decision_tree_usage.ipynb
Visualization pipeline
visualize.py is the operational entry point for inspection. It accepts dataset, output, and model-control arguments; trains the same decision-tree implementation; then translates the tree structure into Graphviz output for PNG/image generation. This separates model logic from presentation while exposing the tree’s learned split hierarchy.
Grounding:
• visualize.py
• decision_tree.py
Runtime and dependencies
The project runs in Python and relies on standard data-science dependencies declared in requirements.txt, notably for tabular loading/manipulation and visualization support. Graphviz is an external system dependency required for rendered tree output; no database, network API, hosted infrastructure, or web runtime is indicated.
Grounding:
• requirements.txt
• visualize.py
Main entry points: visualize.py Visualization CLI · decision_tree.py MyDecisionTreeRegressor · decision_tree_usage.ipynb Usage notebook · utils.py Feature handling
Components 16
Tree model
Core regression-tree training and inference.
- MyDecisionTreeRegressor · Public fit and predict API.
decision_tree.py - Recursive tree builder · Partitions rows until configured stopping rules are met.
decision_tree.py - Split selection · Chooses numeric or categorical splits by variance reduction.
decision_tree.py - Feature handling · Supports numeric and categorical feature evaluation.
utils.py - Learned tree · Stored root-to-leaf split structure and leaf predictions.
decision_tree.py - Prediction traversal · Routes each row to a leaf prediction.
decision_tree.py
Usage and visualization
Interactive and CLI entry points.
- Hotel sample CSV · Example hotel-price dataset.
example_data/hotel_data.csv - Usage notebook · Demonstrates estimator use.
decision_tree_usage.ipynb - Visualization CLI · Loads data, trains a tree, and exports a visualization.
visualize.py - CLI arguments · Dataset, output, and model-control options.
visualize.py - CSV loading and cleaning · Prepares features and regression targets.
visualize.py - Graphviz image output · PNG or other image of the learned split hierarchy.
visualize.py
Other components
- Tabular X and y · Cleaned feature matrix and regression target.
- Default settings · Centralizes depth, split, gain, and categorical limits.
config.py - Python dependencies · Declares tabular and visualization dependencies.
requirements.txt - Graphviz · System dependency for rendered tree output.
Connections 21
- Hotel sample CSV → CSV loading and cleaning: input
- CSV loading and cleaning → Tabular X and y: prepares
- Usage notebook → Tabular X and y: provides
- Usage notebook → MyDecisionTreeRegressor: uses
- CLI arguments → Visualization CLI: configures
- Visualization CLI → CSV loading and cleaning: loads
- Visualization CLI → MyDecisionTreeRegressor: trains
- Tabular X and y → MyDecisionTreeRegressor: fit / predict input
- Default settings → MyDecisionTreeRegressor: defaults
- MyDecisionTreeRegressor → Recursive tree builder: fit
- Recursive tree builder → Split selection: evaluates candidates
- Feature handling → Split selection: supports
- Split selection → Learned tree: selected splits
- Recursive tree builder → Learned tree: builds
- MyDecisionTreeRegressor → Prediction traversal: predict
- Learned tree → Prediction traversal: routes rows
- Visualization CLI → Learned tree: inspects
- Learned tree → Graphviz image output: translated to graph
- Visualization CLI → Graphviz: invokes
- Graphviz → Graphviz image output: renders
- Python dependencies → Visualization CLI: runtime dependencies