所以我有这段代码:
$arr = array(
1,2,3,4,5,6
);
$num = count($arr);
$sum = array_sum($arr);
$average = $sum/$num;
foreach($arr as $val) {
$sum += pow(($val - $average), 2);
}
$stdev = sqrt($sum / ($num - 1));
与
SELECT STDDEV_POP(something) FROM table;
表格是
Something
'1'
'2'
'3'
'4'
'5'
'6'
然而$ stdev返回
2.7748873851023
而select选择返回1.707825127659933
我的stdev代码出了什么问题?
答案 0 :(得分:2)
答案 1 :(得分:1)
这给了我1.70
$arr = array(
1,2,3,4,5,6
);
$num = count($arr);
$sum2 = 0;
$sum = array_sum($arr);
$average = $sum/$num;
foreach($arr as $val) {
$sum2 += pow(($val - $average), 2);
}
$stdev = sqrt($sum2 / ($num));
echo $stdev;exit;
答案 2 :(得分:0)
我使用这个功能,这对我的工作很重要
function find_stdev($arr){
$a=0;
foreach($arr as $val) {
$a += (pow($val,2));
}
$n = count($arr);
$sum = array_sum($arr);
$a = $a*$n;
$b = pow($sum,2);
$c = ($a-$b)/($n*($n-1));
$stdev = sqrt($c);
return $stdev;
}