【问题标题】:How to add more features in multi text classification?如何在多文本分类中添加更多特征?
【发布时间】:2020-11-29 21:23:05
【问题描述】:

我有一个以product_description, price, supplier, category 为列的零售数据集。 我使用product_description 作为特征:

from sklearn import model_selection, preprocessing, naive_bayes

# split the dataset into training and validation datasets 
train_x, valid_x, train_y, valid_y = model_selection.train_test_split(df['product_description'], df['category'])

# label encode the target variable 
encoder = preprocessing.LabelEncoder()
train_y = encoder.fit_transform(train_y)
valid_y = encoder.fit_transform(valid_y)

tfidf_vect = TfidfVectorizer(analyzer='word', token_pattern=r'\w{1,}', max_features=5000)
tfidf_vect.fit(df['product_description'])
xtrain_tfidf =  tfidf_vect.transform(train_x)
xvalid_tfidf =  tfidf_vect.transform(valid_x)

classifier = naive_bayes.MultinomialNB().fit(xtrain_tfidf, train_y)

# predict the labels on validation dataset
predictions = classifier.predict(xvalid_tfidf)
metrics.accuracy_score(predictions, valid_y) # ~20%, very low

由于准确性非常低,我也想将供应商和价格添加为特征。如何将其合并到代码中?

我尝试过其他分类器,例如 LR、SVM 和 Random Forrest,但它们的结果(几乎)相同。

【问题讨论】:

    标签: python-3.x scikit-learn text-classification supervised-learning


    【解决方案1】:

    TF-IDF 矢量化器返回一个矩阵:每个示例一行带有分数。在将其输入分类器之前,您可以根据需要修改此矩阵。

    • 将您的附加特征准备为 NumPy 形状数组:示例数 × 特征数

    • np.concatenateaxis=1 一起使用。

    • 像以前一样拟合分类器。

    标准化实值特征通常是一个好主意。此外,您可以尝试不同的分类器:逻辑回归或 SVM 可能比朴素贝叶斯更适合实值特征。

    【讨论】:

    • 我会试试的,谢谢。我确实尝试过其他分类器,例如 LR、SVM 和 Random Forrest,但它们的结果(几乎)相同
    猜你喜欢
    • 2017-03-03
    • 2015-04-23
    • 2019-06-04
    • 1970-01-01
    • 2020-09-10
    • 1970-01-01
    • 2019-06-30
    • 2019-05-24
    • 2019-06-13
    相关资源
    最近更新 更多