输出预测结果

main
鸽子 1 year ago
parent 019a7f039a
commit 4a24ea47fe

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="pytorch_gpu" project-jdk-type="Python SDK" />
<component name="ProjectRootManager" version="2" project-jdk-name="C:\anaconda\envs\pytorch" project-jdk-type="Python SDK" />
</project>

@ -2,7 +2,7 @@
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="pytorch_gpu" jdkType="Python SDK" />
<orderEntry type="jdk" jdkName="C:\anaconda\envs\pytorch" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

@ -96,10 +96,8 @@ import numpy as np
loaded_model = xgb.XGBRegressor()
loaded_model.load_model('lishui.bin')
X_eval = np.array([
[22.5,15.4,23,0,0],
[22.3,15.5,23,1,0],
[20.0,15.7,23,1,0],
[22.0,15.0,23,0,0],
[23.6,13.9,23,0,0]])
[22.3,16.19,23,1,0],
[23.69,14.5,23,0,0],
[23.69,14,23,0,0]])
print(model.predict(X_eval))

@ -82,10 +82,9 @@ model.save_model('taizhou.bin')
import numpy as np
loaded_model = xgb.XGBRegressor()
loaded_model.load_model('taizhou.bin')
X_eval = np.array([[25.1,16.8,23,0,0],
[22.8,16.3,23,1,0],
[22.7,14.6,23,1,0],
[22.5,14.4,23,0,0],
[22.6,15.6,23,0,0]])
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]])
print(model.predict(X_eval))

@ -88,9 +88,8 @@ model.save_model('jiaxing.bin')
loaded_model = xgb.XGBRegressor()
loaded_model.load_model('jiaxing.bin')
import numpy as np
X_eval = np.array([[23.4,16.1,23,0,0],
[23.3,16.0,23,1,0],
[22.0,15.8,23,1,0],
[23.8,15.7,23,0,0],
[24.1,15.3,23,0,0]])
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))

@ -85,10 +85,9 @@ import numpy as np
loaded_model = xgb.XGBRegressor()
loaded_model.load_model('ningbo.bin')
X_eval = np.array([[23.3,15.6,23,0,0],
[22.5,16.0,23,1,0],
[23.4,16.4,23,1,0],
[20.8,15.3,23,0,0],
[23.6,14.0,23,0,0]])
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]])
print(model.predict(X_eval))

@ -112,11 +112,9 @@ model.save_model('hangzhou.bin')
loaded_model = xgb.XGBRegressor()
loaded_model.load_model('hangzhou.bin')
# X_eval = np.array([[26.1,16.1,23,0,0],
# [24.5,14.6,23,1,0],
# [24.0,15.2,23,1,0],
# [22.7,14.9,23,0,0],
# [24.1,13.4,23,0,0]])
#
# print(loaded_model.predict(X_eval))
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))

@ -90,9 +90,8 @@ loaded_model = xgb.XGBRegressor()
loaded_model.load_model('wenzhou.bin')
import numpy as np
X_eval = np.array([[24.8,17.9,23,0,0],
[23.1,15.4,23,1,0],
[22.2,16.0,23,1,0],
[22.1,14.9,23,0,0],
[23.5,14.3,23,0,0]])
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))

@ -77,10 +77,8 @@ model.save_model('huzhou.bin')
loaded_model = xgb.XGBRegressor()
loaded_model.load_model('huzhou.bin')
import numpy as np
X_eval = np.array([[22.2,14.8,23,0,0],
[23.4,15.9,23,1,0],
[22.5,15.6,23,1,0],
[23.8,14.3,23,0,0],
[23.9,14.0,23,0,0]])
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]])
print(model.predict(X_eval))

@ -90,10 +90,8 @@ model.save_model('shaoxing.bin')
loaded_model = xgb.XGBRegressor()
loaded_model.load_model('shaoxing.bin')
import numpy as np
X_eval = np.array([[24.7,15.9,23,0,0],
[24.4,16.2,23,1,0],
[23.6,15.1,23,1,0],
[24.7,15.3,23,0,0],
[24.9,14.6,23,0,0]])
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))

