使用熊猫的基本矩阵计算

时间:2018-09-21 00:52:19

标签: python pandas dataframe

变量:

y_hat = pd.DataFrame([x1,x2,x3,x4,x5])
y_actual = pd.DataFrame(Macro.iloc[8:13,1:2])

我有一个熊猫数据框y_hat

y_hat 
Out[24]: 
            0
0  409.612553
1  573.936775
2  256.213344
3  136.219153
4  419.977863

我有一个熊猫数据框y_actual

y_actual
Out[25]: 
         y
8   422.40
9   580.42
10  256.76
11  128.96
12  445.42

我实质上是在尝试计算y_actual和y_hat之间的MAPE(平均平均百分比误差)。 R代码如下所示:

MAPE = colmeans(abs((y_hat - y_actual)/y_actual)*100)

但是在Python中,我什至无法走过第一步y_hat - y_actual,因为它返回错误:

y_hat - y_actual
Out[29]: 
     0   y
0  NaN NaN
1  NaN NaN
2  NaN NaN
3  NaN NaN
4  NaN NaN
8  NaN NaN
9  NaN NaN
10 NaN NaN
11 NaN NaN
12 NaN NaN

我可以使用Pandas数据框运行此计算吗?是否有一个相当于colmeans(列均值)的Python函数?

1 个答案:

答案 0 :(得分:0)

您可以在其他answer中尝试类似的方法:

import numpy as np

print(np.mean(np.abs((y_actual.values - y_hat.values) / y_actual.values)) * 100)