【发布时间】:2018-07-30 00:17:13
【问题描述】:
我有一个包含如下训练数据集的文件:
sentence F1 F2 F3 F4 F5 class
this is a dog 0 1 0 0 0 1
i like cats 1 0 0 0 0 1
go to the fridge 0 0 1 0 0 0
i drive a car 0 0 0 1 0 0
i dislike rabbits 0 0 0 0 1 1
我有一组句子。我想预测(在这个例子中,现实生活中的句子更长),每个句子中是否包含动物(类)。我为每个句子分配了特征 F1 = 句子中提到的猫,F2 = 句子中提到的狗,F3 = 句子中提到的冰箱,F4 = 句子中提到的汽车,F5 = 句子中提到的兔子,类是句中是否为动物)。
那么我有另一个包含句子列表的文件(测试数据集):
dolphins live in the sea
bears live in the woods
there is no milk left
where are the zebras
我想使用训练数据集(上面的特征矩阵)训练朴素贝叶斯分类器,然后使用在句子测试文件上制作的模型。我可以这样做吗?
我试过了:
import numpy as np
import sklearn.naive_bayes import BernoulliNB
sentence = []
feature1 = []
feature2 = []
feature3 = []
feature4 = []
feature5 = []
class_name = []
test_dataset = [line.strip() for line in open(sys.argv[2])]
for line in open(sys.argv[1]):
line = line.strip().split('\t')
sentence.append(line[0])
feature1.append(line[1])
feature2.append(line[2])
feature3.append(line[3])
feature4.append(line[4])
feature5.append(line[5])
class_name.append(line[6])
list_of_features = [feature1,feature2,feature3,feature4,feature5]
#I'm not sure if this is right: question 1 below
clf = BernoulliNB()
clf.fit(list_of_features,class_name)
# I'm not sure what goes in the next line: question 2 below
print clf.predict(??)
我有一些问题。
-
当我将代码运行到句子 clf.fit 时,我得到了错误:
文件“naive_bayes.py”,第 28 行,在 clf.fit(list_of_features,class_name) 文件“/usr/local/lib/python2.7/dist-packages/sklearn/naive_bayes.py”,第 527 行,适合 X, y = check_X_y(X, y, 'csr') 文件“/usr/local/lib/python2.7/dist-packages/sklearn/utils/validation.py”,第 520 行,在 check_X_y check_consistent_length(X, y) 文件“/usr/local/lib/python2.7/dist-packages/sklearn/utils/validation.py”,第 176 行,在 check_consistent_length "%s" % str(唯一)) ValueError:发现样本数量不一致的数组:[5 10]
但是当我计算列表的长度时,它们的长度似乎都一样?谁能解释我在这里做错了什么?
-
我的第二个问题是,'print clf.predict()' 行读取 'print clf.predict(test_dataset)' 是否正确(即,我想要的句子列表,没有附加功能或类分配给 0 或 1 类;我现在无法测试这个,因为我似乎无法克服问题 1) 中的错误。
-
作为旁注,一旦我最终可以让它发挥作用,以某种方式计算出预测器的准确性会很棒。但是,我正在努力让基础知识先发挥作用。
编辑 1:修改后的脚本
import numpy as np
from sklearn.naive_bayes import BernoulliNB
import sys
sentence = []
feature1 = []
feature2 = []
feature3 = []
feature4 = []
feature5 = []
class_name = []
for line in open(sys.argv[1]):
line = line.strip().split('\t')
sentence.append(line[0])
feature1.append(int(line[1]))
feature2.append(int(line[2]))
feature3.append(int(line[3]))
feature4.append(int(line[4]))
feature5.append(int(line[5]))
class_name.append(int(line[6]))
print feature1
print feature2
print feature3
print feature4
print feature5
print class_name
list_of_features = [feature1,feature2,feature3,feature4,feature5]
transpos_list_of_features = np.array(list_of_features).T
clf = BernoulliNB()
print clf.fit(transpos_list_of_features,class_name)
#print clf.predict(??)
输出:
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 0, 1, 1, 1, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1]
[0, 0, 0, 0, 0, 0, 0, 1, 1, 1]
BernoulliNB(alpha=1.0, binarize=0.0, class_prior=None, fit_prior=True)
【问题讨论】:
标签: python machine-learning scikit-learn naivebayes