首页 > 解决方案 > 如何在当前的词袋分类中添加另一个文本特征?在 Scikit-learn 中

问题描述

这是我的输入矩阵在此处输入图像描述

我的示例代码:

from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.naive_bayes import MultinomialNB
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score

X_train, X_test, y_train, y_test = train_test_split(data['Extract'], 
data['Expense Account code Description'], random_state = 0)

from sklearn.pipeline import Pipeline , FeatureUnion
text_clf = Pipeline([('vect', CountVectorizer(ngram_range=(1,1))),
              ('tfidf', TfidfTransformer(use_idf = False)),
              ('clf', RandomForestClassifier(n_estimators =100, 
 max_features='log2',criterion = 'entropy')),
 ])
 text_clf = text_clf.fit(X_train, y_train)

在这里,我正在应用“提取”列分类“费用帐户代码描述”的词袋模型,在这里我得到大约 92% 的准确度,但是如果我想将“供应商名称”作为另一个输入功能的集合,如何我可以这样做吗?有没有什么办法可以和词袋一起做?,

标签: pythonmachine-learningscikit-learnnlptext-classification

解决方案


您可以使用 FeatureUnion。您还需要创建一个新的 Transformer 类,其中包含您需要采取的必要操作,即包括供应商名称,获取假人。

Feature Union 将适合您的管道。

以供参考。

class get_Vendor(BaseEstimator,TransformerMixin):

    def transform(self, X,y):
        return 

lr_tfidf = Pipeline([('features',FeatureUnion([('other',get_vendor()),
        ('vect', tfidf)])),('clf', RandomForestClassifier())])

推荐阅读