Skip to main content

rulewise

Repository: https://github.com/ShadowGodd1/Rulewise Documentation: https://shadowgodd1.github.io/Rulewise/

Association rule mining you can trust: every rule that comes back has survived exact statistical testing, correction for the number of rules tried, and a test that it still beats simpler versions of itself. The library also reports how far to trust the result.

Classical rule mining returns thousands of rules ranked by support and confidence. Many are redundant, explained by a simpler rule; on pure noise, all of them are chance. rulewise mines the candidates, tests each one with an exact test, corrects for the number of rules tried, drops rules whose extra antecedent items add nothing over a simpler version of the same rule, and reports validation metrics. On data with no real structure it almost never returns a rule: across 1,800 pure-noise datasets it reported a spurious rule in at most 1.7% of them (nominal level 5%).

The guarantee is conservative. The price of almost never reporting a spurious rule is that weak associations need more data to be detected, and recall is lower than a classical list. See "Limitations" below and docs/limitations.md.

Install

pip install rulewise             # core, pure-Python Eclat
pip install "rulewise[sklearn]"  # plus scikit-learn integration
pip install "rulewise[plot]"     # plus matplotlib plots

PyFIM is an optional legacy C backend (backend="pyfim"). It has no wheel for modern Python, so it is not installed by default. Install it manually if your platform can build it; the pure-Python backend is the default.

Find rules

from rulewise import RuleMiner

baskets = [["milk", "bread", "butter"], ["beer", "chips"], ...]   # or a 0/1 matrix,
                                                                  # scipy sparse, pandas, polars
miner = RuleMiner(min_support=0.01).fit(baskets)

print(miner.summary())
miner.rules_                       # DataFrame of significant rules and their measures
miner.query(min_lift=2, contains="milk", sort_by="lift", top=10)
miner.recommend(["milk"], k=5, explain=True)
miner.export("rules.csv")          # .csv, .json or .parquet
miner.to_polars()                  # requires polars
miner.plot("effect")               # also "forest" and "network"; requires matplotlib

RuleMiner splits the data by default: it finds candidates on one part and tests and measures them on the other (mode="holdout"). That makes the reported confidence, lift and intervals honest. Set mode="direct" to use all data and Webb's layered critical values; direct mode is usually more powerful on small data, but its effect sizes are measured on the same data that selected the rules and are therefore optimistic. See "Modes" below.

What comes back

miner.rules_ contains one row per significant rule:

column meaning
antecedent, consequent tuples of item names
support share of transactions containing antecedent and consequent
confidence P(consequent present given antecedent present)
lift confidence divided by the consequent's base rate
leverage support minus the support expected under independence
conviction how much more confident the rule is than a rule with no antecedent
count, count_antecedent, count_consequent raw counts
p_value exact productivity p-value
p_adjusted p-value after the chosen correction
confidence_low, confidence_high Wilson interval on confidence
lift_low, lift_high bootstrap interval on lift (when n_bootstrap > 0)
leverage_low, leverage_high bootstrap interval on leverage (when n_bootstrap > 0)

In holdout mode all measures are computed on the holdout part. In direct mode they are computed on the full data and are in-sample estimates.

Validate the pipeline

report = miner.validate(baskets, n_null=50, n_stability=20)
print(report)

validate refits the pipeline on shuffled data with the same item frequencies. A sound pipeline returns almost nothing there; the share of null datasets with at least one rule estimates the empirical family-wise error rate. It also reports holdout replication and, if n_stability > 0, how stable the rule set is across random subsamples. The result is a ValidationReport, a dict with a readable summary().

Classify with rules

from rulewise import RuleClassifier

clf = RuleClassifier(min_support=0.01).fit(X, y)   # X: transactions, 0/1 matrix or sparse
clf.predict(X_new)
clf.predict_proba(X_new)
clf.score(X_test, y_test)
clf.explain(X_new[:1])                              # the rule behind each prediction
clf.validate(X, y, cv=5)                            # permuted-label null plus cross-validation

RuleClassifier mines rules of the form antecedent -> class, keeps only significant ones, and predicts with the best matching rule ordered by the Wilson lower bound on confidence. When no rule matches, it returns the class priors. It works in scikit-learn pipelines and cross-validation.

Numeric features must be binned first, for example with KBinsDiscretizer(encode="onehot"). add_negations=True adds a ("not", item) item for every feature a row lacks, so rules can express "feature absent".

How it works

  1. Mine frequent itemsets with a bitset Eclat implementation, or PyFIM when installed.
  2. Generate candidates X -> Y with confidence above min_confidence and lift above 1.
  3. Test productivity. A rule must beat every simpler version of itself: for every proper subset Z of X, adding X - Z must raise the rate of Y. Each comparison is a one-sided Fisher exact test; the rule's p-value is the maximum over comparisons, so a rule is significant only if all comparisons are. Rules with multi-item consequents must also show positive dependence across every bipartition of the itemset.
  4. Correct for multiple testing. Holm or Bonferroni control the family-wise error rate; Benjamini-Hochberg or Benjamini-Yekutieli control the false discovery rate.
  5. Estimate honestly. Holdout mode finds rules on one part and tests and measures them on the other. Direct mode uses all data and Webb's layered critical values.

Modes

