php高斯朴素贝叶斯,Naive Bayes(朴素贝叶斯算法)[分类算法],naivebayes_PHP教程

1 php2 /*

3 *Naive Bayes朴素贝叶斯算法(分类算法的实现)4 */

5

6 /*

7 *把.txt中的内容读到数组中保存8 *$filename:文件名称9 */

10 //--------------------------------------------------------------------

11 function getFileContent($filename)12 {13 $array = array(null);14 $content = file_get_contents($filename);15 $result = explode("\r\n",$content);16 //print_r(count($result));

17 for($j=0;$j

");

20 $con = explode(" ",$result[$j]);21 array_push($array,$con);22 }23 array_splice($array,0,1);24 return $array;25 }26 //--------------------------------------------------------------------

27

28

29 /*

30 *NaiveBayes朴素贝叶斯算法31 *$test:测试文本;$train:训练文本;$flagsyes:yes;$flagsno:no32 */

33 //--------------------------------------------------------------------

34 function NaiveBayes($test,$train,$flagsyes,$flagsno)35 {36 $count_yes = 0;37 $num = count($train[0]);38 for($i=1;$i

45 $count_no = count($train)-1 - $count_yes;46

47

48 for($i=1;$i

58 $array_yes[$i] = $testnumyes / $count_yes;59 $array_no[$i] = $testnumno / $count_no;60 /*

61 print_r($testnumyes."

");62 print_r($testnumno."

");63 print_r($count_yes."

");64 print_r($count_no."

");65 print_r($array_no[$i]."

");66 */

67 }68

69 $py=1;70 $pn=1;71 for($i=1;$i

76 $py *= $p_yes;77 $pn *= $p_no;78

79 if($py>$pn)return $flagsyes;80 else return $flagsno;81

82 /*print_r($py."

");83 print_r($pn."

");84 */

85

86 }87 //--------------------------------------------------------------------

88

89 $train = getFileContent("train.txt");90 $test = getFileContent("test.txt");91

92 for($i=1;$i

97 /*

98 *将数组中的内容读到.txt中99 */

100 //--------------------------------------------------------------------

101 $fp= fopen('result.txt','wb');102 for($i=0;$i

114

115 /*

116 *打印输出117 */

118 //--------------------------------------------------------------------

119 echo "";120 print_r($test);121 echo "";122 //--------------------------------------------------------------------

123 ?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值