Skip to main content

infogain-tree

A decision tree classifier written from scratch with numpy and pandas. It picks splits by entropy and information gain.

  • Numeric features get binary threshold splits (x <= t). Categorical features get multiway splits (one branch per category, as in ID3).
  • The size of the tree is controlled by hyperparameters: max_depth, min_samples_split, min_samples_leaf, min_information_gain, max_leaf_nodes.
  • Three ways to display the tree: text (print_tree()), matplotlib (plot()), Graphviz (to_dot()).
  • Two example datasets are included: load_play_tennis() and load_loan_approval().

Installation

pip install infogain-tree            # from PyPI
pip install "infogain-tree[plot]"    # with matplotlib, for tree.plot()
# or, from a clone of this repository:
pip install -e ".[plot]"

The package installs as infogain-tree and is imported as entropy_tree.

Quick start

from entropy_tree import DecisionTreeClassifier, load_play_tennis

df = load_play_tennis()
tree = DecisionTreeClassifier(max_depth=3).fit(df, "play")   # "play" is the target column
tree.print_tree()
split on 'outlook' (n=14, H=0.940, IG=0.247)
├── outlook = overcast → predict 'yes' (n=4, H=0.000, counts=no:0 yes:4)
├── outlook = rainy → split on 'windy' (n=5, H=0.971, IG=0.971)
│   ├── windy = False → predict 'yes' (n=3, H=0.000, counts=no:0 yes:3)
│   └── windy = True → predict 'no' (n=2, H=0.000, counts=no:2 yes:0)
└── outlook = sunny → split on 'humidity' (n=5, H=0.971, IG=0.971)
    ├── humidity = high → predict 'no' (n=3, H=0.000, counts=no:3 yes:0)
    └── humidity = normal → predict 'yes' (n=2, H=0.000, counts=no:0 yes:2)
tree.predict(df.drop(columns="play"))       # labels
tree.predict_proba(df.drop(columns="play")) # class probabilities (order: tree.classes_)
tree.score(df.drop(columns="play"), df["play"])
tree.plot()                                 # matplotlib figure
open("tree.dot", "w").write(tree.to_dot())  # Graphviz

fit(X, y) also takes a numpy array or a separate label vector: DecisionTreeClassifier().fit(X, y).

API

Item Description
DecisionTreeClassifier(max_depth=None, min_samples_split=2, min_samples_leaf=1, min_information_gain=0.0, max_leaf_nodes=None) The model
.fit(X, y) / .predict(X) / .predict_proba(X) / .score(X, y) Train, predict, evaluate
.get_depth(), .get_n_leaves(), .feature_importances_, .classes_, .root_ Inspect the tree
.print_tree(), .export_text(), .plot(), .to_dot() Display the tree
any_df_decision_tree(df, target_column, **params) One-call helper: build a tree from a DataFrame
entropy(y), information_gain(parent, children) Impurity functions
train_test_split, accuracy_score, confusion_matrix Small helpers, so scikit-learn is not needed
load_play_tennis(), load_loan_approval() Example datasets

Examples and tests

python examples/demo.py --plot     # full walkthrough on both datasets
python -m unittest discover -s tests

See report.md for a description of the algorithm and results.

Metadata

Release files for infogain-tree 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for infogain-tree 0.1.0
File Size Uploaded Provenance
infogain_tree-0.1.0.tar.gz 19.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for infogain-tree 0.1.0
File Interpreter ABI Platform
infogain_tree-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 37.2 kB

Release files / infogain_tree-0.1.0.tar.gz

Download URL infogain_tree-0.1.0.tar.gz
Size 19.7 kB
Tags Source
SHA-256 checksum
How to use checksums
e6105a7781eb00b9648815ee97eee9174eae4c00ae3e896de88aae7b84cf7034
BLAKE2b-256 checksum
How to use checksums
614205b4948f0017e17d8e0de1148a81dd66c872819c19ff8c158d8865036f67
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.10

Release files / infogain_tree-0.1.0-py3-none-any.whl

Download URL infogain_tree-0.1.0-py3-none-any.whl
Size 17.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
725c8828f48ff424e6c505ab369a3726937e731353af5bd6417e2d53d562ceff
BLAKE2b-256 checksum
How to use checksums
28f5c16a425d6d106a8c08469180eefdf3871aeeadee39e754e0c190088849b1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.10

Security for infogain-tree 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page