Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - issue with using a list of frozenset entries in a for loop

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.

like image 752
user3755648 Avatar asked Feb 11 '16 10:02

user3755648


People also ask

Why is a Frozenset () different from a regular set?

Frozen set is just an immutable version of a Python set object. While elements of a set can be modified at any time, elements of the frozen set remain the same after creation. Due to this, frozen sets can be used as keys in Dictionary or as elements of another set.

How do you convert a Frozenset to a list?

3) Convert a frozenset to a list. Python frozenset object is an immutable unordered collection of data elements. Therefore, you cannot modify the elements of the frozenset. To convert this set into a list, you have to use the list function and pass the set as a parameter to get the list object as an output.

Is Frozenset mutable in Python?

Python frozenset() Method creates an immutable Set object from an iterable. It is a built-in Python function. As it is a set object therefore we cannot have duplicate values in the frozenset.

What kind of parameter need to pass for Frozenset function in the Python?

Parameters of frozenset in Python The frozenset() function accepts only one parameter, i.e., an iterable (list, tuple, string, dictionary, or a set).


1 Answers

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 to itertools.imap in python2.x.

To solve the issue, use

 Ck2=list(map(frozenset, Ck)))

and see Getting a map() to return a list in Python 3.x for more information and other solutions.

like image 158
jofel Avatar answered Sep 23 '22 03:09

jofel