资源简介
自己写的比较初级的基于标签的用户协同算法,数据集用的是ml-100k,对数据集进行训练集和测试集的划分,里面还有准确率,召回率,标准平均绝对误差等的计算代码
代码片段和文件信息
# coding=utf-8
# -*- coding=utf-8 -*-
#见185
import math
import sys
import imp
import datetime
from texttable import Texttable
import random
import pprint pickle
#
# 使用 |A&B|/sqrt(|A || B |)计算余弦距离
#
#
#
def calcCosDistSpe(user1 user2):
avg_x = 0.0
avg_y = 0.0
for key in user1:
avg_x += key[1]
avg_x = avg_x / len(user1)
for key in user2:
avg_y += key[1]
avg_y = avg_y / len(user2)
u1_u2 = 0.0
for key1 in user1:
for key2 in user2:
if key1[1] > avg_x and key2[1] > avg_y and key1[0] == key2[0]:
u1_u2 += 1
u1u2 = len(user1) * len(user2) * 1.0
sx_sy = u1_u2 / math.sqrt(u1u2)
return sx_sy
#
# 计算余弦距离
#
#
def calcCosDist(user1 user2):
sum_x = 0.0
sum_y = 0.0
sum_xy = 0.0
for key1 in user1:
for key2 in user2:
if key1[0] == key2[0]:
sum_xy += key1[1] * key2[1]
sum_y += key2[1] * key2[1]
sum_x += key1[1] * key1[1]
if sum_xy == 0.0:
return 0
sx_sy = math.sqrt(sum_x * sum_y)
return sum_xy / sx_sy
#
#
# 相似余弦距离
#
#
#
def calcSimlaryCosDist(user1 user2):
#print(user1)
#print(user2)
sum_x = 0.0
sum_y = 0.0
sum_xy = 0.0
avg_x = 0.0
avg_y = 0.0
for key in user1:#user1的评分
avg_x += key[1]
avg_x = avg_x / len(user1)
for key in user2:
avg_y += key[1]
avg_y = avg_y / len(user2)
for key1 in user1:
for key2 in user2:
if key1[0] == key2[0]:
sum_xy += (key1[1] - avg_x) * (key2[1] - avg_y)
sum_y += (key2[1] - avg_y) * (key2[1] - avg_y)
sum_x += (key1[1] - avg_x) * (key1[1] - avg_x)
if sum_xy == 0.0:
return 0
sx_sy = math.sqrt(sum_x * sum_y)
return sum_xy / sx_sy
def calcSimlaryCosDist1(user1 user2):
#print(user1)
#print(user2)
sum_x = 0.0
sum_y = 0.0
sum_xy = 0.0
avg_x = 0.0
avg_y = 0.0
for key in user1:#user1的评分
#print(key)
avg_x += key[1]
avg_x = avg_x / len(user1)
for key in user2:
avg_y += key[1]
avg_y = avg_y / len(user2)
for key1 in user1:
for key2 in user2:
if key1[0] == key2[0]:
sum_xy += (key1[1] - avg_x) * (key2[1] - avg_y)
sum_y += (key2[1] - avg_y) * (key2[1] - avg_y)
sum_x += (key1[1] - avg_x) * (key1[1] - avg_x)
#print(sum_xy)
return sum_xy
#该函数根据余弦相似度计算的
def calcSimlaryCosDist2(users1user2):
#[(174 5) (259 4) (308 5) (603 4)]
n=0#user1和user2共同的项目
#print(users1)
for item1 in users1:
for item2 in user2:
if item1[0]==item2[0]:
n+=1
return n/math.sqrt(len(user2)*len(users1))n
#基于余弦的相似度计算
def calcSimlaryCosDist3(users1user2):
#[(174 5) (259 4) (308 5) (603 4
- 上一篇:12306抢票Python代码,内含视频教程
- 下一篇:自动扫雷系统+Python
相关资源
- 自动扫雷系统+Python
- 12306抢票Python代码,内含视频教程
- 个人博客网站源码python3.6+django2.0+my
- python网盘.txt
- Python Flask开发自己敲的试验楼小Demo
- python内置K-means聚类算法对鸢尾花数据
- KCFpython算法
- 指定步数节点内容的PROCAST仿真结果导
- python自然语言处理中文停用词
- 最好中国大学近几年排名及python爬虫
- Tensorflow-BiLSTM分类
- 感知机算法Python实现
- python 实现将TXT文件内容逐行存到EXC
- python 打开并计算两幅dicom图像感兴趣
- python 决策树代码
- 银行ATM系统(Python实现)
- pygame实现的贪吃蛇游戏RetroSnaker.py
- Python文件
- QT文件转换成Python的自动化工具*.ui转
- fcntl模块 win
- python爬虫爬取企业详细信息
- Kruskal算法python实现
- 蚁群算法的python代码
- 最小二乘法python代码,不用库函数
- sm3 python encode
- openopc for python 3.x
- 张正友相机标定Python代码
- Python调用QQ微信截图
- python图像裁剪
- 海明校验 python源代码 海明码
评论
共有 条评论