years = np.array(nc_label['year'][:]) months = np.array(nc_label['month'][:])
year_month_index = [] vs = [] for i,year in enumerate(years): for j,month in enumerate(months): year_month_index.append('year_{}_month_{}'.format(year,month)) vs.append(np.array(nc_label['nino'][i,j]))
df_SODA_label = pd.DataFrame({'year_month':year_month_index}) df_SODA_label['year_month'] = year_month_index df_SODA_label['label'] = vs
deftrans_df(df, vals, lats, lons, years, months): ''' (100, 36, 24, 72) -- year, month,lat,lon ''' for j,lat_ in enumerate(lats): for i,lon_ in enumerate(lons): c = 'lat_lon_{}_{}'.format(int(lat_),int(lon_)) v = [] for y in range(len(years)): for m in range(len(months)): v.append(vals[y,m,j,i]) df[c] = v return df
years = np.array(nc_label['year'][:]) months = np.array(nc_label['month'][:])
year_month_index = [] vs = [] for i,year in enumerate(years): for j,month in enumerate(months): year_month_index.append('year_{}_month_{}'.format(year,month)) vs.append(np.array(nc_label['nino'][i,j]))
df_CMIP_label = pd.DataFrame({'year_month':year_month_index}) df_CMIP_label['year_month'] = year_month_index df_CMIP_label['label'] = vs
last_thre_years = 1000 for year in years: ''' 數(shù)據(jù)的原因,我們 ''' if year >= 4645 - last_thre_years: for month in months: year_month_index.append('year_{}_month_{}'.format(year,month))
deftrans_thre_df(df, vals, lats, lons, years, months, last_thre_years = 1000): ''' (4645, 36, 24, 72) -- year, month,lat,lon ''' for j,lat_ in (enumerate(lats)): # print(j) for i,lon_ in enumerate(lons): c = 'lat_lon_{}_{}'.format(int(lat_),int(lon_)) v = [] for y_,y in enumerate(years): ''' 數(shù)據(jù)的原因,我們 ''' if y >= 4645 - last_thre_years: for m_,m in enumerate(months): v.append(vals[y_,m_,j,i]) df[c] = v return df
%%time df_CMIP_sst = trans_thre_df(df = df_CMIP_sst, vals = np.array(nc_CMIP['sst'][:]), lats = lats, lons = lons, years = years, months = months) df_CMIP_sst.to_csv(CMIP_trans_path + 'df_CMIP_sst.csv',index = None) del df_CMIP_sst gc.collect()
df_CMIP_t300 = trans_thre_df(df = df_CMIP_t300, vals = np.array(nc_CMIP['t300'][:]), lats = lats, lons = lons, years = years, months = months) df_CMIP_t300.to_csv(CMIP_trans_path + 'df_CMIP_t300.csv',index = None) del df_CMIP_t300 gc.collect()
df_CMIP_ua = trans_thre_df(df = df_CMIP_ua, vals = np.array(nc_CMIP['ua'][:]), lats = lats, lons = lons, years = years, months = months) df_CMIP_ua.to_csv(CMIP_trans_path + 'df_CMIP_ua.csv',index = None) del df_CMIP_ua gc.collect()
df_CMIP_va = trans_thre_df(df = df_CMIP_va, vals = np.array(nc_CMIP['va'][:]), lats = lats, lons = lons, years = years, months = months) df_CMIP_va.to_csv(CMIP_trans_path + 'df_CMIP_va.csv',index = None) del df_CMIP_va gc.collect()
# (36036, 1729)
數(shù)據(jù)建模
工具包導(dǎo)入&數(shù)據(jù)讀取
1. 工具包導(dǎo)入
import pandas as pd import numpy as np import tensorflow as tf from tensorflow.keras.optimizers import Adam import matplotlib.pyplot as plt import scipy import joblib from netCDF4 import Dataset import netCDF4 as nc from tensorflow.keras.callbacks import LearningRateScheduler, Callback import tensorflow.keras.backend as K from tensorflow.keras.layers import * from tensorflow.keras.models import * from tensorflow.keras.optimizers import * from tensorflow.keras.callbacks import * from tensorflow.keras.layers import Input import gc %matplotlib inline
x = Dense(64, activation='relu')(x_1) x = Dropout(0.25)(x) x = Dense(32, activation='relu')(x) x = Dropout(0.25)(x) output = Dense(24, activation='linear')(x) model = Model(inputs=inp, outputs=output)
adam = tf.optimizers.Adam(lr=1e-3,beta_1=0.99,beta_2 = 0.99) model.compile(optimizer=adam, loss=RMSE)
import tensorflow as tf import tensorflow.keras.backend as K from tensorflow.keras.layers import * from tensorflow.keras.models import * from tensorflow.keras.optimizers import * from tensorflow.keras.callbacks import * from tensorflow.keras.layers import Input import numpy as np import os import zipfile
x = Dense(64, activation='relu')(x_1) x = Dropout(0.25)(x) x = Dense(32, activation='relu')(x) x = Dropout(0.25)(x) output = Dense(24, activation='linear')(x) model = Model(inputs=inp, outputs=output)
adam = tf.optimizers.Adam(lr=1e-3,beta_1=0.99,beta_2 = 0.99) model.compile(optimizer=adam, loss=RMSE)
return model
model = build_model() model.load_weights('./user_data/model_data/model_mlp_baseline.h5')
模型預(yù)測
test_path = './tcdata/enso_round1_test_20210201/'
### 1. 測試數(shù)據(jù)讀取 files = os.listdir(test_path) test_feas_dict = {} for file in files: test_feas_dict[file] = np.load(test_path + file)
### 2. 結(jié)果預(yù)測 test_predicts_dict = {} for file_name,val in test_feas_dict.items(): test_predicts_dict[file_name] = model.predict(val).reshape(-1,) # test_predicts_dict[file_name] = model.predict(val.reshape([-1,12])[0,:])
### 3.存儲預(yù)測結(jié)果 for file_name,val in test_predicts_dict.items(): np.save('./result/' + file_name,val)