【问题标题】:Predicted array has a strange shape预测数组有一个奇怪的形状
【发布时间】:2020-04-25 23:06:54
【问题描述】:

数据集包含 3 列:comment、parent_comment 和 label(0 或 1)。我尝试预测 y_test 的标签,但出现错误

Found input variables with inconsistent numbers of samples: [2, 758079]

不知何故,y_predict 的形状是 (2,0)。 为什么会这样以及如何解决?

当我这样做时

y_test = y_test_["comment"]

一切都很好。

由于某种原因 y_predict 的形状为 (2,) 而 y_true 的形状为正常的 (252694,)

x_train, y_test_ = train_test_split(df1, random_state=17)
y_test = y_test_[["comment", "parent_comment"]]
y_true = y_test_["label"]

tf_idf = TfidfVectorizer(stop_words = 'english', ngram_range=(1, 2), max_features=700000, min_df=0.01)
# multinomial logistic regression a.k.a softmax classifier
logit = LogisticRegression(C=1, n_jobs=4, solver='lbfgs', 
                           random_state=17, verbose=1)
# sklearn's pipeline
tfidf_logit_pipeline = Pipeline([('tf_idf', tf_idf), 
                                 ('logit', logit)])

tfidf_logit_pipeline.fit(x_train[['comment',"parent_comment"]], x_train["label"])
y_predict = tfidf_logit_pipeline.predict(y_test)

accuracy_score(y_predict, y_true)

【问题讨论】:

  • 你的错误发生在哪一行?
  • @PV8 最后一个,我在这里尝试测量准确性

标签: python machine-learning classification tfidfvectorizer


【解决方案1】:

我认为问题在于您未在此处发布的火车/测试拆分。如果你检查这个:https://datascience.stackexchange.com/questions/20199/train-test-split-error-found-input-variables-with-inconsistent-numbers-of-sam 你需要有相同长度的 X 和 y 来分割。

对此的检查是:

X.shape 
Y.shape

按照帖子删除错误: o 修复此错误:

  1. 在定义 X 时从 np.array() 内部删除额外的列表,或者在之后使用以下命令删除额外的维度:X = X.reshape(X.shape[1:])
  2. 通过运行X = X.transpose() 转置X 以获得X 和Y 中相同数量的样本。

更新: 用你最后的评论: 检查y_predict, y_true 的形状,这可能不匹配

【讨论】:

  • 但我需要在 2 个列上训练,而不是一个
  • 如果最后一行出现错误,y_predicty_true的形状可能不匹配
  • 这正是我要说的。 y_predict 是 array([0, 0], dtype=int64) (shape (2,)) 并且 y_true 有一个 shape (252694,) 我不知道为什么
  • 然后检查形状y_true
  • 我检查并写在我以前的评论中。 y_true 形状是 (252694,),应该是这样的
猜你喜欢
  • 1970-01-01
  • 2018-02-08
  • 2023-01-17
  • 1970-01-01
  • 2015-10-03
  • 1970-01-01
  • 1970-01-01
  • 2016-10-06
  • 1970-01-01
相关资源
最近更新 更多