1

我正在尝试在 python 中使用 TextBlob 在推文上实现朴素贝叶斯分类器。我已经能够训练数据集,并且可以使用以下方法成功对单个推文进行分类:

print cl.classify("text")

现在我想打开一个 csv 文件并对该文件中的所有推文进行分类。关于如何实现这一目标的任何建议?我的代码如下:

import csv
from textblob import TextBlob

with open(test_path, 'rU') as csvfile:
    lineReader = csv.reader(csvfile,delimiter=',',quotechar="\"")
    lineReader = csv.reader(csvfile,delimiter=',')

    test = []
    for row in lineReader:
      blob = (row[0]) 
      blob = TextBlob(blob)
      test.append([blob])

      print (test.classify())

AttributeError:“列表”对象没有属性“分类”

4

1 回答 1

0

You need to train first too also (not clear if you have done this?),

train = [] 
# then repeat your above lines, appending each tweet to train set
# but for a separate training set (or slice up the rows)

# do your test append loop -----

# 1. Now train a model
my_classifier = NaiveBayesClassifier(train)

# 2. test given to the model to get accuracy
accu = my_classifier.accuracy(test)
于 2016-07-17T17:57:14.543 回答