語句 | 類型 |
Symfony is the best | Positive |
PhpStorm is great | Positive |
Iltar complains a lot | Negative |
No Symfony is bad | Negative |
如果給定語句是“Symfony is the best”,那么你可以說這個語句是積極地。你平常也會根據之前學習到的相應知識做出對應的決定,樸素貝葉斯算法也是同樣的道理:它根據之前的訓練集來決定哪一個類型更加相近。
學習
在這個算法正式工作之前,它需要大量的歷史信息作為訓練集。它需要知道兩件事:每一個類型對應的詞產生了多少次和每一個語句對應的類型是什么。我們在實施的時候會將這兩種信息存儲在兩個數組當中。一個數組包含每一類型的詞語統計,另一個數組包含每一個類型的語句統計。所有的其他信息都可以從這兩個數組中聚合。代碼就像下面的一樣:
function learn($statement, $type) { $words = $this->getWords($statement); foreach ($words as $word) { if (!isset($this->words[$type][$word])) { $this->words[$type][$word] = 0; } $this->words[$type][$word]++; // 增加類型的詞語統計 } $this->documents[$type]++; // 增加類型的語句統計 }
有了這個集合以后,現在這個算法就可以根據歷史數據接受預測訓練了。
定義
為了解釋這個算法是如何工作的,幾個定義是必要的。首先,讓我們定義一下輸入的語句是給定類型中的一個的概率。這個將會表示為P(Type)。它是以已知類型的數據的類型作為分子,還有整個訓練集的數據數量作為分母來得出的。一個數據就是整個訓練集中的一個。到現在為止,這個方法可以將會命名為totalP,像下面這樣:
function totalP($type) { return ($this->documents[$type] + 1) / (array_sum($this->documents) + 1); }
請注意,在這里分子和分母都加了1。這是為了避免分子和分母都為0的情況。
根據上面的訓練集的例子,積極和消極的類型都會得出0.6的概率。每中類型的數據都是2個,一共是4個數據所以就是(2+1)/(4+1)。
第二個要定義的是對于給定的一個詞是屬于哪個確定類型的概率。這個我們定義成P(word,Type)。首先我們要得到一個詞在訓練集中給出確定類型出現的次數,然后用這個結果來除以整個給定類型數據的詞數。這個方法我們定義為p:
function p($word, $type) { $count = isset($this->words[$type][$word]) ? $this->words[$type][$word] : 0; return ($count + 1) / (array_sum($this->words[$type]) + 1); }
在本次的訓練集中,“is”的是積極類型的概率為0.375。這個詞在整個積極的數據中的7個詞中占了兩次,所以結果就是(2+1)/(7+1)。
最后,這個算法應該只關心關鍵詞而忽略其他的因素。一個簡單的方法就是將給定的字符串中的單詞分離出來:
function getWords($string) { return preg_split('/\s+/', preg_replace('/[^A-Za-z0-9\s]/', '', strtolower($string))); }
準備工作都做好了,開始真正實施我們的計劃吧!
預測
為了預測語句的類型,這個算法應該計算所給定語句的兩個類型的概率。像上面一樣,我們定義一個P(Type,sentence)。得出概率高的類型將會是Classifier類中算法返回的結果。
為了計算P(Type,sentence),算法當中將用到貝葉斯定理。算法像這樣被定義:P(Type,sentence)= P(Type)* P(sentence,Type)/ P(sentence)。這意味著給定語句的類型概率和給定類型語句概率除以語句的概率的結果是相同的。
那么算法在計算每一個相同語句的P(Tyoe,sentence),P(sentence)是保持一樣的。這意味著算法就可以省略其他因素,我們只需要關心最高的概率而不是實際的值。計算就像這樣:P(Type,sentence) = P(Type)* P(sentence,Type)。
最后,為了計算P(sentence,Type),我們可以為語句中的每個詞添加一條鏈式規則。所以在一條語句中如果有n個詞的話,它將會和P(word_1,Type)* P(word_2,Type)* P(word_3,Type)* .....*P(word_n,Type)是一樣的。每一個詞計算結果的概率使用了我們前面看到的定義。
好了,所有的都說完了,是時候在php中實際操作一下了:
function guess($statement) { $words = $this->getWords($statement); // 得到單詞 $best_likelihood = 0; $best_type = null; foreach ($this->types as $type) { $likelihood = $this->pTotal($type); //計算 P(Type) foreach ($words as $word) { $likelihood *= $this->p($word, $type); // 計算 P(word, Type) } if ($likelihood > $best_likelihood) { $best_likelihood = $likelihood; $best_type = $type; } } return $best_type; }
這就是所有的工作,現在算法可以預測語句的類型了。你要做的就是讓你的算法開始學習:
$classifier = new Classifier(); $classifier->learn('Symfony is the best', Type::POSITIVE); $classifier->learn('PhpStorm is great', Type::POSITIVE); $classifier->learn('Iltar complains a lot', Type::NEGATIVE); $classifier->learn('No Symfony is bad', Type::NEGATIVE); var_dump($classifier->guess('Symfony is great')); // string(8) "positive" var_dump($classifier->guess('I complain a lot')); // string(8) "negative"
所有的代碼我已經上傳到了GIT上,https://github.com/yannickl88/blog-articles/blob/master/src/machine-learning-naive-bayes/Classifier.php
github上完整php代碼如下:
?php class Type { const POSITIVE = 'positive'; const NEGATIVE = 'negative'; } class Classifier { private $types = [Type::POSITIVE, Type::NEGATIVE]; private $words = [Type::POSITIVE => [], Type::NEGATIVE => []]; private $documents = [Type::POSITIVE => 0, Type::NEGATIVE => 0]; public function guess($statement) { $words = $this->getWords($statement); // get the words $best_likelihood = 0; $best_type = null; foreach ($this->types as $type) { $likelihood = $this->pTotal($type); // calculate P(Type) foreach ($words as $word) { $likelihood *= $this->p($word, $type); // calculate P(word, Type) } if ($likelihood > $best_likelihood) { $best_likelihood = $likelihood; $best_type = $type; } } return $best_type; } public function learn($statement, $type) { $words = $this->getWords($statement); foreach ($words as $word) { if (!isset($this->words[$type][$word])) { $this->words[$type][$word] = 0; } $this->words[$type][$word]++; // increment the word count for the type } $this->documents[$type]++; // increment the document count for the type } public function p($word, $type) { $count = 0; if (isset($this->words[$type][$word])) { $count = $this->words[$type][$word]; } return ($count + 1) / (array_sum($this->words[$type]) + 1); } public function pTotal($type) { return ($this->documents[$type] + 1) / (array_sum($this->documents) + 1); } public function getWords($string) { return preg_split('/\s+/', preg_replace('/[^A-Za-z0-9\s]/', '', strtolower($string))); } } $classifier = new Classifier(); $classifier->learn('Symfony is the best', Type::POSITIVE); $classifier->learn('PhpStorm is great', Type::POSITIVE); $classifier->learn('Iltar complains a lot', Type::NEGATIVE); $classifier->learn('No Symfony is bad', Type::NEGATIVE); var_dump($classifier->guess('Symfony is great')); // string(8) "positive" var_dump($classifier->guess('I complain a lot')); // string(8) "negative"
結束語
盡管我們只進行了很少的訓練,但是算法還是應該能給出相對精確的結果。在真實環境,你可以讓機器學習成百上千的記錄,這樣就可以給出更精準的結果。你可以下載查看這篇文章(英文):樸素貝葉斯已經被證明可以給出情緒統計的結果。
而且,樸素貝葉斯不僅僅可以運用到文本類的應用。希望通過這篇文章可以拉近你和機器學習的一點點距離。
原文地址:https://stovepipe.systems/post/machine-learning-naive-bayes
更多關于PHP相關內容感興趣的讀者可查看本站專題:《PHP數據結構與算法教程》、《php程序設計算法總結》、《php字符串(string)用法總結》、《PHP數組(Array)操作技巧大全》、《PHP常用遍歷算法與技巧總結》及《PHP數學運算技巧總結》
希望本文所述對大家PHP程序設計有所幫助。