我正在尝试对 pandas 数据框中的某些数据拟合自回归模型。
我当前的代码:-
import pandas as pd
import statsmodels.tsa.api as smt
store=[]
df = pd.DataFrame({'A':[0.345, 0.985, 0.912, 0.645, 0.885, 0.121],
'B':[0.475, 0.502, 0.312, 0.231, 0.450, 0.234],
'C':[0.098, 0.534, 0.125, 0.984, 0.236, 0.734],
'D':[0.345, 0.467, 0.935, 0.074, 0.623, 0.469]})
for i in range(len(df.columns)):
x=smt.AR(df.iloc[:,i]).fit(maxlag=1, ic='aic', trend='nc')
store.append(x)
我想知道我是否可以使用 apply 或 applymap 或 lambda 函数来代替 for 循环
最佳答案
我无法测试它,因为我没有这些包,但从 .apply()
's docs 中给出的示例来看你应该能够做到这一点:
def fit_it(vector):
return smt.AR(vector).fit(maxlag=1, ic='aic', trend='nc').params[0]
results = df.apply(fit_it, axis=0, reduce=True)
关于python - 在所有 pandas 数据框列上应用 statsmodels 自回归函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59255155/