【发布时间】:2019-02-14 13:15:54
【问题描述】:
我已经发布了我的示例火车数据以及测试数据以及我的代码。我正在尝试使用朴素贝叶斯算法来训练模型。
但是,在评论中,我得到了列表列表。所以,我认为我的代码因以下错误而失败:
return lambda x: strip_accents(x.lower())
AttributeError: 'list' object has no attribute 'lower'
你们中的任何人都可以帮我解决一下我是 python 新手的问题吗....
train.txt:
review,label
Colors & clarity is superb,positive
Sadly the picture is not nearly as clear or bright as my 40 inch Samsung,negative
test.txt:
review,label
The picture is clear and beautiful,positive
Picture is not clear,negative
我的代码:
from sklearn.preprocessing import MultiLabelBinarizer
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import BernoulliNB
from sklearn.metrics import confusion_matrix
from sklearn.feature_extraction.text import CountVectorizer
def load_data(filename):
reviews = list()
labels = list()
with open(filename) as file:
file.readline()
for line in file:
line = line.strip().split(',')
labels.append(line[1])
reviews.append(line[0].split())
return reviews, labels
X_train, y_train = load_data('/Users/7000015504/Desktop/Sep_10/sample_train.csv')
X_test, y_test = load_data('/Users/7000015504/Desktop/Sep_10/sample_test.csv')
clf = CountVectorizer()
X_train_one_hot = clf.fit(X_train)
X_test_one_hot = clf.transform(X_test)
bnbc = BernoulliNB(binarize=None)
bnbc.fit(X_train_one_hot, y_train)
score = bnbc.score(X_test_one_hot, y_test)
print("score of Naive Bayes algo is :" , score)
【问题讨论】:
-
lower()不是list的属性。尝试将其转换为numpy数组。然后.lower()应该可以工作了。 -
你的代码中
return lambda x: strip_accents(x.lower())在哪里? -
这是一段简单的代码。可以自己管理,????
标签: python scikit-learn