diff --git a/.idea/misc.xml b/.idea/misc.xml index 695b918..3141537 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/.idea/pytorch2.iml b/.idea/pytorch2.iml index 5cfdc49..719cec4 100644 --- a/.idea/pytorch2.iml +++ b/.idea/pytorch2.iml @@ -2,7 +2,7 @@ - + \ No newline at end of file diff --git a/入模数据/丽水.xlsx b/入模数据/丽水.xlsx index 5a919d7..66d3c30 100644 Binary files a/入模数据/丽水.xlsx and b/入模数据/丽水.xlsx differ diff --git a/各地级市日电量模型/hangzhou.bin b/各地级市日电量模型/hangzhou.bin index b63515a..90f056e 100644 Binary files a/各地级市日电量模型/hangzhou.bin and b/各地级市日电量模型/hangzhou.bin differ diff --git a/各地级市日电量模型/huzhou.bin b/各地级市日电量模型/huzhou.bin index c37fa7d..b087cdd 100644 Binary files a/各地级市日电量模型/huzhou.bin and b/各地级市日电量模型/huzhou.bin differ diff --git a/各地级市日电量模型/jiaxing.bin b/各地级市日电量模型/jiaxing.bin index 1d2f3b2..5a113d9 100644 Binary files a/各地级市日电量模型/jiaxing.bin and b/各地级市日电量模型/jiaxing.bin differ diff --git a/各地级市日电量模型/jinhua.bin b/各地级市日电量模型/jinhua.bin index 61e2fe0..1ba6d54 100644 Binary files a/各地级市日电量模型/jinhua.bin and b/各地级市日电量模型/jinhua.bin differ diff --git a/各地级市日电量模型/quzhou.bin b/各地级市日电量模型/quzhou.bin index 1a3d1b4..bc812cd 100644 Binary files a/各地级市日电量模型/quzhou.bin and b/各地级市日电量模型/quzhou.bin differ diff --git a/各地级市日电量模型/shaoxing.bin b/各地级市日电量模型/shaoxing.bin index 9f1d451..8095997 100644 Binary files a/各地级市日电量模型/shaoxing.bin and b/各地级市日电量模型/shaoxing.bin differ diff --git a/各地级市日电量模型/taizhou.bin b/各地级市日电量模型/taizhou.bin index f25a190..6f7d37f 100644 Binary files a/各地级市日电量模型/taizhou.bin and b/各地级市日电量模型/taizhou.bin differ diff --git a/各地级市日电量模型/wenzhou.bin b/各地级市日电量模型/wenzhou.bin index a23febb..f24a947 100644 Binary files a/各地级市日电量模型/wenzhou.bin and b/各地级市日电量模型/wenzhou.bin differ diff --git a/各地级市日电量模型/zhoushan.bin b/各地级市日电量模型/zhoushan.bin index 8d569a6..2d89acf 100644 Binary files a/各地级市日电量模型/zhoushan.bin and b/各地级市日电量模型/zhoushan.bin differ diff --git a/各地级市日电量模型/丽水.py b/各地级市日电量模型/丽水.py index e536fa7..b454534 100644 --- a/各地级市日电量模型/丽水.py +++ b/各地级市日电量模型/丽水.py @@ -5,8 +5,10 @@ from sklearn.metrics import r2_score from sklearn.model_selection import train_test_split import matplotlib as mpl import matplotlib.pyplot as plt -mpl.rcParams['font.sans-serif']=['kaiti'] -pd.set_option('display.width',None) + +mpl.rcParams['font.sans-serif'] = ['kaiti'] +pd.set_option('display.width', None) + def season(x): if str(x)[5:7] in ('01', '10', '11'): @@ -15,16 +17,17 @@ def season(x): return 1 else: return 2 -def normal(nd): - high = nd.describe()['75%'] + 1.5*(nd.describe()['75%']-nd.describe()['25%']) - low = nd.describe()['25%'] - 1.5*(nd.describe()['75%']-nd.describe()['25%']) - return nd[(ndlow)] +def normal(nd): + high = nd.describe()['75%'] + 1.5 * (nd.describe()['75%'] - nd.describe()['25%']) + low = nd.describe()['25%'] - 1.5 * (nd.describe()['75%'] - nd.describe()['25%']) + return nd[(nd < high) & (nd > low)] + -parent_dir = os.path.abspath(os.path.join(os.getcwd(),os.pardir)) -data = pd.read_excel(os.path.join(parent_dir,'入模数据/丽水.xlsx'),index_col='dtdate') -data.index = pd.to_datetime(data.index,format='%Y-%m-%d') +parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir)) +data = pd.read_excel(os.path.join(parent_dir, '入模数据/丽水.xlsx'), index_col='dtdate') +data.index = pd.to_datetime(data.index, format='%Y-%m-%d') data = data.loc[normal(data['售电量']).index] # list2 = [] @@ -42,56 +45,50 @@ data = data.loc[normal(data['售电量']).index] # print(list0,list1,list2) - data['season'] = data.index.map(season) - df_eval = data.loc['2023-10'] # df_train = data.loc['2021-1':'2023-8'] df_train = data[450:-1] # df_train = data.loc['2022-4':'2023-9'][:-3] -df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']] +df_train = df_train[['tem_max', 'tem_min', 'holiday', '24ST', '售电量', 'season']] -X = df_train[['tem_max','tem_min','24ST','holiday','season']] -X_eval = df_eval[['tem_max','tem_min','24ST','holiday','season']] +X = df_train[['tem_max', 'tem_min', '24ST', 'holiday', 'season']] +X_eval = df_eval[['tem_max', 'tem_min', '24ST', 'holiday', 'season']] y = df_train['售电量'] # best_goal = 1 # best_i = {} # for i in range(200): -x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=42) +x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) model = xgb.XGBRegressor(max_depth=6, learning_rate=0.05, n_estimators=150) -model.fit(x_train,y_train) +model.fit(x_train, y_train) y_pred = model.predict(x_test) -result_test = pd.DataFrame({'test':y_test,'pred':y_pred},index=y_test.index) +result_test = pd.DataFrame({'test': y_test, 'pred': y_pred}, index=y_test.index) # 指标打印 print(abs(y_test - y_pred).mean() / y_test.mean()) eval_pred = model.predict(X_eval) -result_eval = pd.DataFrame({'eval':df_eval['售电量'],'pred':eval_pred},index=df_eval['售电量'].index) +result_eval = pd.DataFrame({'eval': df_eval['售电量'], 'pred': eval_pred}, index=df_eval['售电量'].index) print(result_eval) -goal = (result_eval['eval'][-3:].sum()-result_eval['pred'][-3:].sum())/result_eval['eval'].sum() +goal = (result_eval['eval'][-3:].sum() - result_eval['pred'][-3:].sum()) / result_eval['eval'].sum() print(goal) -goal2 = (result_eval['eval'][-23:].sum()-result_eval['pred'][-23:].sum())/result_eval['eval'].sum() +goal2 = (result_eval['eval'][-23:].sum() - result_eval['pred'][-23:].sum()) / result_eval['eval'].sum() print(goal2) - # if abs(goal) < best_goal: - # best_goal = abs(goal) - # best_i['best_i'] = i -# print(best_i,best_goal) - # with open(r'C:\Users\user\Desktop\9月各地市日电量预测结果\偏差率.txt','a',encoding='utf-8') as f: # f.write(f'丽水月末3天偏差率:{goal},9号-月底偏差率:{goal2}') # # 保存模型 # model.save_model('lishui.bin') import numpy as np -loaded_model = xgb.XGBRegressor() -loaded_model.load_model('lishui.bin') + X_eval = np.array([ - [22.3,16.19,23,1,0], - [23.69,14.5,23,0,0], - [23.69,14,23,0,0]]) + [21,10,10,0,0], + [21, 11, 10, 0, 0], + [18, 7, 10, 0, 0], + [17, 9, 10, 0, 0], + [17, 10, 10, 0, 0] +]) print(model.predict(X_eval)) - diff --git a/各地级市日电量模型/台州.py b/各地级市日电量模型/台州.py index 3ae6660..2680d68 100644 --- a/各地级市日电量模型/台州.py +++ b/各地级市日电量模型/台州.py @@ -83,11 +83,12 @@ print(result_eval) # 保存模型 model.save_model('taizhou.bin') import numpy as np -loaded_model = xgb.XGBRegressor() -loaded_model.load_model('taizhou.bin') + X_eval = np.array([ - [22.6,15.3,23,1,0], - [23.89,13.4,23,0,0], - [24.69,13.4,23,0,0]]) + [19,11,10,0,0], + [21, 7, 10, 0, 0], + [19, 5, 10, 0, 0], + [17, 8, 10, 0, 0], + [16, 7, 10, 0, 0] +]) print(model.predict(X_eval)) - diff --git a/各地级市日电量模型/嘉兴.py b/各地级市日电量模型/嘉兴.py index 78e5f86..01dbc3f 100644 --- a/各地级市日电量模型/嘉兴.py +++ b/各地级市日电量模型/嘉兴.py @@ -5,8 +5,10 @@ from sklearn.metrics import r2_score from sklearn.model_selection import train_test_split import matplotlib as mpl import matplotlib.pyplot as plt -mpl.rcParams['font.sans-serif']=['kaiti'] -pd.set_option('display.width',None) + +mpl.rcParams['font.sans-serif'] = ['kaiti'] +pd.set_option('display.width', None) + def season(x): if str(x)[5:7] in ('04', '10'): @@ -16,15 +18,16 @@ def season(x): else: return 2 + def normal(nd): - high = nd.describe()['75%'] + 1.5*(nd.describe()['75%']-nd.describe()['25%']) - low = nd.describe()['25%'] - 1.5*(nd.describe()['75%']-nd.describe()['25%']) - return nd[(ndlow)] + high = nd.describe()['75%'] + 1.5 * (nd.describe()['75%'] - nd.describe()['25%']) + low = nd.describe()['25%'] - 1.5 * (nd.describe()['75%'] - nd.describe()['25%']) + return nd[(nd < high) & (nd > low)] -parent_dir = os.path.abspath(os.path.join(os.getcwd(),os.pardir)) -data = pd.read_excel(os.path.join(parent_dir,'入模数据/嘉兴.xlsx'),index_col='dtdate') -data.index = pd.to_datetime(data.index,format='%Y-%m-%d') +parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir)) +data = pd.read_excel(os.path.join(parent_dir, '入模数据/嘉兴.xlsx'), index_col='dtdate') +data.index = pd.to_datetime(data.index, format='%Y-%m-%d') data = data.loc[normal(data['售电量']).index] # list2 = [] @@ -46,33 +49,30 @@ df_train = data.iloc[450:-1] # df_train = data[450:][:-3] print(df_train) +df_train = df_train[['tem_max', 'tem_min', 'holiday', '24ST', '售电量', 'season']] -df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']] - - -X = df_train[['tem_max','tem_min','24ST','holiday','season']] -X_eval = df_eval[['tem_max','tem_min','24ST','holiday','season']] +X = df_train[['tem_max', 'tem_min', '24ST', 'holiday', 'season']] +X_eval = df_eval[['tem_max', 'tem_min', '24ST', 'holiday', 'season']] y = df_train['售电量'] # best_goal = 1 # best_i = {} # for i in range(400): -x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.1,random_state=158) +x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=158) model = xgb.XGBRegressor(max_depth=6, learning_rate=0.05, n_estimators=150) -model.fit(x_train,y_train) - +model.fit(x_train, y_train) y_pred = model.predict(x_test) -result_test = pd.DataFrame({'test':y_test,'pred':y_pred},index=y_test.index) +result_test = pd.DataFrame({'test': y_test, 'pred': y_pred}, index=y_test.index) # 指标打印 print(abs(y_test - y_pred).mean() / y_test.mean()) eval_pred = model.predict(X_eval) -result_eval = pd.DataFrame({'eval':df_eval['售电量'],'pred':eval_pred},index=df_eval['售电量'].index) -goal = (result_eval['eval'][-3:].sum()-result_eval['pred'][-3:].sum())/result_eval['eval'].sum() -goal2 = (result_eval['eval'][-23:].sum()-result_eval['pred'][-23:].sum())/result_eval['eval'].sum() -print(goal,goal2) +result_eval = pd.DataFrame({'eval': df_eval['售电量'], 'pred': eval_pred}, index=df_eval['售电量'].index) +goal = (result_eval['eval'][-3:].sum() - result_eval['pred'][-3:].sum()) / result_eval['eval'].sum() +goal2 = (result_eval['eval'][-23:].sum() - result_eval['pred'][-23:].sum()) / result_eval['eval'].sum() +print(goal, goal2) print(result_eval) # print(goal2) # if abs(goal) < best_goal : @@ -87,8 +87,12 @@ model.save_model('jiaxing.bin') loaded_model = xgb.XGBRegressor() loaded_model.load_model('jiaxing.bin') import numpy as np + X_eval = np.array([ - [23.8,16.69,23,1,0], - [24.8,15.09,23,0,0], - [25.5,14.3,23,0,0]]) -print(model.predict(X_eval)) \ No newline at end of file + [17, 9, 10, 0, 1], + [16, 3, 10, 0, 1], + [15, 3, 10, 0, 1], + [15, 7, 10, 0, 1], + [14, 5, 10, 0, 1] +]) +print(model.predict(X_eval)) diff --git a/各地级市日电量模型/宁波.py b/各地级市日电量模型/宁波.py index 1be7cd3..f4c6ea2 100644 --- a/各地级市日电量模型/宁波.py +++ b/各地级市日电量模型/宁波.py @@ -5,8 +5,10 @@ from sklearn.metrics import r2_score from sklearn.model_selection import train_test_split import matplotlib as mpl import matplotlib.pyplot as plt -mpl.rcParams['font.sans-serif']=['kaiti'] -pd.set_option('display.width',None) + +mpl.rcParams['font.sans-serif'] = ['kaiti'] +pd.set_option('display.width', None) + def season(x): if str(x)[5:7] in ('01', '04', '10'): @@ -15,14 +17,17 @@ def season(x): return 1 else: return 2 + + def normal(nd): - high = nd.describe()['75%'] + 1.5*(nd.describe()['75%']-nd.describe()['25%']) - low = nd.describe()['25%'] - 1.5*(nd.describe()['75%']-nd.describe()['25%']) - return nd[(ndlow)] + high = nd.describe()['75%'] + 1.5 * (nd.describe()['75%'] - nd.describe()['25%']) + low = nd.describe()['25%'] - 1.5 * (nd.describe()['75%'] - nd.describe()['25%']) + return nd[(nd < high) & (nd > low)] + -parent_dir = os.path.abspath(os.path.join(os.getcwd(),os.pardir)) -data = pd.read_excel(os.path.join(parent_dir,'入模数据/宁波.xlsx'),index_col='dtdate') -data.index = pd.to_datetime(data.index,format='%Y-%m-%d') +parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir)) +data = pd.read_excel(os.path.join(parent_dir, '入模数据/宁波.xlsx'), index_col='dtdate') +data.index = pd.to_datetime(data.index, format='%Y-%m-%d') data = data.loc[normal(data['售电量']).index] # list2 = [] @@ -45,34 +50,32 @@ df_eval = data.loc['2023-10'] df_train = data.loc['2022-01':'2023-10'][:-1] +df_train = df_train[['tem_max', 'tem_min', 'holiday', '24ST', '售电量', 'season']] - -df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']] - -X = df_train[['tem_max','tem_min','24ST','holiday','season']] -X_eval = df_eval[['tem_max','tem_min','24ST','holiday','season']] +X = df_train[['tem_max', 'tem_min', '24ST', 'holiday', 'season']] +X_eval = df_eval[['tem_max', 'tem_min', '24ST', 'holiday', 'season']] y = df_train['售电量'] # best_goal = 1 # best_i = {} # for i in range(400): -x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.1,random_state=142) +x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=142) model = xgb.XGBRegressor(max_depth=6, learning_rate=0.05, n_estimators=150) -model.fit(x_train,y_train) +model.fit(x_train, y_train) y_pred = model.predict(x_test) -result_test = pd.DataFrame({'test':y_test,'pred':y_pred},index=y_test.index) +result_test = pd.DataFrame({'test': y_test, 'pred': y_pred}, index=y_test.index) # 指标打印 print(abs(y_test - y_pred).mean() / y_test.mean()) eval_pred = model.predict(X_eval) -result_eval = pd.DataFrame({'eval':df_eval['售电量'],'pred':eval_pred},index=df_eval['售电量'].index) +result_eval = pd.DataFrame({'eval': df_eval['售电量'], 'pred': eval_pred}, index=df_eval['售电量'].index) -goal = (result_eval['eval'][-3:].sum()-result_eval['pred'][-3:].sum())/result_eval['eval'].sum() +goal = (result_eval['eval'][-3:].sum() - result_eval['pred'][-3:].sum()) / result_eval['eval'].sum() print(goal) -goal2 = (result_eval['eval'][-23:].sum()-result_eval['pred'][-23:].sum())/result_eval['eval'].sum() +goal2 = (result_eval['eval'][-23:].sum() - result_eval['pred'][-23:].sum()) / result_eval['eval'].sum() print(goal2) print(result_eval) # if abs(goal) < best_goal : @@ -85,14 +88,12 @@ print(result_eval) # 保存模型 # model.save_model('ningbo.bin') import numpy as np -loaded_model = xgb.XGBRegressor() -loaded_model.load_model('ningbo.bin') X_eval = np.array([ - [24.39,17.39,23,1,0], - [24.8,15.09,23,0,0], - [25.8,14.3,23,0,0]]) + [20, 10, 10, 0, 1], + [19, 12, 10, 0, 1], + [18, 9, 10, 0, 1], + [21, 9, 10, 0, 1], + [14, 9, 10, 0, 1] +]) print(model.predict(X_eval)) - - - diff --git a/各地级市日电量模型/杭州.py b/各地级市日电量模型/杭州.py index 95adec6..862c91e 100644 --- a/各地级市日电量模型/杭州.py +++ b/各地级市日电量模型/杭州.py @@ -6,8 +6,10 @@ from sklearn.metrics import r2_score from sklearn.model_selection import train_test_split import matplotlib as mpl import matplotlib.pyplot as plt -mpl.rcParams['font.sans-serif']=['kaiti'] -pd.set_option('display.width',None) + +mpl.rcParams['font.sans-serif'] = ['kaiti'] +pd.set_option('display.width', None) + def season(x): if str(x)[5:7] in ('04', '10'): @@ -16,19 +18,24 @@ def season(x): return 1 else: return 2 + + def month(x): - if str(x)[5:7] in ('08','09','10','12','01','02'): + if str(x)[5:7] in ('08', '09', '10', '12', '01', '02'): return 1 else: return 0 + + def normal(nd): - high = nd.describe()['75%'] + 1.5*(nd.describe()['75%']-nd.describe()['25%']) - low = nd.describe()['25%'] - 1.5*(nd.describe()['75%']-nd.describe()['25%']) - return nd[(ndlow)] + high = nd.describe()['75%'] + 1.5 * (nd.describe()['75%'] - nd.describe()['25%']) + low = nd.describe()['25%'] - 1.5 * (nd.describe()['75%'] - nd.describe()['25%']) + return nd[(nd < high) & (nd > low)] + -parent_dir = os.path.abspath(os.path.join(os.getcwd(),os.pardir)) -data = pd.read_excel(os.path.join(parent_dir,'入模数据/杭州.xlsx'),index_col='dtdate') -data.index = pd.to_datetime(data.index,format='%Y-%m-%d') +parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir)) +data = pd.read_excel(os.path.join(parent_dir, '入模数据/杭州.xlsx'), index_col='dtdate') +data.index = pd.to_datetime(data.index, format='%Y-%m-%d') data = data.loc[normal(data['售电量']).index] # list2 = [] @@ -54,33 +61,33 @@ df_train = data[500:-1] df_eval = data.loc['2023-10'] print(df_train) -X = df_train[['tem_max','tem_min','24ST','holiday','season']] -X_eval = df_eval[['tem_max','tem_min','24ST','holiday','season']] +X = df_train[['tem_max', 'tem_min', '24ST', 'holiday', 'season']] +X_eval = df_eval[['tem_max', 'tem_min', '24ST', 'holiday', 'season']] y = df_train['售电量'] # best_goal = 1 # best_i = {} # for i in range(400): -x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=42) +x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) model = xgb.XGBRegressor(max_depth=6, learning_rate=0.05, n_estimators=150) -model.fit(x_train,y_train) +model.fit(x_train, y_train) y_pred = model.predict(x_test) -result_test = pd.DataFrame({'test':y_test,'pred':y_pred},index=y_test.index) +result_test = pd.DataFrame({'test': y_test, 'pred': y_pred}, index=y_test.index) # 指标打印 print(abs(y_test - y_pred).mean() / y_test.mean()) eval_pred = model.predict(X_eval) -result_eval = pd.DataFrame({'eval':df_eval['售电量'],'pred':eval_pred},index=df_eval['售电量'].index) +result_eval = pd.DataFrame({'eval': df_eval['售电量'], 'pred': eval_pred}, index=df_eval['售电量'].index) -goal = (result_eval['eval'][-3:].sum()-result_eval['pred'][-3:].sum())/result_eval['eval'].sum() -print('goal:',goal) +goal = (result_eval['eval'][-3:].sum() - result_eval['pred'][-3:].sum()) / result_eval['eval'].sum() +print('goal:', goal) -goal2 = (result_eval['eval'][-23:].sum()-result_eval['pred'][-23:].sum())/result_eval['eval'].sum() +goal2 = (result_eval['eval'][-23:].sum() - result_eval['pred'][-23:].sum()) / result_eval['eval'].sum() -print('goal2:',goal2) +print('goal2:', goal2) print(result_eval) -print('r2:',r2_score(y_test,y_pred)) +print('r2:', r2_score(y_test, y_pred)) # # # result_eval.to_csv('asda.csv',encoding='gbk') # # if abs(goal) < best_goal: @@ -107,9 +114,11 @@ model.save_model('hangzhou.bin') loaded_model = xgb.XGBRegressor() loaded_model.load_model('hangzhou.bin') -X_eval = np.array([[24.19,15.30,23,1,0], - [25.1,13.3,23,0,0], - [26.1,11.69,23,0,0]]) - -print(loaded_model.predict(X_eval)) - +X_eval = np.array([ + [18, 9, 10, 0, 0], + [20, 7, 10, 0, 0], + [17, 4, 10, 0, 0], + [15, 8, 10, 0, 0], + [12, 7, 10, 0, 0] +]) +print(model.predict(X_eval)) diff --git a/各地级市日电量模型/温州.py b/各地级市日电量模型/温州.py index ad2a1f5..9d8ab27 100644 --- a/各地级市日电量模型/温州.py +++ b/各地级市日电量模型/温州.py @@ -5,8 +5,10 @@ from sklearn.metrics import r2_score from sklearn.model_selection import train_test_split import matplotlib as mpl import matplotlib.pyplot as plt -mpl.rcParams['font.sans-serif']=['kaiti'] -pd.set_option('display.width',None) + +mpl.rcParams['font.sans-serif'] = ['kaiti'] +pd.set_option('display.width', None) + def season(x): if str(x)[5:7] in ('01', '02', '10'): @@ -16,15 +18,16 @@ def season(x): else: return 2 + def normal(nd): - high = nd.describe()['75%'] + 1.5*(nd.describe()['75%']-nd.describe()['25%']) - low = nd.describe()['25%'] - 1.5*(nd.describe()['75%']-nd.describe()['25%']) - return nd[(ndlow)] + high = nd.describe()['75%'] + 1.5 * (nd.describe()['75%'] - nd.describe()['25%']) + low = nd.describe()['25%'] - 1.5 * (nd.describe()['75%'] - nd.describe()['25%']) + return nd[(nd < high) & (nd > low)] -parent_dir = os.path.abspath(os.path.join(os.getcwd(),os.pardir)) -data = pd.read_excel(os.path.join(parent_dir,'入模数据/温州.xlsx'),index_col='dtdate') -data.index = pd.to_datetime(data.index,format='%Y-%m-%d') +parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir)) +data = pd.read_excel(os.path.join(parent_dir, '入模数据/温州.xlsx'), index_col='dtdate') +data.index = pd.to_datetime(data.index, format='%Y-%m-%d') data = data.loc[normal(data['售电量']).index] # list2 = [] @@ -49,33 +52,33 @@ df_train = data[450:-1] # df_train = data[450:][:-3] print(df_train) -df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']] +df_train = df_train[['tem_max', 'tem_min', 'holiday', '24ST', '售电量', 'season']] -X = df_train[['tem_max','tem_min','24ST','holiday','season']] -X_eval = df_eval[['tem_max','tem_min','24ST','holiday','season']] +X = df_train[['tem_max', 'tem_min', '24ST', 'holiday', 'season']] +X_eval = df_eval[['tem_max', 'tem_min', '24ST', 'holiday', 'season']] y = df_train['售电量'] # best_goal = 1 # best_i = {} # for i in range(400): -x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.1,random_state=42) +x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=42) model = xgb.XGBRegressor(max_depth=6, learning_rate=0.05, n_estimators=150) -model.fit(x_train,y_train) +model.fit(x_train, y_train) y_pred = model.predict(x_test) -result_test = pd.DataFrame({'test':y_test,'pred':y_pred},index=y_test.index) +result_test = pd.DataFrame({'test': y_test, 'pred': y_pred}, index=y_test.index) # 指标打印 print(abs(y_test - y_pred).mean() / y_test.mean()) eval_pred = model.predict(X_eval) -result_eval = pd.DataFrame({'eval':df_eval['售电量'],'pred':eval_pred},index=df_eval['售电量'].index) +result_eval = pd.DataFrame({'eval': df_eval['售电量'], 'pred': eval_pred}, index=df_eval['售电量'].index) -goal = (result_eval['eval'][-3:].sum()-result_eval['pred'][-3:].sum())/result_eval['eval'].sum() +goal = (result_eval['eval'][-3:].sum() - result_eval['pred'][-3:].sum()) / result_eval['eval'].sum() print(goal) -goal2 = (result_eval['eval'][-23:].sum()-result_eval['pred'][-23:].sum())/result_eval['eval'].sum() +goal2 = (result_eval['eval'][-23:].sum() - result_eval['pred'][-23:].sum()) / result_eval['eval'].sum() print(goal2) print(result_eval) # if abs(goal) < best_goal : @@ -90,8 +93,12 @@ loaded_model = xgb.XGBRegressor() loaded_model.load_model('wenzhou.bin') import numpy as np -X_eval = np.array([[22.8,15.0,23,1,0], - [24,13.59,23,0,0], - [24.39,14.09,23,0,0]]) -print(model.predict(X_eval)) \ No newline at end of file +X_eval = np.array([ + [23, 11, 10, 0, 1], + [21, 8, 10, 0, 1], + [21, 8, 10, 0, 1], + [21, 9, 10, 0, 1], + [17, 7, 10, 0, 1] +]) +print(model.predict(X_eval)) diff --git a/各地级市日电量模型/湖州.py b/各地级市日电量模型/湖州.py index 7edc090..6bc0de8 100644 --- a/各地级市日电量模型/湖州.py +++ b/各地级市日电量模型/湖州.py @@ -4,11 +4,13 @@ import os from sklearn.metrics import r2_score from sklearn.model_selection import train_test_split import matplotlib as mpl -mpl.rcParams['font.sans-serif']=['kaiti'] -pd.set_option('display.width',None) + +mpl.rcParams['font.sans-serif'] = ['kaiti'] +pd.set_option('display.width', None) import random import matplotlib.pyplot as plt + def season(x): if str(x)[5:7] in ('10'): return 0 @@ -17,15 +19,16 @@ def season(x): else: return 2 + def normal(nd): - high = nd.describe()['75%'] + 1.5*(nd.describe()['75%']-nd.describe()['25%']) - low = nd.describe()['25%'] - 1.5*(nd.describe()['75%']-nd.describe()['25%']) - return nd[(ndlow)] + high = nd.describe()['75%'] + 1.5 * (nd.describe()['75%'] - nd.describe()['25%']) + low = nd.describe()['25%'] - 1.5 * (nd.describe()['75%'] - nd.describe()['25%']) + return nd[(nd < high) & (nd > low)] -parent_dir = os.path.abspath(os.path.join(os.getcwd(),os.pardir)) -data = pd.read_excel(os.path.join(parent_dir,'入模数据/湖州.xlsx'),index_col='dtdate') -data.index = pd.to_datetime(data.index,format='%Y-%m-%d') +parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir)) +data = pd.read_excel(os.path.join(parent_dir, '入模数据/湖州.xlsx'), index_col='dtdate') +data.index = pd.to_datetime(data.index, format='%Y-%m-%d') data = data.loc[normal(data['售电量']).index] # list2 = [] @@ -49,40 +52,41 @@ df_eval = data.loc['2023-10'] df_train = data[450:-1] # df_train = data[450:][:-3] print(df_train) -df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']] +df_train = df_train[['tem_max', 'tem_min', 'holiday', '24ST', '售电量', 'season']] - -X = df_train[['tem_max','tem_min','24ST','holiday','season']] -X_eval = df_eval[['tem_max','tem_min','24ST','holiday','season']] +X = df_train[['tem_max', 'tem_min', '24ST', 'holiday', 'season']] +X_eval = df_eval[['tem_max', 'tem_min', '24ST', 'holiday', 'season']] y = df_train['售电量'] -x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.1,random_state=158) +x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=158) model = xgb.XGBRegressor(max_depth=6, learning_rate=0.05, n_estimators=150) -model.fit(x_train,y_train) - +model.fit(x_train, y_train) y_pred = model.predict(x_test) -result_test = pd.DataFrame({'test':y_test,'pred':y_pred},index=y_test.index) +result_test = pd.DataFrame({'test': y_test, 'pred': y_pred}, index=y_test.index) # 指标打印 print(abs(y_test - y_pred).mean() / y_test.mean()) eval_pred = model.predict(X_eval) -result_eval = pd.DataFrame({'eval':df_eval['售电量'],'pred':eval_pred},index=df_eval['售电量'].index) +result_eval = pd.DataFrame({'eval': df_eval['售电量'], 'pred': eval_pred}, index=df_eval['售电量'].index) -goal = (result_eval['eval'][-3:].sum()-result_eval['pred'][-3:].sum())/result_eval['eval'].sum() +goal = (result_eval['eval'][-3:].sum() - result_eval['pred'][-3:].sum()) / result_eval['eval'].sum() print(goal) -goal2 = (result_eval['eval'][-23:].sum()-result_eval['pred'][-23:].sum())/result_eval['eval'].sum() +goal2 = (result_eval['eval'][-23:].sum() - result_eval['pred'][-23:].sum()) / result_eval['eval'].sum() print(goal2) print(result_eval) # 保存模型 model.save_model('huzhou.bin') loaded_model = xgb.XGBRegressor() loaded_model.load_model('huzhou.bin') -import numpy as np -X_eval = np.array([[23.89,15.5,23,1,0], - [24.5,13.30,23,0,0], - [25.39,13.5,23,0,0] - ]) +import numpy as np + +X_eval = np.array([ + [15, 8, 10, 0, 1], + [18, 9, 10, 0, 1], + [15, 8, 10, 0, 1], + [16, 8, 10, 0, 1], + [12, 7, 10, 0, 1] +]) print(model.predict(X_eval)) - diff --git a/各地级市日电量模型/绍兴.py b/各地级市日电量模型/绍兴.py index a192be4..65a1702 100644 --- a/各地级市日电量模型/绍兴.py +++ b/各地级市日电量模型/绍兴.py @@ -5,8 +5,10 @@ from sklearn.metrics import r2_score from sklearn.model_selection import train_test_split import matplotlib as mpl import matplotlib.pyplot as plt -mpl.rcParams['font.sans-serif']=['kaiti'] -pd.set_option('display.width',None) + +mpl.rcParams['font.sans-serif'] = ['kaiti'] +pd.set_option('display.width', None) + def season(x): if str(x)[5:7] in ('01', '02', '10', '11'): @@ -16,15 +18,16 @@ def season(x): else: return 2 + def normal(nd): - high = nd.describe()['75%'] + 1.5*(nd.describe()['75%']-nd.describe()['25%']) - low = nd.describe()['25%'] - 1.5*(nd.describe()['75%']-nd.describe()['25%']) - return nd[(ndlow)] + high = nd.describe()['75%'] + 1.5 * (nd.describe()['75%'] - nd.describe()['25%']) + low = nd.describe()['25%'] - 1.5 * (nd.describe()['75%'] - nd.describe()['25%']) + return nd[(nd < high) & (nd > low)] -parent_dir = os.path.abspath(os.path.join(os.getcwd(),os.pardir)) -data = pd.read_excel(os.path.join(parent_dir,'入模数据/绍兴.xlsx'),index_col='dtdate') -data.index = pd.to_datetime(data.index,format='%Y-%m-%d') +parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir)) +data = pd.read_excel(os.path.join(parent_dir, '入模数据/绍兴.xlsx'), index_col='dtdate') +data.index = pd.to_datetime(data.index, format='%Y-%m-%d') data = data.loc[normal(data['售电量']).index] # list2 = [] @@ -48,35 +51,33 @@ df_train = data[450:-1] # df_train = data[450:][:-3] print(df_train) +df_train = df_train[['tem_max', 'tem_min', 'holiday', '24ST', '售电量', 'season']] -df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']] - -X = df_train[['tem_max','tem_min','24ST','holiday','season']] -X_eval = df_eval[['tem_max','tem_min','24ST','holiday','season']] +X = df_train[['tem_max', 'tem_min', '24ST', 'holiday', 'season']] +X_eval = df_eval[['tem_max', 'tem_min', '24ST', 'holiday', 'season']] y = df_train['售电量'] # best_goal = 1 # best_i = {} # for i in range(400): -x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.1,random_state=42) +x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=42) model = xgb.XGBRegressor(max_depth=6, learning_rate=0.05, n_estimators=150) -model.fit(x_train,y_train) - +model.fit(x_train, y_train) y_pred = model.predict(x_test) -result_test = pd.DataFrame({'test':y_test,'pred':y_pred},index=y_test.index) +result_test = pd.DataFrame({'test': y_test, 'pred': y_pred}, index=y_test.index) # 指标打印 print(abs(y_test - y_pred).mean() / y_test.mean()) eval_pred = model.predict(X_eval) -result_eval = pd.DataFrame({'eval':df_eval['售电量'],'pred':eval_pred},index=df_eval['售电量'].index) +result_eval = pd.DataFrame({'eval': df_eval['售电量'], 'pred': eval_pred}, index=df_eval['售电量'].index) -goal = (result_eval['eval'][-3:].sum()-result_eval['pred'][-3:].sum())/result_eval['eval'].sum() +goal = (result_eval['eval'][-3:].sum() - result_eval['pred'][-3:].sum()) / result_eval['eval'].sum() print(goal) -goal2 = (result_eval['eval'][-23:].sum()-result_eval['pred'][-23:].sum())/result_eval['eval'].sum() +goal2 = (result_eval['eval'][-23:].sum() - result_eval['pred'][-23:].sum()) / result_eval['eval'].sum() print(goal2) # if abs(goal) < best_goal : # best_goal = abs(goal) @@ -89,8 +90,12 @@ model.save_model('shaoxing.bin') loaded_model = xgb.XGBRegressor() loaded_model.load_model('shaoxing.bin') import numpy as np -X_eval = np.array([[25.1,17.1,23,1,0], - [25.8,13.5,23,0,0], - [26.6,13.5,23,0,0]]) -print(model.predict(X_eval)) +X_eval = np.array([ + [17, 9, 10, 0, 0], + [20, 6, 10, 0, 0], + [17, 5, 10, 0, 0], + [16, 8, 10, 0, 0], + [12, 7, 10, 0, 0] +]) +print(model.predict(X_eval)) diff --git a/各地级市日电量模型/舟山.py b/各地级市日电量模型/舟山.py index c94bbe9..7837aa2 100644 --- a/各地级市日电量模型/舟山.py +++ b/各地级市日电量模型/舟山.py @@ -4,28 +4,31 @@ import os from sklearn.metrics import r2_score from sklearn.model_selection import train_test_split import matplotlib as mpl -mpl.rcParams['font.sans-serif']=['kaiti'] -pd.set_option('display.width',None) + +mpl.rcParams['font.sans-serif'] = ['kaiti'] +pd.set_option('display.width', None) import random import matplotlib.pyplot as plt + def season(x): if str(x)[5:7] in ('01', '02', '03', '04', '05', '06', '09', '10', '11', '12'): return 1 else: return 2 + + def normal(nd): - high = nd.describe()['75%'] + 1.5*(nd.describe()['75%']-nd.describe()['25%']) - low = nd.describe()['25%'] - 1.5*(nd.describe()['75%']-nd.describe()['25%']) - return nd[(ndlow)] + high = nd.describe()['75%'] + 1.5 * (nd.describe()['75%'] - nd.describe()['25%']) + low = nd.describe()['25%'] - 1.5 * (nd.describe()['75%'] - nd.describe()['25%']) + return nd[(nd < high) & (nd > low)] -parent_dir = os.path.abspath(os.path.join(os.getcwd(),os.pardir)) -data = pd.read_excel(os.path.join(parent_dir,'入模数据/舟山.xlsx'),index_col='dtdate') -data.index = pd.to_datetime(data.index,format='%Y-%m-%d') +parent_dir = os.path.abspath(os.path.join(os.getcwd(), os.pardir)) +data = pd.read_excel(os.path.join(parent_dir, '入模数据/舟山.xlsx'), index_col='dtdate') +data.index = pd.to_datetime(data.index, format='%Y-%m-%d') data = data.loc[normal(data['售电量']).index] - # list2 = [] # list0 = [] # list1 = [] @@ -46,30 +49,28 @@ df_eval = data.loc['2023-10'] df_train = data.iloc[450:-1] # df_train = data.iloc[450:][:-3] -df_train = df_train[['tem_max','tem_min','holiday','24ST','售电量','season']] -X = df_train[['tem_max','tem_min','24ST','holiday','season']] -X_eval = df_eval[['tem_max','tem_min','24ST','holiday','season']] +df_train = df_train[['tem_max', 'tem_min', 'holiday', '24ST', '售电量', 'season']] +X = df_train[['tem_max', 'tem_min', '24ST', 'holiday', 'season']] +X_eval = df_eval[['tem_max', 'tem_min', '24ST', 'holiday', 'season']] y = df_train['售电量'] -x_train,x_test,y_train,y_test = train_test_split(X,y,test_size=0.1,random_state=42) +x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=42) model = xgb.XGBRegressor(max_depth=6, learning_rate=0.05, n_estimators=150) -model.fit(x_train,y_train) - +model.fit(x_train, y_train) y_pred = model.predict(x_test) -result_test = pd.DataFrame({'test':y_test,'pred':y_pred},index=y_test.index) - +result_test = pd.DataFrame({'test': y_test, 'pred': y_pred}, index=y_test.index) # 指标打印 print(abs(y_test - y_pred).mean() / y_test.mean()) eval_pred = model.predict(X_eval) -result_eval = pd.DataFrame({'eval':df_eval['售电量'],'pred':eval_pred},index=df_eval['售电量'].index) +result_eval = pd.DataFrame({'eval': df_eval['售电量'], 'pred': eval_pred}, index=df_eval['售电量'].index) -goal = (result_eval['eval'][-3:].sum()-result_eval['pred'][-3:].sum())/result_eval['eval'].sum() +goal = (result_eval['eval'][-3:].sum() - result_eval['pred'][-3:].sum()) / result_eval['eval'].sum() print(goal) -goal2 = (result_eval['eval'][-23:].sum()-result_eval['pred'][-23:].sum())/result_eval['eval'].sum() +goal2 = (result_eval['eval'][-23:].sum() - result_eval['pred'][-23:].sum()) / result_eval['eval'].sum() print(goal2) print(result_eval) @@ -77,8 +78,12 @@ model.save_model('zhoushan.bin') loaded_model = xgb.XGBRegressor() loaded_model.load_model('zhoushan.bin') import numpy as np -X_eval = np.array([[23.19,19.3,23,1,0], - [23.8,18.39,23,0,0], - [23.69,18.3,23,0,0]]) -print(model.predict(X_eval)) +X_eval = np.array([ + [18, 11, 10, 0, 1], + [17, 9, 10, 0, 1], + [17, 8, 10, 0, 1], + [18, 10, 10, 0, 1], + [14, 7, 10, 0, 1] +]) +print(model.predict(X_eval)) diff --git a/各地级市日电量模型/衢州.py b/各地级市日电量模型/衢州.py index dc5314c..36ca8db 100644 --- a/各地级市日电量模型/衢州.py +++ b/各地级市日电量模型/衢州.py @@ -73,13 +73,17 @@ model.save_model('quzhou.bin') loaded_model = xgb.XGBRegressor() loaded_model.load_model('quzhou.bin') import numpy as np -X_eval = np.array([[23.69,16.5,23,1,0], - [24.5,15.19,23,0,0], - [25.19,13.9,23,0,0]]) - +X_eval = np.array([ + [19,10,10,0,1], + [19, 7, 10, 0, 1], + [18, 6, 10, 0, 1], + [16, 7, 10, 0, 1], + [14, 10, 10, 0, 1] +]) print(model.predict(X_eval)) + # import torch # from torch import nn # from torch.utils.data import TensorDataset,DataLoader diff --git a/各地级市日电量模型/金华.py b/各地级市日电量模型/金华.py index 1cb6eb8..e674061 100644 --- a/各地级市日电量模型/金华.py +++ b/各地级市日电量模型/金华.py @@ -88,8 +88,11 @@ model.save_model('jinhua.bin') loaded_model = xgb.XGBRegressor() loaded_model.load_model('jinhua.bin') import numpy as np -X_eval = np.array([[25,16.39,23,1,0], - [25.8,13.69,23,0,0], - [26.39,13.69,23,0,0]]) +X_eval = np.array([ + [19,12,10,0,1], + [20, 10, 10, 0, 1], + [18, 5, 10, 0, 1], + [17, 6, 10, 0, 1], + [14, 8, 10, 0, 1] +]) print(model.predict(X_eval)) - diff --git a/文档处理.py b/文档处理.py index bfe4db5..5de17ae 100644 --- a/文档处理.py +++ b/文档处理.py @@ -60,3 +60,10 @@ # df = pd.concat(list1,ignore_index=True) # df.to_csv('各市行业电量预测结果.csv',encoding='gbk') # print(df) +import pandas as pd +df = pd.read_excel(r'C:\Users\鸽子\Desktop\浙江省11月分行业售电量预测v2.xlsx',sheet_name=1) +print(df.head()) +print(df[df.columns[2:]].groupby(df['city_name']).sum().T) +df2 = df[df.columns[2:]].groupby(df['city_name']).sum().T +df2.to_excel(r'C:\Users\鸽子\Desktop\1.xlsx') + diff --git a/浙江电压等级电量/电压等级_输入10_输出3.py b/浙江电压等级电量/电压等级_输入10_输出3.py index 9fa622d..1066cd7 100644 --- a/浙江电压等级电量/电压等级_输入10_输出3.py +++ b/浙江电压等级电量/电压等级_输入10_输出3.py @@ -49,40 +49,39 @@ def data_preprocessing(data): data = data.astype(float) for col in data.columns: data[col] = normal(data[col]) - return data # 拼接数据集 -file_dir = r'./浙江各地市分电压日电量数据' -excel = os.listdir(file_dir)[0] -data = pd.read_excel(os.path.join(file_dir, excel), sheet_name=0, index_col='stat_date') -data.drop(columns='地市',inplace=True) -data = data_preprocessing(data) - -df = data[data.columns[0]] -df.dropna(inplace = True) -dataset_x, dataset_y = create_dataset(df, DAYS_FOR_TRAIN) - -for level in data.columns[1:]: - df = data[level] - df.dropna(inplace=True) - x, y = create_dataset(df, DAYS_FOR_TRAIN) - dataset_x = np.concatenate((dataset_x, x)) - dataset_y = np.concatenate((dataset_y, y)) - - -for excel in os.listdir(file_dir)[1:]: - - data = pd.read_excel(os.path.join(file_dir,excel), sheet_name=0,index_col='stat_date') - data.drop(columns='地市', inplace=True) - data = data_preprocessing(data) - - for level in data.columns: - df = data[level] - df.dropna(inplace=True) - x,y = create_dataset(df,DAYS_FOR_TRAIN) - dataset_x = np.concatenate((dataset_x,x)) - dataset_y = np.concatenate((dataset_y,y)) +# file_dir = r'./浙江各地市分电压日电量数据' +# excel = os.listdir(file_dir)[0] +# data = pd.read_excel(os.path.join(file_dir, excel), sheet_name=0, index_col='stat_date') +# data.drop(columns='地市',inplace=True) +# data = data_preprocessing(data) +# +# df = data[data.columns[0]] +# df.dropna(inplace = True) +# dataset_x, dataset_y = create_dataset(df, DAYS_FOR_TRAIN) +# +# for level in data.columns[1:]: +# df = data[level] +# df.dropna(inplace=True) +# x, y = create_dataset(df, DAYS_FOR_TRAIN) +# dataset_x = np.concatenate((dataset_x, x)) +# dataset_y = np.concatenate((dataset_y, y)) +# +# +# for excel in os.listdir(file_dir)[1:]: +# +# data = pd.read_excel(os.path.join(file_dir,excel), sheet_name=0,index_col='stat_date') +# data.drop(columns='地市', inplace=True) +# data = data_preprocessing(data) +# +# for level in data.columns: +# df = data[level] +# df.dropna(inplace=True) +# x,y = create_dataset(df,DAYS_FOR_TRAIN) +# dataset_x = np.concatenate((dataset_x,x)) +# dataset_y = np.concatenate((dataset_y,y)) @@ -90,31 +89,31 @@ for excel in os.listdir(file_dir)[1:]: device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # # # 标准化到0~1 -max_value = np.max(dataset_x) -min_value = np.min(dataset_x) -dataset_x = (dataset_x - min_value) / (max_value - min_value) -dataset_y = (dataset_y - min_value) / (max_value - min_value) -# -# print(max_value,min_value) +# max_value = np.max(dataset_x) +# min_value = np.min(dataset_x) +# dataset_x = (dataset_x - min_value) / (max_value - min_value) +# dataset_y = (dataset_y - min_value) / (max_value - min_value) +# # +# # print(max_value,min_value) + +# # # 划分训练集和测试集 +# train_size = int(len(dataset_x)*0.7) +# train_x = dataset_x[:train_size] +# train_y = dataset_y[:train_size] +# eval_x = dataset_x[train_size:] +# eval_y = dataset_y[train_size:] + +# # 将数据改变形状,RNN 读入的数据维度是 (seq_size, batch_size, feature_size) +# train_x = train_x.reshape(-1, 1, DAYS_FOR_TRAIN) +# train_y = train_y.reshape(-1, 1, 3) +# eval_x = eval_x.reshape(-1, 1, DAYS_FOR_TRAIN) +# eval_y = eval_y.reshape(-1, 1, 3) # -# # 划分训练集和测试集 -train_size = int(len(dataset_x)*0.7) -train_x = dataset_x[:train_size] -train_y = dataset_y[:train_size] -eval_x = dataset_x[train_size:] -eval_y = dataset_y[train_size:] - -# 将数据改变形状,RNN 读入的数据维度是 (seq_size, batch_size, feature_size) -train_x = train_x.reshape(-1, 1, DAYS_FOR_TRAIN) -train_y = train_y.reshape(-1, 1, 3) -eval_x = eval_x.reshape(-1, 1, DAYS_FOR_TRAIN) -eval_y = eval_y.reshape(-1, 1, 3) - -# 转为pytorch的tensor对象 -train_x = torch.from_numpy(train_x).to(device).type(torch.float32) -train_y = torch.from_numpy(train_y).to(device).type(torch.float32) -eval_x = torch.from_numpy(eval_x).to(device).type(torch.float32) -eval_y = torch.from_numpy(eval_y).to(device).type(torch.float32) +# # 转为pytorch的tensor对象 +# train_x = torch.from_numpy(train_x).to(device).type(torch.float32) +# train_y = torch.from_numpy(train_y).to(device).type(torch.float32) +# eval_x = torch.from_numpy(eval_x).to(device).type(torch.float32) +# eval_y = torch.from_numpy(eval_y).to(device).type(torch.float32) model = LSTM_Regression(DAYS_FOR_TRAIN, 32, output_size=3, num_layers=2).to(device) # 导入模型并设置模型的参数输入输出层、隐藏层等 @@ -164,16 +163,15 @@ model = LSTM_Regression(DAYS_FOR_TRAIN, 32, output_size=3, num_layers=2).to(devi # 创建测试集 max_value,min_value = 192751288.47,0.0 -model.load_state_dict(torch.load('8_dy3.pth')) # cpu跑加上,map_location=torch.device('cpu') -file_dir = r'./浙江各地市分电压日电量数据' -for excel in os.listdir(file_dir): - df_city = pd.read_excel(os.path.join(file_dir,excel),index_col='stat_date') - df_city.index = pd.to_datetime(df_city.index) - df_city = df_city.loc['2023-9'][:-3] - df_city.drop(columns=[i for i in df_city.columns if (df_city[i] == 0).sum() / len(df_city) >= 0.5], inplace=True) +model.load_state_dict(torch.load('best_dy3.pth',map_location=torch.device('cpu'))) # cpu跑加上,map_location=torch.device('cpu') +# file_dir = r'./浙江各地市分电压日电量数据' - city = df_city['地市'].iloc[0] +df = pd.read_excel(r'C:\Users\鸽子\Desktop\浙江电量20231127.xlsx',sheet_name=1) +df = df[df['county_name'].isnull()] +for city in df['city_name'].drop_duplicates(): + df_city = df[df['city_name']==city].drop(columns=['county_name','500kv(含330kv)以上']).set_index('pt_date').sort_index() + # df_city.drop(columns=[i for i in df_city.columns if (df_city[i] == 0).sum() / len(df_city) >= 0.5], inplace=True) result_dict = {} for level in df_city.columns[1:]: x, y = create_dataset(df_city[level], 10) @@ -184,13 +182,11 @@ for excel in os.listdir(file_dir): pred = pred * (max_value - min_value) + min_value result = pred.cpu().detach().numpy()[-3:] result_dict[level] = list(result) - df = pd.DataFrame(result_dict,index=['2023-09-28','2023-09-29','2023-09-30']) - df.to_excel(fr'C:\Users\user\Desktop\1\9月分压电量预测28-30\{city} .xlsx') -# print(result_dict) - - + df1 = pd.DataFrame(result_dict,index=['2023-11-28','2023-11-29','2023-11-30']) + df1.to_excel(fr'C:\Users\鸽子\Desktop\11月分压电量预测28-30\{city} .xlsx') +print(result_dict) -# # 打印指标 +# 打印指标 # print(abs(pred - df[-3:]).mean() / df[-3:].mean()) # result_eight = pd.DataFrame({'pred': np.round(pred,1),'real': df[-3:]}) # target = (result_eight['pred'].sum() - result_eight['real'].sum()) / df[-31:].sum() diff --git a/浙江行业电量/行业电量_输出为3_步长为10.py b/浙江行业电量/行业电量_输出为3_步长为10.py index bbed281..b0c39a4 100644 --- a/浙江行业电量/行业电量_输出为3_步长为10.py +++ b/浙江行业电量/行业电量_输出为3_步长为10.py @@ -141,36 +141,36 @@ min_loss = 1 # # 保存/读取模型 # torch.save(best_para,'hy3.pth') model = LSTM_Regression(DAYS_FOR_TRAIN, 32, output_size=3, num_layers=2).to(device) -model.load_state_dict(torch.load('hy3.pth')) +model.load_state_dict(torch.load('hy3.pth',map_location=torch.device('cpu'))) # 测试 model = model.eval() -dataset_x = dataset_x.reshape(-1, 1, DAYS_FOR_TRAIN) # (seq_size, batch_size, feature_size) -dataset_x = torch.from_numpy(dataset_x).to(device).type(torch.float32) - -pred_test = model(dataset_x) # 全量训练集 -pred_test = pred_test.view(-1) -pred_test = np.concatenate((np.zeros(DAYS_FOR_TRAIN), pred_test.cpu().detach().numpy())) - -plt.plot(pred_test.reshape(-1), 'r', label='prediction') -plt.plot(dataset_y.reshape(-1), 'b', label='real') -plt.plot((train_size*3, train_size*3), (0, 1), 'g--') -plt.legend(loc='best') -plt.show() +# dataset_x = dataset_x.reshape(-1, 1, DAYS_FOR_TRAIN) # (seq_size, batch_size, feature_size) +# dataset_x = torch.from_numpy(dataset_x).to(device).type(torch.float32) +# +# pred_test = model(dataset_x) # 全量训练集 +# pred_test = pred_test.view(-1) +# pred_test = np.concatenate((np.zeros(DAYS_FOR_TRAIN), pred_test.cpu().detach().numpy())) +# +# plt.plot(pred_test.reshape(-1), 'r', label='prediction') +# plt.plot(dataset_y.reshape(-1), 'b', label='real') +# plt.plot((train_size*3, train_size*3), (0, 1), 'g--') +# plt.legend(loc='best') +# plt.show() # model.load_state_dict(torch.load('hy3.pth',map_location=torch.device('cpu'))) # max_value = 354024930.8 # min_value = 0.0 -# 创建测试集 -file_dir = './浙江各地市行业电量数据' -for excel in os.listdir(file_dir): - df_city = pd.read_excel(os.path.join(file_dir,excel),index_col='stat_date') - df_city.index = df_city.index.map(lambda x:str(x).strip()[:10]) +# 测试 +# file_dir = './浙江各地市行业电量数据' +df = pd.read_excel(r'C:\Users\鸽子\Desktop\浙江电量20231127.xlsx',sheet_name=2) +for city in df['city_name'].drop_duplicates(): + df_city = df[df['city_name']==city].sort_values(by='stat_date').set_index('stat_date') - df_city.index = pd.to_datetime(df_city.index) - df_city = df_city.loc['2023-9'][:-3] - city = df_city['地市'].iloc[0] + # df_city.index = df_city.index.map(lambda x:str(x).strip()[:10]) + # df_city.index = pd.to_datetime(df_city.index) + # df_city = df_city.loc['2023-9'][:-3] result_dict = {} for industry in df_city.columns[1:]: df_city[industry] = df_city[industry].astype('float') @@ -182,8 +182,8 @@ for excel in os.listdir(file_dir): pred = pred * (max_value - min_value) + min_value result = pred.cpu().detach().numpy()[-3:] result_dict[industry] = list(result) - df = pd.DataFrame(result_dict,index=['2023-09-28','2023-09-29','2023-09-30']) - df.to_excel(fr'C:\Users\user\Desktop\9月行业电量预测28-30\{city} .xlsx') + df1 = pd.DataFrame(result_dict,index=['2023-11-28','2023-11-29','2023-11-30']) + df1.to_excel(fr'C:\Users\鸽子\Desktop\11月行业电量预测27-30\{city} .xlsx') print(time.time()-t1) print(result_dict)