【问题标题】:The number of features of the model must match the input. Model n_features is 7985 and input n_features is 1模型的特征数量必须与输入相匹配。模型 n_features 为 7985,输入 n_features 为 1
【发布时间】:2021-08-04 16:16:51
【问题描述】:

我用随机森林构建了一个垃圾邮件分类器,并想制作一个单独的函数来将短信分类为垃圾邮件或非垃圾邮件,我尝试了:

def predict_message(pred_text):
    pred_text=[pred_text]
    pred_text2 = tfidf_vect.fit_transform(pred_text)
    pred_features = pd.DataFrame(pred_text2.toarray())
    prediction = rf_model.predict(pred_features)
    return (prediction)

pred_text = "how are you doing today?"

prediction = predict_message(pred_text)
print(prediction)

但它给了我错误:

The number of features of the model must match the input.
Model n_features is 7985 and input n_features is 1 

我看不到问题所在,如何解决?

【问题讨论】:

  • 你为什么在Meta Stack Overflow 元网站上再次提出同样的问题?你已经在这里得到了答案,在重新问你的问题之前,你显然已经完全粗鲁地忽略了这个答案。不好。
  • 我刚开始使用这个网站,在我的理解过程中。对你不必要的批评是粗鲁的,不是我的举动。 @HovercraftFullOfEels
  • 请阅读help center 链接并通过tour 了解如何最好地使用本网站。我这样说是为了你自己的利益,因为如果你在这个网站上的更多问题没有得到很好的接受,你可能会被禁止提问,这是你想要避免的。

标签: python machine-learning email-spam


【解决方案1】:

通过调用tfidf_vect.fit_transform(pred_text),您的矢量化器会丢失它从原始训练语料库中获得的所有信息。

你应该打电话给transform

以下这些更改应该会有所帮助:

def predict_message(pred_text):
    pred_text=[pred_text]
    pred_text2 = tfidf_vect.transform(pred_text)  # Changed
    prediction = rf_model.predict(pred_text2)
    return (prediction)

pred_text = "how are you doing today?"

prediction = predict_message(pred_text)
print(prediction)

【讨论】:

  • 感谢您的帮助!
  • @IşılBerfinKoparan:很好,你终于承认了答案
猜你喜欢
  • 2021-07-28
  • 2018-11-08
  • 2020-10-01
  • 2017-11-04
  • 2023-04-07
  • 2021-04-14
  • 2021-01-19
  • 2021-03-04
  • 1970-01-01
相关资源
最近更新 更多