- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
所以我试图在 PHP 中实现样本 Pearson 相关系数:
(转到 http://en.wikipedia.org/wiki/Pearson_correlation_coefficient 并搜索“样本 Pearson 相关系数的替代公式也可用”以获取我尝试实现的特定公式)
$sum = 0;
$TF1 = 0;
$TF2 = 0;
$wSquare1 = 0;
$wSquare2 = 0;
$m = sizeof($sample);
foreach($sample as $x){
if(!isset($obj1[$x])){
$obj1[$x]['count'] = 0;
}
if(!isset($obj2[$x])){
$obj2[$x]['count'] = 0;
}
$sum += $obj1[$x]['count'] * $obj2[$x]['count'];
$TF1 += $obj1[$x]['count'];
$TF2 += $obj2[$x]['count'];
$wSquare1 += $obj1[$x]['count']^2;
$wSquare2 += $obj2[$x]['count']^2;
}
$numer = $sum * $m - $TF1 * $TF2;
$denom_left = $m*$wSquare1 - $TF1^2;
$denom_right = $m*$wSquare2 - $TF2^2;
$denom = sqrt($denom_left) * sqrt($denom_right);
$pears = $numer / $denom;
return $pears;
但有时我的代码会返回一个大于 1 的值,而 PCC 不应超过 1....
我做错了什么吗?
最佳答案
我还没有完全检查你的数学,但我突然想到的一件事是$TF1^2
和 $obj1[$x]['count']^2
。他们正在使用 bitwise XOR operator .
我相信你想要 pow($TF1, 2)
和 pow($obj1[$x]['count'], 2)
或者:$TF1 * $TF1
和 $obj1[$x]['count'] * $obj1[$x]['count']
这是一个常见的错误。
另请注意文章中的免责声明:
The above formula suggests a convenient single-pass algorithm for calculating sample correlations, but, depending on the numbers involved, it can sometimes be numerically unstable.
关于php - 我的 PHP PIL 逊相关系数代码有什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7440810/
使用Python 3.6。使用曼哈顿距离进行相似性测量时,我没有得到逻辑结果。即使与 PIL 逊相关性和欧几里德相关性的结果进行比较,欧几里德相关性和曼哈顿相关性的单位也看起来不一样? 我正在研究一个
我是一名优秀的程序员,十分优秀!