博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据挖据之余弦相识度
阅读量:5893 次
发布时间:2019-06-19

本文共 1485 字,大约阅读时间需要 4 分钟。

# coding:utf-8__author__ = 'hdfs'from math import sqrtusers = {    "Angelica": {"Blues Traveler": 3.5, "Broken Bells": 2.0, "Norah Jones": 4.5, "Phoenix": 5.0,                 "Slightly Stoopid": 1.5,                 "The Strokes": 2.5, "Vampire Weekend": 2.0},    "Hailey": {"Broken Bells": 4.0, "Deadmau5": 1.0, "Norah Jones": 4.0, "The Strokes": 4.0, "Vampire Weekend": 1.0},    "Jordyn": {"Broken Bells": 4.5, "Deadmau5": 4.0, "Norah Jones": 5.0, "Phoenix": 5.0, "Slightly Stoopid": 4.5,               "The Strokes": 4.0, "Vampire Weekend": 4.0},    "Sam": {"Blues Traveler": 5.0, "Broken Bells": 2.0, "Norah Jones": 3.0, "Phoenix": 5.0, "Slightly Stoopid": 4.0,            "The Strokes": 5.0},    "Veronica": {"Blues Traveler": 3.0, "Norah Jones": 5.0, "Phoenix": 4.0, "Slightly Stoopid": 2.5, "The Strokes": 3.0}}def coscoefficient(rating1, rating2):    '''    余弦相识度    :param rating1:     :param rating2:     :return:    '''    xleng, yleng = 0, 0    for ratval01key, ratval01 in rating1.items():        xleng = xleng + ratval01 * ratval01    for ratval02key, ratval02 in rating2.items():        yleng = yleng + ratval02 * ratval02    neiji = 0    for key1 in rating1.keys():        if key1 in rating2.keys():            neiji = neiji + rating1[key1] * rating2[key1]    result = neiji / (sqrt(xleng) * sqrt(yleng))    return resultif __name__ == "__main__":    print(coscoefficient(users['Angelica'], users['Veronica']))

  

转载于:https://www.cnblogs.com/similarface/p/5636549.html

你可能感兴趣的文章
梳理一份机器学习的学习目录
查看>>
Java并发编程:深入剖析ThreadLocal
查看>>
Mac OSX 中java7 java8环境的配置
查看>>
我所理解的JDK自动装箱和拆箱
查看>>
30分钟入门Java
查看>>
elasticsearch学习——环境搭建2
查看>>
数据科学求职过程中总结的四点经验
查看>>
git代码首次提交
查看>>
mysql安装,远程连接,以及修改密码
查看>>
Dart服务器端 shelf_route包
查看>>
spring security(四)
查看>>
js中return、return false、return true的区别
查看>>
Mybatis查询返回Map类型数据
查看>>
java的深拷贝与浅拷贝
查看>>
程序员如何提高工作效率
查看>>
数据库水平切分(其他应用)
查看>>
easyui 将已经是 textbox 的输入框改成时间框
查看>>
python基础整理——计算机语言分类
查看>>
python基础学习整理——字典
查看>>
promise
查看>>