【问题标题】:ValueError Inconsistent number of samples error with MultinomialNBValueError Inconsistent number of samples error with MultinomialNB
【发布时间】:2018-11-06 14:35:00
【问题描述】:

我需要创建一个模型,该模型可以根据变量对记录进行准确分类。例如,如果一条记录具有预测值 AB,我希望它被归类为具有预测值 X。实际数据是这样的形式:

    Predicted    Predictor
      X            A
      X            B
      Y            D
      X            A

对于我的解决方案,我执行了以下操作: 1. 使用LabelEncoderPredicted 列创建数值 2. 预测变量有多个类别,我使用get_dummies 将其解析为单独的列。

这是数据框的一个子部分,其中包含 (dummy)Predictor 和几个预测器类别(请原谅未对齐):

    Predicted Predictor_A    Predictor_B
9056    30  0   0
2482    74  1   0
3407    56  1   0
12882   15  0   0
7988    30  0   0
13032   12  0   0
9738    28  0   0
6739    40  0   0
373 131 0   0
3030    62  0   0
8964    30  0   0
691 125 0   0
6214    41  0   0
6438    41  1   0
5060    42  0   0
3703    49  0   0
12461   16  0   0
2235    75  0   0
5107    42  0   0
4464    46  0   0
7075    39  1   0
11891   16  0   0
9190    30  0   0
8312    30  0   0
10328   24  0   0
1602    97  0   0
8804    30  0   0
8286    30  0   0
6821    40  0   0
3953    46  1   

如上所示将数据重塑为 datframe 后,我尝试使用来自sklearnMultinomialNB。这样做时,我遇到的错误是:

ValueError: Found input variables with inconsistent numbers of samples: [1, 8158]

我在尝试使用只有 2 列的数据框时遇到了错误 -> PredictedPredictor_A

我的问题是:

  1. 我需要做什么来解决这个错误?
  2. 我的方法正确吗?

【问题讨论】:

  • 对我来说你的问题发生了什么并不明显,更不用说问题是什么了。请阅读minimal reproducible example 并相应地考虑edit您的问题。这对社区更好,更容易回答您的问题。
  • 感谢您的反馈。我已经编辑了这个问题,希望它更容易理解。
  • @kurious 您使用哪些列进行模型拟合?

标签: pandas machine-learning scikit-learn sklearn-pandas


【解决方案1】:
  • 要拟合 MultinomialNB 模型,您需要训练样本及其特征及其对应的标签(目标值)。

  • 在您的情况下,Predictedtarget 变量,Predictor_A and Predictor_Bfeatures(预测器)。


示例 1:

from sklearn.naive_bayes import MultinomialNB
import pandas as pd
from sklearn.model_selection import train_test_split

df = pd.read_csv("dt.csv", delim_whitespace=True)

# X is the features
X = df[['Predictor_A','Predictor_B']]
#y is the labels or targets or classes 
y = df['Predicted']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)

clf = MultinomialNB()
clf.fit(X_train, y_train)

clf.predict(X_test)

#array([30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30])

#this result makes sense if you look at X_test. all the samples are similar
print(X_test)

       Predictor_A  Predictor_B
8286             0            0
12461            0            0
6214             0            0
9190             0            0
373              0            0
3030             0            0
11891            0            0
9056             0            0
8804             0            0
6438             1            0

#get the probabilities 
clf.predict_proba(X_test)

注2:我使用的数据可以在here找到


编辑

如果您使用一些具有 4 个标签(预测变量)的文档来训练模型,那么您要预测的新文档也应该具有相同数量的标签。

示例 2:

clf.fit(X, y)

这里,X 是一个 [29, 2] 数组。所以我们有29 训练样本(文档),它有2 标签(预测器)

clf.predict(X_new)

在这里,X_new 可以是 [n, 2]。因此,我们可以预测 n 新文档上的类,但这些新文档也应该恰好有 2 标签(预测器)。

【讨论】:

  • 有关更多上下文,此问题与文档分类有关。 Predicted 列有多个类别,而不是连续值。已手动为文档分配这些类别。一个文档可以有多个指定的类别。 Predictor 列具有与指定类别不同的​​自动生成标签。一个文档可以有多个标签。我想要做的是对于具有一个或多个自动生成标签的新文档,我想根据旧文档的标签预测正确的手动标签。
  • 所以我在Predicted 列中看到的数字是类。知道了。接下来,可以按照我的描述进行预测。您可以使用clf.predict(X_new)。这里,X_new 是新文档,应该与X 中的文档具有相同的形状
  • 知道了。我正在考虑比较 MultinomialaNB、Multinomial Logistic Regression、kNN 和 Neural Net,看看我在哪里得到了好的结果。你觉得这种做法合理吗?
  • 是的,听起来不错。你总共有多少个训练样本?最终目标是什么?分类准确率高?
  • 当我有 1 个值/单元格时,我有大约 12k 行(即,当我为每个 PredictedPredictor 值创建一个新行时)。因此,这些行具有重复的 ID。我有大约 3.7k 文档。在为Predictor 值执行pd.get_dummies 时,我得到一个带有8158 rows × 186 columns 的数据框。如果我检查Predictors 的频率,数据不是正态分布的。因此,一些预测变量的频率非常高,而另一些则很少见。目标是在分类中具有高度的准确性。
猜你喜欢
  • 1970-01-01
  • 2018-05-05
  • 2018-12-16
  • 1970-01-01
  • 2020-11-12
  • 1970-01-01
  • 1970-01-01
  • 2021-03-25
  • 1970-01-01
相关资源
最近更新 更多