Implementation of Breiman's CART (Classification and Regression Trees) algorithm
Project description
Breiman CART Implementation (1984)
A production-grade, from-scratch implementation of Classification and Regression Trees (CART) following the original 1984 methodology by Breiman, Friedman, Olshen, and Stone.
Key Features
- True Categorical Handling: Native subset splitting for categorical features (not one-hot encoding)
- Cost-Complexity Pruning: Minimal cost-complexity pruning with alpha-based regularization
- No ML Libraries: Pure NumPy/Pandas implementation for educational and research purposes
- Type-Safe: Full type hints throughout the codebase
Installation
pip install numpy pandas
cd breiman_cart
Quick Start
Classification Example
import numpy as np
import pandas as pd
from src.tree import CARTClassifier
# Create sample data
X = pd.DataFrame({
'age': [25, 45, 35, 50, 23],
'city': ['NYC', 'LA', 'NYC', 'SF', 'LA']
})
y = np.array([0, 1, 0, 1, 0])
# Specify categorical columns
categorical_features = ['city']
# Fit the tree
tree = CARTClassifier(
max_depth=5,
min_samples_split=2,
categorical_features=categorical_features
)
tree.fit(X, y)
# Predict
predictions = tree.predict(X)
# Prune the tree
tree.prune(X_val, y_val)
Regression Example
from src.tree import CARTRegressor
X = pd.DataFrame({
'sqft': [1200, 1800, 1500, 2200],
'neighborhood': ['A', 'B', 'A', 'C']
})
y = np.array([300000, 450000, 350000, 550000])
tree = CARTRegressor(
max_depth=4,
min_samples_split=2,
categorical_features=['neighborhood']
)
tree.fit(X, y)
predictions = tree.predict(X)
Architecture
node.py: Node data structure with categorical supportsplitter.py: Splitting logic (Gini, MSE, subset splitting)tree.py: Main CART classes (Classifier/Regressor)pruning.py: Cost-complexity pruning implementation
Mathematical Background
See docs/theory.tex for detailed mathematical formulation.
Differences from Scikit-Learn
- Categorical Features: True subset splitting vs one-hot encoding
- Pruning: Implements full cost-complexity pruning sequence
- Transparency: Educational codebase following the original paper
Testing
python -m pytest tests/
References
Breiman, L., Friedman, J., Olshen, R., & Stone, C. (1984). Classification and Regression Trees. Wadsworth.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file breiman_cart-0.2.0.tar.gz.
File metadata
- Download URL: breiman_cart-0.2.0.tar.gz
- Upload date:
- Size: 194.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bfdaa483fa72f2359412600ae9e70b405339b312257b55e17a04896ba47ef9f5
|
|
| MD5 |
eee710d3fdf52f42e079a5b173d1e1af
|
|
| BLAKE2b-256 |
55f465f7611daf6d1b66cf79b9349b2b8ef90c802bb4a9b38b02b32ee7cdf584
|
File details
Details for the file breiman_cart-0.2.0-py3-none-any.whl.
File metadata
- Download URL: breiman_cart-0.2.0-py3-none-any.whl
- Upload date:
- Size: 32.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d6154ffe7a2358e4a8c7919c6a33e95b3775f356a89b31c9434fb65fdb3bc0e5
|
|
| MD5 |
05cb548e497c7cf9a522758da3caa668
|
|
| BLAKE2b-256 |
0e5c771676000820f7185e363b4a3f271bdeab6343e14565fb71ed6cfb5fe7ca
|