@ -76,10 +76,8 @@ model.save_model('zhoushan.bin')
loaded_model = xgb.XGBRegressor()
loaded_model.load_model('zhoushan.bin')
import numpy as np
X_eval = np.array([[22.6,18.7,23,0,0],
[21.6,17.9,23,1,0],
[21.9,18.2,23,1,0],
[20.7,18.2,23,0,0],
[22.3,18.0,23,0,0]])
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))

@ -72,11 +72,9 @@ model.save_model('quzhou.bin')
loaded_model = xgb.XGBRegressor()
loaded_model.load_model('quzhou.bin')
import numpy as np
X_eval = np.array([[24.0,15.6,23,0,0],
[24.2,17.1,23,1,0],
[22.6,16.7,23,1,0],
[23.5,15.5,23,0,0],
[24.5,13.9,23,0,0]])
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]])
print(model.predict(X_eval))

@ -84,10 +84,8 @@ model.save_model('jinhua.bin')
loaded_model = xgb.XGBRegressor()
loaded_model.load_model('jinhua.bin')
import numpy as np
X_eval = np.array([[26.0,15.0,23,0,0],
[24.9,17.6,23,1,0],
[24.0,17.7,23,1,0],
[24.8,15.8,23,0,0],
[25.2,14.0,23,0,0]])
X_eval = np.array([[25,16.39,23,1,0],
[25.8,13.69,23,0,0],
[26.39,13.69,23,0,0]])
print(model.predict(X_eval))

@ -1,3 +1,5 @@
import os
import numpy as np
import pandas as pd
n1 = np.array([[1,1,1]])
@ -7,12 +9,26 @@ n2 = np.array([]).reshape(3,-1)
print(np.max([[1,2,3],[4,5,6]]))
file_dir = r'C:\Users\鸽子\Desktop\浙江各地市分电压日电量数据'
df = pd.read_csv(r'C:\Users\鸽子\Desktop\浙江省各地市日电量数据21-23年 .csv',encoding='gbk')
df.columns = df.columns.map(lambda x:x.strip())
for city in df['地市'].drop_duplicates():
df_city = df[df['地市']== city]
df_city['stat_date'] = pd.to_datetime(df_city['stat_date'],format='%Y/%m/%d')
df_city = df_city[df_city.columns[:-1]]
df_city['stat_date'] = df_city['stat_date'].astype('str')
df_city.to_excel(fr'C:\Users\鸽子\Desktop\浙江各地市分电压日电量数据\{city}.xlsx',index=False)
# file_dir = r'C:\Users\鸽子\Desktop\浙江各地市分电压日电量数据'
# df = pd.read_csv(r'C:\Users\鸽子\Desktop\浙江省各地市日电量数据21-23年 .csv',encoding='gbk')
# df.columns = df.columns.map(lambda x:x.strip())
# for city in df['地市'].drop_duplicates():
# df_city = df[df['地市']== city]
# df_city['stat_date'] = pd.to_datetime(df_city['stat_date'],format='%Y/%m/%d')
# df_city = df_city[df_city.columns[:-1]]
# df_city['stat_date'] = df_city['stat_date'].astype('str')
# df_city.to_excel(fr'C:\Users\鸽子\Desktop\浙江各地市分电压日电量数据\{city}.xlsx',index=False)
file_Dir = r'C:\Users\鸽子\Desktop\浙江各地市行业电量数据'
for excel in os.listdir(file_Dir):
df1 = pd.read_excel(r'C:\Users\鸽子\Desktop\浙江各地市日电量数据-27-28).xlsx',sheet_name=1)
df1.columns = df1.columns.map(lambda x:x.strip())
df2 = pd.read_excel(os.path.join(file_Dir,excel))
df2['地市'] = df2['地市'].map(lambda x:x.strip())
city = df2['地市'].iloc[0]
col_list = df2.columns
df1 = df1[col_list]
df1 = df1[(df1['stat_date']==20231028)&(df1['地市']==city)]
df1['stat_date'] = pd.to_datetime(df1['stat_date'],format='%Y%m%d')
df2 = pd.concat((df2,df1),ignore_index=True)
df2.to_excel(fr'C:\Users\鸽子\Desktop\浙江各地市行业电量数据\{city}.xlsx')

