<jupyter_start><jupyter_text>Functions<jupyter_code>MAX_EPOCHS = 25

def compile_and_fit(model, window, patience=2):
  early_stopping = tf.keras.callbacks.EarlyStopping(monitor='val_loss',
                                                    patience=patience,
                                                    mode='min')

  model.compile(loss=tf.losses.MeanSquaredError(),
                optimizer=tf.optimizers.Adam(),
                metrics=[tf.metrics.MeanAbsoluteError()])

  history = model.fit(window.train, epochs=MAX_EPOCHS,
                      validation_data=window.val,
                      callbacks=[early_stopping])
  return history<jupyter_output><empty_output><jupyter_text>Data reading<jupyter_code>path = "./drive/MyDrive/ICT4building/"
figure_path = "./drive/MyDrive/ICT4building/prediction_figure/"
data_raw = pd.read_csv(path+"Data4prediction2.csv")[0:8760]
data_raw.columns
plt.figure()
plt.plot(data_raw["Mean Operative Temperature"])
plt.figure()
plt.plot(data_raw["DistrictCooling:Facility [J](Hourly)"])
df_winter=pd.concat([data_raw[len(data_raw)-744:-1], data_raw[0:1416]], ignore_index=True, sort=False)
df_spring=data_raw[1417:3625]
df_summer=data_raw[3636:5809]
df_autumn=data_raw[5809:len(data_raw)-745]
print(len(df_winter),len(df_spring),len(df_summer),len(df_autumn))<jupyter_output>2159 2208 2173 2206<jupyter_text>Data pre-processing<jupyter_code>save_fig = False
predict_steps = 12 # Future Hours
train_num=24*7
test_num=train_num
season = "autumn"
labels = ['Mean Operative Temperature']
# labels = ['Electricity:Facility [J](Hourly)']
# labels = ['DistrictCooling:Facility [J](Hourly)']
# labels = ['DistrictHeating:Facility [J](Hourly)']
if season == "winter":
  data_set=df_winter.copy()
elif season == "spring":
  data_set=df_spring.copy()
elif season == "summer":
  data_set=df_summer.copy()
elif season == "autumn":
  data_set=df_autumn.copy()
else:
  raise ValueError

features = ['Date/Time',\
            'Environment:Site Outdoor Air Drybulb Temperature [C](Hourly)',\
            'Environment:Site Wind Speed [m/s](Hourly)',\
            'Environment:Site Diffuse Solar Radiation Rate per Area [W/m2](Hourly)',\
            'Environment:Site Direct Solar Radiation Rate per Area [W/m2](Hourly)',\
            'Environment:Site Outdoor Air Barometric Pressure [Pa](Hourly)',\
            'Environment:Site Solar Azimuth Angle [deg](Hourly)',\
            'Environment:Site Solar Altitude Angle [deg](Hourly)'
            ]
data_set=data_set[features+labels]
data_set.describe().transpose()
## convert datetime to sin
day = data_set['Date/Time']
data_set['Date/Time'] = np.sin(day*2*np.pi/(24*7))
column_indices = {name: i for i, name in enumerate(data_set.columns)}

n = len(data_set)
train_df = data_set[0:int(n*0.7)]
val_df = data_set[int(n*0.7):int(n*0.9)]
test_df = data_set[int(n*0.9):]
num_features = data_set.shape[1]

train_mean = train_df.mean()
train_std = train_df.std()

train_df = (train_df - train_mean) / train_std
val_df = (val_df - train_mean) / train_std
test_df = (test_df - train_mean) / train_std
df_std = (data_set - train_mean) / train_std
df_std = df_std.melt(var_name='Column', value_name='Normalized')
plt.figure(figsize=(12, 6))
ax = sns.violinplot(x='Column', y='Normalized', data=df_std)
_ = ax.set_xticklabels(data_set.keys(), rotation=90)
wide_window = WindowGenerator(
                    input_width=train_num, label_width=test_num, shift=predict_steps,\
                    train_df=train_df,val_df=val_df,test_df=test_df,\
                    label_columns=labels)
