Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.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 如何在scikit学习中提取多项式NB管道训练模型中的单词特征?_Python_Machine Learning_Nlp_Scikit Learn_Artificial Intelligence - Fatal编程技术网

Python 如何在scikit学习中提取多项式NB管道训练模型中的单词特征?

Python 如何在scikit学习中提取多项式NB管道训练模型中的单词特征?,python,machine-learning,nlp,scikit-learn,artificial-intelligence,Python,Machine Learning,Nlp,Scikit Learn,Artificial Intelligence,现在我可以从基于的gs_分类器中获取特征_log_prob_。这里有一个例子 我的问题是如何使单词对应于每个对数概率? CountVectorizer()和TfidTransformer()都进行了功能选择。 GridSearchCV对象在何处存储选定的word/ngram功能?如何使它们与概率相匹配 我已经检查了gs_分类器的成员,但没有找到所选的特征。谢谢 以下是一个可运行的示例: # Note: The runnable code example is at the end of this

现在我可以从基于的gs_分类器中获取特征_log_prob_。这里有一个例子

我的问题是如何使单词对应于每个对数概率? CountVectorizer()和TfidTransformer()都进行了功能选择。 GridSearchCV对象在何处存储选定的word/ngram功能?如何使它们与概率相匹配

我已经检查了gs_分类器的成员,但没有找到所选的特征。谢谢

以下是一个可运行的示例:

# Note: The runnable code example is at the end of this question ####
# Assume X_train contains cleaned sentence text as input data. Y_train are class labels. 
# parameters stores the parameter to be tried by GridSearchCV

text_clf_Pipline_MultinomialNB = Pipeline([('vect', CountVectorizer()),
                                           ('tfidf', TfidfTransformer()),
                                           ('clf', MultinomialNB()),                     
                                          ])
gs_clf = GridSearchCV(text_clf_Pipline_MultinomialNB, parameters, n_jobs=-1)   
gs_classifier = gs_clf.fit(X_train, y_train)
然后问题是:如何在训练模型中得到对应于对数概率的单词特征列表?此外,例如,_log_prob__输出中的哪个概率对应于类“1”的单词“qwe”


获得答案后编辑: 安德烈亚斯的答案是有效的:

from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.grid_search import GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.naive_bayes import MultinomialNB
from inspect import getmembers

X_train = ['qwe rtyuiop', 'asd fghj kl', 'zx cv bnm', 'qw erty ui op', 'as df ghj kl', 'zxc vb nm', 'qwe rt yu iop', 'asdfg hj kl', 'zx cvb nm',
          'qwe rt yui op', 'asd fghj kl', 'zx cvb nm', 'qwer tyui op', 'asd fg hjk l', 'zx cv b nm', 'qw ert yu iop', 'as df gh jkl', 'zx cvb nm',
           'qwe rty uiop', 'asd fghj kl', 'zx cvbnm', 'qw erty ui op', 'as df ghj kl', 'zxc vb nm', 'qwe rtyu iop', 'as dfg hj kl', 'zx cvb nm',
          'qwe rt yui op', 'asd fg hj kl', 'zx cvb nm', 'qwer tyuiop', 'asd fghjk l', 'zx cv b nm', 'qw ert yu iop', 'as df gh jkl', 'zx cvb nm']    

y_train = ['1', '2', '3', '1', '1', '3', '1', '2', '3',
          '1', '2', '3', '1', '4', '1', '2', '2', '4', 
          '1', '2', '3', '1', '1', '3', '1', '2', '3',
          '1', '2', '3', '1', '4', '1', '2', '2', '4']    


parameters = {  
                'clf__alpha': (1e-1, 1e-2),
                 'vect__ngram_range': [(1,2),(1,3)],
                 'vect__max_df': (0.9, 0.98)
            }

text_clf_Pipline_MultinomialNB = Pipeline([('vect', CountVectorizer()),
                                           ('tfidf', TfidfTransformer()),
                                           ('clf', MultinomialNB()),                     
                                          ])
gs_clf = GridSearchCV(text_clf_Pipline_MultinomialNB, parameters, n_jobs=-1)   

gs_classifier = gs_clf.fit(X_train, y_train)

nbclf = getmembers(gs_classifier.best_estimator_)[2][1]['named_steps']['clf']
nbclf.feature_log_prob_ 
与此类似,有一种更好的方法可以索引到GridSearchCV以获得经过训练的分类器

gs_classifier.best_estimator_.named_steps['vect'].get_feature_names() 

为什么需要
getmembers
? 要获取与
功能\u log\u prob\u对应的功能名称


gs\u分类器。最佳估计器。命名步骤['vect']。获取特征名称()

谢谢!你的回答不仅解决了我的问题,还为我提供了一种优雅/正确的方法来索引GridSearchCV。
nbclf = gs_classifier.best_estimator_.named_steps['clf']