@ -52,113 +52,123 @@ def data_preprocessing(data):
return data
# 拼接数据集
file_dir = r'C:\Users\user\Desktop\浙江各地市分电压日电量数据'
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'C:\Users\鸽子\Desktop\浙江各地市分电压日电量数据'
# 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))
# 训练
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)
# 划分训练集和测试集
train_size = int(len(dataset_x)*0.7)
train_x = dataset_x[:train_size]
train_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, 5)
# 转为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)
#
# # 标准化到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)
# # 划分训练集和测试集
# train_size = int(len(dataset_x)*0.7)
# train_x = dataset_x[:train_size]
# train_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, 5)
#
# # 转为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)
model = LSTM_Regression(DAYS_FOR_TRAIN, 32, output_size=5, num_layers=2).to(device) # 导入模型并设置模型的参数输入输出层、隐藏层等
train_loss = []
loss_function = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.005, betas=(0.9, 0.999), eps=1e-08, weight_decay=0)
for i in range(1500):
out = model(train_x)
loss = loss_function(out, train_y)
loss.backward()
optimizer.step()
optimizer.zero_grad()
train_loss.append(loss.item())
# train_loss = []
# loss_function = nn.MSELoss()
# optimizer = torch.optim.Adam(model.parameters(), lr=0.005, betas=(0.9, 0.999), eps=1e-08, weight_decay=0)
# for i in range(1500):
# out = model(train_x)
# loss = loss_function(out, train_y)
# loss.backward()
# optimizer.step()
# optimizer.zero_grad()
# train_loss.append(loss.item())
# 保存模型
torch.save(model.state_dict(),'dy5.pth')
# torch.save(model.state_dict(),'dy5.pth')
# for test
model = model.eval() # 转换成测试模式
# model.load_state_dict(torch.load(os.path.join(model_save_dir,model_file))) # 读取参数
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)
# model = model.eval() # 转换成测试模式
# # model.load_state_dict(torch.load(os.path.join(model_save_dir,model_file))) # 读取参数
# 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) # 全量训练集
# # 模型输出 (seq_size, batch_size, output_size)
# 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*5, train_size*5), (0, 1), 'g--') # 分割线 左边是训练数据 右边是测试数据的输出
# plt.legend(loc='best')
# plt.show()
pred_test = model(dataset_x) # 全量训练集
# 模型输出 (seq_size, batch_size, output_size)
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*5, train_size*5), (0, 1), 'g--') # 分割线 左边是训练数据 右边是测试数据的输出
plt.legend(loc='best')
plt.show()
# 创建测试集
max_value,min_value = 199482558.1,0.0
model.load_state_dict(torch.load('dy5.pth',map_location=torch.device('cpu')))
file_dir = r'C:\Users\鸽子\Desktop\浙江各地市分电压日电量数据'
for excel in os.listdir(file_dir):
df_city = pd.read_excel(os.path.join(file_dir,excel))
df_city.drop(columns=[i for i in df_city.columns if (df_city[i] == 0).sum() / len(df_city) >= 0.5], inplace=True)
city = df_city['地市'].iloc[0]
result_dict = {}
for level in df_city.columns[2:]:
x, y = create_dataset(df_city[level], 10)
x = (x - min_value) / (max_value - min_value)
x = x.reshape(-1, 1, 10)
x = torch.from_numpy(x).type(torch.float32).to(device)
pred = model(x).view(-1)
pred = pred * (max_value - min_value) + min_value
result = pred.detach().numpy()[-5:-2]
result_dict[level] = list(result)
df = pd.DataFrame(result_dict,index=['2023-10-29','2023-10-30','2023-10-31'])
df.to_excel(fr'C:\Users\鸽子\Desktop\分压电量预测29-31\{city}.xlsx')
print(result_dict)
# 创建测试集
# result_list = []
# 以x为基础实际数据滚动预测未来3天
# x = torch.from_numpy(df[-14:-4]).to(device)
# pred = model(x.reshape(-1,1,DAYS_FOR_TRAIN)).view(-1).detach().numpy()
x2 = np.array([227964890.1,220189256.2,220189256.2,220189256.2,220189256.2,220189256.2,220189256.2,220189256.2,220189256.2,220189256.2])
x2 = (x2 - min_value) / (max_value - min_value)
x2 = x2.reshape(-1,1,10)
print(x2)
x2 = torch.from_numpy(x2).type(torch.float32).to(device)
pred2 = model(x2)
# 反归一化
# pred = pred * (max_value - min_value) + min_value
# df = df * (max_value - min_value) + min_value
pred2 = pred2 * (max_value - min_value) + min_value
# print(pred)
# # 打印指标
# print(abs(pred - df[-3:]).mean() / df[-3:].mean())
# result_eight = pd.DataFrame({'pred': np.round(pred,1),'real': df[-3:]})

