使用 Sklearn.naive_bayes.Bernoulli 的朴素贝叶斯分类器;如何使用模型进行预测?
Naive Bayes Classifier using Sklearn.naive_bayes.Bernoulli; how to use model to predict?
我有一个包含这样训练数据集的文件:
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
我有一套句子。我想预测(在这个例子中,现实生活中的句子更长),每个句子中是否包含动物(class)。我已经为每个句子分配了特征 F1 = 是句子中提到的猫,F2 = 是句子中提到的狗,F3 = 是句子中提到的冰箱,F4 = 是句子中提到的汽车,F5 = 是句子中提到的兔子,class是句子中是否是动物)。
那么我有另一个包含句子列表的文件(测试数据集):
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(uniques))
ValueError:发现样本数量不一致的数组:[ 5 10]
但是当我计算列表的长度时,它们似乎都一样长?谁能阐明我在这里做错了什么?
我的第二个问题 'print clf.predict()' 行读作 'print clf.predict(test_dataset)' 是否正确(即句子列表,没有特征或附加 classes ,我想分配给 class 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)
1- 这里有几个问题。
- 首先,确保正确解析文件。如果您的训练文件与上面几行完全一样,那么您可能希望跳过第一行。它包含 header 并且不应出现在您的 X 或 Y 矩阵中。确保功能和 class_name 变量包含您想要的内容。您可以通过打印来查看它们。
sentence.append(line[0])
我猜,您得到的是字符串“0”或“1”,而不是整数值。我不认为这个 scikit 模块可以处理字符串值。您应该将它们转换为整数。它可能类似于 sentence.append(int(line[0]))
list_of_features
变量是 no_of_features x no_of_features 矩阵。它的形状应该是 n_samples x n_features。您可以通过 list_of_features = np.array(list_of_features).T
转置它
2 - 分类器不知道如何将句子映射到特征,所以你必须明确地给出特征。您可以通过遍历句子并检查目标词是否存在来实现。
编辑:
import numpy as np
from sklearn.naive_bayes import BernoulliNB
feature_word_list = ["cat", "dog", "fridge", "car", "rabbit"]
feature1 = [0, 1, 0, 0, 0]
feature2 = [1, 0, 0, 0, 0]
feature3 = [0, 0, 1, 0, 0]
feature4 = [0, 0, 0, 1, 0]
feature5 = [1, 1, 0, 0, 1]
class_name_list = [1, 1, 0, 0, 1]
train_features = np.array([feature1,feature2,feature3,feature4,feature5]).T
clf = BernoulliNB()
clf.fit(train_features, class_name_list)
上面的代码是相同的,除了我直接输入特征值而不是从文件中读取。
test_data = ["this is about dog and cats","not animal related sentence"]
test_feature_list = []
for test_instance in test_data:
test_feature = [1 if feature_word in test_instance else 0 for feature_word in feature_word_list]
test_feature_list.append(test_feature)
test_feature_matrix = np.array(test_feature_list)
print(test_feature_matrix)
现在你的 test_feature_matrix 看起来像这样:
[[1 1 0 0 0]
[0 0 0 0 0]]
请注意,我有 2 个测试数据,因此矩阵有 2 个对应的行,每列代表一个特征值(即句子中是否存在特定单词)。这就是我在第 2 点试图说的,分类器不知道 "cat"、"fridge" 或其他东西,但它需要的是单词是否存在,1 或 0。
现在你可以预测这些测试数据(句子)的标签:
predicted_label_list = clf.predict(test_feature_matrix)
print(predicted_label_list)
给出
的结果
[1 0]
注意: 它可能不适用于您的测试数据,因为它包含不在您的特征 space 和训练数据中的单词。我的意思是你的测试数据包含 "zebra" 但训练集中没有 "zebra" 所以它可能被归类为 0.
我有一个包含这样训练数据集的文件:
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
我有一套句子。我想预测(在这个例子中,现实生活中的句子更长),每个句子中是否包含动物(class)。我已经为每个句子分配了特征 F1 = 是句子中提到的猫,F2 = 是句子中提到的狗,F3 = 是句子中提到的冰箱,F4 = 是句子中提到的汽车,F5 = 是句子中提到的兔子,class是句子中是否是动物)。
那么我有另一个包含句子列表的文件(测试数据集):
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(uniques)) ValueError:发现样本数量不一致的数组:[ 5 10]
但是当我计算列表的长度时,它们似乎都一样长?谁能阐明我在这里做错了什么?
我的第二个问题 'print clf.predict()' 行读作 'print clf.predict(test_dataset)' 是否正确(即句子列表,没有特征或附加 classes ,我想分配给 class 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)
1- 这里有几个问题。
- 首先,确保正确解析文件。如果您的训练文件与上面几行完全一样,那么您可能希望跳过第一行。它包含 header 并且不应出现在您的 X 或 Y 矩阵中。确保功能和 class_name 变量包含您想要的内容。您可以通过打印来查看它们。
sentence.append(line[0])
我猜,您得到的是字符串“0”或“1”,而不是整数值。我不认为这个 scikit 模块可以处理字符串值。您应该将它们转换为整数。它可能类似于sentence.append(int(line[0]))
list_of_features
变量是 no_of_features x no_of_features 矩阵。它的形状应该是 n_samples x n_features。您可以通过list_of_features = np.array(list_of_features).T
转置它
2 - 分类器不知道如何将句子映射到特征,所以你必须明确地给出特征。您可以通过遍历句子并检查目标词是否存在来实现。
编辑:
import numpy as np
from sklearn.naive_bayes import BernoulliNB
feature_word_list = ["cat", "dog", "fridge", "car", "rabbit"]
feature1 = [0, 1, 0, 0, 0]
feature2 = [1, 0, 0, 0, 0]
feature3 = [0, 0, 1, 0, 0]
feature4 = [0, 0, 0, 1, 0]
feature5 = [1, 1, 0, 0, 1]
class_name_list = [1, 1, 0, 0, 1]
train_features = np.array([feature1,feature2,feature3,feature4,feature5]).T
clf = BernoulliNB()
clf.fit(train_features, class_name_list)
上面的代码是相同的,除了我直接输入特征值而不是从文件中读取。
test_data = ["this is about dog and cats","not animal related sentence"]
test_feature_list = []
for test_instance in test_data:
test_feature = [1 if feature_word in test_instance else 0 for feature_word in feature_word_list]
test_feature_list.append(test_feature)
test_feature_matrix = np.array(test_feature_list)
print(test_feature_matrix)
现在你的 test_feature_matrix 看起来像这样:
[[1 1 0 0 0]
[0 0 0 0 0]]
请注意,我有 2 个测试数据,因此矩阵有 2 个对应的行,每列代表一个特征值(即句子中是否存在特定单词)。这就是我在第 2 点试图说的,分类器不知道 "cat"、"fridge" 或其他东西,但它需要的是单词是否存在,1 或 0。
现在你可以预测这些测试数据(句子)的标签:
predicted_label_list = clf.predict(test_feature_matrix)
print(predicted_label_list)
给出
的结果[1 0]
注意: 它可能不适用于您的测试数据,因为它包含不在您的特征 space 和训练数据中的单词。我的意思是你的测试数据包含 "zebra" 但训练集中没有 "zebra" 所以它可能被归类为 0.