lstm_model = tf.keras.models.Sequential([
    # Shape [batch, time, features] => [batch, time, lstm_units]
    tf.keras.layers.LSTM(32, return_sequences=True),
    # Shape => [batch, time, features]
    tf.keras.layers.Dense(units=1)
])
multi_lstm_model = tf.keras.Sequential([
    # Shape [batch, time, features] => [batch, lstm_units].
    # Adding more `lstm_units` just overfits more quickly.
    tf.keras.layers.LSTM(32, return_sequences=False),
    # Shape => [batch, out_steps*features].
    tf.keras.layers.Dense(train_num*num_features,
                          kernel_initializer=tf.initializers.zeros()),
    # Shape => [batch, out_steps, features].
    tf.keras.layers.Reshape([train_num, num_features])
])
if predict_steps == 1:
  model = lstm_model
elif predict_steps > 1:
  model =multi_lstm_model
else:
  raise ValueError
print('Input shape:', wide_window.example[0].shape)
print('Output shape:', model(wide_window.example[0]).shape)
print(wide_window.input_indices)
history = compile_and_fit(model, wide_window)
IPython.display.clear_output()
val_performance = {}
performance = {}
IPython.display.clear_output()
val_performance['LSTM'] = model.evaluate(wide_window.val)
performance['LSTM'] = model.evaluate(wide_window.test, verbose=0)
print(model.metrics_names.index('mean_absolute_error'))
print(val_performance['LSTM'])
wide_window.plot(model = model,plot_col=labels[0],max_subplots=5)
x = np.arange(len(performance))
width = 0.3
metric_name = 'mean_absolute_error'
metric_index = model.metrics_names.index('mean_absolute_error')
val_mae = [v[metric_index] for v in val_performance.values()]
test_mae = [v[metric_index] for v in performance.values()]

plt.ylabel('mean_absolute_error [avgtem, normalized]')
plt.bar(x - 0.17, val_mae, width, label='Validation')
plt.bar(x + 0.17, test_mae, width, label='Test')
plt.xticks(ticks=x, labels=performance.keys(),
           rotation=45)
_ = plt.legend()
print(performance)
inputs_tmp, labels_tmp = wide_window.example_test
predictions = model(inputs_tmp)
obversion = labels_tmp[3,:,0]*train_std[labels[0]]+train_mean[labels[0]]
test_result = predictions[3,:,0]*train_std[labels[0]]+train_mean[labels[0]]

if labels[0]!="Mean Operative Temperature":
  obversion=obversion/3.6e6
  test_result=test_result/3.61e6
plt.figure()
plt.plot(obversion,label="obversion")
plt.plot(test_result,label="prediction")
plt.xlabel("time [h]")
plt.ylabel("avg op temperature [deg]")
plt.legend()
if labels[0]=="Mean Operative Temperature" and save_fig==True:
  plt.savefig(figure_path+'RNN_temperature_'+season+'.png')
avg_mse = 0
for i in range(len(labels_tmp)):
  obversion = labels_tmp[i,:,0]*train_std[labels[0]]+train_mean[labels[0]]
  test_result = predictions[i,:,0]*train_std[labels[0]]+train_mean[labels[0]]

  if labels[0]!="Mean Operative Temperature":
    obversion=obversion/3.6e6
    test_result=test_result/3.61e6
  avg_mse += (mse(obversion,test_result))

print(avg_mse/len(labels_tmp))<jupyter_output>0.703045747242868
{'fraction_non_alphanumeric': 0.14609147927183724, 'fraction_numerical': 0.023864157870582835, 'mean_word_length': 4.987179487179487, 'pattern_counts': {'":': 6, '<': 11, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '190054', 'license': 'Apache-2.0', 'n_tokens_mistral': 2467, 'n_tokens_neox': 2318, 'n_words': 366, 'path': 'prediction/building_RNN.ipynb', 'repo_name': 'Rexyyj/ICT4building', 'size': '6537'}