- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
您是否见过 Netflix 如何根据您以前看过并喜欢看的电影向您推荐某些电影?除了一套书,我正在尝试做同样的事情。
我有 53 本书和 32 个用户。 32 位用户对每本书的评分从 5 到 -5,其中 5 是我喜欢它。用于计算两本书相互比较“相似”程度的公式如下:
x1*y1
表示用户 1 对书 x 和书 y 的评分,x2*y2
表示第 2 个用户对相同 2 本书的评分,对所有用户继续。
传入此方法的数组是主数组。主数组的每个元素对应一个用户,用户数组中的每个元素对应一本书。 (32个用户数组,每个用户数组本身就是一个53元素的数组)
保存每个用户评分的数组是有序的,compValuehold[0][0]
代表第一个用户对第一本书的评分,compValuehold[0][2]
表示第一个用户对第二本书的评分等
public static void DisplayRatings(double[][] compValuehold)
{
double eachUserProduct = 0;
double denominatorXSum = 0;
double denominatorYSum = 0;
double Score = 0;
int counterForScore = 0;
double[] calculatedValues = new double[52];
//this for loop should calculate each book's ratings and store it
//in an array
for (int i = 0; i < 52; i++)
{
for (int j = 0; j < 32; j++)
{
eachUserProduct += compValuehold[j][i] * compValuehold[j][i + 1];
denominatorXSum += compValuehold[j][i] * compValuehold[j][i];
denominatorYSum += compValuehold[j][i + 1] * compValuehold[j][i + 1];
}
denominatorXSum = Math.Sqrt(denominatorXSum);
denominatorYSum = Math.Sqrt(denominatorYSum);
Score = eachUserProduct / (denominatorXSum * denominatorYSum);
calculatedValues[counterForScore] = Score;
counterForScore += 1;
denominatorXSum = 0;
denominatorYSum = 0;
eachUserProduct = 0;
}
}
我能够编写代码将第一本书与其余书籍进行比较。 我的问题是我需要为每本书找出哪本书最相似。这意味着要多次计算该公式。我不知道如何对所有书籍执行此操作。
最佳答案
看起来你正在做的是确定 Cosine Similarity “书籍向量”的集合,其中每个向量由每个用户对特定书籍的评分组成。
尝试在一个函数中完成所有这些可能会让您在调试时头疼;我建议将您的问题分解成更易于管理的部分:
compValuehold
矩阵中提取给定列 similarity(a, b) == similarity(b, a)
)如果您在某处想出更好的比较书籍的方法,这种方法还可以更轻松地更改相似度函数。
这是前两个子问题的示例实现(请记住,它们不是特别有效):
static int[] GetBookVector(int[][] ratingMatrix, int bookIndex)
{
int[] book = new int[ratingMatrix.Length];
for (int i = 0; i < ratingMatrix.Length; i++)
{
book[i] = ratingMatrix[i][bookIndex];
}
return book;
}
static double Similarity(int[] v1, int[] v2)
{
if (v1.Length != v2.Length)
{
throw new ArgumentException("Vectors must be of the same length.");
}
int numerator = 0;
double v1Norm = 0;
double v2Norm = 0;
for (int i = 0; i < v1.Length; i++)
{
numerator += v1[i] * v2[i];
v1Norm += v1[i] * v1[i];
v2Norm += v2[i] * v2[i];
}
v1Norm = Math.Sqrt(v1Norm);
v2Norm = Math.Sqrt(v2Norm);
return (numerator / (v1Norm * v2Norm));
}
关于c# - 如何用这种方法计算几本书的接近度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15671394/
我想创建一个返回值的方法(我们称之为“z”)。它的值由另一个值决定(我们称之为“y”)。基本上我想要的是满足以下条件: 当 x 接近 0 时,z 接近 100。 当 x 接近无穷大时,z 接近 0。
我正在尝试使用 Java 中的PreparedStatement 执行查询。 当我尝试执行查询时,收到错误号 1064(语法错误)。 我已经在 MySQL 查询浏览器中使用替换值对此进行了测试,效果很
我正在开发一个应用程序来解析 Scala 中的命令。命令的一个例子是: todo get milk for friday 所以计划是让一个非常智能的解析器将行分开并识别命令部分以及字符串中有时间引用的
来自 http://directwebremoting.org/dwr/reverse-ajax/index.html ,它表示它支持轮询、 cometd 、搭载。这是否意味着当我们实现这种方法时,我
我开始研究一个概念,该概念要求我找到一种方法,以给定的速度将矩形移向给定的点。我正在为 Android 开发,所以这对速度非常关键(它也将针对可能的数百个对象计算每一帧。) 我能想到的解决方案如下:
我正在处理一个处理“门票”的表(状态=“开放”或状态=“关闭”)。当票证关闭时,相关系统不会更改状态,而是会创建一个具有“已关闭”状态的重复条目。 对于“ticket_number”关键字段,如果存在
我正在尝试在 python 中执行一些 n-gram 计数,我想我可以使用 MySQL(MySQLdb 模块)来组织我的文本数据。 我有一个很大的表,大约有 1000 万条记录,代表由唯一数字 ID(
我正在尝试将数据添加到 mariadb 表中。我想将 val0 到 val5 作为查询的值传递。但我收到错误 OperationalError: close "%": 语法错误代码 list_Valu
我正在使用 (Py)OpenGL 显示 256 色索引图像。我将着色器与包含调色板的一维纹理一起使用。这是片段着色器代码: #version 330 uniform sampler2D texture
对于我的元素 areallybigpage.com (*),我想看看我们能用 CSS 的 transform: scale(...) 走多远。 这有效并以正常大小显示文本: #id1 { positi
我有两列带有数字数据的 Pandas 表(dtype flaot64)。 我将每列四舍五入到小数点后有 2 位数字,然后使用函数将其四舍五入到接近 0.5,但由于某种原因,只有一列四舍五入为 0.05
我正在构建一个由用户登录和注册组成的应用程序,但每次我在模拟器上测试它时,我都会收到强制关闭。以下是我在日志猫中收到的错误: 08-14 14:06:28.853: D/dalvikvm(828):
我正在尝试在 Python 中实现 Strassen 矩阵乘法。我已经让它发挥了一些作用。这是我的代码: a = [[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4]] b
为什么这不起作用?这与 = 附近的命令字符串语法有关,但我似乎无法弄清楚,在线示例似乎完全相同。编辑: Activated In 是一列。 示例来自 How to select data from d
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求提供代码的问题必须表现出对所解决问题的最低限度理解。包括尝试过的解决方案、为什么它们不起作用,以及
我有一个测试区,它是来自数据库的动态文本,可能有数千个单词。我希望它中断并在每段中用句号将近 100 个(任意长度)单词作为一个段落。我能够在 100 个单词后中断,但不能完全停止。为了在 100 个
我是 hadoop 和 hive 的新手。我正在尝试将数据加载到配置单元表中,但遇到以下错误。 另一方面,我尝试使用语句 stmt.execute("INSERT INTO employee VALU
这是来自一个统计项目。我定义了下面的函数,但是当n接近400时,第二个方法很慢。第一个方法很好(这里有人帮助了我in this question) import Math.Combinatorics.
我正在尝试创建一个 css 侧边菜单,但是当我关闭菜单并将 div 容器宽度设置为 0 时,链接仍然可见。 这是 jsfiddle - https://jsfiddle.net/atLvp6k7/ 有
我对 MySQL 还很陌生。我必须使用输出参数调用存储过程。我在互联网上搜索了很多,但没有找到解决我的问题的正确方法。如果我使用 @outputParamName 调用存储过程,它会说我在 NULL
我是一名优秀的程序员,十分优秀!