設計素材網站源碼seo谷歌外貿推廣
目錄
1 實驗數(shù)據(jù)集
2 如何運行自己的數(shù)據(jù)集
3 報錯分析
1 實驗數(shù)據(jù)集
實驗數(shù)據(jù)集采用數(shù)據(jù)集4:2016年電工數(shù)學建模競賽負荷預測數(shù)據(jù)集(下載鏈接),數(shù)據(jù)集包含日期、最高溫度℃?、最低溫度℃、平均溫度℃?、相對濕度(平均)?、降雨量(mm)、日需求負荷(KWh),時間間隔為1H。
在使用數(shù)據(jù)之前相對數(shù)據(jù)進行處理,用其他數(shù)據(jù)集時也是同樣的處理方法。首先讀取數(shù)據(jù),發(fā)數(shù)據(jù)不是UTF-8格式,通過添加encoding = 'gbk'讀取數(shù)據(jù),模型傳入的數(shù)據(jù)必須是UTF-8格式。
df= pd.read_table('E:\\課題\\08數(shù)據(jù)集\\2016年電工數(shù)學建模競賽負荷預測數(shù)據(jù)集\\2016年電工數(shù)學建模競賽負荷預測數(shù)據(jù)集.txt',encoding = 'gbk')
然后檢查數(shù)據(jù)是否有缺失值:
df.isnull().sum()
發(fā)現(xiàn)數(shù)據(jù)存在少量缺失值,分析數(shù)據(jù)特點,可以通過前項或后項填充填補缺失值:
df = df.fillna(method='ffill')
后面需要將表格列名改為英文,時間列名為date,不然后面運行時會報錯:
df.columns = ["date","max_temperature(℃)","Min_temperature(℃ )","Average_temperature(℃)","Relative_humidity(average)","Rainfall(mm)","Load"]
最后將數(shù)據(jù)按UTF-8格式保存
load.to_csv('E:\\課題\\08數(shù)據(jù)集\\2016年電工數(shù)學建模競賽負荷預測數(shù)據(jù)集\\2016年電工數(shù)學建模競賽負荷預測數(shù)據(jù)集_處理后.csv', index=False,encoding = 'utf-8')
最后可視化看一下數(shù)據(jù):
# 可視化
load.drop(['date'], axis=1, inplace=True)
cols = list(load.columns)
fig = plt.figure(figsize=(16,6))
plt.tight_layout()
plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=0.8)
for i in range(len(cols)):ax = fig.add_subplot(3,2,i+1)ax.plot(load.iloc[:,i])ax.set_title(cols[i])# plt.subplots_adjust(hspace=1)
?
2 如何運行自己的數(shù)據(jù)集
前面兩篇文章介紹了論文的原理、代碼解析和官方數(shù)據(jù)集訓練和運行,那么大家在利用模型訓練自己的數(shù)據(jù)集的時候需要修改的幾處地方。
parser.add_argument('--data', type=str, default='custom', help='data')
parser.add_argument('--root_path', type=str, default='./data/Load/', help='root path of the data file')
parser.add_argument('--data_path', type=str, default='load.csv', help='data file')
parser.add_argument('--features', type=str, default='MS', help='forecasting task, options:[M, S, MS]; M:multivariate predict multivariate, S:univariate predict univariate, MS:multivariate predict univariate')
parser.add_argument('--target', type=str, default='Load', help='target feature in S or MS task')
parser.add_argument('--freq', type=str, default='h', help='freq for time features encoding, options:[s:secondly, t:minutely, h:hourly, d:daily, b:business days, w:weekly, m:monthly], you can also use more detailed freq like 15min or 3h')
- data:必須填寫?default='custom',也就是改為自定義的數(shù)據(jù)
- root_path:填寫數(shù)據(jù)文件夾路徑
- data_path:填寫具體的數(shù)據(jù)文件名
- features:前面有講解,features有三個選項(M,MS,S),分別是多元預測多元,多元預測單元,單元預測單元,具體是看你自己的數(shù)據(jù)集。
- target:就是你數(shù)據(jù)集中你想要知道那列的預測值的列名,這里改為Load
- freq:就是你兩條數(shù)據(jù)之間的時間間隔。
parser.add_argument('--seq_len', type=int, default=96, help='input sequence length of Informer encoder')
parser.add_argument('--label_len', type=int, default=48, help='start token length of Informer decoder')
parser.add_argument('--pred_len', type=int, default=24, help='prediction sequence length')
- seq_len:用過去的多少條數(shù)據(jù)來預測未來的數(shù)據(jù)
- label_len:可以裂解為更高的權重占比的部分要小于seq_len
- pred_len:預測未來多少個時間點的數(shù)據(jù)
parser.add_argument('--enc_in', type=int, default=6, help='encoder input size')
parser.add_argument('--dec_in', type=int, default=6, help='decoder input size')
parser.add_argument('--c_out', type=int, default=1, help='output size')
- enc_in:你數(shù)據(jù)有多少列,要減去時間那一列,這里我是輸入8列數(shù)據(jù)但是有一列是時間所以就填寫7
- dec_in:同上
- c_out:這里有一些不同如果你的features填寫的是M那么和上面就一樣,如果填寫的MS那么這里要輸入1因為你的輸出只有一列數(shù)據(jù)。
## 解析數(shù)據(jù)集的信息 ##
# 字典data_parser中包含了不同數(shù)據(jù)集的信息,鍵值為數(shù)據(jù)集名稱('ETTh1'等),對應一個包含.csv數(shù)據(jù)文件名
# 目標特征、M、S和MS等參數(shù)的字典
data_parser = {'ETTh1':{'data':'ETTh1.csv','T':'OT','M':[7,7,7],'S':[1,1,1],'MS':[7,7,1]},'ETTh2':{'data':'ETTh2.csv','T':'OT','M':[7,7,7],'S':[1,1,1],'MS':[7,7,1]},'ETTm1':{'data':'ETTm1.csv','T':'OT','M':[7,7,7],'S':[1,1,1],'MS':[7,7,1]},'ETTm2':{'data':'ETTm2.csv','T':'OT','M':[7,7,7],'S':[1,1,1],'MS':[7,7,1]},'WTH':{'data':'WTH.csv','T':'WetBulbCelsius','M':[12,12,12],'S':[1,1,1],'MS':[12,12,1]},'ECL':{'data':'ECL.csv','T':'MT_320','M':[321,321,321],'S':[1,1,1],'MS':[321,321,1]},'Solar':{'data':'solar_AL.csv','T':'POWER_136','M':[137,137,137],'S':[1,1,1],'MS':[137,137,1]},'Custom':{'data':'load.csv','T':'Load','M':[137,137,137],'S':[1,1,1],'MS':[6,6,1]},
}
預測結果保存在result文件下,保存格式為numpy,可以通過下面的腳本進行可視化預測結果:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt# 指定.npy文件路徑
file_path1 = "results/informer_ETTh1_ftM_sl96_ll48_pl24_dm512_nh8_el2_dl1_df2048_atprob_fc5_ebtimeF_dtTrue_mxTrue_test_0/true.npy"
file_path2 = "results/informer_ETTh1_ftM_sl96_ll48_pl24_dm512_nh8_el2_dl1_df2048_atprob_fc5_ebtimeF_dtTrue_mxTrue_test_1/pred.npy"# 使用NumPy加載.npy文件
true_value = []
pred_value = []
data1 = np.load(file_path1)
data2 = np.load(file_path2)
print(data2)
for i in range(24):true_value.append(data2[0][i][6])pred_value.append(data1[0][i][6])# 打印內容
print(true_value)
print(pred_value)#保存數(shù)據(jù)
df = pd.DataFrame({'real': true_value, 'pred': pred_value})
df.to_csv('results.csv', index=False)#繪制圖形
fig = plt.figure(figsize=( 16, 8))
plt.plot(df['real'], marker='o', markersize=8)
plt.plot(df['pred'], marker='o', markersize=8)
plt.tick_params(labelsize = 28)
plt.legend(['real','pred'],fontsize=28)
plt.show()
最后預測的效果如下,發(fā)現(xiàn)并不是太好,后續(xù)看參數(shù)調優(yōu)后是否能提升模型預測效果。?
3 報錯分析
報錯1:UnicodeDecodeError: 'utf-8' codec can't decode bytes in position 56-57: invalid continuation byte,具體來說,'utf-8' 編解碼器無法解碼文件中的某些字節(jié),因為它們不符合 UTF-8 編碼的規(guī)則。
File "D:\Progeam Files\python\lib\site-packages\pandas\io\parsers\c_parser_wrapper.py", line 93, in __init__self._reader = parsers.TextReader(src, **kwds)File "pandas\_libs\parsers.pyx", line 548, in pandas._libs.parsers.TextReader.__cinit__File "pandas\_libs\parsers.pyx", line 637, in pandas._libs.parsers.TextReader._get_headerFile "pandas\_libs\parsers.pyx", line 848, in pandas._libs.parsers.TextReader._tokenize_rowsFile "pandas\_libs\parsers.pyx", line 859, in pandas._libs.parsers.TextReader._check_tokenize_statusFile "pandas\_libs\parsers.pyx", line 2017, in pandas._libs.parsers.raise_parser_error
UnicodeDecodeError: 'utf-8' codec can't decode bytes in position 56-57: invalid continuation byte
解決辦法:
(1) 根據(jù)提示,要將數(shù)據(jù)更改'utf-8'格式,最簡便的方法將數(shù)據(jù)用記事本打開,另存為是通過UTF-8格式保存??
(2)?嘗試使用其他編解碼器(如 'latin1')來讀取文件,或者在讀取文件時指定正確的編碼格式。
?報錯2:ValueError: list.remove(x): x not in list,試從列表中刪除兩個元素,但是這兩個元素中至少有一個不在列表中。
File "E:\課題\07代碼\Informer2020-main\Informer2020-main\data\data_loader.py", line 241, in __read_data__
cols = list(df_raw.columns); cols.remove(self.target); cols.remove('date')
ValueError: list.remove(x): x not in list
解決辦法:在沒有找到具體原因的時候可以在刪除元素之前先檢查一下列表中是否包含要刪除的元素,或者使用 try-except 語句來捕獲異常,以便在元素不存在時不會導致程序中斷。通過檢查,數(shù)據(jù)中的列名最好改為英文,避免產生亂碼。
if self.cols:cols=self.cols.copy()cols.remove(self.target)
else:# 添加調試信息cols = list(df_raw.columns)print(cols) # 輸出列的內容if self.target in cols:cols.remove(self.target)else:print(f"{self.target} not in columns")if 'date' in cols:cols.remove('date')else:print("date not in columns")# 添加調試信息cols = list(df_raw.columns); cols.remove(self.target); cols.remove('date')
df_raw = df_raw[['date']+cols+[self.target]]