Naive Bayes (naive Bayesian algorithm) [Classification algorithm],naivebayes_php tutorial

Source: Internet
Author: User

Naive Bayes (naive Bayesian algorithm) [Classification algorithm],naivebayes


Implementation of Naïve Bayes (naive Bayesian) classification algorithm

(1) Introduction:

(2) Algorithm Description:

(3)

1 
 Php2 /*3 *naive Bayes naive Bayesian algorithm (implementation of classification algorithm)4 */5 6 /*7 * Read the contents of. txt into an array to save8 * $filename: File name9 */Ten //-------------------------------------------------------------------- One functionGetfilecontent ($filename) A { -     $array=Array(NULL); -     $content=file_get_contents($filename); the     $result=Explode("\ r \ n",$content); -     //Print_r (Count ($result)); -      for($j= 0;$j<Count($result);$j++) -     { +         //Print_r ($result [$j]. "
"); - $con=Explode(" ",$result[$j]); + Array_push($array,$con); A } at Array_splice($array, 0,1); - return $array; - } - //-------------------------------------------------------------------- - - in /* - *naivebayes naive Bayesian algorithm to * $test: Test text, $train: training text; $flagsyes: yes; $flagsno: no + */ - //-------------------------------------------------------------------- the functionNaivebayes ($test,$train,$flagsyes,$flagsno) * { $ $count _yes= 0;Panax Notoginseng $num=Count($train[0]); - for($i= 1;$i<Count($train);$i++) the { + if($train[$i][$num-1]==$flagsyes)$count _yes++; A } the $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++) the { - $testnumyes= 0;Wuyi $testnumno= 0; the for($j= 1;$j<Count($train);$j++) - { Wu 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++; About } $ - $array _yes[$i] =$testnumyes/$count _yes ; - $array _no[$i] =$testnumno/$count _no ; - /* A Print_r ($testnumyes. "
"); + Print_r ($testnumno. "
"); the Print_r ($count _yes. "
"); - Print_r ($count _no. "
"); $ Print_r ($array _no[$i]. "
"); the */ the } the the $py=1; - $PN=1; in for($i= 1;$i<Count($test)-1;$i++){ the $py*=$array _yes[$i]; the $PN*=$array _no[$i]; About } the the $py*=$p _yes; the $PN*=$p _no; + - if($py>$PN)return $flagsyes; the Else return $flagsno;Bayi the /*Print_r ($py. "
"); the Print_r ($PN. "
"); - */ - the } the //-------------------------------------------------------------------- the the $train= Getfilecontent ("Train.txt"); - $test= Getfilecontent ("Test.txt"); the the for($i= 1;$i<Count($test);$i++) the {94 $test[$i][Count($test[0]) -1] = Naivebayes ($test[$i],$trainYN); the } the the /*98 * Read the contents of the array into the. txt About */ - //--------------------------------------------------------------------101 $fp=fopen(' Result.txt ', ' WB ');102 for($i= 0;$i<Count($test);$i++)103 {104 $temp=NULL; the for($j= 0;$j<Count($test[$i]);$j++)106 {107 $temp=$test[$i][$j]." \ t ";108 fwrite($fp,$temp);109 } the fwrite($fp, "\ r \ n");111 } the fclose($fp);113 //-------------------------------------------------------------------- the the /* the * Print Output117 */118 //--------------------------------------------------------------------119 Echo"
";  - Print_r ($test); 121 Echo "
";122 //--------------------------------------------------------------------123?>

  

http://www.bkjia.com/PHPjc/1070666.html www.bkjia.com true http://www.bkjia.com/PHPjc/1070666.html techarticle Naive Bayes (naive Bayesian algorithm) [classification algorithm],naivebayes Nave Bayes (naive Bayesian) classification algorithm implementation (1) Introduction: (2) algorithm Description: (3) 1? PHP 2/* 3 *naive Bayes Plain ... /c5>

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.