基于评分的商品top-N推荐系统
import io # needed because of weird encoding of u.item file
import os
from surprise import KNNBaseline
from surprise import Dataset
from surprise import get_dataset_dir
from surprise import Reader
from surprise import dump def read_item_names(item_file_path,split_flag='\t'):
"""
从MOVIELNEN 100-K数据集读取UE项目文件并返回两个
映射将原始ID转换成电影名称和电影名称为原始ID。
Read the u.item file from MovieLens 100-k dataset and return two
mappings to convert raw ids into movie names and movie names into raw ids.
""" # file_name = r'C:\Users\FELIX\Desktop\surprise库源码分析\uitems.txt'
file_name=item_file_path
rid_to_name = {}
name_to_rid = {}
with io.open(file_name, 'r', encoding='utf8') as f:
for line in f:
line = line.split(split_flag)
rid_to_name[line[0]] = line[1].strip()
name_to_rid[line[1].strip()] = line[0] return rid_to_name, name_to_rid save_path=os.path.expanduser(r'~/dump_file')
def train_data(user_item_score_path,split_flag='\t',user_based=False):
# path to dataset file
# 数据集路径
# file_path = os.path.expanduser(r'C:\Users\FELIX\Desktop\surprise库源码分析\uuu.txt')
file_path = os.path.expanduser(user_item_score_path)
reader = Reader(line_format='user item rating timestamp', sep=split_flag)
data = Dataset.load_from_file(file_path, reader=reader) # First, train the algortihm to compute the similarities between items
# 首先训练算法来计算不同项目之间的相似度
# data = Dataset.load_builtin('ml-100k')
trainset = data.build_full_trainset()
sim_options = {'name': 'pearson_baseline', 'user_based': user_based}
algo = KNNBaseline(sim_options=sim_options)
algo.fit(trainset) # Dump algorithm and reload it.
# file_name = os.path.expanduser(r'C:\Users\FELIX\Desktop\surprise库源码分析\uuu.txt\dump_file')
dump.dump(save_path, algo=algo) # 模型保存 def get_neighbors(item_name,item_file_path,kk=10):
_, algo = dump.load(save_path) # 模型加载
# # Read the mappings raw id <-> movie name
rid_to_name, name_to_rid = read_item_names(item_file_path)
# print(name_to_rid)
# # Retrieve inner id of the movie Toy Story
# item_name_raw_id = name_to_rid['uitems10\n']
item_name_raw_id = name_to_rid[item_name.strip()] item_name_inner_id = algo.trainset.to_inner_iid(item_name_raw_id) # # Retrieve inner ids of the nearest neighbors of Toy Story.
item_name_neighbors = algo.get_neighbors(item_name_inner_id, k=kk) # Convert inner ids of the neighbors into names.
item_name_neighbors = (algo.trainset.to_raw_iid(inner_id)
for inner_id in item_name_neighbors)
item_name_neighbors = (rid_to_name[rid]
for rid in item_name_neighbors)
return item_name_neighbors u_i_path=r'C:\Users\FELIX\Desktop\surprise库源码分析\uuu.txt'
train_data(u_i_path)
i_path=r'C:\Users\FELIX\Desktop\surprise库源码分析\uitems.txt'
nei_items=get_neighbors('uitems685',i_path,kk=10)
for nei in nei_items:
print(nei)
如果没有数据的话,可以随机生成测试数据:
# 自己生成数据 1000人 5000商品 1000人,随机对5000个商品中的东西进行评价,评分为1-10
import random
for n in range(4):
for i in range(1000):
t=int(random.random()*100)
for j in range(t):
# kk=int(random.random()*200)
# for k in range(kk):
item=int(random.random()*5000)
goal=int(random.random()*10)
with open('uu.txt','a',encoding='utf8') as f:
line=str(i)+'\t'+str(item)+'\t'+str(goal)+'\t'+'\n'
f.write(line) # 随机打乱评分数据
with open('uu.txt','r',encoding='utf8')as f:
data=f.readlines()
data2=random.shuffle(data)
with open('uuu.txt','a',encoding='utf8')as f2:
for line in data:
f2.write(line) # 随机生成商品数据
with open('uitems.txt','w',encoding='utf8')as f:
for i in range(5000):
s=str(i)+'\t'+'uitems{}'.format(str(i))+'\n'
f.write(s)
基于评分的商品top-N推荐系统的更多相关文章
- 【新鲜出炉的个人项目】基于 Flink 的商品推荐系统
FlinkCommodityRecommendationSystem Recs FlinkCommodityRecommendationSystem(基于 Flink 的商品推荐系统) 1. 前言 系 ...
- 文献综述八:基于JAVA的商品网站的研究
一.基本信息 标题:基于JAVA的商品网站的研究 时间:2015 出版源:信息技术 文件分类:对java语言的研究 二.研究背景 本文主要介绍了系统的分析,设计和开发的全部过程. 三.具体内容 文献的 ...
- 文献综述三:基于JSP的商品信息管理系统设计与开发
一.基本信息 标题:基于JSP的商品信息管理系统设计与开发 时间:2015 出版源:Computer Knowledge and Technology 文件分类:jsp技术的系统开发 二.研究背景 通 ...
- 基于卷积神经网络CNN的电影推荐系统
本项目使用文本卷积神经网络,并使用MovieLens数据集完成电影推荐的任务. 推荐系统在日常的网络应用中无处不在,比如网上购物.网上买书.新闻app.社交网络.音乐网站.电影网站等等等等,有人的地方 ...
- Python基于机器学习方法实现的电影推荐系统
推荐算法在互联网行业的应用非常广泛,今日头条.美团点评等都有个性化推荐,推荐算法抽象来讲,是一种对于内容满意度的拟合函数,涉及到用户特征和内容特征,作为模型训练所需维度的两大来源,而点击率,页面停留时 ...
- 【Machine Learning】决策树案例:基于python的商品购买能力预测系统
决策树在商品购买能力预测案例中的算法实现 作者:白宁超 2016年12月24日22:05:42 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现的深入理解.本 ...
- 基于神经网络的embeddding来构建推荐系统
在之前的博客中,我主要介绍了embedding用于处理类别特征的应用,其实,在学术界和工业界上,embedding的应用还有很多,比如在推荐系统中的应用.本篇博客就介绍了如何利用embedding来构 ...
- 基于neighborhood models(item-based) 的个性化推荐系统
文章主要介绍的是koren 08年发的论文[1], 2.2neighborhood models部分内容(其余部分会陆续补充上来). koren论文中用到netflix 数据集, 过于大, 在普通的 ...
- 【转】基于 Kylin 的推荐系统效果评价系统
OLAP(联机分析处理)是数据仓库的主要应用之一,通过设计维度.度量,我们可以构建星型模型或雪花模型,生成数据多维立方体Cube,基于Cube可以做钻取.切片.旋转等多维分析操作.早在十年前,SQL ...
随机推荐
- Hibernate常用api以及增删改查
一 .API的定义 所谓的API全称就是(Application Programming Interface,应用程序编程接口).就是类库对外提供的接口.类.枚举.注解等元素. 如:JDK API ...
- Generator 实现
Generator 是 ES6 中新增的语法,和 Promise 一样,都可以用来异步编程 // 使用 * 表示这是一个 Generator 函数 // 内部可以通过 yield 暂停代码 // 通过 ...
- rabbitmd
一.前期准备 (1)条件:准备3台linux系统,确保能连到download.yunwei.edu (2)编写yum源下载脚本: vim yum-repo.sh wget -O /etc/yu ...
- Ext之页面多次请求问题(下拉框发送无关请求)
extjs 下拉框在拉取本地数据,然后又要展示后台数据时,出现过此问题(加载页面,自动发送无关的请求导致后台出现错误日志) { xtype:'combo', id:'state', width:130 ...
- springboot学习入门简易版一---springboot2.0介绍
1.1为什么用springboot(2) 传统项目,整合ssm或ssh,配置文件,jar冲突,整合麻烦.Tomcat容器加载web.xml配置内容 springboot完全采用注解化(使用注解方式启动 ...
- 一语道破Java 11的ZGC为何如此高效
GC是大部分现代语言内置的特性,Java 11 新加入的ZGC号称可以达到10ms 以下的 GC 停顿,本文作者对这一新功能进行了深入解析.同时还对还对这一新功能带来的其他可能性做了展望.ZGC是否可 ...
- JS函数篇【2】
什么是函数 函数的作用,可以写一次代码,然后反复地重用这个代码. <h3 onload="add2(1,2,3);add3(4,5,6)"></h3> &l ...
- Java关键字—final
final—不可变的,用来说明最终属性,表明一个类不能派生出子类,或者成员方法不能被覆盖,或者成员域的值不能被改变. 修饰范围: 1.修饰类,被final关键字修饰的类无法被继承: 2.修饰方法,被f ...
- Vim热键总结
最近学习linux环境,总结一下Vim的常用热键~~~
- Luogu P1290 欧几里得的游戏/UVA10368 Euclid's Game
Luogu P1290 欧几里得的游戏/UVA10368 Euclid's Game 对于博弈论的题目没接触过多少,而这道又是比较经典的SG博弈,所以就只能自己来推关系-- 假设我们有两个数$m,n$ ...