亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

熊貓循環優化

熊貓循環優化

繁星coding 2021-04-30 14:15:49
是否有更好的方法(在性能方面)在熊貓中執行以下循環(假設df為DataFrame)?for i in range(len(df)):    if df['signal'].iloc[i] == 0:   # if the signal is negative        if df['position'].iloc[i - 1] - 0.02 < -1:   # if the row above - 0.1 < -1 set the value of current row to -1            df['position'].iloc[i] = -1        else:   # if the new col value above -0.1 is > -1 then subtract 0.1 from that value            df['position'].iloc[i] = df['position'].iloc[i - 1] - 0.02    elif df['signal'].iloc[i] == 1:     # if the signal is positive        if df['position'].iloc[i - 1] + 0.02 > 1:     # if the value above + 0.1 > 1 set the current row to 1            df['position'].iloc[i] = 1        else:   # if the row above + 0.1 < 1 then add 0.1 to the value of the current row            df['position'].iloc[i] = df['position'].iloc[i - 1] + 0.02我將不勝感激,因為我剛開始走熊貓路,很顯然,可能會錯過一些關鍵的事情。源CSV數據:Date,sp500,sp500 MA,UNRATE,UNRATE MA,signal,position2000-01-01,,,4.0,4.191666666666665,1,02000-01-02,,,4.0,4.191666666666665,1,02000-01-03,102.93,95.02135,4.0,4.191666666666665,1,02000-01-04,98.91,95.0599,4.0,4.191666666666665,1,02000-01-05,99.08,95.11245000000001,4.0,4.191666666666665,1,02000-01-06,97.49,95.15450000000001,4.0,4.191666666666665,1,02000-01-07,103.15,95.21575000000001,4.0,4.191666666666665,1,02000-01-08,103.15,95.21575000000001,4.0,4.191666666666665,1,02000-01-09,103.15,95.21575000000001,4.0,4.191666666666665,1,0更新下面的所有答案(在撰寫本文時)產生的position0.02常數與我的幼稚循環方法不同。換句話說,我要尋找這樣會給一個解決方案0.02,0.04,0.06,0.08等為position列。
查看完整描述

3 回答

?
回首憶惘然

TA貢獻1847條經驗 獲得超11個贊

感謝您添加數據和示例輸出。首先,我很確定您不能對它進行矢量化處理,因為每個計算都取決于上一個的輸出。所以這是我所能做到的最好的。


您的方法大約0.116999在我的機器上幾秒鐘


這個大約在0.0039999幾秒鐘之內


沒有向量化,但是速度得到了很好的提高,因為為此使用列表并將其添加回末尾的數據幀更快。


def myfunc(pos_pre, signal):

    if signal == 0:  # if the signal is negative

        # if the new col value above -0.2 is > -1 then subtract 0.2 from that value

        pos = pos_pre - 0.02

        if pos < -1:  # if the row above - 0.2 < -1 set the value of current row to -1

            pos = -1


    elif signal == 1:

        # if the row above + 0.2 < 1 then add 0.2 to the value of the current row

        pos = pos_pre + 0.02

        if pos > 1:  # if the value above + 0.1 > 1 set the current row to 1

            pos = 1


    return pos



''' set first position value because you aren't technically calculating it correctly in your method since there is no 

position minus 1... IE: it will always be 0.02'''

new_pos = [0.02]


# skip index zero since there is no position 0 minus 1

for i in range(1, len(df)):

    new_pos.append(myfunc(pos_pre=new_pos[i-1], signal=df['signal'].iloc[i]))


df['position'] = new_pos

輸出:


df.position

0    0.02

1    0.04

2    0.06

3    0.08

4    0.10

5    0.12

6    0.14

7    0.16

8    0.18


查看完整回答
反對 回復 2021-05-11
?
慕森卡

TA貢獻1806條經驗 獲得超8個贊

不要使用循環。熊貓專門從事矢量化運算,例如signal == 0:


pos_shift = df['position'].shift() - 0.02

m1 = df['signal'] == 0

m2 = pos_shift < -1


df.loc[m1 & m2, 'position'] = -1

df['position'] = np.where(m1 & ~m2, pos_shift, df['position'])

您可以為編寫類似的內容signal == 1。


查看完整回答
反對 回復 2021-05-11
?
汪汪一只貓

TA貢獻1898條經驗 獲得超8個贊

有很多更好的方法,但是這種方法也應該起作用:


df['previous'] = df.signal.shift()


def get_signal_value(row):

    if row.signal == 0:

        compare = row.previous - 0.02

        if compare < -1:

            return -1

        else:

            return compare

    elif row.signal == 1: 

        compare = row.previous + 0.01

        if compare > 1:

            return 1

        else:

            return compare


df['new_signal'] = df.apply(lambda row: get_signal_value(row), axis=1)


查看完整回答
反對 回復 2021-05-11
  • 3 回答
  • 0 關注
  • 173 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號