预测数组的形状很奇怪

时间:2020-01-08 11:59:44

标签: python machine-learning classification tfidfvectorizer

数据集包含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,)

enter image description here

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)

1 个答案:

答案 0 :(得分: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的形状,这可能不匹配