Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (140 points)
from sklearn.preprocessing import MinMaxScaler

def rec_items(customer_key, mf_train, user_vecs, item_vecs, customer_list, item_list, item_lookup, num_items = 10):

    '''

    This function will return the top recommended items to our users

    

    parameters:

    

    customer_key - Input the customer's id number that you want to get recommendations for

    

    mf_train - The training matrix you used for matrix factorization fitting

    

    user_vecs - the user vectors from your fitted matrix factorization

    

    item_vecs - the item vectors from your fitted matrix factorization

    

    customer_list - an array of the customer's ID numbers that make up the rows of your ratings matrix

                    (in order of matrix)

    

    item_list - an array of the products that make up the columns of your ratings matrix

                    (in order of matrix)

    

    item_lookup - A simple pandas dataframe of the unique product ID/product descriptions available

    

    num_items - The number of items you want to recommend in order of best recommendations. Default is 10.

    

    returns:

    

    - The top n recommendations chosen based on the user/item vectors for items never interacted with/purchased

    '''

    

    cust_ind = np.where(customer_list == customer_key)[0][0] # Returns the index row of our customer id

    pref_vec = mf_train[cust_ind,:].toarray() # Get the ratings from the training set ratings matrix

    pref_vec = pref_vec.reshape(-1) + 1 # Add 1 to everything, so that items not purchased yet become equal to 1

    pref_vec[pref_vec > 1] = 0 # Make everything already purchased zero

    rec_vector = user_vecs[cust_ind,:].dot(item_vecs.T) # Get dot product of user vector and all item vectors

    # Scale this recommendation vector between 0 and 1

    min_max = MinMaxScaler()

    rec_vector_scaled = min_max.fit_transform(rec_vector.reshape(-1,1))[:,0]

    recommend_vector = pref_vec*rec_vector_scaled

    # Items already purchased have their recommendation multiplied by zero

    product_idx = np.argsort(recommend_vector)[::-1][:num_items] # Sort the indices of the items into order

    # of best recommendations

    rec_list = [] # start empty list to store items

    for index in product_idx:

        code = item_list[index]

        rec_list.append([code, item_lookup.EnglishProductName.loc[item_lookup.ProductKey == code].iloc[0]])

        # Append our descriptions to the list

    codes = [item[0] for item in rec_list]

    descriptions = [item[1] for item in rec_list]

    final_frame = pd.DataFrame({'ProductKey': codes, 'EnglishProductName': descriptions}) # Create a dataframe

    return final_frame[['ProductKey', 'EnglishProductName']] # Switch order of columns around

rec_items(11001, product_train, user_vecs, item_vecs, customers_arr, products_arr, item_lookup,

                       num_items = 10)

Please log in or register to answer this question.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...