def __get_lfm_matrix(self, lfm_matrix_path):
     try:
         print("开始载入隐语义矩阵....")
         self.user_p, self.movie_q = load_file(lfm_matrix_path)
         print("载入隐语义矩阵完成")
     except FileNotFoundError:
         print("载入隐语义矩阵失败,重新计算隐语义矩阵")
         # 建立隐语义矩阵
         self.user_p, self.movie_q = self.__build_matrix()
     print("开始保存隐语义矩阵")
     save_file(lfm_matrix_path, (self.user_p, self.movie_q))
     print("保存隐语义矩阵完成")
示例#2
0
文件: usercf.py 项目: whille/RecSys
 def train(self, origin_data, sim_matrix_path="store/user_sim.pkl"):
     """训练模型
         @param origin_data: 原始数据
         @param sim_matrix_path:  协同矩阵保存的路径
     """
     # 初始化训练集
     self._init_train(origin_data)
     print("开始训练模型", file=sys.stderr)
     try:
         print("开始载入用户协同矩阵....", file=sys.stderr)
         self.user_sim_matrix = load_file(sim_matrix_path)
         print("载入协同过滤矩阵完成", file=sys.stderr)
     except BaseException:
         print("载入用户协同过滤矩阵失败,重新计算协同过滤矩阵", file=sys.stderr)
         # 计算用户协同矩阵
         self.user_sim_matrix = self.user_similarity()
     print("开始保存协同过滤矩阵", file=sys.stderr)
     save_file(sim_matrix_path, self.user_sim_matrix)
     print("保存协同过滤矩阵完成", file=sys.stderr)
    def train(self, sim_matrix_path="store/item_sim.pkl"):
        """
        训练模型
        :param sim_matrix_path: 协同矩阵保存的路径
        :return:
        """
        # 初始化训练集
        UserCF._init_train(self)
        print("开始训练模型", file=sys.stderr)
        try:
            print("开始载入用户协同矩阵....", file=sys.stderr)
            self.item_sim_matrix = load_file(sim_matrix_path)
            print("载入协同过滤矩阵完成", file=sys.stderr)
        except FileNotFoundError:
            print("载入用户协同过滤矩阵失败,重新计算协同过滤矩阵", file=sys.stderr)
            # 计算用户协同矩阵
            self.item_sim_matrix = self._item_similarity()

        print("开始保存协同过滤矩阵", file=sys.stderr)
        save_file(sim_matrix_path, self.item_sim_matrix)
        print("保存协同过滤矩阵完成", file=sys.stderr)
示例#4
0
from flask import Flask, jsonify
from main.util.utils import load_file
from main.LFM import LFM
import numpy as np

app = Flask(__name__)

lfm = LFM(10)
lfm.read_data()
item_pool, user_item=lfm.init_data(lfm.trainset)
user_p = load_file("store/lfm_user.pkl")
movie_q = load_file("store/lfm_movie.pkl")

@app.route('/')
def hello_world():
    return 'Hello World!'

@app.route('/recom/<int:userId>')
def show_post(userId):
    movie_rating=dict()

    for movieId in item_pool.keys():
        if movieId in user_item[userId]:
            continue

        movie_rating[movieId] = np.dot(user_p[userId], movie_q[movieId])

    movie_rating = dict(sorted(movie_rating.items(), key=lambda x: x[1], reverse=True)[:10])

    ret = list(movie_rating.keys())