/
apyori.py
executable file
·329 lines (278 loc) · 10.1 KB
/
apyori.py
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/usr/bin/env python
"""
Implementation of Apriori algorithm.
"""
import sys
import argparse
import json
from collections import namedtuple
from itertools import combinations
from itertools import chain
__version__ = '0.9.0'
__author__ = 'ymoch'
__author_email__ = 'ymoch@githib.com'
# Ignore name errors because these names are namedtuples.
SupportRecord = namedtuple( # pylint: disable=C0103
'SupportRecord', ('items', 'support'))
RelationRecord = namedtuple( # pylint: disable=C0103
'RelationRecord', SupportRecord._fields + ('ordered_statistics',))
OrderedStatistic = namedtuple( # pylint: disable=C0103
'OrderedStatistic', ('items_base', 'items_add', 'confidence', 'lift',))
class TransactionManager(object):
"""
Transaction manager class.
"""
def __init__(self, transactions):
"""
Initialize.
@param transactions A transaction list.
"""
self.__num_transaction = 0
self.__items = []
self.__transaction_index_map = {}
for transaction in transactions:
self.add_transaction(transaction)
def add_transaction(self, transaction):
"""
Add a transaction.
@param transaction A transaction.
"""
for item in transaction:
if item not in self.__transaction_index_map:
self.__items.append(item)
self.__transaction_index_map[item] = set()
self.__transaction_index_map[item].add(self.__num_transaction)
self.__num_transaction += 1
def calc_support(self, items):
"""
Returns a support for items.
"""
# Empty items is supported by all transactions.
if not items:
return 1.0
# Create the transaction index intersection.
sum_indexes = None
for item in items:
indexes = self.__transaction_index_map.get(item)
if indexes is None:
# No support for any set that contains a not existing item.
return 0.0
if sum_indexes is None:
sum_indexes = indexes
else:
sum_indexes = sum_indexes.intersection(indexes)
# Calculate the support.
return float(len(sum_indexes)) / self.__num_transaction
def initial_candidates(self):
"""
Returns the initial candidates.
"""
return [frozenset([item]) for item in self.__items]
@property
def num_transaction(self):
"""
Returns the number of transactions.
"""
return self.__num_transaction
@property
def items(self):
"""
Returns the item list that the transaction is consisted of.
"""
return self.__items
@staticmethod
def create(transactions):
"""
Create the TransactionManager with a transaction instance.
If the given instance is a TransactionManager, this returns itself.
"""
if isinstance(transactions, TransactionManager):
return transactions
return TransactionManager(transactions)
def create_next_candidates(prev_candidates, length):
"""
Returns the apriori candidates.
"""
# Solve the items.
items = set()
for candidate in prev_candidates:
for item in candidate:
items.add(item)
def check_subsets(candidate):
"""
Check if the subsets of a candidate is present
in the previous candidates.
"""
candidate_subsets = [
frozenset(x) for x in combinations(candidate, length - 1)]
for candidate_subset in candidate_subsets:
if candidate_subset not in prev_candidates:
return False
return True
# Create candidates.
next_candidates = []
for candidate in [frozenset(x) for x in combinations(items, length)]:
if length > 2 and not check_subsets(candidate):
continue
next_candidates.append(candidate)
return next_candidates
def gen_support_records(
transaction_manager,
min_support,
max_length=None,
_generate_candidates_func=create_next_candidates):
"""
Returns a generator of support records with given transactions.
"""
candidates = transaction_manager.initial_candidates()
length = 1
while candidates:
relations = set()
for relation_candidate in candidates:
support = transaction_manager.calc_support(relation_candidate)
if support < min_support:
continue
candidate_set = frozenset(relation_candidate)
relations.add(candidate_set)
yield SupportRecord(candidate_set, support)
length += 1
if max_length and length > max_length:
break
candidates = _generate_candidates_func(relations, length)
def gen_ordered_statistics(transaction_manager, record):
"""
Returns a generator of ordered statistics.
"""
items = record.items
combination_sets = [
frozenset(x) for x in combinations(items, len(items) - 1)]
for items_base in combination_sets:
items_add = frozenset(items.difference(items_base))
confidence = (
record.support / transaction_manager.calc_support(items_base))
lift = confidence / transaction_manager.calc_support(items_add)
yield OrderedStatistic(
frozenset(items_base), frozenset(items_add), confidence, lift)
def apriori(transactions, **kwargs):
"""
Run Apriori algorithm.
@param transactions A list of transactions.
@param min_support The minimum support of the relation (float).
@param max_length The maximum length of the relation (integer).
"""
min_support = kwargs.get('min_support', 0.1)
max_length = kwargs.get('max_length', None)
min_confidence = kwargs.get('min_confidence', 0.0)
_gen_support_records = kwargs.get(
'_gen_support_records', gen_support_records)
_gen_ordered_statistics = kwargs.get(
'_gen_ordered_statistics', gen_ordered_statistics)
# Calculate supports.
transaction_manager = TransactionManager.create(transactions)
support_records = _gen_support_records(
transaction_manager, min_support, max_length)
# Calculate ordered stats.
for support_record in support_records:
ordered_statistics = _gen_ordered_statistics(
transaction_manager, support_record)
filtered_ordered_statistics = [
x for x in ordered_statistics if x.confidence >= min_confidence]
if not filtered_ordered_statistics:
continue
yield RelationRecord(
support_record.items, support_record.support,
filtered_ordered_statistics)
def dump_as_json(record, output_file):
"""
Dump an relation record as a json value.
@param record A record.
@param output_file An output file.
"""
def default_func(value):
"""
Default conversion for JSON value.
"""
if isinstance(value, frozenset):
return sorted(value)
raise TypeError(repr(value) + " is not JSON serializable")
converted_record = record._replace(
ordered_statistics=[x._asdict() for x in record.ordered_statistics])
output_file.write(
json.dumps(converted_record._asdict(), default=default_func))
output_file.write('\n')
def dump_as_two_item_tsv(record, output_file):
"""
Dump a relation record as TSV only for 2 item relations.
@param record A record.
@param output_file An output file.
"""
for ordered_stats in record.ordered_statistics:
if len(ordered_stats.items_base) != 1:
return
if len(ordered_stats.items_add) != 1:
return
output_file.write('{0}\t{1}\t{2:.8f}\t{3:.8f}\t{4:.8f}\n'.format(
list(ordered_stats.items_base)[0], list(ordered_stats.items_add)[0],
record.support, ordered_stats.confidence, ordered_stats.lift))
def parse_args(argv):
"""
Parse commandline arguments.
"""
output_funcs = {
'json': dump_as_json,
'tsv': dump_as_two_item_tsv,
}
default_output_func_key = 'json'
parser = argparse.ArgumentParser()
parser.add_argument(
'-v', '--version', action='version',
version='%(prog)s {0}'.format(__version__))
parser.add_argument(
'input', metavar='inpath', nargs='*',
help='Input transaction file (default: stdin).',
type=argparse.FileType('r'), default=[sys.stdin])
parser.add_argument(
'-o', '--output', metavar='outpath',
help='Output file (default: stdout).',
type=argparse.FileType('w'), default=sys.stdout)
parser.add_argument(
'-l', '--max-length', metavar='int',
help='Max length of relations (default: infinite).',
type=int, default=None)
parser.add_argument(
'-s', '--min-support', metavar='float',
help='Minimum support ratio (must be > 0, default: 0.1).',
type=float, default=0.1)
parser.add_argument(
'-c', '--min-confidence', metavar='float',
help='Minimum confidence (default: 0.5).',
type=float, default=0.5)
parser.add_argument(
'-d', '--delimiter', metavar='str',
help='Delimiter for items of transactions (default: tab).',
type=str, default='\t')
parser.add_argument(
'-f', '--out-format', help='Output format (default or tsv).',
metavar='str', type=str, choices=output_funcs.keys(),
default=default_output_func_key)
args = parser.parse_args(argv)
if args.min_support <= 0:
raise ValueError('min support must be > 0')
args.output_func = output_funcs[args.out_format]
return args
def main():
"""
Main.
"""
args = parse_args(sys.argv[1:])
transactions = [
line.strip().split(args.delimiter) for line in chain(*args.input)]
result = apriori(
transactions,
max_length=args.max_length,
min_support=args.min_support,
min_confidence=args.min_confidence)
for record in result:
args.output_func(record, args.output)
if __name__ == '__main__':
main()