PHP实现机器学习之朴素贝叶斯算法详解

2025-05-29 0 66

本文实例讲述了php实现机器学习朴素贝叶斯算法。分享给大家供大家参考,具体如下:

机器学习已经在我们的生活中变得随处可见了。比如从你在家的时候温控器开始工作到智能汽车以及我们口袋中的智能手机。机器学习看上去已经无处不在并且是一个非常值得探索的领域。但是什么是机器学习呢?通常来说,机器学习就是让系统不断的学习并且对新的问题进行预测。从简单的预测购物商品到复杂的数字助理预测。

在这篇文章我将会使用朴素贝叶斯算法clasifier作为一个类来介绍。这是一个简单易于实施的算法,并且可给出满意的结果。但是这个算法是需要一点统计学的知识去理解的。在文章的最后部分你可以看到一些实例代码,甚至自己去尝试着自己做一下你的机器学习

起步

那么,这个classifier是要用来实现什么功能呢?其实它主要是用来判断给定的语句是积极地还是消极的。比如,“symfony is the best”是一个积极的语句,“no symfony is bad”是一个消极的语句。所以在给定了一个语句之后,我想让这个classifier在我不给定一个新的规则的情况就返回一个语句类型。

我给classifier命名了一个相同名称的类,并且包含一个guess方法。这个方法接受一个语句的输入,并且会返回这个语句是积极的还是消极的。这个类就像下面这样:

?

1

2

3

4

5
class classifier

{

public function guess($statement)

{}

}

我更喜欢使用枚举类型的类而不是字符串作为我的返回值。我将这个枚举类型的类命名为type,并且包含两个常量:一个positive,一个negative。这两个常量将会当做guess方法的返回值。

?

1

2

3

4

5
class type

{

const positive = 'positive';

const negative = 'negative';

}

初始化工作已经完成,接下来就是要编写我们的算法进行预测了。

朴素贝叶斯

朴素贝叶斯算法是基于一个训练集合工作的,根据这个训练集从而做出相应的预测。这个算法运用了简单的统计学以及一点数学去进行结果的计算。比如像下面四个文本组成的训练集合:

语句 类型
symfony is the best positive
phpstorm is great positive
iltar complains a lot negative
no symfony is bad negative



如果给定语句是“symfony is the best”,那么你可以说这个语句是积极地。你平常也会根据之前学习到的相应知识做出对应的决定,朴素贝叶斯算法也是同样的道理:它根据之前的训练集来决定哪一个类型更加相近。

学习

在这个算法正式工作之前,它需要大量的历史信息作为训练集。它需要知道两件事:每一个类型对应的词产生了多少次和每一个语句对应的类型是什么。我们在实施的时候会将这两种信息存储在两个数组当中。一个数组包含每一类型的词语统计,另一个数组包含每一个类型的语句统计。所有的其他信息都可以从这两个数组中聚合。代码就像下面的一样:

?

1

2

3

4

5

6

7

8

9

10

11
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,像下面这样:

?

1

2

3

4
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:

?

1

2

3

4

5
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)。

最后,这个算法应该只关心关键词而忽略其他的因素。一个简单的方法就是将给定的字符串中的单词分离出来:

?

1

2

3

4
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中实际操作一下了:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17
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;

}

这就是所有的工作,现在算法可以预测语句的类型了。你要做的就是让你的算法开始学习:

?

1

2

3

4

5

6

7
$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代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63
<?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程序设计有所帮助。

原文链接:http://blog.csdn.net/u012543061/article/details/53005029

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 PHP实现机器学习之朴素贝叶斯算法详解 https://www.kuaiidc.com/93495.html

相关文章

发表评论
暂无评论