Naive Bayes(樸素貝葉斯演算法)[分類演算法],naivebayes_PHP教程

來源:互聯網
上載者:User

Naive Bayes(樸素貝葉斯演算法)[分類演算法],naivebayes


Naïve Bayes(樸素貝葉斯)分類演算法的實現

(1) 簡介:

(2) 演算法描述:

(3)

  1 php  2 /*  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<count($result);$j++) 18     { 19         //print_r($result[$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:no 32 */ 33 //-------------------------------------------------------------------- 34 function NaiveBayes($test,$train,$flagsyes,$flagsno) 35 { 36 $count_yes = 0; 37 $num = count($train[0]); 38 for($i=1;$i<count($train);$i++) 39 { 40 if($train[$i][$num-1]==$flagsyes)$count_yes++; 41 } 42 $p_yes = $count_yes / (count($train)-1); 43 $p_no = 1- $p_yes; 44 45 $count_no = count($train)-1 - $count_yes; 46 47 48 for($i=1;$i<count($test)-1;$i++) 49 { 50 $testnumyes = 0; 51 $testnumno = 0; 52 for($j=1;$j<count($train);$j++) 53 { 54 if(($train[$j][$i]==$test[$i])&&($train[$j][count($test)-1]==$flagsyes))$testnumyes++; 55 else if(($train[$j][$i]==$test[$i])&&($train[$j][count($test)-1]==$flagsno))$testnumno++; 56 } 57 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<count($test)-1;$i++){ 72 $py *= $array_yes[$i]; 73 $pn *= $array_no[$i]; 74 } 75 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<count($test);$i++) 93 { 94 $test[$i][count($test[0])-1] = NaiveBayes($test[$i],$train,Y,N); 95 } 96 97 /* 98 *將數組中的內容讀到.txt中 99 */100 //--------------------------------------------------------------------101 $fp= fopen('result.txt','wb');102 for($i=0;$i<count($test);$i++)103 {104 $temp = NULL;105 for($j=0;$j<count($test[$i]);$j++)106 {107 $temp = $test[$i][$j]."\t";108 fwrite($fp,$temp);109 }110 fwrite($fp,"\r\n");111 }112 fclose($fp);113 //--------------------------------------------------------------------114 115 /*116 *列印輸出117 */118 //--------------------------------------------------------------------119 echo "
";120 print_r($test);121 echo "
";122 //--------------------------------------------------------------------123 ?>

  

http://www.bkjia.com/PHPjc/1070666.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1070666.htmlTechArticleNaive Bayes(樸素貝葉斯演算法)[分類演算法],naivebayes Nave Bayes(樸素貝葉斯)分類演算法的實現 (1) 簡介: (2) 演算法描述: (3) 1 ? php 2 /* 3 *Naive Bayes樸素...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.