请选择 进入手机版 | 继续访问电脑版
楼主: oliyiyi
2983 12

Naive Bayes(示例代码 python 和 R) [推广有奖]

版主

泰斗

0%

还不是VIP/贵宾

-

TA的文库  其他...

计量文库

威望
7
论坛币
271951 个
通用积分
31269.3519
学术水平
1435 点
热心指数
1554 点
信用等级
1345 点
经验
383775 点
帖子
9598
精华
66
在线时间
5468 小时
注册时间
2007-5-21
最后登录
2024-4-18

初级学术勋章 初级热心勋章 初级信用勋章 中级信用勋章 中级学术勋章 中级热心勋章 高级热心勋章 高级学术勋章 高级信用勋章 特级热心勋章 特级学术勋章 特级信用勋章

oliyiyi 发表于 2017-9-12 15:55:24 |显示全部楼层 |坛友微信交流群

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

本帖隐藏的内容

Naive Bayes

It is a classification technique based on Bayes’ theorem with an assumption of independence between predictors. In simple terms, a Naive Bayes classifier assumes that the presence of a particular feature in a class is unrelated to the presence of any other feature. For example, a fruit may be considered to be an apple if it is red, round, and about 3 inches in diameter. Even if these features depend on each other or upon the existence of the other features, a naive Bayes classifier would consider all of these properties to independently contribute to the probability that this fruit is an apple.

Naive Bayesian model is easy to build and particularly useful for very large data sets. Along with simplicity, Naive Bayes is known to outperform even highly sophisticated classification methods.

Bayes theorem provides a way of calculating posterior probability P(c|x) from P(c), P(x) and P(x|c). Look at the equation below:

Here,

  • P(c|x) is the posterior probability of class (target) given predictor (attribute).
  • P(c) is the prior probability of class.
  • P(x|c) is the likelihood which is the probability of predictor given class.
  • P(x) is the prior probability of predictor.

Example: Let’s understand it using an example. Below I have a training data set of weather and corresponding target variable ‘Play’. Now, we need to classify whether players will play or not based on weather condition. Let’s follow the below steps to perform it.

Step 1: Convert the data set to frequency table

Step 2: Create Likelihood table by finding the probabilities like Overcast probability = 0.29 and probability of playing is 0.64.

Step 3: Now, use Naive Bayesian equation to calculate the posterior probability for each class. The class with the highest posterior probability is the outcome of prediction.

Problem: Players will pay if weather is sunny, is this statement is correct?

We can solve it using above discussed method, so P(Yes | Sunny) = P( Sunny | Yes) * P(Yes) / P (Sunny)

Here we have P (Sunny |Yes) = 3/9 = 0.33, P(Sunny) = 5/14 = 0.36, P( Yes)= 9/14 = 0.64

Now, P (Yes | Sunny) = 0.33 * 0.64 / 0.36 = 0.60, which has higher probability.

Naive Bayes uses a similar method to predict the probability of different class based on various attributes. This algorithm is mostly used in text classification and with problems having multiple classes.

Python Code
  1. #Import Library
  2. from sklearn.naive_bayes import GaussianNB
  3. #Assumed you have, X (predictor) and Y (target) for training data set and x_test(predictor) of test_dataset
  4. # Create SVM classification object model = GaussianNB() # there is other distribution for multinomial classes like Bernoulli Naive Bayes, Refer link
  5. # Train the model using the training sets and check score
  6. model.fit(X, y)
  7. #Predict Output
  8. predicted= model.predict(x_test)
复制代码

R Code

  1. library(e1071)
  2. x <- cbind(x_train,y_train)
  3. # Fitting model
  4. fit <-naiveBayes(y_train ~ ., data = x)
  5. summary(fit)
  6. #Predict Output
  7. predicted= predict(fit,x_test)
复制代码


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:python naive Bayes baye bay

缺少币币的网友请访问有奖回帖集合
https://bbs.pinggu.org/thread-3990750-1-1.html
西门高 发表于 2017-9-12 15:58:00 来自手机 |显示全部楼层 |坛友微信交流群
oliyiyi 发表于 2017-9-12 15:55
**** 本内容被作者隐藏 ****
谢谢分享

使用道具

hyq2003 发表于 2017-9-12 16:03:50 |显示全部楼层 |坛友微信交流群

使用道具

deem 学生认证  发表于 2017-9-12 17:05:10 |显示全部楼层 |坛友微信交流群
谢谢分享

使用道具

MouJack007 发表于 2017-9-12 19:31:29 |显示全部楼层 |坛友微信交流群
谢谢楼主分享!

使用道具

MouJack007 发表于 2017-9-12 19:32:00 |显示全部楼层 |坛友微信交流群

使用道具

ekscheng 发表于 2017-9-12 23:36:56 |显示全部楼层 |坛友微信交流群

使用道具

minixi 发表于 2017-9-13 10:53:49 |显示全部楼层 |坛友微信交流群
谢谢分享

使用道具


Thanks for Sharing!

使用道具

tl19870822 发表于 2017-9-26 11:14:07 |显示全部楼层 |坛友微信交流群
好,非常好的帖子

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-18 22:12