surprise库协同过滤做音乐推荐实现

https://surprise.readthedocs.io/en/stable/index.html
推荐系统Python实现库
https://github.com/NicolasHug/Surprise
文档
https://surprise.readthedocs.io/en/stable/index.html

加载数据集合

from __future__ import (absolute_import, division, print_function, unicode_literals)
import os
import io

from surprise import KNNBaseline, Reader
from surprise import Dataset

import pickle as pickle
# 重建歌单id到歌单名的映射字典
id_name_dic = pickle.load(open("./recommend/playlist.pkl","rb"))
print("加载歌单id到歌单名的映射字典完成...")
# 重建歌单名到歌单id的映射字典
name_id_dic = {}
for playlist_id in id_name_dic:
    name_id_dic[id_name_dic[playlist_id]] = playlist_id
print("加载歌单名到歌单id的映射字典完成...")

file_path = os.path.expanduser('./recommend/163_music_suprise_format.txt')
# 指定文件格式
reader = Reader(line_format='user item rating timestamp', sep=',')
# 从文件读取数据
music_data = Dataset.load_from_file(file_path, reader=reader)
# 计算歌曲和歌曲之间的相似度
print("构建数据集...")
trainset = music_data.build_full_trainset()
#sim_options = {'name': 'pearson_baseline', 'user_based': False}

开始训练 找出用户最近邻居

"""
模板之查找最近的user(在这里是歌单)
根据用户的协同过滤算法
找到用户最相近的一些函数
选取KNNBaseline算法训练
找到最近的User  歌单

https://surprise.readthedocs.io/en/stable/index.html
推荐系统Python实现库
https://github.com/NicolasHug/Surprise
文档
https://surprise.readthedocs.io/en/stable/index.html
"""
print("开始训练模型...")
#sim_options = {'user_based': False}
#algo = KNNBaseline(sim_options=sim_options)
algo = KNNBaseline()
algo.fit(trainset)

current_playlis
  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 13
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值