【问题标题】:Naive bayes Classification in PythonPython中的朴素贝叶斯分类
【发布时间】:2017-03-17 22:33:26
【问题描述】:

我对python很陌生。我已使用

从 csv 文件中读取所有数据
import csv
import nltk

f = open('C:/Users/Documents/Data/exp.csv')
csv_f = csv.reader(f)

dataset = []

for row in csv_f:
    dataset.append(row)

print (dataset)

现在,我想做nltk.NaiveBayesClassifier 我该怎么做?

【问题讨论】:

  • 你读过this吗?它有一个关于如何使用NaiveBayesClassifier 的示例
  • 您可以在这里通过一些示例找到答案:link

标签: python nltk naivebayes


【解决方案1】:

例如,如果 CSV 的内容如下:

CSV

Size,Color,Shape,Accept
small,blue,oval,yes
small,green,oval,yes
big,green,oval,no
big,red,square,no
small,red,square,no
small,blue,square,yes
big,red,circle,yes

我们想知道使用 nltk 朴素贝叶斯是否会接受 small-red-oval 项,我们可以使用以下代码:

蟒蛇

import csv
import nltk

f = open('C:/Users/Amrit/Documents/Data/exp.csv')
csv_f = csv.reader(f)
csv_f.next()  #skip the header line

dataset = []

for row in csv_f:
    dataset.append(({'size': row[0], 'color': row[1], 'shape': row[2]}, row[3]))

print (dataset)

classifier = nltk.NaiveBayesClassifier.train(dataset)

mydata = {'size':'small', 'color':'red', 'shape':'oval'}
print (mydata, classifier.classify(mydata))

注意:我也在学习。感谢@Francisco Couzo 和@Milad M 提供的链接

【讨论】:

    猜你喜欢
    • 2015-08-27
    • 2016-09-28
    • 2012-07-02
    • 2017-01-10
    • 2013-04-19
    • 2013-11-24
    • 2013-12-02
    • 2015-11-03
    相关资源
    最近更新 更多