Why is the append code so slow?I need to make recommendations faster

62 Views Asked by At

I need to make recommendations after als collaborative filtering but the code is extremely slow. I need to make recommendations to millions of customers. can someone please help. The recommendations take 10 mins. even for 100 customers and 100 recommendations each. Any help would be greatly useful.

def recommend(account_num, data_sparse, user_vecs, item_vecs, item_lookup, num_items=10):

    # Get all interactions by the user
    user_interactions = data_sparse[account_num,:].toarray()

    # We don't want to recommend items the user has consumed. So let's
    # set them all to 0 and the unknowns to 1.
    user_interactions = user_interactions.reshape(-1) + 1 
    user_interactions[user_interactions > 1] = 0

    # This is where we calculate the recommendation by taking the 
    # dot-product of the user vectors with the item vectors.
    rec_vector = user_vecs[account_num,:].dot(item_vecs.T).toarray()


    rec_vector_scaled = (rec_vector.reshape(-1,1))[:,0]
    recommend_vector = user_interactions*rec_vector_scaled

    # Get all the artist indices in order of recommendations (descending) and
    # select only the top "num_items" items. 
    item_idx = np.argsort(recommend_vector)[::-1][:num_items]

    vertasp = []
    scores = []


    for idx in item_idx:
        vertasp.append(item_lookup.BxASP.loc[item_lookup.BxASP_num==str(idx)].iloc[0])
scores.append(recommend_vector[idx])

    # Create a new dataframe with recommended artist names and scores
    recommendations = pd.DataFrame({'BrandxASP': vertasp, 'score': scores,'account':account_num})

    return recommendations

# Let's generate and print our recommendations
account_num=0
final=pd.DataFrame()
while account_num<=999:
    recommendations = recommend(account_num, data_sparse, user_vecs, item_vecs, item_lookup)
    final=final.append(recommendations)
    account_num=account_num+1
0

There are 0 best solutions below