Skip to main content

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 support
  • splitter.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

  1. Categorical Features: True subset splitting vs one-hot encoding
  2. Pruning: Implements full cost-complexity pruning sequence
  3. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

breiman_cart-0.1.1.tar.gz (180.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

breiman_cart-0.1.1-py3-none-any.whl (19.0 kB view details)

Uploaded Python 3

File details

Details for the file breiman_cart-0.1.1.tar.gz.

File metadata

  • Download URL: breiman_cart-0.1.1.tar.gz
  • Upload date:
  • Size: 180.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for breiman_cart-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c57913f839e2130b5db49b828fc78bd4f7b265bcfaf25caff14c9a7eee889f8e
MD5 3ee7d199931f74fd2c72b82164460cf7
BLAKE2b-256 e3cf77a861009d4dcae55c4ffca7b141fad4a00369c851cae7e91649346d669f

See more details on using hashes here.

File details

Details for the file breiman_cart-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: breiman_cart-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 19.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for breiman_cart-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 07723196d38ae541661550ccb99b35794699e6ab1007d61e89429f4eee738434
MD5 faf2a8cc3a8c2628a4efe3fd26c3b114
BLAKE2b-256 3194597c50ee8ad5a9f01dc31e1dcdb6c0abf73bb1e72f77505850bd53ff02c7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page