PHP实现机器学习之朴素贝叶斯算法详解
发布时间:2021-03-31 11:46:25 所属栏目:编程 来源:网络整理
导读:本篇章节讲授PHP实现呆板进修之朴实贝叶斯算法。供各人参考研究详细如下: 呆板进修已经在我们的糊口中变获得处可见了。好比从你在家的时辰温控器开始事变到智能汽车以及我们口袋中的智妙手机。呆板进修看上去已经无处不在而且是一个很是值得试探
github上完备php代码如下: [],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+/',strtolower($string))); } } $classifier = new Classifier(); $classifier->learn('Symfony is the best',Type::NEGATIVE); var_dump($classifier->guess('Symfony is great')); // string(8) "positive" var_dump($classifier->guess('I complain a lot')); // string(8) "negative"竣事语 尽量我们只举办了很少的实习,可是算法照旧应该能给出相对准确的功效。在真真相形,你可以让呆板进修成百上千的记录,这样就可以给出更精准的功效。你可以下载查察这篇文章(英文):。 并且,朴实贝叶斯不只仅可以运用到文本类的应用。但愿通过这篇文章可以拉近你和呆板进修的一点点间隔。 原文地点: 更多关于PHP相干内容感乐趣的读者可查察本站专题:《》、《》、《》、《》、《》及《》 但愿本文所述对各人PHP措施计划有所辅佐。 (编辑:湖南网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
站长推荐
热点阅读