Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Your task. (basket_rules_test: 4 points). Your final task in this notebook is to mine this dataset for pairwise association rules. In particular, your code should

Your task. (basket_rules_test: 4 points). Your final task in this notebook is to mine this dataset for pairwise association rules. In particular, your code should produce (no pun intended!) a final dictionary, basket_rules, that meet these conditions (read carefully!):

  1. The keys are pairs (a,b), where a and b are item names (as strings).
  2. The values are the corresponding confidence scores, conf(a⇒b)
  3. Only include rules a⇒b where item a occurs at least MIN_COUNT times and conf(a⇒b) is at least THRESHOLD.

Pay particular attention to Condition 3: not only do you have to filter by a confidence threshold, but you must exclude rules a⇒b where the item a does not appear "often enough." There is a code cell below that defines values of MIN_COUNT and THRESHOLD, but your code should work even if we decide to change those values later on.

 

Your solution can use the groceries_file string variable defined above as its starting point. And since it's in the same notebook, you may, of course, reuse any of the code you've written above as needed. Lastly, if you feel you need additional code cells, you can create them after the code cell marked for your solution but before the code marked, ### TEST CODE ###.

 

 

My code is below but for some reason it is failing the test case which is also below. can ou help debug my solution

?

# Confidence threshold    THRESHOLD = 0.5        # Only consider rules for items appearing at least `MIN_COUNT` times.    MIN_COUNT = 10        #Normalize String    norm_groceries = groceries_file.lower()        #Get List     def get_lists(g):        grocery_lists = []        for basket in g.split('\n'):            grocery_lists.append(basket.split(','))        return grocery_lists        grocery_list = get_lists(norm_groceries)        #make itemset    def make_itemsets(grocery_list):        return [set(items) for items in grocery_list]                 item_set = make_itemsets(grocery_list)        #create default dictionary to update pair counts    from collections import defaultdict    from itertools import permutations        def update_food_pair_counts (food_pair_counts, item_set):        assert type (food_pair_counts) is defaultdict        for items in item_set:            unique_pairs = permutations(items,2)        for pair in unique_pairs:            food_pair_counts[pair] += 1            def update_food_item_counts(food_item_counts,item_set):        assert type (food_item_counts) is defaultdict        for items in item_set:            for item in items:                food_item_counts[item] += 1                    def filter_rules_by_conf_and_min_count(food_pairs_counts, food_item_counts, threshold, min_count):        rules = {}        for pair in food_pair_counts:            if food_item_counts[pair[0]] >= min_count:                confidence = food_pair_counts[pair]/ food_item_counts[pair[0]]                if confidence >= threshold:                    rules[pair] = confidence        return rules        def find_assoc_rules(itemized_shopping_lists, threshold, MIN_COUNT):        for r in receipts:            if len(r)>= 2:                pairs = permutations(r,2)                 update_food_pair_counts(food_pair_counts, r)                update_food_item_counts(food_item_counts, r)        return filter_rules_by_conf_and_min_count (food_pair_counts, food_item_counts,THRESHOLD, MIN_COUNT)          food_pair_counts = defaultdict(int)    food_item_counts = defaultdict(int)    shopping_lists = get_lists(groceries_file)    itemized_shopping_lists = make_itemsets(shopping_lists)    update_food_pair_counts(food_pair_counts, itemized_shopping_lists)    update_food_item_counts(food_item_counts, itemized_shopping_lists)    basket_rules = find_assoc_rules (itemized_shopping_lists, THRESHOLD, MIN_COUNT)


Test Code Below

### `basket_rules_test`: TEST CODE ###    print("Found {} rules whose confidence exceeds {}.".format(len(basket_rules), THRESHOLD))    print("Here they are:\n")    print_rules(basket_rules)        assert len(basket_rules) == 19    assert all([THRESHOLD <= v < 1.0 for v in basket_rules.values()])    ans_keys = [("pudding powder", "whole milk"), ("tidbits", "rolls/buns"), ("cocoa drinks", "whole milk"), ("cream", "sausage"), ("rubbing alcohol", "whole milk"), ("honey", "whole milk"), ("frozen fruits", "other vegetables"), ("cream", "other vegetables"), ("ready soups", "rolls/buns"), ("cooking chocolate", "whole milk"), ("cereals", "whole milk"), ("rice", "whole milk"), ("specialty cheese", "other vegetables"), ("baking powder", "whole milk"), ("rubbing alcohol", "butter"), ("rubbing alcohol", "citrus fruit"), ("jam", "whole milk"), ("frozen fruits", "whipped/sour cream"), ("rice", "other vegetables")]    for k in ans_keys:        assert k in basket_rules        print("\n(Passed!)")

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Business Communication Essentials a skill based approach

Authors: Courtland L. Bovee, John V. Thill

6th edition

978-0132971324

More Books

Students also viewed these Algorithms questions

Question

Why is proofreading an important part of the writing process?

Answered: 1 week ago

Question

=+yni for each i).

Answered: 1 week ago