Naïve Bayes(朴素贝叶斯)分类算法的实现
(1) 简介:
(2) 算法描述:
(3)
<?php
/*
*Naive Bayes朴素贝叶斯算法(分类算法的实现)
*/ /*
*把.txt中的内容读到数组中保存
*$filename:文件名称
*/
//--------------------------------------------------------------------
function getFileContent($filename)
{
$array = array(null);
$content = file_get_contents($filename);
$result = explode("\r\n",$content);
//print_r(count($result));
for($j=0;$j<count($result);$j++)
{
//print_r($result[$j]."<br>");
$con = explode(" ",$result[$j]);
array_push($array,$con);
}
array_splice($array,0,1);
return $array;
}
//-------------------------------------------------------------------- /*
*NaiveBayes朴素贝叶斯算法
*$test:测试文本;$train:训练文本;$flagsyes:yes;$flagsno:no
*/
//--------------------------------------------------------------------
function NaiveBayes($test,$train,$flagsyes,$flagsno)
{
$count_yes = 0;
$num = count($train[0]);
for($i=1;$i<count($train);$i++)
{
if($train[$i][$num-1]==$flagsyes)$count_yes++;
}
$p_yes = $count_yes / (count($train)-1);
$p_no = 1- $p_yes; $count_no = count($train)-1 - $count_yes; for($i=1;$i<count($test)-1;$i++)
{
$testnumyes = 0;
$testnumno = 0;
for($j=1;$j<count($train);$j++)
{
if(($train[$j][$i]==$test[$i])&&($train[$j][count($test)-1]==$flagsyes))$testnumyes++;
else if(($train[$j][$i]==$test[$i])&&($train[$j][count($test)-1]==$flagsno))$testnumno++;
} $array_yes[$i] = $testnumyes / $count_yes ;
$array_no[$i] = $testnumno / $count_no ;
/*
print_r($testnumyes."<br>");
print_r($testnumno."<br>");
print_r($count_yes."<br>");
print_r($count_no."<br>");
print_r($array_no[$i]."<br>");
*/
} $py=1;
$pn=1;
for($i=1;$i<count($test)-1;$i++){
$py *= $array_yes[$i];
$pn *= $array_no[$i];
} $py *= $p_yes;
$pn *= $p_no; if($py>$pn)return $flagsyes;
else return $flagsno; /* print_r($py."<br>");
print_r($pn."<br>");
*/ }
//-------------------------------------------------------------------- $train = getFileContent("train.txt");
$test = getFileContent("test.txt"); for($i=1;$i<count($test);$i++)
{
$test[$i][count($test[0])-1] = NaiveBayes($test[$i],$train,Y,N);
} /*
*将数组中的内容读到.txt中
*/
//--------------------------------------------------------------------
$fp= fopen('result.txt','wb');
for($i=0;$i<count($test);$i++)
{
$temp = NULL;
for($j=0;$j<count($test[$i]);$j++)
{
$temp = $test[$i][$j]."\t";
fwrite($fp,$temp);
}
fwrite($fp,"\r\n");
}
fclose($fp);
//-------------------------------------------------------------------- /*
*打印输出
*/
//--------------------------------------------------------------------
echo "<pre>";
print_r($test);
echo "</pre>";
//--------------------------------------------------------------------
?>