【问题标题】:NaN values: I am getting an error in finding the MSE from the data setNaN 值:我在从数据集中查找 MSE 时出错
【发布时间】:2019-05-13 01:25:44
【问题描述】:

我正在做一个 SARIMA 时间序列模型,想找到预测数据和实际数据的 MSE。但是我不断获得一个 NaN 值来获取 MSE。

这是用来计算均方误差的。此外,所有值都已打印在下面。

y_forecasted = pred.predicted_mean
y_truth = indexedDataset['2018-01-01':]

mse = (np.mean(y_truth - y_forecasted)**2)
print(mse)


2018-01-01 00:00:00   NaN
2018-02-01 00:00:00   NaN
2018-03-01 00:00:00   NaN
2018-04-01 00:00:00   NaN
2018-05-01 00:00:00   NaN
2018-06-01 00:00:00   NaN
2018-07-01 00:00:00   NaN
2018-08-01 00:00:00   NaN
2018-09-01 00:00:00   NaN
2018-10-01 00:00:00   NaN
2018-11-01 00:00:00   NaN
2018-12-01 00:00:00   NaN
2019-01-01 00:00:00   NaN
2019-02-01 00:00:00   NaN
2019-03-01 00:00:00   NaN
kwh                   NaN
dtype: float64



print(y_truth)

2018-01-01  120
2018-02-01  113
2018-03-01  123
2018-04-01  168
2018-05-01  142
2018-06-01  149
2018-07-01  116
2018-08-01  123
2018-09-01   38
2018-10-01   41
2018-11-01   48
2018-12-01   52
2019-01-01   48
2019-02-01   49
2019-03-01   36



print(y_forecasted)

2018-01-01    116.544320
2018-02-01    118.167629
2018-03-01    149.221148
2018-04-01    145.603930
2018-05-01    182.453446
2018-06-01    136.816330
2018-07-01    127.626132
2018-08-01    130.638331
2018-09-01    115.672435
2018-10-01     78.242700
2018-11-01     65.631016
2018-12-01     44.679845
2019-01-01     60.412676
2019-02-01     26.324014
2019-03-01     59.369507
Freq: MS, dtype: float64

【问题讨论】:

  • 不清楚pred.predicted_meanindexedDataset是什么;我假设 predicted_mean 在 SARIMAX 预测结果上被调用,但 indexedDataset 一点也不清楚。请提供一个最小的、完整的和可验证的例子stackoverflow.com/help/mcve
  • 你是怎么解决这个问题的?

标签: python jupyter-notebook mean-square-error


【解决方案1】:

您可以直接使用 sklearn.metrics 中的 mean_squared_error 方法

>>> from sklearn.metrics import mean_squared_error
    >>> y_truth = [116, 118, ....59]
    >>> y_forecasted = [120, 113, ...36]
    >>> mean_squared_error(y_truth , y_forecasted)
    0.375                                         #Some number to illustrate the working

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-30
    • 2012-09-19
    • 1970-01-01
    • 2015-03-13
    • 1970-01-01
    • 2021-01-03
    • 1970-01-01
    相关资源
    最近更新 更多