如何将另一个文本特征添加到当前的词袋分类中?在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

使用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中创建了一个多类分类项目。该项目可以对…

发表回复

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