77 lines
2.8 KiB
Python
77 lines
2.8 KiB
Python
from collections import defaultdict
|
|
import random
|
|
|
|
# 商品类别和商品名称映射
|
|
CATEGORIES = ["电器", "服饰", "食品", "玩具", "手机"]
|
|
|
|
PRODUCT_NAMES = {
|
|
"电器": ["电视", "冰箱", "洗衣机", "空调", "吸尘器", "电饭煲", "微波炉", "电磁炉", "热水器", "空气净化器"],
|
|
"服饰": ["T恤", "牛仔裤", "羽绒服", "衬衫", "运动鞋", "夹克", "卫衣", "连衣裙", "短裤", "风衣"],
|
|
"食品": ["巧克力", "饼干", "方便面", "牛奶", "饮料", "面包", "糖果", "果冻", "薯片", "蛋挞"],
|
|
"玩具": ["积木", "拼图", "玩偶", "遥控车", "毛绒玩具", "魔方", "乐高", "变形金刚", "洋娃娃", "电子琴"],
|
|
"手机": ["华为", "苹果", "小米", "OPPO", "vivo", "荣耀", "三星", "魅族", "联想", "努比亚"]
|
|
}
|
|
|
|
# 用户评分 (0, 50, 100) -> 不喜欢、还行、很喜欢
|
|
RATINGS = [0, 50, 100]
|
|
IS_VALID = ["Y", "N"]
|
|
|
|
|
|
# ================== 生成假数据 ==================
|
|
def generate_fake_orders(n=50):
|
|
orders = []
|
|
for _ in range(n):
|
|
category = random.choice(CATEGORIES)
|
|
product = random.choice(PRODUCT_NAMES[category])
|
|
rating = random.choice(RATINGS)
|
|
is_valid = random.choice(IS_VALID)
|
|
|
|
orders.append({
|
|
'category': category,
|
|
'product': product,
|
|
'rating': rating,
|
|
'isValid': is_valid
|
|
})
|
|
return orders
|
|
|
|
|
|
# ================== 推荐逻辑 ==================
|
|
def recommend_top_n(orders, n=3):
|
|
# 存储:{ category: { product: (total_rating, count) } }
|
|
product_ratings = defaultdict(lambda: defaultdict(lambda: [0, 0])) # [总评分, 数量]
|
|
|
|
for order in orders:
|
|
if order['isValid'] != 'Y':
|
|
continue
|
|
category = order['category']
|
|
product = order['product']
|
|
rating = order['rating']
|
|
|
|
product_ratings[category][product][0] += rating
|
|
product_ratings[category][product][1] += 1
|
|
|
|
# 计算平均评分并排序
|
|
recommendations = {}
|
|
for category, products in product_ratings.items():
|
|
rated_products = [
|
|
(product, total / count) for product, (total, count) in products.items()
|
|
]
|
|
rated_products.sort(key=lambda x: x[1], reverse=True)
|
|
recommendations[category] = [p[0] for p in rated_products[:n]]
|
|
|
|
return recommendations
|
|
|
|
|
|
# ================== 主程序入口 ==================
|
|
if __name__ == '__main__':
|
|
fake_data = generate_fake_orders(100)
|
|
|
|
print("=== 假数据样本 ===")
|
|
for d in fake_data[:10]: # 打印前10条数据看看
|
|
print(d)
|
|
|
|
print("\n=== 开始推荐 Top 3 商品 ===")
|
|
result = recommend_top_n(fake_data, n=3)
|
|
|
|
for category, top_products in result.items():
|
|
print(f"[{category}] 推荐商品:{', '.join(top_products)}") |