-
Notifications
You must be signed in to change notification settings - Fork 194
FIX : upgrade python 2.x to 3.x #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yeonsssu26
wants to merge
2
commits into
enaeseth:master
Choose a base branch
from
yeonsssu26:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,11 +10,13 @@ | |
| """ | ||
|
|
||
| from collections import defaultdict, namedtuple | ||
| from itertools import imap | ||
|
|
||
| __author__ = 'Eric Naeseth <[email protected]>' | ||
| __copyright__ = 'Copyright © 2009 Eric Naeseth' | ||
| __license__ = 'MIT License' | ||
| # from itertools import imap | ||
|
|
||
| __author__ = "Eric Naeseth <[email protected]>" | ||
| __copyright__ = "Copyright © 2009 Eric Naeseth" | ||
| __license__ = "MIT License" | ||
|
|
||
|
|
||
| def find_frequent_itemsets(transactions, minimum_support, include_support=False): | ||
| """ | ||
|
|
@@ -31,7 +33,7 @@ def find_frequent_itemsets(transactions, minimum_support, include_support=False) | |
| If `include_support` is true, yield (itemset, support) pairs instead of | ||
| just the itemsets. | ||
| """ | ||
| items = defaultdict(lambda: 0) # mapping from items to their supports | ||
| items = defaultdict(lambda: 0) # mapping from items to their supports | ||
|
|
||
| # Load the passed-in transactions and count the support that individual | ||
| # items have. | ||
|
|
@@ -40,19 +42,25 @@ def find_frequent_itemsets(transactions, minimum_support, include_support=False) | |
| items[item] += 1 | ||
|
|
||
| # Remove infrequent items from the item support dictionary. | ||
| items = dict((item, support) for item, support in items.iteritems() | ||
| if support >= minimum_support) | ||
| items = dict( | ||
| (item, support) | ||
| # for item, support in items.iteritems() | ||
| for item, support in items.items() | ||
| if support >= minimum_support | ||
| ) | ||
|
|
||
| # Build our FP-tree. Before any transactions can be added to the tree, they | ||
| # must be stripped of infrequent items and their surviving items must be | ||
| # sorted in decreasing order of frequency. | ||
| def clean_transaction(transaction): | ||
| transaction = filter(lambda v: v in items, transaction) | ||
| transaction.sort(key=lambda v: items[v], reverse=True) | ||
| # transaction.sort(key=lambda v: items[v], reverse=True) | ||
| transaction = sorted(transaction, key=lambda v: items[v], reverse=True) | ||
| return transaction | ||
|
|
||
| master = FPTree() | ||
| for transaction in imap(clean_transaction, transactions): | ||
| # for transaction in imap(clean_transaction, transactions): | ||
| for transaction in list(map(clean_transaction, transactions)): | ||
| master.add(transaction) | ||
|
|
||
| def find_with_suffix(tree, suffix): | ||
|
|
@@ -67,12 +75,13 @@ def find_with_suffix(tree, suffix): | |
| # itemsets within it. | ||
| cond_tree = conditional_tree_from_paths(tree.prefix_paths(item)) | ||
| for s in find_with_suffix(cond_tree, found_set): | ||
| yield s # pass along the good news to our caller | ||
| yield s # pass along the good news to our caller | ||
|
|
||
| # Search for frequent itemsets, and yield the results we find. | ||
| for itemset in find_with_suffix(master, []): | ||
| yield itemset | ||
|
|
||
|
|
||
| class FPTree(object): | ||
| """ | ||
| An FP tree. | ||
|
|
@@ -81,7 +90,7 @@ class FPTree(object): | |
| (i.e., all items must be valid as dictionary keys or set members). | ||
| """ | ||
|
|
||
| Route = namedtuple('Route', 'head tail') | ||
| Route = namedtuple("Route", "head tail") | ||
|
|
||
| def __init__(self): | ||
| # The root node of the tree. | ||
|
|
@@ -124,7 +133,7 @@ def _update_route(self, point): | |
|
|
||
| try: | ||
| route = self._routes[point.item] | ||
| route[1].neighbor = point # route[1] is the tail | ||
| route[1].neighbor = point # route[1] is the tail | ||
| self._routes[point.item] = self.Route(route[0], point) | ||
| except KeyError: | ||
| # First node for this item; start a new route. | ||
|
|
@@ -136,7 +145,8 @@ def items(self): | |
| element of the tuple is the item itself, and the second element is a | ||
| generator that will yield the nodes in the tree that belong to the item. | ||
| """ | ||
| for item in self._routes.iterkeys(): | ||
| # for item in self._routes.iterkeys(): | ||
| for item in self._routes: | ||
| yield (item, self.nodes(item)) | ||
|
|
||
| def nodes(self, item): | ||
|
|
@@ -167,15 +177,16 @@ def collect_path(node): | |
| return (collect_path(node) for node in self.nodes(item)) | ||
|
|
||
| def inspect(self): | ||
| print 'Tree:' | ||
| print("Tree:") | ||
| self.root.inspect(1) | ||
|
|
||
| print 'Routes:' | ||
| print() | ||
| print("Routes:") | ||
| for item, nodes in self.items(): | ||
| print ' %r' % item | ||
| print(" %r" % item) | ||
| for node in nodes: | ||
| print ' %r' % node | ||
| print(" %r" % node) | ||
|
|
||
|
|
||
| def conditional_tree_from_paths(paths): | ||
| """Build a conditional FP-tree from the given prefix paths.""" | ||
|
|
@@ -212,6 +223,7 @@ def conditional_tree_from_paths(paths): | |
|
|
||
| return tree | ||
|
|
||
|
|
||
| class FPNode(object): | ||
| """A node in an FP tree.""" | ||
|
|
||
|
|
@@ -312,7 +324,7 @@ def children(self): | |
| return tuple(self._children.itervalues()) | ||
|
|
||
| def inspect(self, depth=0): | ||
| print (' ' * depth) + repr(self) | ||
| print((" " * depth) + repr(self)) | ||
| for child in self.children: | ||
| child.inspect(depth + 1) | ||
|
|
||
|
|
@@ -322,21 +334,31 @@ def __repr__(self): | |
| return "<%s %r (%r)>" % (type(self).__name__, self.item, self.count) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| if __name__ == "__main__": | ||
| from optparse import OptionParser | ||
| import csv | ||
|
|
||
| p = OptionParser(usage='%prog data_file') | ||
| p.add_option('-s', '--minimum-support', dest='minsup', type='int', | ||
| help='Minimum itemset support (default: 2)') | ||
| p.add_option('-n', '--numeric', dest='numeric', action='store_true', | ||
| help='Convert the values in datasets to numerals (default: false)') | ||
| p = OptionParser(usage="%prog data_file") | ||
| p.add_option( | ||
| "-s", | ||
| "--minimum-support", | ||
| dest="minsup", | ||
| type="int", | ||
| help="Minimum itemset support (default: 2)", | ||
| ) | ||
| p.add_option( | ||
| "-n", | ||
| "--numeric", | ||
| dest="numeric", | ||
| action="store_true", | ||
| help="Convert the values in datasets to numerals (default: false)", | ||
| ) | ||
| p.set_defaults(minsup=2) | ||
| p.set_defaults(numeric=False) | ||
|
|
||
| options, args = p.parse_args() | ||
| if len(args) < 1: | ||
| p.error('must provide the path to a CSV file to read') | ||
| p.error("must provide the path to a CSV file to read") | ||
|
|
||
| transactions = [] | ||
| with open(args[0]) as database: | ||
|
|
@@ -351,8 +373,8 @@ def __repr__(self): | |
|
|
||
| result = [] | ||
| for itemset, support in find_frequent_itemsets(transactions, options.minsup, True): | ||
| result.append((itemset,support)) | ||
| result.append((itemset, support)) | ||
|
|
||
| result = sorted(result, key=lambda i: i[0]) | ||
| for itemset, support in result: | ||
| print str(itemset) + ' ' + str(support) | ||
| print(str(itemset) + " " + str(support)) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Recommend we either retain old English explanation or add a section in English translated from the Korean above.