如何将另一个文本特征添加到当前的词袋分类中?在Scikit-learn中

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

我的示例代码:

from sklearn.model_selection import train_test_splitfrom sklearn.feature_extraction.text import CountVectorizerfrom sklearn.feature_extraction.text import TfidfTransformerfrom sklearn.naive_bayes import MultinomialNBfrom sklearn.ensemble import RandomForestClassifierfrom sklearn.metrics import accuracy_scoreX_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 , FeatureUniontext_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)

在这里,我对’Extract’列应用了词袋模型来分类’Expense Account code Description’,我得到了大约92%的准确率。但是,如果我想包括’Vendor name’作为另一组输入特征,我该怎么做呢?有没有办法在词袋模型中同时使用这个特征?


回答:

你可以使用FeatureUnion。另外,你需要创建一个新的Transformer类,包含你需要执行的必要操作,例如包含供应商名称,生成虚拟变量。

Feature Union将适应你的pipeline。

供参考。

class get_Vendor(BaseEstimator,TransformerMixin):    def transform(self, X,y):        return lr_tfidf = Pipeline([('features',FeatureUnion([('other',get_vendor()),        ('vect', tfidf)])),('clf', RandomForestClassifier())])

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

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