【问题标题】:How to forecast future numbers using Random Forest Regressor in Python如何在 Python 中使用随机森林回归器预测未来的数字
【发布时间】:2021-01-07 07:12:19
【问题描述】:

我正在尝试使用 RandomForestRegressor 预测未来的冠状病毒病例数,但是当我尝试执行它时它给了我这种错误:

 ValueError                                Traceback (most recent call last)
<ipython-input-181-c9a9a8208098> in <module>()
      1 test_data = np.arange(260, 367).reshape(-1, 1)
----> 2 rf_regressor_fit_future = rf_regressor.fit(test_data, target)
      3 forecast_rf_future = rf_regressor_fit_future.predict(test_data)

10 frames
/usr/local/lib/python3.6/dist-packages/sklearn/tree/_classes.py in fit(self, X, y, sample_weight, check_input, X_idx_sorted)
    263         if len(y) != n_samples:
    264             raise ValueError("Number of labels=%d does not match "
--> 265                              "number of samples=%d" % (len(y), n_samples))
    266         if not 0 <= self.min_weight_fraction_leaf <= 0.5:
    267             raise ValueError("min_weight_fraction_leaf must in [0, 0.5]")

ValueError: Number of labels=259 does not match number of samples=107

这是我使用示例创建未来日子和预测的代码:

test_data = np.arange(260, 367).reshape(-1, 1)
rf_regressor_fit_future = rf_regressor.fit(test_data, target)
forecast_rf_future = rf_regressor_fit_future.predict(test_data)

数据集只有 259 天和样本,预测实际数据运行良好。但我在未来的日子里遇到了问题。我应该怎么做才能通过匹配样本数量来解决这个错误?任何建议都非常感谢。

【问题讨论】:

    标签: python machine-learning data-science random-forest


    【解决方案1】:

    随机森林回归不适用于此类任务。我做过一个类似的项目,但我使用了多项式回归。如果你喜欢,你可以在这里查看:
    https://github.com/sanyogthescholar/covid_19

    【讨论】:

    • 我想试试,有什么方法或参数可以预测未来日期的未来值吗?使用随机森林回归?
    【解决方案2】:

    随机森林几乎肯定不适合预测未来的冠状病毒病例。它可以预测的最大事例数是训练数据集中的最大事例数。因此,它对于预测呈指数增长的病例数几乎为零。请使用更合适的方法。

    【讨论】:

    • 谢谢你,罗伯特。注意到这一点。我想知道如何使用它来预测未来的日期?有什么参数可以解决吗?
    猜你喜欢
    • 2023-01-02
    • 2021-11-20
    • 2020-12-05
    • 2020-10-15
    • 1970-01-01
    • 2020-04-06
    • 2021-10-10
    • 2020-07-06
    • 2014-08-07
    相关资源
    最近更新 更多