python实现lfm_【知识发现】隐语义模型(LFM,Latent Factor Model)推荐算法python实现

1、隐语义模型:

物品:表示为长度为k的向量q(每个分量都表示  物品具有某个特征的程度)

用户兴趣:表示为长度为k的向量p(每个分量都表示  用户对某个特征的喜好程度)

用户u对物品i的兴趣可以表示为:

其损失函数定义为:

使用随机梯度下降,获得参数p,q。

负样本生成:对于只有正反馈信息(用户收藏了,关注了xxx)的数据集,需要生成负样本,原则如下

1)生成的负样本要和正样本数量相当

2)物品越热门(用户没有收藏该物品),越有可能是负样本

2、数据集:https://grouplens.org/datasets/movielens/

格式:

3、参考代码:

# -*- coding: utf-8 -*-

'''

Created on 2017年9月15日

@author: Jason.F

'''

from math import exp

import pandas as pd

import numpy as np

import pickle

import time

from numpy import rank

def getUserNegativeItem(frame, userID):

'''

获取用户负反馈物品:热门但是用户没有进行过评分 与正反馈数量相等

:param frame: ratings数据

:param userID:用户ID

:return: 负反馈物品

'''

userItemlist = list(set(frame[frame['userid'] == userID]['itemid'])) #用户评分过的物品

otherItemList = [item for item in set(frame['itemid'].values) if item not in userItemlist] #用户没有评分的物品

itemCount = [len(frame[frame['itemid'] == item]['userid']) for item in otherItemList] #物品热门程度

series = pd.Series(itemCount, index=otherItemList)

series = series.sort_values(ascending=False)[:len(userItemlist)] #获取正反馈物品数量的负反馈物品

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值