#读取数据
data = pd.read_excel("./order_data.xlsx")
data.head()
data.dtypes
#数据清洗
data.isnull().any()
data = data.fillna(0)
data.head()
data.isnull().any()
#数据类型转化
data = data[(data.product_id > 0)]
data["catage_id"] = data["catage_id"].apply(lambda x: int(x))
#筛选出用户列表,只购买过一个商品的用户记录对共现矩阵没有影响,这样的数据没有意义
user_data = data.groupby("user_id").size()
user_data = user_data[user_data > 1]
data = data[data.user_id.isin(user_data.keys())]
user_list = data.values.tolist()
#获取商品列表
all_product_id = list(set(data["product_id"].values.tolist()))
#建立商品字典
product_to_index = {}
index_to_product = {}
for index,value in enumerate(all_product_id):
product_to_index[value] = index
index_to_product[index] = value
#1.创建用户-物品索引
user_item_index = {}
for user_id in user_data.keys():
product_ids = data[data.user_id == user_id]["product_id"].values.tolist()
for index,value in enumerate(product_ids):
product_ids[index] = product_to_index[value]
user_item_index[user_id] = product_ids
#2.创建共现矩阵
product_length = len(product_to_index)
matrix_c = np.zeros((product_length,product_length))
#循环用户-商品倒排索引 对于同一个用户购买的任意的两个商品 在共现矩阵中都要加1
for user_id in user_item_index:
product_ids = user_item_index[user_id]
for i,value in enumerate(product_ids):
if(i < len(product_ids) - 1):
list_other = product_ids[(i+1):len(product_ids)]
for second_product_index in list_other:
matrix_c[value][second_product_index] += 1
matrix_c[second_product_index][value] += 1
#3.根据算法得到商品的相似矩阵 算法:cij/sqrt(|N(i)|*|N(j)|)
product_index_count_dic = {}
product_group = data.groupby("product_id").size()
for product_id in product_group.keys():
product_index_count_dic[product_to_index[product_id]] = product_group[product_id]
matrix_w = np.zeros((product_length,product_length))
index_i_list,index_j_list = np.where(matrix_c > 0)
for index,value in enumerate(index_i_list):
i = value
j = index_j_list[index]
score = matrix_c[i][j]/math.sqrt(product_index_count_dic[i] * product_index_count_dic[j])
matrix_w[i][j] = score
matrix_w[j][i] = score
#归一化
def normalize(value):
value = (value - np.min(value))/(np.max(value) - np.min(value))
return value
#4.创建用户的喜好商品矩阵
user_like_item_dic = {}
for user_id in user_data.keys():
user_like_item = data[data.user_id == user_id]
user_item_like_matrix = np.zeros(product_length)
for i in range(len(user_like_item)):
index = product_to_index[user_like_item.iloc[i].product_id]
user_item_like_matrix[index] = user_like_item.iloc[i].orders_num
user_like_item_dic[user_id] = normalize(user_item_like_matrix)
#获得最相似的k个商品
def getMostSimilar(matrix_w,index,k):
c_list = matrix_w[index]
similar_item = pd.DataFrame({"value":c_list})
similar_item = similar_item.sort_values(by="value",ascending=False).iloc[0:k]
similar_item_dic = {}
for i in range(len(similar_item)):
similar_item_dic[similar_item.iloc[i].name] = similar_item.iloc[i].value
return similar_item_dic
def reommendItem(user_id,matrix_w,user_like_item_dic,k):
recommend_dic = {}
user_like_list = user_like_item_dic[user_id]
user_like_item_index_list = np.where(user_like_list > 0)
user_like_item_index_list = user_like_item_index_list[0]
for product_index in user_like_item_index_list:
like_score = user_like_list[product_index]
most_similar_item = getMostSimilar(matrix_w,product_index,k)
for key in most_similar_item.keys():
if key in user_like_item_index_list:
continue
#最终得分是用户对商品的喜欢程度 * 商品的相似程度
score = like_score * most_similar_item[key]
if key in recommend_dic.keys():
score += recommend_dic[key]
recommend_dic[key] = score
#返回得分最高的k个商品
sorted_x = sorted(recommend_dic.items(), key=operator.itemgetter(1))
sorted_x.reverse()
return sorted_x[0:k]
#5.给用户推荐商品
def getAllUserRecommend():
user_recommend = {}
for user_id in user_like_item_dic.keys():
#print(user_id)
recommend_dic = reommendItem(user_id,matrix_w,user_like_item_dic,10)
value = ""
for key in recommend_dic:
index = key[0]
if value == "":
value += str(index_to_product[index])
else:
value += "," + str(index_to_product[index])
user_recommend[user_id] = value
return user_recommend
res = getAllUserRecommend()
print(res)