I am trying to learn the apriori machine learning algorithm from a book that uses Python, and as part of that learning, I am currently stuck with this following problem:
The following code construct seems to work fine:
Ck = [[1], [2], [3], [4], [5]]
for tranid in range(10):
for candidate in Ck:
print("Printing candidate value: ", candidate)
However, the following does not work:
Ck = [[1], [2], [3], [4], [5]]
Ck2 = map(frozenset, Ck)
for tranid in range(10):
for candidate in Ck2:
print("Printing candidate value: ", candidate)
When I map every element of my original iterable to a frozenset, I notice that the inner loop ("for candidate in Ck2") executes only once. After that it never executes. The code above without the frozenset properly loops through the inner loop 10 times. However, with frozenset mapped, I can get the inner loop to execute only once.
Please help me with fixing this. The book has mapped the iterable values to frozenset because they don't want it to be mutable for the purposes of the algorithm. I am simply trying to follow it as is.
I am using Python 3.5.1 on Anaconda (Spyder).
Please help, as I am new to both Python and Machine Learning.
Thanks and Regards, Mahesh.
The map operator does not return a list in python3 which you can iterate repeatily, but a one-time-iterable iterator. In python3.x,
map
works similar toitertools.imap
in python2.x.To solve the issue, use
and see Getting a map() to return a list in Python 3.x for more information and other solutions.