我正在使用 PHP 5.2.13 版和 Kohana 框架 v2.3.4,我想计算标准偏差。
我在PHP手册中找到了一个函数:stats_standard_deviation
问题是当我尝试时出现此错误:
Fatal error: Call to undefined function stats_standard_deviation() in /folder/test.php on line 1799
Run Code Online (Sandbox Code Playgroud)
这是我正在使用的代码:
function std_dev ($attr, $test1,$test2,$test3,$test4,$test5,$test6) {
$items[] = array();
if (isset($test1) && $test1->$attr != 9 && $test1->$attr != 0) {
$items[] = $test1->$attr;
}
if (isset($test2) && $test2->$attr != 9 && $test2->$attr != 0) {
$items[] = $test2->$attr;
}
if (isset($test3) && $test3->$attr != 9 && $test3->$attr != 0) {
$items[] = $test3->$attr;
}
if (isset($test4) && $test4->$attr != 9 && $test4->$attr != 0) {
$items[] = $test4->$attr;
}
if (isset($test5) && $test5->$attr != 9 && $test5->$attr != 0) {
$items[] = $test5->$attr;
}
if (isset($test6) && $test6->$attr != 9 && $test6->$attr != 0) {
$items[] = $test6->$attr;
}
$standard_deviation = stats_standard_deviation($items);
return round($standard_deviation,2);
}
Run Code Online (Sandbox Code Playgroud)
所有帮助将不胜感激。
谢谢!
正如评论所说,PECL包未安装在您的系统中,请看这里安装。
但是,如果你不能安装,或者你不想安装它,你可以使用这个功能
function std_deviation($arr){
$arr_size=count($arr);
$mu=array_sum($arr)/$arr_size;
$ans=0;
foreach($arr as $elem){
$ans+=pow(($elem-$mu),2);
}
return sqrt($ans/$arr_size);
}
Run Code Online (Sandbox Code Playgroud)
这遵循标准偏差公式。