TASK:冷启动热度推荐回退;
This commit is contained in:
@@ -40,34 +40,44 @@ def softmax(scores):
|
||||
sum_exp = sum(exp_scores)
|
||||
return [s / sum_exp for s in exp_scores]
|
||||
|
||||
# def get_random_recommendations(category: str, num: int) -> List[str]:
|
||||
# """根据预加载热度向量推荐(冷启动)"""
|
||||
# try:
|
||||
# heat_data = matrix_data.get("heat_data", {})
|
||||
#
|
||||
# if category not in heat_data:
|
||||
# raise ValueError(f"热度数据缺少类别 {category},使用随机推荐")
|
||||
#
|
||||
# heat_dict = heat_data[category] # {url: score}
|
||||
# urls = list(heat_dict.keys())
|
||||
# scores = list(heat_dict.values())
|
||||
#
|
||||
# if not urls:
|
||||
# raise ValueError("该类别下无热度记录,使用随机推荐")
|
||||
#
|
||||
# probs = softmax(scores)
|
||||
# sample_size = min(num, len(urls))
|
||||
# sampled_urls = random.choices(urls, weights=probs, k=sample_size)
|
||||
#
|
||||
# return sampled_urls
|
||||
#
|
||||
# except Exception as e:
|
||||
# # 回退:完全随机推荐
|
||||
# all_iids = list(matrix_data["iid_to_sketch"].keys())
|
||||
# category_iids = matrix_data["category_to_iids"].get(category, all_iids)
|
||||
# sample_size = min(num, len(category_iids))
|
||||
# sampled = np.random.choice(category_iids, size=sample_size, replace=False)
|
||||
# return [matrix_data["iid_to_sketch"][iid] for iid in sampled]
|
||||
|
||||
def get_random_recommendations(category: str, num: int) -> List[str]:
|
||||
"""根据预加载热度向量推荐(冷启动)"""
|
||||
try:
|
||||
heat_data = matrix_data.get("heat_data", {})
|
||||
|
||||
if category not in heat_data:
|
||||
raise ValueError(f"热度数据缺少类别 {category},使用随机推荐")
|
||||
|
||||
heat_dict = heat_data[category] # {url: score}
|
||||
urls = list(heat_dict.keys())
|
||||
scores = list(heat_dict.values())
|
||||
|
||||
if not urls:
|
||||
raise ValueError("该类别下无热度记录,使用随机推荐")
|
||||
|
||||
probs = softmax(scores)
|
||||
sample_size = min(num, len(urls))
|
||||
sampled_urls = random.choices(urls, weights=probs, k=sample_size)
|
||||
|
||||
return sampled_urls
|
||||
|
||||
except Exception as e:
|
||||
# 回退:完全随机推荐
|
||||
all_iids = list(matrix_data["iid_to_sketch"].keys())
|
||||
category_iids = matrix_data["category_to_iids"].get(category, all_iids)
|
||||
sample_size = min(num, len(category_iids))
|
||||
sampled = np.random.choice(category_iids, size=sample_size, replace=False)
|
||||
return [matrix_data["iid_to_sketch"][iid] for iid in sampled]
|
||||
"""全品类随机推荐"""
|
||||
all_iids = list(matrix_data["iid_to_sketch"].keys())
|
||||
# 优先从当前品类选择
|
||||
category_iids = matrix_data["category_to_iids"].get(category, all_iids)
|
||||
# 确保不超出实际数量
|
||||
sample_size = min(num, len(category_iids))
|
||||
sampled = np.random.choice(category_iids, size=sample_size, replace=False)
|
||||
return [matrix_data["iid_to_sketch"][iid] for iid in sampled]
|
||||
|
||||
|
||||
@router.get("/recommend/{user_id}/{category}/{num_recommendations}/{brand_id}/{brand_scale}", response_model=List[str])
|
||||
|
||||
Reference in New Issue
Block a user