训练两个特征而不是一个

我有这段代码。我有两个特征。我如何将这两个特征一起训练?

from textblob import TextBlob, Word, Blobberfrom textblob.classifiers import NaiveBayesClassifierfrom textblob.taggers import NLTKTaggerimport reimport nltkdef 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))

这是我得到的输出。

word_before_you = ‘do’ refere : generi = 2.2 : 1.0
word_before_you = ‘when’ generi : refere = 1.1 : 1.0

看起来它只获取了最后一个特征。我如何让分类器同时训练两个特征而不是一个?


回答:

您定义了lang_detector两次,第二次定义直接覆盖了第一次。您应该定义一个特征提取函数,该函数返回一个特征字典,每个特征名称作为键。在您的案例中,您可以定义get_word_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)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注