mode where candidates are found where they are tested and measured use when
holdout (default for RuleMiner) exploration part independent holdout part you need honest effect sizes and intervals
direct (default for RuleClassifier) all data all data, with layered critical values data is small, or you need maximum power, and you accept optimistic effect sizes

In direct mode, alpha is split equally across rule sizes and Bonferroni-corrected within each size over the full search space. The correction parameter is ignored. In holdout mode, holdout_fraction=0 disables the split; p-values are then not valid and a warning is raised.

Statistical guarantees and assumptions

The guarantee is family-wise or false-discovery control over the rules actually tested, under these assumptions:

  • transactions are independent and identically distributed;
  • the null model is independence between the items in a rule;
  • in holdout mode, the holdout part is independent of the part used to select candidates;
  • in direct mode, the correction covers the full search space, not only the candidates that happened to pass the other filters.

The testing is conservative, which is why the null-data error rate is well below 5%. It does not model dependence caused by basket size or by other variables. Items can be associated only because some baskets are larger, or because a third variable drives both, and such rules can be reported. It also does not establish causation. See docs/methodology.md and docs/limitations.md.

Input formats

RuleMiner and RuleClassifier accept:

  • an iterable of item collections, including a generator;
  • a 2D numpy array or a 0/1 matrix (any numeric dtype, nonzero means present);
  • a scipy sparse matrix;
  • a pandas DataFrame (dense or sparse one-hot) or a Series of lists;
  • a polars DataFrame (one-hot or a single list column) or a Series of lists.

Use item_names to label matrix columns with strings. Strings as transactions are rejected because a single string is ambiguous; split or encode them first. Matrix input rejects NaN, infinity, complex numbers and zero-width data.

When to use it, and when not to

Good for:

  • sparse transaction-style or categorical data where a few rules carry the structure;
  • explainable models and auditable discovery;
  • separating real associations from noise in small or exploratory samples;
  • reporting an interval and a p-value with every rule.

Weak for:

  • signal spread thinly over many dense numeric features; bin first, and expect low accuracy compared with regularized linear models or trees;
  • maximum recall: a classical list contains more true rules, mixed with more redundancy;
  • recommendation hit rate: a popularity baseline can beat rule-based recommendations;
  • negative associations, closed or maximal itemsets, constrained mining, and the full metric catalogue that mlxtend provides.

Measured results and the head-to-head against mlxtend are in benchmarks/COMPARISON.md. The methodology and limits are in docs/methodology.md and docs/limitations.md.

Documentation

  • docs/index.md: documentation home.
  • docs/api.md: parameters, attributes, methods and output columns.
  • docs/methodology.md: the statistical method and its assumptions.
  • docs/limitations.md: known failure modes and power.
  • docs/comparison.md: rulewise compared with existing Python libraries.
  • docs/tutorials/groceries.md: a real supermarket basket walkthrough.
  • docs/tutorials/classification.md: rules as a classifier on Mushrooms and Titanic.
  • docs/publishing.md: release steps.

Development

pip install -e ".[dev]"
python benchmarks/get_data.py
python benchmarks/library_comparison.py --section all --runs 500 --seeds 5
python -m pytest

The test suite checks p-values and corrections against scipy and statsmodels, itemsets against mlxtend and across backends, error control by simulation on null data, and recovery of planted rules. Optional-dependency tests skip when the dependency is absent, so the core install is tested without PyFIM, scikit-learn, matplotlib, mlxtend, statsmodels or polars.

References

  • Webb, G.I. (2007). Discovering significant patterns. Machine Learning 68(1).
  • Webb, G.I. (2008). Layered critical values: a powerful direct-adjustment approach to discovering significant patterns. Machine Learning 71.
  • Hamalainen, W. and Webb, G.I. (2019). A tutorial on statistically sound pattern discovery. Data Mining and Knowledge Discovery 33.
  • Webb, G.I. (2010). Self-sufficient itemsets. ACM TKDD 4(1).
  • Holm (1979); Benjamini and Hochberg (1995); Benjamini and Yekutieli (2001).
  • Borgelt, C. PyFIM and Eclat (MIT licence).

License

MIT. See LICENSE.

Metadata

Release files for rulewise 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 rulewise 0.1.0
File Size Uploaded Provenance Actions
rulewise-0.1.0.tar.gz 336.9 kB With provenance Details for rulewise-0.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 370.7 kB

Release files / rulewise-0.1.0.tar.gz

Download URL rulewise-0.1.0.tar.gz
Size 336.9 kB
Tags Source
SHA-256 checksum
How to use checksums
6c9e4cf9181aa8f6590c9edb6d5c22fc0ae54fccf1d4b886d6b6fb531db71bc6
BLAKE2b-256 checksum
How to use checksums
25fae9cdb1064a5652d544f8117864d3e8cff495bb904f434b9488107185691e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 12, 2026.

Transparency log

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

Download URL rulewise-0.1.0-py3-none-any.whl
Size 33.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
27f7c7dfd2a445821a03585b56eda8c3d0e61b73a5b946cb719c70c8bb68cc36
BLAKE2b-256 checksum
How to use checksums
3a8cc740a593a70864e56bf20ead354ee318689bd3b45435de941d636ca487bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 12, 2026.

Transparency log

Security for rulewise 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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for rulewise-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 12, 2026.

Transparency log

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 Full 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