Maison >développement back-end >tutoriel php >Naive Bayes(朴素贝叶斯算法)[分类算法],naivebayes_PHP教程
(1) 简介:
(2) 算法描述:
(3)
<span> 1</span> <?<span>php </span><span> 2</span> <span>/*</span> <span> 3</span> <span>*Naive Bayes朴素贝叶斯算法(分类算法的实现) </span><span> 4</span> <span>*/</span> <span> 5</span> <span> 6</span> <span>/*</span> <span> 7</span> <span>*把.txt中的内容读到数组中保存 </span><span> 8</span> <span>*$filename:文件名称 </span><span> 9</span> <span>*/</span> <span> 10</span> <span>//</span><span>--------------------------------------------------------------------</span> <span> 11</span> <span>function</span> getFileContent(<span>$filename</span><span>) </span><span> 12</span> <span>{ </span><span> 13</span> <span>$array</span> = <span>array</span>(<span>null</span><span>); </span><span> 14</span> <span>$content</span> = <span>file_get_contents</span>(<span>$filename</span><span>); </span><span> 15</span> <span>$result</span> = <span>explode</span>("\r\n",<span>$content</span><span>); </span><span> 16</span> <span>//</span><span>print_r(count($result));</span> <span> 17</span> <span>for</span>(<span>$j</span>=0;<span>$j</span><<span>count</span>(<span>$result</span>);<span>$j</span>++<span>) </span><span> 18</span> <span> { </span><span> 19</span> <span>//</span><span>print_r($result[$j]."<br>");</span> <span> 20</span> <span>$con</span> = <span>explode</span>(" ",<span>$result</span>[<span>$j</span><span>]); </span><span> 21</span> <span>array_push</span>(<span>$array</span>,<span>$con</span><span>); </span><span> 22</span> <span> } </span><span> 23</span> <span>array_splice</span>(<span>$array</span>,0,1<span>); </span><span> 24</span> <span>return</span> <span>$array</span><span>; </span><span> 25</span> <span>} </span><span> 26</span> <span>//</span><span>--------------------------------------------------------------------</span> <span> 27</span> <span> 28</span> <span> 29</span> <span>/*</span> <span> 30</span> <span>*NaiveBayes朴素贝叶斯算法 </span><span> 31</span> <span>*$test:测试文本;$train:训练文本;$flagsyes:yes;$flagsno:no </span><span> 32</span> <span>*/</span> <span> 33</span> <span>//</span><span>--------------------------------------------------------------------</span> <span> 34</span> <span>function</span> NaiveBayes(<span>$test</span>,<span>$train</span>,<span>$flagsyes</span>,<span>$flagsno</span><span>) </span><span> 35</span> <span>{ </span><span> 36</span> <span>$count_yes</span> = 0<span>; </span><span> 37</span> <span>$num</span> = <span>count</span>(<span>$train</span>[0<span>]); </span><span> 38</span> <span>for</span>(<span>$i</span>=1;<span>$i</span><<span>count</span>(<span>$train</span>);<span>$i</span>++<span>) </span><span> 39</span> <span> { </span><span> 40</span> <span>if</span>(<span>$train</span>[<span>$i</span>][<span>$num</span>-1]==<span>$flagsyes</span>)<span>$count_yes</span>++<span>; </span><span> 41</span> <span> } </span><span> 42</span> <span>$p_yes</span> = <span>$count_yes</span> / (<span>count</span>(<span>$train</span>)-1<span>); </span><span> 43</span> <span>$p_no</span> = 1- <span>$p_yes</span><span>; </span><span> 44</span> <span> 45</span> <span>$count_no</span> = <span>count</span>(<span>$train</span>)-1 - <span>$count_yes</span><span>; </span><span> 46</span> <span> 47</span> <span> 48</span> <span>for</span>(<span>$i</span>=1;<span>$i</span><<span>count</span>(<span>$test</span>)-1;<span>$i</span>++<span>) </span><span> 49</span> <span> { </span><span> 50</span> <span>$testnumyes</span> = 0<span>; </span><span> 51</span> <span>$testnumno</span> = 0<span>; </span><span> 52</span> <span>for</span>(<span>$j</span>=1;<span>$j</span><<span>count</span>(<span>$train</span>);<span>$j</span>++<span>) </span><span> 53</span> <span> { </span><span> 54</span> <span>if</span>((<span>$train</span>[<span>$j</span>][<span>$i</span>]==<span>$test</span>[<span>$i</span>])&&(<span>$train</span>[<span>$j</span>][<span>count</span>(<span>$test</span>)-1]==<span>$flagsyes</span>))<span>$testnumyes</span>++<span>; </span><span> 55</span> <span>else</span> <span>if</span>((<span>$train</span>[<span>$j</span>][<span>$i</span>]==<span>$test</span>[<span>$i</span>])&&(<span>$train</span>[<span>$j</span>][<span>count</span>(<span>$test</span>)-1]==<span>$flagsno</span>))<span>$testnumno</span>++<span>; </span><span> 56</span> <span> } </span><span> 57</span> <span> 58</span> <span>$array_yes</span>[<span>$i</span>] = <span>$testnumyes</span> / <span>$count_yes</span><span> ; </span><span> 59</span> <span>$array_no</span>[<span>$i</span>] = <span>$testnumno</span> / <span>$count_no</span><span> ; </span><span> 60</span> <span>/*</span> <span> 61</span> <span> print_r($testnumyes."<br>"); </span><span> 62</span> <span> print_r($testnumno."<br>"); </span><span> 63</span> <span> print_r($count_yes."<br>"); </span><span> 64</span> <span> print_r($count_no."<br>"); </span><span> 65</span> <span> print_r($array_no[$i]."<br>"); </span><span> 66</span> <span>*/</span> <span> 67</span> <span> } </span><span> 68</span> <span> 69</span> <span>$py</span>=1<span>; </span><span> 70</span> <span>$pn</span>=1<span>; </span><span> 71</span> <span>for</span>(<span>$i</span>=1;<span>$i</span><<span>count</span>(<span>$test</span>)-1;<span>$i</span>++<span>){ </span><span> 72</span> <span>$py</span> *= <span>$array_yes</span>[<span>$i</span><span>]; </span><span> 73</span> <span>$pn</span> *= <span>$array_no</span>[<span>$i</span><span>]; </span><span> 74</span> <span> } </span><span> 75</span> <span> 76</span> <span>$py</span> *= <span>$p_yes</span><span>; </span><span> 77</span> <span>$pn</span> *= <span>$p_no</span><span>; </span><span> 78</span> <span> 79</span> <span>if</span>(<span>$py</span>><span>$pn</span>)<span>return</span> <span>$flagsyes</span><span>; </span><span> 80</span> <span>else</span> <span>return</span> <span>$flagsno</span><span>; </span><span> 81</span> <span> 82</span> <span>/*</span><span> print_r($py."<br>"); </span><span> 83</span> <span> print_r($pn."<br>"); </span><span> 84</span> <span>*/</span> <span> 85</span> <span> 86</span> <span>} </span><span> 87</span> <span>//</span><span>--------------------------------------------------------------------</span> <span> 88</span> <span> 89</span> <span>$train</span> = getFileContent("train.txt"<span>); </span><span> 90</span> <span>$test</span> = getFileContent("test.txt"<span>); </span><span> 91</span> <span> 92</span> <span>for</span>(<span>$i</span>=1;<span>$i</span><<span>count</span>(<span>$test</span>);<span>$i</span>++<span>) </span><span> 93</span> <span>{ </span><span> 94</span> <span>$test</span>[<span>$i</span>][<span>count</span>(<span>$test</span>[0])-1] = NaiveBayes(<span>$test</span>[<span>$i</span>],<span>$train</span>,Y,<span>N); </span><span> 95</span> <span>} </span><span> 96</span> <span> 97</span> <span>/*</span> <span> 98</span> <span>*将数组中的内容读到.txt中 </span><span> 99</span> <span>*/</span> <span>100</span> <span>//</span><span>--------------------------------------------------------------------</span> <span>101</span> <span>$fp</span>= <span>fopen</span>('result.txt','wb'<span>); </span><span>102</span> <span>for</span>(<span>$i</span>=0;<span>$i</span><<span>count</span>(<span>$test</span>);<span>$i</span>++<span>) </span><span>103</span> <span>{ </span><span>104</span> <span>$temp</span> = <span>NULL</span><span>; </span><span>105</span> <span>for</span>(<span>$j</span>=0;<span>$j</span><<span>count</span>(<span>$test</span>[<span>$i</span>]);<span>$j</span>++<span>) </span><span>106</span> <span> { </span><span>107</span> <span>$temp</span> = <span>$test</span>[<span>$i</span>][<span>$j</span>]."\t"<span>; </span><span>108</span> <span>fwrite</span>(<span>$fp</span>,<span>$temp</span><span>); </span><span>109</span> <span> } </span><span>110</span> <span>fwrite</span>(<span>$fp</span>,"\r\n"<span>); </span><span>111</span> <span>} </span><span>112</span> <span>fclose</span>(<span>$fp</span><span>); </span><span>113</span> <span>//</span><span>--------------------------------------------------------------------</span> <span>114</span> <span>115</span> <span>/*</span> <span>116</span> <span>*打印输出 </span><span>117</span> <span>*/</span> <span>118</span> <span>//</span><span>--------------------------------------------------------------------</span> <span>119</span> <span>echo</span> "<pre class="brush:php;toolbar:false">"<span>; </span><span>120</span> <span>print_r</span>(<span>$test</span><span>); </span><span>121</span> <span>echo</span> ""; 122 //-------------------------------------------------------------------- 123 ?>