2017-04-04 88 views
0

我想训练一个朴素贝叶斯分类器,我遇到了数据问题。我打算将其用于抽取文字摘要。朴素贝叶斯分类器提取摘要

Example_Input: It was a sunny day. The weather was nice and the birds were singing. 
Example_Output: The weather was nice and the birds were singing. 

我有一个数据集,我打算使用,并且在每个文档中至少有1个句子用于摘要。

我决定使用sklearn,但我不知道如何表示我拥有的数据。即X和y。

from sklearn.naive_bayes import MultinomialNB 
clf = MultinomialNB().fit(X, y) 

最接近于我的脑海里是让这样的:

X = [ 
     'It was a sunny day. The weather was nice and the birds were singing.', 
     'I like trains. Hi, again.' 
    ] 

y = [ 
     [0,1], 
     [1,0] 
    ] 

在目标值意味着1 - 包含在总结和0 - 不包括在内。不幸的是,这可能会导致不良的形状异常,因为预计y是一维数组。我想不出一种代表它的方式,所以请帮助。

顺便说一句,我没有直接使用X中的字符串值,而是将它们表示为来自sklearn的具有CountVectorizerTfidfTransformer的向量。

回答

1

根据您的要求,您正在对数据进行分类。这意味着,你需要分开每个句子来预测它的类。

例如:
而不是使用:

X = [ 
     'It was a sunny day. The weather was nice and the birds were singing.', 
     'I like trains. Hi, again.' 
    ] 

使用它如下:NLTK的

X = [ 
     'It was a sunny day.', 
     'The weather was nice and the birds were singing.', 
     'I like trains.', 
     'Hi, again.' 
    ] 

用文标记生成器来实现这一目标。

现在,对于标签,使用两个类。让我们说1是,0代表没有。

y = [ 
     [0,], 
     [1,], 
     [1,], 
     [0,] 
    ] 

现在,使用这些数据来拟合和预测你想要的方式!

希望它有帮助!

+0

感谢您的回答。它会起作用,肯定比我的要好,但这样分类器就不会考虑文档中句子的位置,因为所有内容都将被视为一个整体。有没有一种方法可以包含这一点。 – Nikola

+0

@nikola以多行作为输入并使用nltk句子标记器分割它,并预测每个句子,但仅将那些句子打印到输出具有第1类预测的输出,即,是 – abhinav