1

在 php中有这样的 java 实现吗?

<?php

/**
* Returns conditional probability of $A given $B and $Data.
* $Data is an indexed array.  Each element of the $Data array 
* consists of an A measurement and B measurment on a sample 
* item.
*/
function getConditionalProbabilty($A, $B, $Data) {
  $NumAB   = 0;
  $NumB    = 0;
  $NumData = count($Data);
  for ($i=0; $i < $NumData; $i++) {
    if (in_array($B, $Data[$i])) {
      $NumB++;
      if (in_array($A, $Data[$i])) {
        $NumAB++;
      }
    }
  }
  return $NumAB / $NumB;
}

?>


<?php

require "getConditionalProbability.php";

/**
* The elements of the $Data array use this coding convention:
*
* +cancer - patient has cancer
* -cancer - patient does not have cancer
* +test   - patient tested positive on cancer test
* -test   - patient tested negative on cancer test
*/

$Data[0] = array("+cancer", "+test");
$Data[1] = array("-cancer", "-test");
$Data[2] = array("+cancer", "+test");
$Data[3] = array("-cancer", "+test");

// specify query variable $A and conditioning variable $B
$A = "+cancer";
$B = "+test";

// compute the conditional probability of having cancer given 1) 
// a positive test and 2) a sample of covariation data
$probability = getConditionalProbabilty($A, $B, $Data);

echo "P($A|$B) = $probability";

// P(+cancer|+test) = 0.66666666666667

?>
4

2 回答 2

0

AFAIK标准JDK中没有,尽管它是一个非常简单的功能,您可以自己编写它,Java版本几乎是直接翻译。虽然有 3rd 方库,例如来自 apache 的这个库,如果您打算做很多此类事情,它可以处理统计和数学函数。

于 2011-03-25T01:22:36.013 回答
0

你可以看看CI Bayes。它是 Naive 的一个实现,它通过让您能够执行 Fisher 分类器(Naive Bayes 的变体)来超越上述内容。它有其局限性,但很容易将其添加到项目中并开始使用。

于 2011-04-16T04:19:46.747 回答