Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
Python/machine_learning/apriori_algorithm.py at master · leether/Python · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
leether
/
Python
Public
forked from
TheAlgorithms/Python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
master
Breadcrumbs
Python
/
machine_learning
/
apriori_algorithm.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
119 lines (93 loc) · 4.02 KB
master
Breadcrumbs
Python
/
machine_learning
/
apriori_algorithm.py
Copy path
Top
File metadata and controls
Code
Blame
119 lines (93 loc) · 4.02 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
"""
Apriori Algorithm is a Association rule mining technique, also known as market basket
analysis, aims to discover interesting relationships or associations among a set of
items in a transactional or relational database.
For example, Apriori Algorithm states: "If a customer buys item A and item B, then they
are likely to buy item C." This rule suggests a relationship between items A, B, and C,
indicating that customers who purchased A and B are more likely to also purchase item C.
WIKI: https://en.wikipedia.org/wiki/Apriori_algorithm
Examples: https://www.kaggle.com/code/earthian/apriori-association-rules-mining
"""
from
collections
import
Counter
from
itertools
import
combinations
def
load_data
()
->
list
[
list
[
str
]]:
"""
Returns a sample transaction dataset.
>>> load_data()
[['milk'], ['milk', 'butter'], ['milk', 'bread'], ['milk', 'bread', 'chips']]
"""
return
[[
"milk"
], [
"milk"
,
"butter"
], [
"milk"
,
"bread"
], [
"milk"
,
"bread"
,
"chips"
]]
def
prune
(
itemset
:
list
,
candidates
:
list
,
length
:
int
)
->
list
:
"""
Prune candidate itemsets that are not frequent.
The goal of pruning is to filter out candidate itemsets that are not frequent. This
is done by checking if all the (k-1) subsets of a candidate itemset are present in
the frequent itemsets of the previous iteration (valid subsequences of the frequent
itemsets from the previous iteration).
Prunes candidate itemsets that are not frequent.
>>> itemset = ['X', 'Y', 'Z']
>>> candidates = [['X', 'Y'], ['X', 'Z'], ['Y', 'Z']]
>>> prune(itemset, candidates, 2)
[['X', 'Y'], ['X', 'Z'], ['Y', 'Z']]
>>> itemset = ['1', '2', '3', '4']
>>> candidates = ['1', '2', '4']
>>> prune(itemset, candidates, 3)
[]
"""
itemset_counter
=
Counter
(
tuple
(
item
)
for
item
in
itemset
)
pruned
=
[]
for
candidate
in
candidates
:
is_subsequence
=
True
for
item
in
candidate
:
item_tuple
=
tuple
(
item
)
if
(
item_tuple
not
in
itemset_counter
or
itemset_counter
[
item_tuple
]
<
length
-
1
):
is_subsequence
=
False
break
if
is_subsequence
:
pruned
.
append
(
candidate
)
return
pruned
def
apriori
(
data
:
list
[
list
[
str
]],
min_support
:
int
)
->
list
[
tuple
[
list
[
str
],
int
]]:
"""
Returns a list of frequent itemsets and their support counts.
>>> data = [['A', 'B', 'C'], ['A', 'B'], ['A', 'C'], ['A', 'D'], ['B', 'C']]
>>> apriori(data, 2)
[(['A', 'B'], 1), (['A', 'C'], 2), (['B', 'C'], 2)]
>>> data = [['1', '2', '3'], ['1', '2'], ['1', '3'], ['1', '4'], ['2', '3']]
>>> apriori(data, 3)
[]
"""
itemset
=
[
list
(
transaction
)
for
transaction
in
data
]
frequent_itemsets
=
[]
length
=
1
while
itemset
:
# Count itemset support
counts
=
[
0
]
*
len
(
itemset
)
for
transaction
in
data
:
for
j
,
candidate
in
enumerate
(
itemset
):
if
all
(
item
in
transaction
for
item
in
candidate
):
counts
[
j
]
+=
1
# Prune infrequent itemsets
itemset
=
[
item
for
i
,
item
in
enumerate
(
itemset
)
if
counts
[
i
]
>=
min_support
]
# Append frequent itemsets (as a list to maintain order)
for
i
,
item
in
enumerate
(
itemset
):
frequent_itemsets
.
append
((
sorted
(
item
),
counts
[
i
]))
length
+=
1
itemset
=
prune
(
itemset
,
list
(
combinations
(
itemset
,
length
)),
length
)
return
frequent_itemsets
if
__name__
==
"__main__"
:
"""
Apriori algorithm for finding frequent itemsets.
Args:
data: A list of transactions, where each transaction is a list of items.
min_support: The minimum support threshold for frequent itemsets.
Returns:
A list of frequent itemsets along with their support counts.
"""
import
doctest
doctest
.
testmod
()
# user-defined threshold or minimum support level
frequent_itemsets
=
apriori
(
data
=
load_data
(),
min_support
=
2
)
print
(
"
\n
"
.
join
(
f"
{
itemset
}
:
{
support
}
"
for
itemset
,
support
in
frequent_itemsets
))
You can’t perform that action at this time.