【问题标题】:How can I use the Randomforest classifier in python to train data?如何在 python 中使用随机森林分类器来训练数据?
【发布时间】:2021-09-01 07:35:27
【问题描述】:

以下代码是我用 Randomforest 分类器训练模型的:

model = Pipeline([('vectorizer', tvec),('classifier', clf)])

model.fit(IV_train, DV_train)

from sklearn.metrics import confusion_matrix
    predictions = model.predict(IV_test)
    confusion_matrix(predictions, DV_test) ```

但是,我收到以下错误消息:

File "<ipython-input-122-6ea0987e0222>", line 7
    predictions = model.predict(IV_test)
    ^
IndentationError: unexpected indent

我该如何解决这个问题?

【问题讨论】:

  • 您的代码有缩进问题,这意味着额外的不必要的空格。

标签: python scikit-learn random-forest training-data


【解决方案1】:

您遇到此错误是因为您在第 7 行和第 8 行进行了缩进(即添加了制表符或使用了空格),即使它并不需要。

你可能想这样使用它(你可以看到这里没有缩进或间距)

from sklearn.metrics import confusion_matrix
predictions = model.predict(IV_test)
confusion_matrix(predictions, DV_test)

查看this link,它对python中的缩进提供了一个非常好的想法

【讨论】:

  • 感谢您的评论我能够解决问题:)
猜你喜欢
  • 1970-01-01
  • 2019-07-20
  • 2019-08-12
  • 1970-01-01
  • 2021-07-06
  • 2018-06-13
  • 2021-10-10
  • 2017-01-22
  • 2021-09-29
相关资源
最近更新 更多