变量:
y_hat = pd.DataFrame([x1,x2,x3,x4,x5])
y_actual = pd.DataFrame(Macro.iloc[8:13,1:2])
我有一个 pandas 数据框y_hat
:
y_hat
Out[24]:
0
0 409.612553
1 573.936775
2 256.213344
3 136.219153
4 419.977863
我有一个 pandas 数据框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 函数?
最佳答案
您可以尝试在其他answer中使用类似的方法进行以下操作:
import numpy as np
print(np.mean(np.abs((y_actual.values - y_hat.values) / y_actual.values)) * 100)
关于python - 使用 Pandas 进行基本矩阵计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52435166/