From 396f1f27589681adb230eaa24906183f9cacc91a Mon Sep 17 00:00:00 2001 From: get <2316994765@qq.com> Date: Fri, 17 Nov 2023 10:32:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E9=87=8D=E5=A4=8D=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E9=9B=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../test1.py | 43 ++++++++++++++++++- 浙江行业电量/行业电量_输出为3.py | 12 +++++- 2 files changed, 52 insertions(+), 3 deletions(-) rename {浙江电压等级电量 => 浙江行业电量}/test1.py (51%) diff --git a/浙江电压等级电量/test1.py b/浙江行业电量/test1.py similarity index 51% rename from 浙江电压等级电量/test1.py rename to 浙江行业电量/test1.py index e6487c6..7e0be8e 100644 --- a/浙江电压等级电量/test1.py +++ b/浙江行业电量/test1.py @@ -9,7 +9,7 @@ n2 = np.array([]).reshape(3,-1) print(np.max([[1,2,3],[4,5,6]])) -file_dir = r'./浙江各地市分电压日电量数据' +file_dir = r'../浙江电压等级电量/浙江各地市分电压日电量数据' excel = os.listdir(file_dir)[0] df = pd.read_excel(os.path.join(file_dir, excel), sheet_name=0, index_col='stat_date') df.columns = df.columns.map(lambda x:x.strip()) @@ -38,4 +38,45 @@ print(df.tail()) # 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') +pd.set_option('display.width',None) +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: + try: + 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%']) + 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='bfill') + except: + pass + return df +file_dir = './浙江各地市行业电量数据' +city1 = os.listdir(file_dir)[0] +df_city = pd.read_excel(os.path.join(file_dir, city1)) +df_city = normal(df_city) +df_city = df_city.drop(columns='地市') +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 = pd.to_datetime(df_city.stat_date) +print(df_city.describe()) + +list_1000 = [] +list_100 = [] +list_10 = [] +list_1 = [] +for i in df_city.columns[1:]: + if df_city[i].describe()['mean']>=1000: + list_1000.append(i) + if df_city[i].describe()['mean'] < 1000 and df_city[i].describe()['mean'] >= 100: + list_100.append(i) + if df_city[i].describe()['mean'] < 100 and df_city[i].describe()['mean'] >= 10: + list_10.append(i) + else: + list_1.append(i) +print('list_1:',list_1) +print('list_10:',list_10) +print('list_100:',list_100) +print('list_1000:',list_1000) diff --git a/浙江行业电量/行业电量_输出为3.py b/浙江行业电量/行业电量_输出为3.py index ab64e4e..c269842 100644 --- a/浙江行业电量/行业电量_输出为3.py +++ b/浙江行业电量/行业电量_输出为3.py @@ -34,15 +34,18 @@ class LSTM(nn.Module): 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: try: 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%']) 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='bfill') except: pass - + return df def create_data(df_industry, industry): 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 = pd.to_datetime(df_city.stat_date) + + industry = '全社会用电总计' df_industry = df_city[['stat_date', industry]] dataset_x, dataset_y = create_data(df_industry, industry) + for industry in df_city.columns[2:]: df_level = df_city[['stat_date', 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:]: 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['stat_date'] = df_city['stat_date'].map(lambda x: str(x).strip()[:10]) 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_y = np.concatenate([dataset_y, y]) + print(dataset_x.shape, dataset_y.shape) 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) 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)