|
|
@ -34,15 +34,18 @@ class LSTM(nn.Module):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def normal(df):
|
|
|
|
def normal(df):
|
|
|
|
|
|
|
|
drop_col = [x for x in df.columns if len(df[df[x]==0])/len(df) >= 0.5]
|
|
|
|
|
|
|
|
df.drop(columns=drop_col,inplace=True)
|
|
|
|
for col in df.columns:
|
|
|
|
for col in df.columns:
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
high = df[col].describe()['75%'] + 1.5 * (df[col].describe()['75%'] - df[col].describe()['25%'])
|
|
|
|
high = df[col].describe()['75%'] + 1.5 * (df[col].describe()['75%'] - df[col].describe()['25%'])
|
|
|
|
low = df[col].describe()['25%'] - 1.5 * (df[col].describe()['75%'] - df[col].describe()['25%'])
|
|
|
|
low = df[col].describe()['25%'] - 1.5 * (df[col].describe()['75%'] - df[col].describe()['25%'])
|
|
|
|
df[col] = df[col].map(lambda x: np.nan if (x >= high) | (x <= low) else x)
|
|
|
|
df[col] = df[col].map(lambda x: np.nan if (x >= high) | (x <= low) else x)
|
|
|
|
df[col] = df[col].fillna(method='ffill')
|
|
|
|
df[col] = df[col].fillna(method='ffill')
|
|
|
|
|
|
|
|
df[col] = df[col].fillna(method='bfill')
|
|
|
|
except:
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
pass
|
|
|
|
|
|
|
|
return df
|
|
|
|
|
|
|
|
|
|
|
|
def create_data(df_industry, industry):
|
|
|
|
def create_data(df_industry, industry):
|
|
|
|
dataset_x = []
|
|
|
|
dataset_x = []
|
|
|
@ -83,10 +86,13 @@ df_city[df_city.columns[1:]] /= 10000
|
|
|
|
df_city['stat_date'] = df_city['stat_date'].map(lambda x: str(x).strip()[:10])
|
|
|
|
df_city['stat_date'] = df_city['stat_date'].map(lambda x: str(x).strip()[:10])
|
|
|
|
df_city.stat_date = pd.to_datetime(df_city.stat_date)
|
|
|
|
df_city.stat_date = pd.to_datetime(df_city.stat_date)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
industry = '全社会用电总计'
|
|
|
|
industry = '全社会用电总计'
|
|
|
|
df_industry = df_city[['stat_date', industry]]
|
|
|
|
df_industry = df_city[['stat_date', industry]]
|
|
|
|
dataset_x, dataset_y = create_data(df_industry, industry)
|
|
|
|
dataset_x, dataset_y = create_data(df_industry, industry)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for industry in df_city.columns[2:]:
|
|
|
|
for industry in df_city.columns[2:]:
|
|
|
|
df_level = df_city[['stat_date', industry]]
|
|
|
|
df_level = df_city[['stat_date', industry]]
|
|
|
|
x, y = create_data(df_level, industry)
|
|
|
|
x, y = create_data(df_level, industry)
|
|
|
@ -95,6 +101,7 @@ for industry in df_city.columns[2:]:
|
|
|
|
|
|
|
|
|
|
|
|
for excel in os.listdir(file_dir)[1:]:
|
|
|
|
for excel in os.listdir(file_dir)[1:]:
|
|
|
|
df_city = pd.read_excel(os.path.join(file_dir, excel)).drop(columns='地市')
|
|
|
|
df_city = pd.read_excel(os.path.join(file_dir, excel)).drop(columns='地市')
|
|
|
|
|
|
|
|
df_city = normal(df_city)
|
|
|
|
df_city[df_city.columns[1:]] /= 10000
|
|
|
|
df_city[df_city.columns[1:]] /= 10000
|
|
|
|
df_city['stat_date'] = df_city['stat_date'].map(lambda x: str(x).strip()[:10])
|
|
|
|
df_city['stat_date'] = df_city['stat_date'].map(lambda x: str(x).strip()[:10])
|
|
|
|
df_city.stat_date = pd.to_datetime(df_city.stat_date)
|
|
|
|
df_city.stat_date = pd.to_datetime(df_city.stat_date)
|
|
|
@ -104,6 +111,7 @@ for excel in os.listdir(file_dir)[1:]:
|
|
|
|
dataset_x = np.concatenate([dataset_x, x])
|
|
|
|
dataset_x = np.concatenate([dataset_x, x])
|
|
|
|
dataset_y = np.concatenate([dataset_y, y])
|
|
|
|
dataset_y = np.concatenate([dataset_y, y])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print(dataset_x.shape, dataset_y.shape)
|
|
|
|
print(dataset_x.shape, dataset_y.shape)
|
|
|
|
|
|
|
|
|
|
|
|
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
|
|
|
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
|
|
@ -114,7 +122,7 @@ dataset_x = (dataset_x - min_value) / (max_value - min_value)
|
|
|
|
dataset_y = (dataset_y - min_value) / (max_value - min_value)
|
|
|
|
dataset_y = (dataset_y - min_value) / (max_value - min_value)
|
|
|
|
|
|
|
|
|
|
|
|
print(max_value, min_value)
|
|
|
|
print(max_value, min_value)
|
|
|
|
print(np.max(dataset_x), np.min(dataset_x), np.max(dataset_y), np.min(dataset_y))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 划分训练集和测试集
|
|
|
|
# 划分训练集和测试集
|
|
|
|
train_size = int(len(dataset_x) * 0.8)
|
|
|
|
train_size = int(len(dataset_x) * 0.8)
|
|
|
|