diff --git a/Part 5 - Association Rule Learning/Section 28 - Apriori/Apriori_Python/apyori.py b/Part 5 - Association Rule Learning/Section 28 - Apriori/Apriori_Python/apyori.py index 564b896..3acab1e 100755 --- a/Part 5 - Association Rule Learning/Section 28 - Apriori/Apriori_Python/apyori.py +++ b/Part 5 - Association Rule Learning/Section 28 - Apriori/Apriori_Python/apyori.py @@ -371,71 +371,3 @@ def load_transactions(input_file, **kwargs): delimiter = kwargs.get('delimiter', '\t') for transaction in csv.reader(input_file, delimiter=delimiter): yield transaction if transaction else [''] - - -def dump_as_json(record, output_file): - """ - Dump an relation record as a json value. - - Arguments: - record -- A RelationRecord instance to dump. - output_file -- A file to output. - """ - 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]) - json.dump( - converted_record._asdict(), output_file, - default=default_func, ensure_ascii=False) - output_file.write(os.linesep) - - -def dump_as_two_item_tsv(record, output_file): - """ - Dump a relation record as TSV only for 2 item relations. - - Arguments: - record -- A RelationRecord instance to dump. - output_file -- A file to output. - """ - for ordered_stats in record.ordered_statistics: - if len(ordered_stats.items_base) != 1: - continue - if len(ordered_stats.items_add) != 1: - continue - output_file.write('{0}\t{1}\t{2:.8f}\t{3:.8f}\t{4:.8f}{5}'.format( - list(ordered_stats.items_base)[0], list(ordered_stats.items_add)[0], - record.support, ordered_stats.confidence, ordered_stats.lift, - os.linesep)) - - -def main(**kwargs): - """ - Executes Apriori algorithm and print its result. - """ - # For tests. - _parse_args = kwargs.get('_parse_args', parse_args) - _load_transactions = kwargs.get('_load_transactions', load_transactions) - _apriori = kwargs.get('_apriori', apriori) - - args = _parse_args(sys.argv[1:]) - transactions = _load_transactions( - chain(*args.input), delimiter=args.delimiter) - 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() \ No newline at end of file