Mengambil kerangka data berikut sebagai contoh:

                     a
2011-01-01 00:00:00  9
2011-01-01 01:00:00  4
2011-01-01 02:00:00  2
2011-01-01 03:00:00  5
2011-01-01 04:00:00  3
2011-01-01 05:00:00  7
2011-01-01 06:00:00  8
2011-01-01 07:00:00  4

Saya ingin memproses potongan kolom "a" dengan jendela bergerak, menggunakan fungsi autoCorr() di bawah ini untuk menghasilkan kolom "b" yang sesuai.

def autoCorr(x):
    return np.corrcoef(x[:-1], x[1:])[0,1]

Untuk jendela bergerak dengan panjang 5, hasil akhirnya adalah:

                     a       b
2011-01-01 00:00:00  9  0.0000
2011-01-01 01:00:00  4  0.0000
2011-01-01 02:00:00  2  0.0000
2011-01-01 03:00:00  5  0.0000
2011-01-01 04:00:00  3 -0.0877    # autoCorr([9,4,2,5,3])
2011-01-01 05:00:00  7 -0.6404    # autoCorr([4,2,5,3,7])
2011-01-01 06:00:00  8  0.2880    # autoCorr([2,5,3,7,8])
2011-01-01 07:00:00  4 -0.1580    # autoCorr([5,3,7,8,4])
1
IamTheWalrus 26 Oktober 2017, 11:02

1 menjawab

Jawaban Terbaik

Gunakan Rolling.apply + fillna :

df = df.rolling(5).apply(autoCorr).fillna(0)
print (df)
                            a
2011-01-01 00:00:00  0.000000
2011-01-01 01:00:00  0.000000
2011-01-01 02:00:00  0.000000
2011-01-01 03:00:00  0.000000
2011-01-01 04:00:00 -0.087706
2011-01-01 05:00:00 -0.640445
2011-01-01 06:00:00  0.288136
2011-01-01 07:00:00 -0.157877

df = df.rolling(5).apply(lambda x: pd.Series(x).autocorr(1)).fillna(0)
print (df)

                            a
2011-01-01 00:00:00  0.000000
2011-01-01 01:00:00  0.000000
2011-01-01 02:00:00  0.000000
2011-01-01 03:00:00  0.000000
2011-01-01 04:00:00 -0.087706
2011-01-01 05:00:00 -0.640445
2011-01-01 06:00:00  0.288136
2011-01-01 07:00:00 -0.157877

EDIT:

df1 = df.join(df.rolling(5).apply(autoCorr).fillna(0)['a'].rename('b'))
print (df1)
                     a         b
2011-01-01 00:00:00  9  0.000000
2011-01-01 01:00:00  4  0.000000
2011-01-01 02:00:00  2  0.000000
2011-01-01 03:00:00  5  0.000000
2011-01-01 04:00:00  3 -0.087706
2011-01-01 05:00:00  7 -0.640445
2011-01-01 06:00:00  8  0.288136
2011-01-01 07:00:00  4 -0.157877
1
jezrael 26 Oktober 2017, 11:23