Question: Complete the function, def prune_itemsets (root_item, itemsets, item_counts, min_count) : ... so that it implements Step 2 and Step 3 of the recommender. That is,
Complete the function,
def prune_itemsets(root_item, itemsets, item_counts, min_count): ... so that it implements Step 2andStep 3 of the recommender. That is, the inputs are:
- root_item: The root item (i.e., the root artist name)
- itemsets: A collection of itemsets
- item_counts: A pre-tabulated count of how many times each item appears in an itemset
- min_count: The minimum number of itemsets in which an item should appear to be considered a recommendation
Function should return the playlists pruned as follows:
- Filter the itemsets to only those containingroot_item. The resulting itemsets are thefiltereditemsets.
- For each filtered itemset, remove any item whereitem_counts[a] < min_count. However, donotremoveroot_item, regardless of its count.
- The resulting itemsets are thepruned itemsets.Discard any pruned itemsets that contain only the root item. Return the remaining pruned itemsets as a Python list of sets.
Note 0:Although the procedure above is written as though your function will modify its input arguments,Use copies as needed instead. The test cell will not pass if you modify the input arguments.
Note 1:You can return pruned itemsets in any order. (So if the test cell does not pass, it isnotbecause it assumes results in a particular order.)
Example.Suppose the itemsets and item counts are given as follows:
In[]:
ex6_demo_itemsets = [{'alicia keys', 'jay z', 'lucio battisti', 'the police'}, {'u2', 'the police'}, {'jay z'}] ex6_demo_item_counts = {'alicia keys': 1, 'jay z': 2, 'lucio battisti': 1, 'the police': 2, 'u2': 1}
Then
prune_itemsets('the police', ex6_demo_itemsets, ex6_demo_item_counts, 2) will end up returning a list with just one itemset,[{'the police', 'jay z'}]. That's because only two itemsets have'the police'in them, and of those, only one has at least one item whose count exceedsmin_count=2.
def prune_itemsets(root_item, itemsets, item_counts, min_count):
prune_itemsets('the police', ex6_demo_itemsets, ex6_demo_item_counts, 2) Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
