data-science-from-scratch icon indicating copy to clipboard operation
data-science-from-scratch copied to clipboard

function calls mismatched dict names

Open iphyer opened this issue 10 years ago • 2 comments

I have been reproduced your code and it failed here and I find that the code below is mismatched in dict names

from collections import defaultdict

# keys are interests, values are lists of user_ids with that interest
user_ids_by_interest = defaultdict(list)

for user_id, interest in interests:
    user_ids_by_interest[interest].append(user_id)

# keys are user_ids, values are lists of interests for that user_id
interests_by_user_id = defaultdict(list)

for user_id, interest in interests:
    interests_by_user_id[user_id].append(interest)

def most_common_interests_with(user_id):
    return Counter(interested_user_id
        for interest in interests_by_user["user_id"]   
        for interested_user_id in users_by_interest[interest]
        if interested_user_id != user_id)

Generally, the problem happens in function most_common_interests_with(user_id) and when most_common_interests_with(user_id) calls dict above it shoulbe be user_ids_by_interest not users_by_interest and interests_by_user_id not interests_by_user.

But even if I have rewritten the code the code above produce no results

iphyer avatar Jun 15 '15 15:06 iphyer

The output is like this:

most_common_interests_with(1)
Out[88]: Counter()

Which is quit strange!

iphyer avatar Jun 15 '15 15:06 iphyer

Indeed, the right code should be the following format:

def most_common_interests_with(user_id):
    return Counter(interested_user_id
        for interest in interests_by_user_id[user_id]   
        for interested_user_id in user_ids_by_interest[interest]
        if interested_user_id != user_id)

iphyer avatar Jun 17 '15 12:06 iphyer