簡體   English   中英

python DataFrame 中的嵌套循環以創建多個時間序列預測

[英]nested loops in python DataFrame to create multiple time series forecasts

我是 Python 的新手,主要用於統計數據。 這是數據集的一瞥:

Code    City    Date    Sales   
K1  W   1/1/2017    46506.92  
K1  X   1/1/2017    187195.2  
K1  Y   1/1/2017    12858.15  
K1  Z   1/1/2017    25300.88  
K2  W   1/1/2017    87731.47  
K2  X   1/1/2017    14952.8  
K3  Y   1/1/2017    167.8204  
K4  A   1/1/2017    9602.108  
K4  B   1/1/2017    16034.13  
K4  C   1/1/2017    106.5196  
K4  D   1/1/2017    1057.269  
K5  W   1/1/2017    12346.57  
K5  X   1/1/2017    528776.5  
K5  Y   1/1/2017    7598.979  
K5  Z   1/1/2017    147969.6  
K6  W   1/1/2017    11770.68  
K6  X   1/1/2017    180867.6  
K6  Y   1/1/2017    11778.6  
K6  Z   1/1/2017    48835.3  

City = 字符串列表和相同的代碼可能位於多個城市,但每個 Code-City 組合都是唯一的,有 32 個數據點。 數據的有效期為 32 個月,每月 1 日收集。 我需要從各個預測中創建一組 rmse 誤差值。 每個預測都是代碼城市級別。 我為 ARIMA 寫了一個 def function(不能使用先知應急)
我嘗試按代碼分層過濾 DataFrame,然后使用以下代碼過濾該代碼的城市:

df.loc[lambda x: x['Code'] in Codelist].loc[lambda x: x['City'] in Citylist]
但是得到錯誤
Series 的真值是模棱兩可的。 使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。

        ##The way I want is for example, if Code exists in list of codes, move over to second for loop. Check if the City is present for that Code, if yes, call the defined function for ARIMA. The reason being same code exists in multiple cities.   

我想將作為預測的 rmse 值的結果存儲在一個數組中,並在每次迭代后繼續附加它。 我期待使用 ARIMA 預測的預測 Output 的 5 個浮點值的數組。

要進行個別預測,您可以只采用預先存在於數據中的代碼、城市組合,而不是嘗試所有組合。

for code, city in df[['Code', 'City']].drop_duplicates().values:
    train_df = df[(df['Code']==code)&(df['City']==city)].sort_values(by='Date')
    .....

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM