gpt4 book ai didi

Python pandas 没有属性 ols - 错误(滚动 OLS)

转载 作者:太空狗 更新时间:2023-10-30 02:26:31 26 4
gpt4 key购买 nike

对于我的评估,我想运行一个滚动的 1000 窗口 OLS 回归估计 在此 URL 中找到的数据集: https://drive.google.com/open?id=0B2Iv8dfU4fTUa3dPYW5tejA0bzg使用以下 Python 脚本。

# /usr/bin/python -tt

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from statsmodels.formula.api import ols

df = pd.read_csv('estimated.csv', names=('x','y'))

model = pd.stats.ols.MovingOLS(y=df.Y, x=df[['y']],
window_type='rolling', window=1000, intercept=True)
df['Y_hat'] = model.y_predict

但是,当我运行我的 Python 脚本时,出现此错误:AttributeError: module 'pandas.stats' has no attribute 'ols'。这个错误可能来 self 正在使用的版本吗?我的 Linux 节点上安装的 pandas 版本为 0.20.2

最佳答案

pd.stats.ols.MovingOLS 已在 Pandas 版本 0.20.0 中删除

http://pandas-docs.github.io/pandas-docs-travis/whatsnew.html#whatsnew-0200-prior-deprecations

https://github.com/pandas-dev/pandas/pull/11898

对于像滚动回归这样明显的用例,我找不到“现成”的解决方案。

以下应该可以解决问题,而无需在更优雅的解决方案上投入太多时间。它使用numpy根据回归参数和滚动窗口中的X值计算回归的预测值。

window = 1000
a = np.array([np.nan] * len(df))
b = [np.nan] * len(df) # If betas required.
y_ = df.y.values
x_ = df[['x']].assign(constant=1).values
for n in range(window, len(df)):
y = y_[(n - window):n]
X = x_[(n - window):n]
# betas = Inverse(X'.X).X'.y
betas = np.linalg.inv(X.T.dot(X)).dot(X.T).dot(y)
y_hat = betas.dot(x_[n, :])
a[n] = y_hat
b[n] = betas.tolist() # If betas required.

上面的代码等价于下面的代码并且快了大约 35%:

model = pd.stats.ols.MovingOLS(y=df.y, x=df.x, window_type='rolling', window=1000, intercept=True)
y_pandas = model.y_predict

关于Python pandas 没有属性 ols - 错误(滚动 OLS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44707384/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com