Skip to main content

Entropy decision tree from scratch

Educational implementation of ID3 for categorical data. The learning algorithm uses only the Python standard library: no scikit-learn decision tree is used. Prepared with AI assistance; review the code and report before submission.

Install locally and run

Open a terminal in this project folder (the folder containing pyproject.toml):

python3 -m pip install -e .
python3 examples/tennis_demo.py
python3 -m unittest discover -s tests -v

Open tree.svg in your browser for a graphical display. The SVG is generated from the learned tree, not hard-coded. model.display() also prints the rules.

Use the package

from ebele_tree import DecisionTreeClassifier
X = [{"Outlook": "Sunny"}, {"Outlook": "Rain"}, {"Outlook": "Overcast"}]
y = ["No", "Yes", "Yes"]
model = DecisionTreeClassifier(max_depth=2).fit(X, y)
print(model.predict([{"Outlook": "Sunny"}]))
model.display()
model.export_svg("my_tree.svg")

In a notebook, a pandas DataFrame can be converted with X.to_dict(orient="records"); labels can be passed as y.tolist(). To display the exported SVG in a notebook:

from IPython.display import SVG, display
model.export_svg("tree.svg")
display(SVG(filename="tree.svg"))

How learning works

  1. Count the target labels and calculate H(S) = -sum(p * log2(p)).
  2. For each feature, group rows by category and calculate the weighted child entropy.
  3. Gain = parent entropy minus weighted child entropy.
  4. Choose the feature with the largest gain.
  5. Repeat separately within each branch, removing the selected feature.
  6. Stop at a pure node, no useful gain, no remaining feature, or a size limit.
  7. A stopped node predicts its majority class. Ties use first appearance order.

max_depth controls depth (root depth is 0). min_samples_split controls how many examples are required before splitting. min_gain controls the minimum useful gain. A category unseen during fitting uses the current node's majority.

Tennis example

The 14 rows reproduce the lecture's Play Tennis example. Day is excluded as an identifier. Categories are used directly; no numeric encoding is needed.

Feature Root information gain
Outlook 0.246750
Temperature 0.029223
Humidity 0.151836
Wind 0.048127

The learned tree starts with Outlook; Sunny uses Humidity and Rain uses Wind. Overcast immediately predicts Yes. With max_depth=2, training accuracy is 1.0. That is a training demonstration, not evidence of perfect generalisation.

Scope and limitations

Categorical, hashable, nonmissing inputs only. Numeric values are treated as discrete categories; continuous threshold splits are not implemented. This is an educational implementation, not an efficient model for the large irrigation dataset. Graphical boxes are sized for short labels such as the tennis example.

See PUBLISHING.md for GitHub and PyPI instructions. The package name ebele-entropy-tree is provisional: availability has not been checked.

Metadata

Release files for ebele-entropy-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 ebele-entropy-tree 0.1.0
File Size Uploaded Provenance
ebele_entropy_tree-0.1.0.tar.gz 10.1 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 16.7 kB

Release files / ebele_entropy_tree-0.1.0.tar.gz

Download URL ebele_entropy_tree-0.1.0.tar.gz
Size 10.1 kB
Tags Source
SHA-256 checksum
How to use checksums
3e8e4ee7d408d80c353aa5b83989ef34a3ad9d9bf467b7f30a71d3990618bff6
BLAKE2b-256 checksum
How to use checksums
fefeb9db98457d64561bcb784a5dc30b6260a5dc88a95501706a5d95cccbac18
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.5

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

Download URL ebele_entropy_tree-0.1.0-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
150646177259c2a8967ab235e16e8ec9ff41507e488ba81898478cb40f6fa670
BLAKE2b-256 checksum
How to use checksums
5a723b5963eaf0f1e645edf373e817c0e384138687a4ecebf7c9b8bd3a3d8d81
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.5

Security for ebele-entropy-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