Compare commits
	
		
			2 Commits
		
	
	
		
			e8ea3c8c62
			...
			0a39957891
		
	
	| Author | SHA1 | Date | 
|---|---|---|
| 
							
							
								
								 | 
						0a39957891 | |
| 
							
							
								
								 | 
						190083cfd0 | 
							
								
								
									
										25
									
								
								推荐系统/main.py
								
								
								
								
							
							
						
						
									
										25
									
								
								推荐系统/main.py
								
								
								
								
							| 
						 | 
					@ -29,7 +29,7 @@ class InitializationArguments(BaseModel):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # 时间窗口(单位为天),平衡实时性和运算效率
 | 
					    # 时间窗口(单位为天),平衡实时性和运算效率
 | 
				
			||||||
    time_window: int = Field(default=30, ge=5, le=360)
 | 
					    time_window: int = Field(default=30, ge=5, le=360)
 | 
				
			||||||
    # 衰减因子兰布达系数,控制兴趣分数衰减速率
 | 
					    # 衰减因子兰布达系数,控制兴趣分数衰减速率(默认不衰减)
 | 
				
			||||||
    decay_lambda: float = Field(default=0, ge=0.00, le=10)
 | 
					    decay_lambda: float = Field(default=0, ge=0.00, le=10)
 | 
				
			||||||
    # 用户特征向量维度数
 | 
					    # 用户特征向量维度数
 | 
				
			||||||
    attributes_dimensions: int = Field(default=10, ge=2.00, le=200)
 | 
					    attributes_dimensions: int = Field(default=10, ge=2.00, le=200)
 | 
				
			||||||
| 
						 | 
					@ -210,9 +210,9 @@ class RecommenderSystem:
 | 
				
			||||||
        # 根据行为类型获取兴趣基础分数和衰减权重
 | 
					        # 根据行为类型获取兴趣基础分数和衰减权重
 | 
				
			||||||
        score_base, weight = self.behavior_arguments[type_]
 | 
					        score_base, weight = self.behavior_arguments[type_]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # 若行为类型为评分则将基础分数转化为0.2~0.8
 | 
					        # 若行为类型为评分则将评分转为基础分数(基于最小值-最大值归一化为0.2~1.0)
 | 
				
			||||||
        if type_ == "rating":
 | 
					        if type_ == "rating":
 | 
				
			||||||
            score_base = 0.1 + 0.8 * (1 / (1 + numpy.exp(3 - rating)))
 | 
					            score_base = 0.2 * (rating - 1) + 0.2
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return score_base * numpy.exp(0 - time_interval * (self.decay_lambda * weight))
 | 
					        return score_base * numpy.exp(0 - time_interval * (self.decay_lambda * weight))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -259,8 +259,6 @@ class RecommenderSystem:
 | 
				
			||||||
        # 基于物品的协同过滤生成推荐物品标识列表
 | 
					        # 基于物品的协同过滤生成推荐物品标识列表
 | 
				
			||||||
        candidates_items = self._generate_items_candidates(user=user, k=k)
 | 
					        candidates_items = self._generate_items_candidates(user=user, k=k)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        print(candidates_items)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        # 基于用户的协同过滤生成推荐物品标识列表
 | 
					        # 基于用户的协同过滤生成推荐物品标识列表
 | 
				
			||||||
        candidates_users = self._generate_users_candidates(user=user, k=k)
 | 
					        candidates_users = self._generate_users_candidates(user=user, k=k)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -327,18 +325,17 @@ class RecommenderSystem:
 | 
				
			||||||
                    else 0
 | 
					                    else 0
 | 
				
			||||||
                )
 | 
					                )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                # 流行度抑制因子
 | 
					                print(pair, similarity)
 | 
				
			||||||
                popularity_suppressed = len(
 | 
					 | 
				
			||||||
                    list(set(users_heuristic) & set(users_recall))
 | 
					 | 
				
			||||||
                ) / numpy.sqrt(len(users_heuristic) * len(users_recall))
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
                # 加权物品的相似度
 | 
					                # 加权物品的相似度
 | 
				
			||||||
                items_recall[item_recall]["scores"] += (
 | 
					                items_recall[item_recall]["scores"] += (
 | 
				
			||||||
                    behaviors["scores"][item_heuristic]
 | 
					                    behaviors["scores"][item_heuristic] * similarity
 | 
				
			||||||
                    * similarity
 | 
					 | 
				
			||||||
                    * popularity_suppressed
 | 
					 | 
				
			||||||
                )
 | 
					                )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        print(items_recall)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        exit()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return self._normalize_scores(items_recall=items_recall, k=k)
 | 
					        return self._normalize_scores(items_recall=items_recall, k=k)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # 基于用户协同过滤算法生成候选物品标识列表
 | 
					    # 基于用户协同过滤算法生成候选物品标识列表
 | 
				
			||||||
| 
						 | 
					@ -385,8 +382,6 @@ class RecommenderSystem:
 | 
				
			||||||
        # 候选物品标识列表
 | 
					        # 候选物品标识列表
 | 
				
			||||||
        candidates = defaultdict(float)
 | 
					        candidates = defaultdict(float)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        print(items_recall)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if items_recall:
 | 
					        if items_recall:
 | 
				
			||||||
            scores = [value["scores"] for value in items_recall.values()]
 | 
					            scores = [value["scores"] for value in items_recall.values()]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -428,7 +423,7 @@ if __name__ == "__main__":
 | 
				
			||||||
            "user": "aaaaaa",
 | 
					            "user": "aaaaaa",
 | 
				
			||||||
            "item": "111111",
 | 
					            "item": "111111",
 | 
				
			||||||
            "type_": "rating",
 | 
					            "type_": "rating",
 | 
				
			||||||
            "timestamp": int(time.time() - 3600),
 | 
					            "timestamp": int(time.time() - 3200),
 | 
				
			||||||
            "rating": 4,
 | 
					            "rating": 4,
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue