Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 训练两个功能,而不是一个_Python_Machine Learning_Nlp_Nltk_Textblob - Fatal编程技术网

Python 训练两个功能,而不是一个

Python 训练两个功能,而不是一个,python,machine-learning,nlp,nltk,textblob,Python,Machine Learning,Nlp,Nltk,Textblob,我有这个密码。我有两个特点。如何同时训练这两个功能 from textblob import TextBlob, Word, Blobber from textblob.classifiers import NaiveBayesClassifier from textblob.taggers import NLTKTagger import re import nltk def get_word_before_you_feature(mystring): keyword = 'yo

我有这个密码。我有两个特点。如何同时训练这两个功能

from textblob import TextBlob, Word, Blobber
from textblob.classifiers import NaiveBayesClassifier
from textblob.taggers import NLTKTagger
import re
import nltk



def get_word_before_you_feature(mystring):
    keyword = 'you'
    before_keyword, keyword, after_keyword = mystring.partition(keyword)
    before_keyword = before_keyword.rsplit(None, 1)[-1]
    return {'word_after_you': before_keyword}


def get_word_after_you_feature(mystring):
    keyword = 'you'
    before_keyword, keyword, after_keyword = mystring.partition(keyword)
    after_keyword = after_keyword.split(None, 1)[0]
    return {'word_after_you': after_keyword}
    classifier = nltk.NaiveBayesClassifier.train(train)



lang_detector = NaiveBayesClassifier(train, feature_extractor=get_word_after_you_feature)
lang_detector = NaiveBayesClassifier(train, feature_extractor=get_word_before_you_feature)


print(lang_detector.accuracy(test))
print(lang_detector.show_informative_features(5))
这是我得到的输出

你之前的单词是“do”referee:generi=2.2:1.0
单词“在你之前”=“当”generi:referee=1.1:1.0


它似乎只有最后一个功能。如何让分类器同时训练两个特征而不是一个特征。

您定义了两次
lang\u检测器
,而第二个定义只是覆盖了第一个定义。定义一个要素提取器函数,该函数返回要素字典,每个要素名称作为键。在您的情况下,您可以定义
get\u word\u features(mystring)
,它可以返回如下字典:

return { 
     'word_after_you': after_keyword, 
     'word_before_you': before_keyword 
      }
剩下的就是您一直在做的事情:将特征检测器函数传递给分类器的构造函数,并检查结果

lang_detector = NaiveBayesClassifier(train, feature_extractor=get_word_features)
lang_detector.show_most_informative_features(5)