python - 时间序列固定技术

标签 python tensorflow

我正在处理时间序列数据(非平稳),我已应用 .diff(periods=n) 对数据进行差分以消除数据中的趋势和季节性因素。

通过使用 .diff(periods=n),从当前观察值(t )。

现在我想将差异数据反转回其原始比例,但我遇到了问题。您可以找到代码 here .

我的差分代码:

data_diff = df.diff(periods=1)     

data_diff.head(5) 

我将差异数据反转回其原始比例的代码:

cols = df.columns
x = []
for col in cols:
    diff_results = df[col] + data_diff[col].shift(-1)
    x.append(diff_results)
diff_df_inverted = pd.concat(x, axis=1)

diff_df_inverted

正如您从代码的最后一个输出中看到的那样,我已成功将数据反转回其原始比例。但是,我没有得到第 1 行的反转数据。它将值反转并向上移动一行。我的问题是,为什么?我错过了什么?

谢谢!

最佳答案

在这一行中:

diff_results = df[col] + data_diff[col].shift(-1)

data_diff 从第二行开始,这就是它看起来可以向上移动的原因。 这是因为您使用了 .shift(-1)

一个简单的解决方案是使用 df.cumsum()因为它与 df.diff() 完全相反。

您唯一需要做的就是获取第一行以替换 data_diff 数据帧中的 NaN 值。您需要这样做,因为它是将每隔一行添加到的原始行。之后,您调用 data_diff.cumsum(),现在您有了原始数据。

这里是详细的代码。

data_diff.iloc[0]=df.iloc[0]
a = data_diff.cumsum()

关于python - 时间序列固定技术,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62865886/

相关文章:

python - 请求库 : missing SSL handshake certificates file after cx_Freeze

python - 使用 Numpy 数组更新 Pandas 数据框中的部分列

python - 使用 beautifulsoup 进行 HTML 解析适用于除我想要的 URL 之外的大多数 URL

python - 忽略 Tensorflow 日志记录警告

machine-learning - nvidia-smi不显示内存使用情况

python - 等效于 Tensorflow 中的 fillPoly

python - for 循环 : C++ and python

python - 无法使用 mod_wsgi 和 apache 部署 django

tensorflow - 如何在 Tensorflow 中拥有可变数量的隐藏层?

python - 用于 DQN 强化学习的 Keras Tensorboard