python 使用Pandas Dataframe 预测价格的For循环未追加

egdjgwm8  于 2022-11-27  发布在  Python
关注(0)|答案(2)|浏览(167)

我目前正在使用一个TensorFlow模型来预测一条曲线的X个下一个价格,这个模型使用一个for循环来调用Pandas Dataframe 的append()函数。
该模型是一个时间序列模型,因此在每个循环中,我使用 Dataframe 的最后一行计算“下一个日期”,并使用 Dataframe 的最后一行计算预测价格,然后将包含“下一个日期”和预测价格的新行附加到 Dataframe ,以便它可以在下一个循环中预测下一个价格。
问题是 Dataframe 未被追加
这是代码,如果有人知道,也如果它不应该这样做,不要犹豫,纠正我我这样做,因为我知道我是整个TensorFlow / Pandas模块的新手

last_data = pd.read_excel("Nickel.xlsx")
print('Old dataset before loop : ', last_data)
for i in range(10):
        new_df = last_data.filter(['Valeur'])
        last_60_days = new_df[-60+(-i):].values
        last_60_days_scaled = scaler.transform(last_60_days)
        X_test = []
        X_test.append(last_60_days_scaled)
        X_test = np.array(X_test)
        X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))
        pred_price = model.predict(X_test)
        pred_price = scaler.inverse_transform(pred_price)
        #print('Prix predit : ', pred_price)
        dernieredate = last_data['Date'].iloc[-1]
        datecorrect = pd.to_datetime(dernieredate)
        print('Old date : ', datecorrect)
        nextdate = datecorrect + pd.to_timedelta(1,unit='d')
        print('New date : ', nextdate)
        last_data.append([nextdate, pred_price])
print('New dataset final after loop : ', last_data)

日志如下:

Old dataset before loop :             Date  Valeur
0    2002-09-16    6770
1    2002-09-17    6550
2    2002-09-18    6590
3    2002-09-19    6610
4    2002-09-20    6580
...         ...     ...
4995 2022-11-14   27000
4996 2022-11-15   29595
4997 2022-11-16   28550
4998 2022-11-17   26050
4999 2022-11-18   24800

[5000 rows x 2 columns]
1/1 [==============================] - 0s 20ms/step
Prix predit :  [[26672.488]]
Old date :  2022-11-18 00:00:00
New date :  2022-11-19 00:00:00
1/1 [==============================] - 0s 22ms/step
Old date :  2022-11-18 00:00:00
New date :  2022-11-19 00:00:00
1/1 [==============================] - 0s 21ms/step
Old date :  2022-11-18 00:00:00
New date :  2022-11-19 00:00:00
1/1 [==============================] - 0s 20ms/step
Old date :  2022-11-18 00:00:00
New date :  2022-11-19 00:00:00
1/1 [==============================] - 0s 22ms/step
Old date :  2022-11-18 00:00:00
New date :  2022-11-19 00:00:00
1/1 [==============================] - 0s 21ms/step
Old date :  2022-11-18 00:00:00
New date :  2022-11-19 00:00:00
1/1 [==============================] - 0s 22ms/step
Old date :  2022-11-18 00:00:00
New date :  2022-11-19 00:00:00
1/1 [==============================] - 0s 21ms/step
Old date :  2022-11-18 00:00:00
New date :  2022-11-19 00:00:00
1/1 [==============================] - 0s 20ms/step
Old date :  2022-11-18 00:00:00
New date :  2022-11-19 00:00:00
1/1 [==============================] - 0s 22ms/step
Old date :  2022-11-18 00:00:00
New date :  2022-11-19 00:00:00
New dataset final after loop :             Date  Valeur
0    2002-09-16    6770
1    2002-09-17    6550
2    2002-09-18    6590
3    2002-09-19    6610
4    2002-09-20    6580
...         ...     ...
4995 2022-11-14   27000
4996 2022-11-15   29595
4997 2022-11-16   28550
4998 2022-11-17   26050
4999 2022-11-18   24800

[5000 rows x 2 columns]

谢谢你的好意!

piztneat

piztneat1#

尝试更改:

last_data.append([nextdate, pred_price])

至:

last_data = last_data.concat([nextdate, pred_price])
q3qa4bjr

q3qa4bjr2#

感谢您发送编修。
append函数不像Python的list.append()函数那样工作,正如他所说,解决方案是将pred_price和next_data赋给一个新变量!

last_data = pd.read_excel("Nickel.xlsx")
print('Old dataset before loop : ', last_data)
for i in range(10):
        new_df = last_data.filter(['Valeur'])
        last_60_days = new_df[-60+(-i):].values
        last_60_days_scaled = scaler.transform(last_60_days)
        X_test = []
        X_test.append(last_60_days_scaled)
        X_test = np.array(X_test)
        X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))
        pred_price = model.predict(X_test)
        pred_price = scaler.inverse_transform(pred_price)
        #print('Prix predit : ', pred_price)
        dernieredate = last_data['Date'].iloc[-1]
        datecorrect = pd.to_datetime(dernieredate)
        print('Old date : ', datecorrect)
        nextdate = datecorrect + pd.to_timedelta(1,unit='d')
        print('New date : ', nextdate)
        Data_Temp = {'Date':nextdate, 'Valeur':pred_price[0]}
        New_data = last_data.append(Data_Temp, ignore_index=True)
        
print('New dataset final after loop : ', New_data)

相关问题