c50py — C5.0‑style Decision Trees for Python (clean 0.2.0)
c50py provides transparent, easily inspectable decision trees modelled on
Quinlan’s C5.0 algorithm. Both classification and regression trees are
supported and expose a scikit‑learn‑like API. The implementation is written
from scratch in pure Python/Numpy and includes support for numeric and
categorical variables, missing values, pre‑ and post‑pruning, boosting,
rule tracing/export and Graphviz visualisation.
What's new in 0.3.0
Version 0.3.0 brings the classifier much closer to Quinlan's C4.5/C5.0 and makes it faster:
- Pessimistic pruning as in C4.5. Leaves are penalised with the binomial upper limit (
AddErrsfromprune.c) and the original confidence-factor table (cf = 0.25 gives a deviate of about 0.69). Pure leaves now receive a positive penalty, so tiny leaves that only memorised the data are pruned. The previous normal approximation gave pure leaves zero penalty, so trees with pure leaves were never pruned, and the old "global" pass could collapse a useful tree into a single leaf on imbalanced data.global_pruningis now off by default. - Split selection as in C4.5. Gain ratio competes only among features whose gain is at least the average gain (
gain_ratio_avg_gain=True); unknown cases count as an extra branch in the split information; and continuous attributes pay the MDL penalty of C4.5 Release 8 (mdl_penalty=True). - Exhaustive, vectorised numeric thresholds.
numeric_threshold_strategy="all"is now the default and is evaluated with cumulative sums, so it is faster than the old 32-quantile subsample. - pandas DataFrames are accepted directly, column names become feature names, and
infer_categorical=Trueis honoured by the classifier (object, category, bool and string columns).print_tree,export_rules,predict_ruleandexport_graphvizdefault to the names seen infit. - Boosting. A perfect first tree no longer gets an arbitrary weight of 10; it gets a finite AdaBoost weight and boosting stops, as in C5.0. Combined with the new pruning,
trials > 1now works frommin_samples_leaf=1.
Features
- Scikit-learn API:
fit(X, y),predict(X),score(X, y). - Categorical support: Pass
categorical_features=[0, 2]to handle categories natively. - Sample weights: Supports
sample_weightinfitfor weighted splitting and pruning. - Missing values: Handles missing values using C5.0's fractional propagation strategy.
- Boosting: Set
trials=10to train a boosted ensemble. - Rule export: call
export_rules()to get a list of human-readable rules. - Graphviz export: call
export_graphviz()to visualize the tree. - Pretty printing: call
print_treeto display the learned splits in a readable nestedif/elseformat (single trees only).
Documentation
For a comprehensive guide on how to use c50py, including advanced features and examples, please see the Usage Guide.
Installation (development mode)
Install the package into your environment in editable mode:
pip install -e .
Quickstart (Classification)
import pandas as pd
from time import perf_counter
from c50py import C5Classifier
df = pd.read_csv("titanic.csv")
t0 = perf_counter(); clf.fit(X, y); print(f"fit: {perf_counter()-t0:.3f}s")
# Inspect the tree
clf.print_tree(feature_names=features, class_names=["No", "Yes"])
# Extract rules for each sample
rules = clf.predict_rule(X, feature_names=features)
print(rules[:5])
# Export as Graphviz
path = clf.export_graphviz(
"titanic_tree",
feature_names=features,
class_names=["No", "Yes"],
format="dot" # save a .dot file directly
)
print(f"DOT file written to {path}")
Quickstart – Regression (Diabetes)
Fit a regression tree to the diabetes dataset and obtain a visualisation:
import pandas as pd
from time import perf_counter
from c50py import C5Regressor
df = pd.read_csv("diabetes.csv")
y = df["target"].values
X_df = df.drop(columns=["target"])
X = X_df.values.astype(object)
features = list(X_df.columns)
reg = C5Regressor(
min_samples_split=30,
min_samples_leaf=10,
pruning=True, cf=0.25, global_pruning=True,
feature_names=features,
random_state=42,
infer_categorical=False, int_as_categorical=False,
numeric_threshold_strategy="quantile", max_numeric_thresholds=64
)
start = perf_counter(); reg.fit(X, y); print(f"fit: {perf_counter()-start:.3f}s")
# Export to DOT (Graphviz installed optional)
dot_path = reg.export_graphviz("diabetes_tree", feature_names=features, format="dot")
print(f"Tree saved to {dot_path}")
# Export human‑readable rules (single trees only)
rules = reg.export_rules(feature_names=features)
print(rules[:3])
Performance tuning
Several hyperparameters influence model complexity and performance:
numeric_threshold_strategy('quantile'|'all'): subsample candidate numeric thresholds. With'quantile'the number of splits considered is limited tomax_numeric_thresholdsper feature per node.'all'evaluates every unique midpoint (slower on large datasets).max_numeric_thresholds: number of candidate thresholds when using'quantile'(typically 32–64).categorical_features: list of names or indices marking categorical columns.max_categories_exhaustive: maximum cardinality for exhaustive subset search on categorical features; beyond this a simpler one‑vs‑rest strategy is used.infer_categorical/int_as_categorical: enable automatic detection of categorical/boolean/integer columns when dtype information is not explicit.max_depth: optional depth limit for extremely noisy or deep trees.
When boosting (trials > 1) the same hyperparameters apply to each base tree.
Metadata
Release files for c50py 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| c50py-0.3.0.tar.gz | 45.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| c50py-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 85.7 kB
Release files / c50py-0.3.0.tar.gz
| Download URL | c50py-0.3.0.tar.gz |
|---|---|
| Size | 45.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b6c16b06128d479a951c45948c4619e829f7a128918938f8437efe1f02ebd000
|
|
BLAKE2b-256 checksum How to use checksums |
536f32d57ad5bbf3654f283199d0f51c451ea2f7dca2a13ff59f0afa05dfe822
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.7
|
Release files / c50py-0.3.0-py3-none-any.whl
| Download URL | c50py-0.3.0-py3-none-any.whl |
|---|---|
| Size | 40.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
4e55ccc2b3160aac7fb68c53bfbdad564c8ef5f37188fdef18af5650ec2634a3
|
|
BLAKE2b-256 checksum How to use checksums |
455811c75be05324c6b86d99a1929f928664b9c871c2cc5ff7b0868bd670b101
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.7
|