Add Apriori Algorithm for Association Rule Mining

### Describe the workflow you want to enable

I would like scikit-learn to support association rule mining by implementing the Apriori algorithm. This would allow users to efficiently find frequent itemsets and generate association rules directly within the scikit-learn framework, similar to other supervised and unsupervised learning algorithms.

Use cases include market basket analysis, recommendation systems, and pattern discovery in categorical datasets. The workflow would involve:

-  Inputting transactional or categorical data.

- Generating frequent itemsets with a minimum support threshold.

- Producing association rules with metrics such as confidence, lift, and leverage.

### Describe your proposed solution

Implement an Apriori class or function in the sklearn.preprocessing or sklearn.association module that provides:

- fit() to generate frequent itemsets.

- generate_rules() to produce association rules.

- Support for configurable metrics: support, confidence, lift, leverage.

- Compatibility with pandas DataFrames and NumPy arrays.

### Describe alternatives you've considered, if relevant

Currently, Python libraries like mlxtend
 provide Apriori and association rule mining. However, integrating it into scikit-learn would:

- Ensure consistent API with scikit-learn estimators.

- Enable seamless integration with pipelines and transformers.

- Benefit users who prefer a single framework for all ML tasks.

### Additional context

- Association rule mining is a common unsupervised learning task, widely used in industry.

- Many users are already using mlxtend alongside scikit-learn, but a native implementation would improve performance, maintainability, and ecosystem consistency.

COMMENT:
Hi @virchan,

Thank you for reviewing my proposal. I’d like to provide additional justification for why an Apriori implementation could be a valuable addition to scikit-learn:

1. Relevance to scikit-learn

Association rule mining is a standard unsupervised learning task, similar to clustering or dimensionality reduction, and is widely used in market basket analysis, recommendation systems, and pattern discovery in categorical datasets.

Currently, users rely on external libraries like mlxtend for Apriori, which breaks consistency with scikit-learn’s API. A native implementation would allow seamless integration into pipelines and workflows.

2. Proposed API

The API is designed to follow scikit-learn conventions:

```
from sklearn.association import Apriori

transactions = [
    ['milk', 'bread', 'eggs'],
    ['milk', 'bread'],
    ['bread', 'butter'],
]

apriori = Apriori(min_support=0.5, min_confidence=0.7)
apriori.fit(transactions)
rules = apriori.generate_rules()
print(rules)
```

- fit() generates frequent itemsets.
 
- generate_rules() produces association rules with configurable metrics: support, confidence, lift, and leverage.

- Compatible with pandas DataFrames and NumPy arrays.

3. Maintenance and simplicity

Apriori is conceptually simple, stable, and widely used in both academia and industry.

It can be fully tested with small datasets, ensuring correctness.

Implementation would rely only on NumPy/pandas, keeping dependencies minimal.

References

R. Agrawal and R. Srikant, “Fast Algorithms for Mining Association Rules”, 1994.

mlxtend implementation: https://rasbt.github.io/mlxtend/user_guide/frequent_patterns/apriori/

In summary, Apriori fits scikit-learn’s goals: it is a general unsupervised algorithm, integrates well with existing workflows, and can be maintained efficiently.

I can provide more details if needed and I’m also willing to contribute by helping implement this algorithm for scikit-learn.

Thank you for considering this!

Labels: New Feature, Needs Info, Needs Triage