如何在Python中获得2个数据帧的减法平均值?

时间:2015-11-25 08:55:52

标签: python pandas dataframe

我在python中有2个数据帧,

ý

2015-06-05 15:00:00.000     20.22
2015-06-05 15:00:00.500     20.22
2015-06-05 15:00:01.000     20.22
                            ...  
2015-06-05 15:31:38.500    114.95
2015-06-05 15:31:39.000    114.95
2015-06-05 15:31:39.500    114.95
Freq: 500L, Name: sensor_19, dtype: float64

y_predict

2015-06-05 15:00:00.000     93.445314
2015-06-05 15:00:00.500     20.224281
2015-06-05 15:00:01.000     20.226055
                              ...    
2015-06-05 15:31:38.500    115.612101
2015-06-05 15:31:39.000    114.682510
2015-06-05 15:31:39.500    114.917647
Freq: 500L, dtype: float64

实际上, y_predict 是由ARMA模型计算的 y 的预测值。如您所见,它们具有相同的数据结构,相同的行数,相同的索引。但是,当我试图获得这两个数据帧的减法平均值时,我收到了一个错误。

def mean_forecast_err(y, y_predict):
   return y.sub(y_predict).mean()

# other preparation before ...
y = df['sensor_19']
arma_mod12 = sm.tsa.ARMA(y, (1, 2)).fit()
y_predict12 = arma_mod12.predict()
print "ARMA(1, 2): err_mean=" + mean_forecast_err(y, y_predict12)

我的问题是:

  • 错误来自哪里?
  • 或者如何生成一个赞赏的异常,以便我可以看到调试的错误消息?

1 个答案:

答案 0 :(得分:2)

这应该有效。我只在最后一行代码中将返回值更改为str类型:

def mean_forecast_err(y, y_predict):
   return y.sub(y_predict).mean()

# other preparation before ...
y = df['sensor_19']
arma_mod12 = sm.tsa.ARMA(y, (1, 2)).fit()
y_predict12 = arma_mod12.predict()
print "ARMA(1, 2): err_mean=" + str(mean_forecast_err(y, y_predict12))