【问题标题】:How can I solve a classification problem with a dependent variable with more than two values如何解决具有两个以上值的因变量的分类问题
【发布时间】:2019-05-25 20:57:59
【问题描述】:

我有一个简单的 NLP 问题,其中我有一些具有简单的二元正面或负面判断的书面评论。在这种情况下,我可以将包含“词袋”(即稀疏矩阵中的单个词)的 X 列作为自变量进行训练和测试。

from sklearn.feature_extraction.text import CountVectorizer
cv = CountVectorizer(max_features = 300)
#indipendent
X = cv.fit_transform(corpus).toarray()
#dependent
y = dataset.iloc[:, 1].values

..和因变量 y,由第 1 列表示,假定值为 0 和 1(因此基本上是正面和负面评论)。

如果不是 0 和 1,我有可以从 1 到 5 星投票的评论,我是否应该继续拥有一个 y 变量列,其值从 0 到 4?换句话说,我会撒谎以知道模型有多大不同如果用户可以在他或她的评论之后给出从 1 到 5 的评分,而不是二元的好/坏评论。 这种问题在机器学习中怎么称呼?

【问题讨论】:

标签: python machine-learning nlp classification


【解决方案1】:

这只是多类分类问题。这是一个示例代码,您可以从中获得一个想法。您所说的“因变量”称为类(输入示例所属的类)

    label_idx = [unique.index(l) for l in labels] """ labels= class. works for your class is string or so. 
here labels can be more than two"""
    label_idx = np.array(label_idx) # just get your class into array
    vectors = np.array(vecs) # vecs are any vectorised form of your text data
    clf = LinearSVC() # classifier of your choice
    clf.fit(vectors, label_idx)

【讨论】:

    【解决方案2】:

    我已将以下链接用于 RandomForest multiClassifier,它是您可以使用的许多可能的 ML 算法之一:

    https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html#sklearn.ensemble.RandomForestClassifier

    但是,我的个人经验表明,深度学习神经网络更适用于“文本数据”,而基于树的模型更适用于具有数值的表格数据。

    【讨论】:

      【解决方案3】:

      这个问题被称为@rishi 提到的多类分类问题。有很多种算法可以解决多类问题。 Look here

      您可以将目标变量设为一个,即评级。

      #dependent
      y = dataset.iloc[:, 'ratings'].values
      

      然后,您可以将此数据放入classifier

      from sklearn import linear_model
      clf = linear_model.SGDClassifier()
      clf.fit(X, y)
      

      【讨论】:

        猜你喜欢
        • 2019-10-31
        • 1970-01-01
        • 1970-01-01
        • 2023-03-08
        • 1970-01-01
        • 2021-01-25
        • 2021-12-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多