algorithms/itemBasedRecommender.py
def topNRecommendations(user_id,items_with_rating,item_sims,n):
    '''
    Calculate the top-N item recommendations for each user using the 
    weighted sums method
    '''

    # initialize dicts to store the score of each individual item,
    # since an item can exist in more than one item neighborhood
    totals = defaultdict(int)
    sim_sums = defaultdict(int)

    for (item,rating) in items_with_rating:

        # lookup the nearest neighbors for this item
        nearest_neighbors = item_sims.get(item,None)

        if nearest_neighbors:
            for (neighbor,(sim,count)) in nearest_neighbors:
                if neighbor != item:

                    # update totals and sim_sums with the rating data
                    totals[neighbor] += sim * rating
                    sim_sums[neighbor] += sim

    # create the normalized list of scored items 
    scored_items = [(total/sim_sums[item],item) for item,total in totals.items()]

    # sort the scored items in ascending order
    scored_items.sort(reverse=True)

    # take out the item score
    # ranked_items = [x[1] for x in scored_items]

    return user_id,scored_items[:n]

    Calculate the top-N item recommendations for each user using the 
    weighted sums method
    
Calculate the top-N item recommendations for each user using the
weighted sums method
Calculate the top - N item recommendations for each user using the weighted sums method
def topNRecommendations(user_id,items_with_rating,item_sims,n):
    totals = defaultdict(int)
    sim_sums = defaultdict(int)
    for (item,rating) in items_with_rating:
        nearest_neighbors = item_sims.get(item,None)
        if nearest_neighbors:
            for (neighbor,(sim,count)) in nearest_neighbors:
                if neighbor != item:
                    totals[neighbor] += sim * rating
                    sim_sums[neighbor] += sim
    scored_items = [(total/sim_sums[item],item) for item,total in totals.items()]
    scored_items.sort(reverse=True)
    return user_id,scored_items[:n]
def topNRecommendations ( user_id , items_with_rating , item_sims , n ) : totals = defaultdict ( int ) sim_sums = defaultdict ( int ) for ( item , rating ) in items_with_rating : nearest_neighbors = item_sims . get ( item , None ) if nearest_neighbors : for ( neighbor , ( sim , count ) ) in nearest_neighbors : if neighbor != item : totals [ neighbor ] += sim * rating sim_sums [ neighbor ] += sim scored_items = [ ( total / sim_sums [ item ] , item ) for item , total in totals . items ( ) ] scored_items . sort ( reverse = True ) return user_id , scored_items [ : n ]
Calculate the top-N item recommendations for each user using the
weighted sums method
Calculate the top - N item recommendations for each user using the weighted sums method
'''
    Calculate the top-N item recommendations for each user using the 
    weighted sums method
    ''' # initialize dicts to store the score of each individual item, # since an item can exist in more than one item neighborhood # lookup the nearest neighbors for this item # update totals and sim_sums with the rating data # create the normalized list of scored items  # sort the scored items in ascending order # take out the item score # ranked_items = [x[1] for x in scored_items]
user_id
items_with_rating
item_sims
n