资源简介

代码片段和文件信息
import random
import math
class UserbasedCF:
def __init__(selfdatafile = None):
self.datafile = datafile
self.readData()
self.splitData(347)
def readData(selfdatafile = None):
“““
read the data from the data file which is a data set
“““
self.datafile = datafile or self.datafile
self.data = []
for line in open(self.datafile):
useriditemidrecordmtime = line.split(“\t“)
self.data.append((useriditemidint(record)))
def splitData(selfkseeddata = NoneM = 8):
“““
split the data set
testdata is a test data set
traindata is a train set
“““
self.testdata = {}
self.traindata = {}
data = data or self.data
random.seed(seed)
for useritemrecord in self.data:
if random.randint(0M) == k:
self.testdata.setdefault(user{})
self.testdata[user][item] = record
else:
self.traindata.setdefault(user{})
self.traindata[user][item] = record
def userSimilarity(selftrain = None):
“““
one method of getting user similarity matrix
“““
train = train or self.traindata
self.userSim = dict()
for u in train.keys():
for v in train.keys():
if u == v:
continue
self.userSim.setdefault(u{})
self.userSim[u][v] = len(set(train[u].keys()) & set(train[v].keys()))
self.userSim[u][v] /= math.sqrt(len(train[u]) * len(train[v]) * 1.0)
def userSimilarityBest(selftrain = None):
“““
the other method of getting user similarity which is better than above
you can get the method on page 46
In this experiment we use this method
“““
train = train or self.traindata
self.userSimBest = dict()
item_users = dict()
#build inverse table for item_users
for uitem in train.items():
for i in item.keys():
item_users.setdefault(iset())
item_users[i].add(u)
#calculate co-rated items between users
user_item_count = dict()
count = dict()
for itemusers in item_users.items():
for u in users:
user_item_count.setdefault(u0)
user_item_count[u] += 1
for v in users:
if u == v:
continue
count.setdefault(u{})
count[u].setdefault(v0)
count[u][v] += 1
#calculate finial similarity matrix W
for urelated_users in count.items():
self.userSimBest.setdefault(udict())
for vcuv in related_users.items():
self.userSimBest[u][v] = cuv / math.sqrt(us
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 6527 2015-03-06 14:59 Userba
文件 266 2015-03-06 15:50 Userba
文件 1979173 2000-07-19 16:09 Userba
目录 0 2015-03-06 15:47 Userba
目录 0 2015-03-06 15:47 Userba
----------- --------- ---------- ----- ----
1985966 5
- 上一篇:决策树回归算法
- 下一篇:crf--python编码
相关资源
- 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的视频播放器设计
评论
共有 条评论