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