2016-11-04 26 views
0

私はPythonにはかなり新しいです。 I今PythonのNaive Bayes分類

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) 

を使用してCSVファイルからのすべてのデータを読んでいる、私はそれをどのように行うことができますnltk.NaiveBayesClassifier をしたいですか?

+1

あなたは[this](http://www.nltk.org/book/ch06.html)を読んでいますか? 'NaiveBayesClassifier'の使い方の例があります –

+1

あなたの答えはここにあります:[link](http://stackoverflow.com/questions/20827741/nltk-naivebayesclassifier-training-for-sentiment-analysis) –

答えて

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 Naive Bayesを使用して承認された場合、次のコードを使用できます。

python

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

注:私も学んでいます。 @Franscisco Couzoと@Milad Mが提供するリンクありがとう

関連する問題