For example, if the content of the CSV is the following:
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
And we want to know whether a small-red-oval item will be accepted using the nltk Naive Bayes, we can use the following code:
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))
Note: I'm also learning. Thanks to links provided by @Franscisco Couzo and @Milad M