0

我需要在协同过滤之后提出建议,但代码非常慢。我需要向数百万客户提出建议。有人可以帮忙吗?建议需要 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
4

0 回答 0