我如何衡量预测时间序列的准确性,其中最重要的方面是输出是负还是正



我有一个预测未来值的模型,我想通过查看结果是否与实际值相同来测试它的准确性。这一切都在python上。

因此,如果预测值是-2,而实际值是-1,那么它将认为它是正确的,并在数据帧中加1(两者都是负数(。但是,如果预测值为2,而实际值为-1,则会在数据帧中添加0。

实现这一点的最简单方法是使用两个数组。一个数组包含真实值(aT(,另一个数组则包含预测值(aP(。如果你取两个数组的numpy.sign((,你会得到另一个数组,其中包含输入向量的符号。sgnaT=np.sign(aT(,sgnaP=np.ssign(aP(。接下来我取np.sum(sgnaT==sgnaP(,它会告诉你在数组中两个符号匹配了多少次。最后,为了得到正确的百分比,我将除以样本总数。

import numpy as np
aP = [your predicted values]
aT = [your true values]
sgnaP = np.sign(aP)
sgnaT = np.sign(aT)
totCor = np.sum(sgnaT==sgnaP)
totIncorrect = np.sum(sgnaT!=sgnaP)
perCor = tot/(totCor+totIncorrect)

这样的东西怎么样?

y_pred = [-2, -1, 1 , 3]
y_true = [-2, 1, 1, 4 ]
df = pd.DataFrame({"y_pred":y_pred, "y_true":y_true})
score = (df.y_pred > 0) & (df.y_true > 0)
score.astype(int)
>>> score.astype(int)
0    0
1    0
2    1
3    1
dtype: int64

最新更新