PHP中的z分数(标准偏差和均值) [英] z-Scores(standard deviation and mean) in PHP

查看:278
本文介绍了PHP中的z分数(标准偏差和均值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHP计算Z分数.本质上,我正在寻找一种最有效的方法来计算数据集(PHP数组)的均值和标准差.有关如何在PHP中执行此操作的任何建议?

I am trying to calculate Z-scores using PHP. Essentially, I am looking for the most efficient way to calculate the mean and standard deviation of a data set (PHP array). Any suggestions on how to do this in PHP?

我正在尝试以最少的步骤来完成此操作.

I am trying to do this in the smallest number of steps.

推荐答案

以计算均值:

$mean = array_sum($array)/count($array)

标准偏差如下:

// Function to calculate square of value - mean
function sd_square($x, $mean) { return pow($x - $mean,2); }

// Function to calculate standard deviation (uses sd_square)    
function sd($array) {
    // square root of sum of squares devided by N-1
    return sqrt(array_sum(array_map("sd_square", $array, array_fill(0,count($array), (array_sum($array) / count($array)) ) ) ) / (count($array)-1) );
}

直接此页面

这篇关于PHP中的z分数(标准偏差和均值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