PHP中的Pearson相关性(Pearson correlation in PHP)
我试图在php中实现两组数据之间人员相关系数的计算。 我只是想做移植python脚本,可以在这个URL找到http://answers.oreilly.com/topic/1066-how-to-find-similar-users-with-python/
我的实现如下:
class LB_Similarity_PearsonCorrelation implements LB_Similarity_Interface{ public function similarity($user1, $user2){ $sharedItem = array(); $pref1 = array(); $pref2 = array(); $result1 = $user1->fetchAllPreferences(); $result2 = $user2->fetchAllPreferences(); foreach($result1 as $pref){ $pref1[$pref->item_id] = $pref->rate; } foreach($result2 as $pref){ $pref2[$pref->item_id] = $pref->rate; } foreach ($pref1 as $item => $preferenza){ if(key_exists($item,$pref2)){ $sharedItem[$item] = 1; } } $n = count($sharedItem); if ($n == 0) return 0; $sum1 = 0;$sum2 = 0;$sumSq1 = 0;$sumSq2 = 0;$pSum = 0; foreach ($sharedItem as $item_id => $pre) { $sum1 += $pref1[$item_id]; $sum2 += $pref2[$item_id]; $sumSq1 += pow($pref1[$item_id],2); $sumSq2 += pow($pref2[$item_id],2); $pSum += $pref1[$item_id] * $pref2[$item_id]; } $num = $pSum - (($sum1 * $sum2) / $n); $den = sqrt(($sumSq1 - pow($sum1,2)/$n) * ($sumSq2 - pow($sum2,2)/$n)); if ($den == 0) return 0; return $num/$den; } }
澄清以更好地理解代码,fetchAllPreferences方法返回一组实际上是项目的对象,将它们转换为数组以便管理
我不确定这个实现是否正确,特别是我对分母计算的正确性有一些怀疑。
欢迎任何建议。
提前致谢!
I'm trying to implement the calculation of correlation coefficient of people between two sets of data in php. I'm just trying to do the porting python script that can be found at this url http://answers.oreilly.com/topic/1066-how-to-find-similar-users-with-python/
my implementation is the following:
class LB_Similarity_PearsonCorrelation implements LB_Similarity_Interface{ public function similarity($user1, $user2){ $sharedItem = array(); $pref1 = array(); $pref2 = array(); $result1 = $user1->fetchAllPreferences(); $result2 = $user2->fetchAllPreferences(); foreach($result1 as $pref){ $pref1[$pref->item_id] = $pref->rate; } foreach($result2 as $pref){ $pref2[$pref->item_id] = $pref->rate; } foreach ($pref1 as $item => $preferenza){ if(key_exists($item,$pref2)){ $sharedItem[$item] = 1; } } $n = count($sharedItem); if ($n == 0) return 0; $sum1 = 0;$sum2 = 0;$sumSq1 = 0;$sumSq2 = 0;$pSum = 0; foreach ($sharedItem as $item_id => $pre) { $sum1 += $pref1[$item_id]; $sum2 += $pref2[$item_id]; $sumSq1 += pow($pref1[$item_id],2); $sumSq2 += pow($pref2[$item_id],2); $pSum += $pref1[$item_id] * $pref2[$item_id]; } $num = $pSum - (($sum1 * $sum2) / $n); $den = sqrt(($sumSq1 - pow($sum1,2)/$n) * ($sumSq2 - pow($sum2,2)/$n)); if ($den == 0) return 0; return $num/$den; } }
clarification to better understand the code, the method fetchAllPreferences return back a set of objects that are actually the items, turns them into an array for ease of management
I'm not sure that this implementation is correct, in particular I have some doubts about the correctness of the calculation of the denominator.
any advice is welcome.
thanks in advance!
原文:https://stackoverflow.com/questions/2408677
最满意答案
发生这种情况是因为Meteor.users 不是被动的 。 我不知道背后的原因是什么,但我看到许多开发人员,特别是那些试图通过发布关于他们的真棒应用程序的非常酷的文章而闻名的开发人员,暴露了令牌。
因此,如果一些白痴将Meteor.users发布到浏览器,那就是一个安全漏洞。 如果它是被动的,那将是最糟糕的,因为令牌将被实时更新。 也许这对于那些并不真正知道他们正在做的新手来说是一个障碍。 只是我对这个决定的看法。
此集合设计用于管理用户,在登录后,使用它来设置数据是没有意义的。
This is happening because Meteor.users is not reactive. I don't know what the reason behind but I saw many developers, specially developers who try to get famous by publish really cool articles about their awesome application, exposing the tokens.
So if some idiot publish the Meteor.users to the browser, it's a security flaw. It would be even worst if it was reactive because the token would be updated in realtime. Maybe this a block to newbie who don't really know that they're doing. Just my opinion about this decision.
This collection is design to be used for managing users and after the login, it makes no sense to use to store data, as it is designed.
相关问答
更多-
你不会错过任何东西,这是Meteor的正常行为。 从服务器发布的数据没有范围。 一旦数据发布到客户端,所有的客户端都可以访问它们,即使在浏览器控制台中运行的代码也可以这样做 这些已发布的数据只有在用于订阅订阅的订阅已关闭时才会在客户端中清除。 就像您的示例中一样,因为您在reportsSummary模板中使用了此订阅,因此当reportsSummary被销毁时( onDestroyed事件被触发时),此订阅将被关闭。 在Meteor中,最好的做法是始终在collection.find查询获取文档。 通过这种 ...
-
流星出版物有计算(Meteor publication with calculations)[2024-04-18]
发生这种情况是因为Meteor.users 不是被动的 。 我不知道背后的原因是什么,但我看到许多开发人员,特别是那些试图通过发布关于他们的真棒应用程序的非常酷的文章而闻名的开发人员,暴露了令牌。 因此,如果一些白痴将Meteor.users发布到浏览器,那就是一个安全漏洞。 如果它是被动的,那将是最糟糕的,因为令牌将被实时更新。 也许这对于那些并不真正知道他们正在做的新手来说是一个障碍。 只是我对这个决定的看法。 此集合设计用于管理用户,在登录后,使用它来设置数据是没有意义的。 This is happe ... -
ReactiveVar和自动运行的组合为我做了诀窍。 这可能是矫枉过正,但它确实有效。 let cutoffTimestamp = new ReactiveVar(); Meteor.setInterval(function() { cutoffTimestamp.set(Date.now() - 1); }, 60000); Meteor.publish("myPub", function() { this.autorun(function() { return myC ...
-
流星出版物和订阅(Meteor publications and subscription)[2023-11-05]
发布中的帖子是服务器端集合。 帮助者中的帖子是客户端集合,其中包含所有已发布的帖子。 如果您有数千个帖子,通常不希望发布所有帖子,因为下载数据需要几秒钟。 您应该只发布所需的数据。 Meteor.publish('posts', function(limit) { return Posts.find({}, { limit: limit}); }); 当您调用此订阅功能时,客户端集合帖子将只包含100个帖子。 var limit = 100; Meteor.subscribe('posts', l ... -
流星出版物:隐藏数组文档字段中的某些字段?(Meteor publication: Hiding certain fields in an array document field?)[2022-01-07]
根据Meteor文档 ,这样的事情可以起作用: Meteor.publish('family', function(famId) { return Families.find(famId, { fields : { "family.relation" : 0 //Exclude family.relation from the sent data } }); }); According to the Meteor docs, something like this co ... -
如何创建汇总多个出版物的流星出版物?(How to create a meteor publication aggregating several publications?)[2023-11-13]
没有直接的方法来组合多个流星应用程序。 如果您想将任何数据推送回应用程序,那么愿望将更加复杂。 准系统方法可能是在每个应用程序中创建JSON端点并在另一个应用程序上聚合它们。 There is no straight forward way to combine multiple meteor applications. The desire would be even more complicated if you want to push any data back to an application. ... -
订阅需要一段时间才能将数据从服务器获取到迷你mongo,因此您必须等待订阅准备就绪,然后再使用它将为您获取的数据。 如果您正在使用Iron Router,请尝试使用waitOn而不是subscribe,这将迫使路由器等待订阅准备就绪,并在获取订阅数据时呈现加载模板。 Router.route('/patients/:_id', { layoutTemplate: 'ApplicationLayout', yieldRegions: { 'single_patient': {to: 'cont ...
-
流星中的发布/订阅(以及几乎所有客户端 - 服务器通信)都是使用称为DDP的协议完成的,通常通过Web套接字完成(如果不支持,则存在回退)。 当客户端订阅发布时,它会向服务器发送请求订阅的消息。 这将调用处理程序(您定义的发布函数并提供给Meteor.publish ,在您的情况下),它可以返回Mongo游标,游标数组或自己处理发布的较低级别详细信息。 如果函数返回游标,服务器会观察它并尽快发送有关数据的消息。 首先,所有匹配的文档都作为added消息发送到客户端,这些消息会自动转换为MiniMongo中的 ...
-
虽然我不明白为什么,使用Arunoda的SubsManager而不是原生的Meteor.subscribe解决了这个问题。 Though I don't understand why, using Arunoda's SubsManager instead of the native Meteor.subscribe resolved the problem.
-
如何在流星出版物中改进或避免查找/获取周期?(How to improve or avoid find / fetch cycle in meteor's publication?)[2023-06-16]
您是否考虑过使用reywood / publishComposite包 ? 使用此包,您可以使用相同的方法发布相关数据,而无需执行大量逻辑来获取正确的数据。 以下代码可以帮助您入门: Meteor.publishComposite("publishNewChat", function() { return [{ find:function(){ return Users.find({ _id: this.userId },{fields:{"profile.chat":1}}); ...