Почему код добавления такой медленный? Мне нужно быстрее давать рекомендации - PullRequest
0 голосов
/ 10 июля 2019

Мне нужно дать рекомендации после совместной фильтрации, но код работает очень медленно. Мне нужно дать рекомендации миллионам клиентов. Может кто-нибудь, пожалуйста, помогите. Рекомендации занимают 10 минут. даже для 100 клиентов и 100 рекомендаций каждый. Любая помощь будет очень полезна.

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
...