首页 \ 问答 \ PHP中的Pearson相关性(Pearson correlation in PHP)

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
更新时间:2022-03-06 19:03

最满意答案

发生这种情况是因为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.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.publish('posts', function(limit) { return Posts.find({}, { limit: limit}); }); 当您调用此订阅功能时,客户端集合帖子将只包含100个帖子。 var limit = 100; Meteor.subscribe('posts', l ...
  • 根据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 ...
  • 没有直接的方法来组合多个流星应用程序。 如果您想将任何数据推送回应用程序,那么愿望将更加复杂。 准系统方法可能是在每个应用程序中创建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.
  • 您是否考虑过使用reywood / publishComposite包 ? 使用此包,您可以使用相同的方法发布相关数据,而无需执行大量逻辑来获取正确的数据。 以下代码可以帮助您入门: Meteor.publishComposite("publishNewChat", function() { return [{ find:function(){ return Users.find({ _id: this.userId },{fields:{"profile.chat":1}}); ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)