使用Java实现贝叶斯推理
java中是否有像php中的这个这样的实现?
<?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
?>
Is there any implementation in java like this in php?
<?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
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
AFAIK 标准 JDK 中没有,尽管它是一个非常简单的函数,您可以自己编写它,Java 版本几乎可以直接翻译。虽然有 3rd 方库,例如来自 apache 的 this ,如果您计划的话,它可以处理统计和数学函数做了很多这样的事情。
AFAIK There isn't in the standard JDK though its a pretty simple function you could just write it yourself the Java version would pretty much be a direct translation. Though there are 3rd party libraries such as this one from apache that deal with statistical and mathematical functions if you plan on doing a lot of this sort of thing.
您可以查看 CI 贝叶斯。它是朴素贝叶斯的实现,它将超越上述功能,让您能够进行费舍尔分类器(朴素贝叶斯的变体)。它有其局限性,但将其添加到项目中并开始使用非常容易。
You could look at CI Bayes. It's an implementation of Naive, and it will go beyond the above by giving you the ability to do a Fisher Classifier (variant on Naive Bayes). It has its limitations, but it's pretty easy to add it to a project and get going.