Python 在滚动底座上涂抹polyfit

Python 在滚动底座上涂抹polyfit,python,pandas,Python,Pandas,我发现这篇关于polyfit的有用文章效果非常好: 现在我想在滚动的基础上使用这个 def test(input_list, i): if sum(~np.isnan(x) for x in input_list) < 2: return np.NaN print(input_list) coefficients, residuals, _, _, _ = np.polyfit(range(len(input_list)),input_list

我发现这篇关于polyfit的有用文章效果非常好:

现在我想在滚动的基础上使用这个

def test(input_list, i):
    if sum(~np.isnan(x) for x in input_list) < 2:
        return np.NaN

    print(input_list)

    coefficients, residuals, _, _, _ = np.polyfit(range(len(input_list)),input_list,1,full=True)
    mse = residuals[0]/(len(input_list))
    nrmse = np.sqrt(mse)/(input_list.max() - input_list.min())
    print('Slope ' + str(coefficients[0]))
    print('NRMSE: ' + str(nrmse))
    a = coefficients[0]*i + coefficients[1]

    return a

df['pred'] = df['abs'].rolling(window=2, min_periods=1, center=False).apply(lambda x: test(x, base1.index))
def测试(输入列表,i):
如果sum(~np.isnan(x)表示输入列表中的x)<2:
返回np.NaN
打印(输入列表)
系数,残差,多项式拟合(范围(len(输入列表)),输入列表,1,满=真)
mse=残差[0]/(len(输入列表))
nrmse=np.sqrt(mse)/(input_list.max()-input_list.min())
打印('Slope'+str(系数[0]))
打印('NRMSE:'+str(NRMSE))
a=系数[0]*i+系数[1]
归还
df['pred']=df['abs']。滚动(窗口=2,最小周期=1,中心=False)。应用(lambda x:test(x,base1.index))
但我不会让它工作:)

我明白了 索引器错误:索引0超出大小为0的轴0的界限,而不是正确的结果:)

有人有主意吗?谢谢e

****编辑1****

对不起,我错过了一个具体的例子。。。 通过在df中转换numpy数组,我成功地使函数工作。 但不知何故,残差是空的

import quandl
import MySQLdb
import pandas as pd
import numpy as np
import sys
import matplotlib.pyplot as plt

def test(input_list, i):

    if sum(~np.isnan(x) for x in input_list) < 2:
        return np.NaN

    abc  = pd.DataFrame(input_list)

    coefficients, residuals, _, _, _ = np.polyfit(range(len(abc)),abc[0],1,full=True)

    #residuals is empty... why?
    a = coefficients[0]*len(abc) + coefficients[1]

    return a

df = quandl.get("WIKI/GOOGL")
df = df.ix[:, ['High', 'Low', 'Close']]


#reseit index for calc
#base1['DateTime'] = base1.index 
#base1.index = range(len(base1))

df['close_pred'] = df['Close'].rolling(window=15, min_periods=2, center=False).apply(lambda x: test(x, 0))

print(df.head(30).to_string())
import-quandl
导入MySQLdb
作为pd进口熊猫
将numpy作为np导入
导入系统
将matplotlib.pyplot作为plt导入
def测试(输入_列表,i):
如果sum(~np.isnan(x)表示输入列表中的x)<2:
返回np.NaN
abc=pd.DataFrame(输入列表)
系数,残差,多项式拟合(范围(len(abc)),abc[0],1,full=True)
#残差是空的。。。为什么?
a=系数[0]*len(abc)+系数[1]
归还
df=quandl.get(“WIKI/GOOGL”)
df=df.ix[:,['High','Low','Close']]
#为calc重新编制索引
#base1['DateTime']=base1.index
#base1.index=范围(len(base1))
df['close_pred']=df['close'].滚动(窗口=15,最小周期=2,中心=False)。应用(lambda x:test(x,0))
打印(df.head(30.to_string())

仅在第1次迭代中,残差为空。请参阅少量修改的代码和答案

def test(data):

    if sum(~np.isnan(x) for x in data) < 2:
        return np.NaN

    df = pd.DataFrame(data)
    coefficients, residuals, _, _, _ = np.polyfit(range(len(data)),df[0],1,full=True)

    #if residuals.size == 0:
    #    residuals = [0] 

    print(coefficients[-2], residuals, data)

    return coefficients[-2]
df_xx['pred'] = df_xx[0].rolling(window=5, min_periods=2, center=False).apply(lambda y: test(y))
0.9999999999999998 [] [0. 1.]
1.0 [4.29279946e-34] [0. 1. 2.]
1.0000000000000002 [3.62112419e-33] [0. 1. 2. 3.]
0.9999999999999999 [8.77574736e-31] [0. 1. 2. 3. 4.]
0.9999999999999999 [1.25461096e-30] [1. 2. 3. 4. 5.]
0.9999999999999999 [2.93468782e-30] [2. 3. 4. 5. 6.]
0.9999999999999997 [1.38665176e-30] [3. 4. 5. 6. 7.]
0.9999999999999997 [2.18347839e-30] [4. 5. 6. 7. 8.]
0.9999999999999999 [6.21693422e-30] [5. 6. 7. 8. 9.]
1.0 [1.07025673e-29] [ 6.  7.  8.  9. 10.]
1.0000000000000002 [1.4374879e-29] [ 7.  8.  9. 10. 11.]
0.9999999999999997 [1.14542951e-29] [ 8.  9. 10. 11. 12.]
1.0000000000000004 [9.73226454e-30] [ 9. 10. 11. 12. 13.]
0.9999999999999997 [1.99069506e-29] [10. 11. 12. 13. 14.]
0.9999999999999997 [1.09437894e-29] [11. 12. 13. 14. 15.]
1.0 [3.60983058e-29] [12. 13. 14. 15. 16.]
1.0000000000000002 [1.90967258e-29] [13. 14. 15. 16. 17.]
1.0000000000000002 [3.13030715e-29] [14. 15. 16. 17. 18.]
1.0 [1.25806434e-29] [15. 16. 17. 18. 19.]
下面的简单代码修复它

if residuals.size == 0:
        residuals = [0] 

到目前为止你做了什么调试?反复试验:)有人吗?谢谢e、 如果你没有领会我的意思,你没有对问题代码进行最小限度的重构,这样其他人就可以对其进行测试或提供见解。@BobDalgleish你是对的。我用一个完全有效的例子更新了我的帖子。干杯
if residuals.size == 0:
        residuals = [0]