资源简介
推荐系统SVD实现代码,python实现

代码片段和文件信息
#Ver1.0
#Zero @2012.5.2
#
import math
import random
import cPickle as pickle
#calculate the overall average
def Average(fileName):
fi = open(fileName ‘r‘)
result = 0.0
cnt = 0
for line in fi:
cnt += 1
arr = line.split()
result += int(arr[2].strip())
return result / cnt
def InerProduct(v1 v2):
result = 0
for i in range(len(v1)):
result += v1[i] * v2[i]
return result
def PredictScore(av bu bi pu qi):
pScore = av + bu + bi + InerProduct(pu qi)
if pScore < 1:
pScore = 1
elif pScore > 5:
pScore = 5
return pScore
def SVD(configureFile testDataFile trainDataFile modelSaveFile):
#get the configure
fi = open(configureFile ‘r‘)
line = fi.readline()
arr = line.split()
averageScore = float(arr[0].strip())
userNum = int(arr[1].strip())
itemNum = int(arr[2].strip())
factorNum = int(arr[3].strip())
learnRate = float(arr[4].strip())
regularization = float(arr[5].strip())
fi.close()
bi = [0.0 for i in range(itemNum)]
bu = [0.0 for i in range(userNum)]
temp = math.sqrt(factorNum)
qi = [[(0.1 * random.random() / temp) for j in range(factorNum)] for i in range(itemNum)]
pu = [[(0.1 * random.random() / temp) for j in range(factorNum)] for i in range(userNum)]
print(“initialization end\nstart training\n“)
#train model
preRmse = 1000000.0
for step in range(100):
fi = open(trainDataFile ‘r‘)
for line in fi:
arr = line.split()
uid = int(arr[0].strip()) - 1
iid = int(arr[1].strip()) - 1
score = int(arr[2].strip())
prediction = PredictScore(averageScore bu[uid] bi[iid] pu[uid] qi[iid])
eui = score - prediction
#update parameters
bu[uid] += learnRate * (eui - regularization * bu[uid])
bi[iid] += learnRate * (eui - regularization * bi[iid])
for k in range(factorNum):
temp = pu[uid][k] #attention here must save the value of pu before updating
pu[uid][k] += learnRate * (eui * qi[iid][k] - regularization * pu[uid][k])
qi[iid][k] += learnRate * (eui * temp - regularization * qi[iid][k])
fi.close()
#learnRate *= 0.9
curRmse = Validate(testDataFile averageScore bu bi pu qi)
print(“test_RMSE in step %d: %f“ %(step curRmse))
if curRmse >= preRmse:
break
else:
preRmse = curRmse
#write the model to files
fo = file(modelSaveFile ‘wb‘)
pickle.dump(bu fo True)
pickle.dump(bi fo True)
pickle.dump(qi fo True)
pickle.dump(pu fo True)
fo.close()
print(“model generation over“)
#validate the model
def Validate(testDataFile av bu bi pu qi):
cnt = 0
rmse = 0.0
fi = open(testDataFile ‘r‘)
for line in fi:
cnt += 1
arr = line.split()
uid = int(arr[0].strip()) - 1
iid = int(arr[1].strip()) - 1
pScore = PredictScore(av bu[uid] bi[iid] pu[uid] qi[iid])
tScore = int(arr[2].strip())
rmse += (tScore - pScore) * (tScore - pScore)
fi.close()
return math.sqrt(rmse / cnt)
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 235016 2011-04-01 18:24 SVD\ml_data\test.txt
文件 12839563 2011-04-01 18:24 SVD\ml_data\training.txt
文件 97 2012-05-03 14:55 SVD\svd.conf
文件 4153 2012-05-06 20:29 SVD\SVD.py
目录 0 2012-05-06 20:31 SVD\ml_data
目录 0 2012-05-06 20:31 SVD
----------- --------- ---------- ----- ----
13078829 6
- 上一篇:python爬取维基百科程序语言消息盒(源码及截图)
- 下一篇:PythonTank
相关资源
- Instant Pygame for Python Game Development How
- Biopython Tutorial
- Think Python 2nd
- 一个小小的表白程序(python)
- Python课堂笔记(高淇400集第一季)
- 二级考试python试题12套(包括选择题和
- pywin32_python3.6_64位
- python+ selenium教程
- PycURL(Windows7/Win32)Python2.7安装包 P
- 英文原版-Scientific Computing with Python
- 7.图像风格迁移 基于深度学习 pyt
- 基于Python的学生管理系统
- A Byte of Python(简明Python教程)(第
- Python实例174946
- Python 人脸识别
- Python 人事管理系统
- 基于python-flask的个人博客系统
- 计算机视觉应用开发流程
- python 调用sftp断点续传文件
- python socket游戏
- 基于Python爬虫爬取天气预报信息
- python函数编程和讲解
- Python开发的个人博客
- 基于python的三层神经网络模型搭建
- python实现自动操作windows应用
- python人脸识别(opencv)
- python 绘图(方形、线条、圆形)
- python疫情卡UN管控
- python 连连看小游戏源码
- 基于PyQt5的视频播放器设计
评论
共有 条评论