@ -146,39 +146,29 @@ model = LSTM_Regression(DAYS_FOR_TRAIN, 32, output_size=5, num_layers=2).to(devi
# plt.legend(loc='best')
# plt.show()
model.load_state_dict(torch.load('hy5.pth'))
model.load_state_dict(torch.load('hy5.pth',map_location=torch.device('cpu')))
max_value = 354024930.8
min_value = 0.0
# 创建测试集
df_eval = pd.read_excel(r'C:\Users\user\Desktop\浙江各地市行业电量数据\ 杭州 .xlsx',index_col='stat_date')
df_eval.columns = df_eval.columns.map(lambda x:x.strip())
df_eval.index = pd.to_datetime(df_eval.index)
x,y = create_dataset(df_eval.loc['2023-7']['第二产业'],10)
file_dir = r'C:\Users\鸽子\Desktop\浙江各地市行业电量数据'
for excel in os.listdir(file_dir):
df_city = pd.read_excel(os.path.join(file_dir,excel))
city = df_city['地市'].iloc[0]
result_dict = {}
for industry in df_city.columns[3:]:
df_city[industry] = df_city[industry].astype('float')
x, y = create_dataset(df_city[industry], 10)
x = (x - min_value) / (max_value - min_value)
x = x.reshape(-1, 1, 10)
x = torch.from_numpy(x).type(torch.float32).to(device)
pred = model(x)
x2 = np.array([227964890.1,220189256.2,220189256.2,220189256.2,220189256.2,220189256.2,220189256.2,220189256.2,220189256.2,220189256.2])
x2 = (x2 - min_value) / (max_value - min_value)
x2 = x2.reshape(-1,1,10)
print(x2)
x2 = torch.from_numpy(x2).type(torch.float32).to(device)
pred2 = model(x2)
# 反归一化
pred = model(x).view(-1)
pred = pred * (max_value - min_value) + min_value
pred2 = pred2 * (max_value - min_value) + min_value
print('pred2:',pred2.view(-1).cpu().detach().numpy())
# df = df * (max_value - min_value) + min_value
df = pd.DataFrame({'real':y.reshape(-1),'pred':pred.view(-1).cpu().detach().numpy()})
print(df)
df.to_csv('7月第二产业.csv',encoding='gbk')
result = pred.detach().numpy()[-5:-2]
result_dict[industry] = list(result)
df = pd.DataFrame(result_dict,index=['2023-10-29','2023-10-30','2023-10-31'])
df.to_excel(fr'C:\Users\鸽子\Desktop\行业电量预测29-31\{city}.xlsx')
print(result_dict)
# 反归一化
# pred = pred * (max_value - min_value) + min_value

Loading…
Cancel
Save