【问题标题】:NotFittedError when using RandomForestRegressor使用 RandomForestRegressor 时出现 NotFittedError
【发布时间】:2019-01-17 06:26:16
【问题描述】:

Python 新手。我正在尝试运行具有 5 个特征和一个标签的简单随机森林模型,但是当我运行 RandomForestRegressor 时,我得到了一个我不明白的 NotFittedError(下)。

任何帮助表示赞赏...

---------------------------------------------------------------------------
NotFittedError                            Traceback (most recent call last)
<ipython-input-86-b670604a6f52> in <module>()
----> 1 predictions = rf.predict(train_features)

/anaconda3/lib/python3.6/site-packages/sklearn/ensemble/forest.py in predict(self, X)
    677             The predicted values.
    678         """
--> 679         check_is_fitted(self, 'estimators_')
    680         # Check data
    681         X = self._validate_X_predict(X)

/anaconda3/lib/python3.6/site-packages/sklearn/utils/validation.py in check_is_fitted(estimator, attributes, msg, all_or_any)
    766 
    767     if not all_or_any([hasattr(estimator, attr) for attr in attributes]):
--> 768         raise NotFittedError(msg % {'name': type(estimator).__name__})
    769 
    770 

NotFittedError: This RandomForestRegressor instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.

【问题讨论】:

  • 请分享代码。猜猜,你使用的是predict 函数而不使用fit

标签: python scikit-learn random-forest


【解决方案1】:

首先要训练fit 数据model,然后是predict

from sklearn.ensemble import RandomForestRegressor
from sklearn.datasets import make_regression

X, y = make_regression(n_features=4, n_informative=2, random_state=0, shuffle=False)
regr = RandomForestRegressor(max_depth=2, random_state=0)
regr.fit(X, y)
regr.predict([[0, 0, 0, 0]])

【讨论】:

    猜你喜欢
    • 2019-08-17
    • 2016-11-17
    • 2013-07-29
    • 2020-05-13
    • 2020-07-11
    • 2020-10-08
    • 2015-03-09
    • 2018-05-18
    • 2018-03-23
    相关资源
    最近更新 更多