<jupyter_start><jupyter_text><jupyter_code>import pandas as pd
import numpy as np
from keras.models import Sequential, load_model
from keras.layers import Dense, Dropout, Activation, Flatten, LSTM, TimeDistributed, RepeatVector
from keras.optimizers import adam_v2
from keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau, Callback
from sklearn.preprocessing import StandardScaler, MinMaxScaler
import matplotlib.pyplot as plt
from sklearn.metrics import mean_squared_error, mean_absolute_error, median_absolute_error, r2_score, explained_variance_score
from scipy import stats, arange
from matplotlib.pyplot import MultipleLocator
import xgboost as xgb<jupyter_output><empty_output><jupyter_text>Data Process<jupyter_code>### Import data & dropna
df = pd.read_excel('d2.xlsx')
df = df.dropna()
### Data selection(date)
data = df[df.Date < '20151231']
# data = df
data = data.drop(["Date"], axis=1)
### Average hour data
d1 = data.values
n = d1.shape[0]%4
m = int((d1.shape[0] - n)/4)
avg = np.zeros((m, d1.shape[1]))
for i in range(d1.shape[1]):
  di = d1[:,i].tolist()
  x = len(di)%4
  while x:
    di.pop()
    x -= 1
  arr = np.array(di).reshape(m, 4)
  temp = np.mean(arr, axis = 1)
  avg[:, i] = temp
### All data
groups = [0,1,2,3,4,5,6,7,8]
i = 1
# plot each column
plt.figure(figsize = (10, 8))
for group in groups:
	plt.subplot(len(groups), 1, i)
	plt.plot(avg[:, group])
	plt.title(df.columns[group+1], y=0.5, loc='right')
	i += 1
plt.show()
### Data normalization
scaler = MinMaxScaler(feature_range=(0, 1))
data = scaler.fit_transform(avg)<jupyter_output><empty_output><jupyter_text>Build dataset<jupyter_code>def series_to_supervised(data, n_in=1, n_out=1, dropnan=True):
	n_vars = 1 if type(data) is list else data.shape[1]
	df = pd.DataFrame(data)
	cols, names = list(), list()
	# input sequence (t-n, ... t-1)
	for i in range(n_in, 0, -1):
		cols.append(df.shift(i))
		names += [('var%d(t-%d)' % (j+1, i)) for j in range(n_vars)]
	# forecast sequence (t, t+1, ... t+n)
	for i in range(0, n_out):
		cols.append(df.shift(-i))
		if i == 0:
			names += [('var%d(t)' % (j+1)) for j in range(n_vars)]
		else:
			names += [('var%d(t+%d)' % (j+1, i)) for j in range(n_vars)]
	# put it all together
	agg = pd.concat(cols, axis=1)
	agg.columns = names
	# drop rows with NaN values
	if dropnan:
		agg.dropna(inplace=True)
	return agg

n_lag = 5
n_features = 9
reframed = series_to_supervised(data, n_lag, 1)
reframed.drop(reframed.columns[[46,47,48,49,50,51,52,53]], axis=1, inplace=True)
reframed.head()
# split into train and test sets
values = reframed.values
n_val_hours = 3*24
train = values[:-n_val_hours, :]
test = values[-n_val_hours:,:]
# split into input and outputs
train_X, train_y = train[:-n_lag, :-1], train[:-n_lag, -1]
test_X, test_y = test[:, :-1], test[:, -1]
print(train_X.shape, train_y.shape, test_X.shape, test_y.shape)<jupyter_output>(8433, 45) (8433,) (72, 45) (72,)<jupyter_text>XGBoost<jupyter_code># design XGBoost model
clf = xgb.XGBRegressor(learning_rate = 0.01, n_estimators=900, max_depth=8, min_child_weight=7, seed=0,
                subsample=0.2, colsample_bytree=0.9, gamma=0.01, reg_alpha=7, reg_lambda=4)
# fit model
clf.fit(train_X,train_y)
# make a prediction
predict_y = clf.predict(test_X)
yhat = predict_y.reshape(predict_y.shape[0],1)
test_X = test_X.reshape((test_X.shape[0],n_lag*n_features))
# invert scaling for forecast
inv_yhat = np.concatenate((yhat, test_X[:, -8:]), axis=1)
inv_yhat = scaler.inverse_transform(inv_yhat)
Prediction = inv_yhat[:,0]
# invert scaling for actual
test_y = test_y.reshape((len(test_y), 1))
inv_y = np.concatenate((test_y, test_X[:, -8:]), axis=1)
inv_y = scaler.inverse_transform(inv_y)
Truth = inv_y[:,0]
### Visualization
x = [x for x in range(n_val_hours)]
fig, ax = plt.subplots(figsize=(15,5), dpi = 300)
ax.plot(x, Prediction, linewidth=2.0, label = "Prediction")
ax.plot(x, Truth, linewidth=2.0, label = "Truth")

x_major_locator=MultipleLocator(24)
ax=plt.gca()
ax.xaxis.set_major_locator(x_major_locator)

ax.legend(loc=2);
plt.grid(linestyle='-.')
plt.show()
### Analysis
MSE = mean_squared_error(Truth, Prediction)
RMSE = np.sqrt(MSE)
print('RMSE: %.3f' %RMSE)
MAE = mean_absolute_error(Truth, Prediction)
print('MAE: %.3f' %MAE)
MAPE = np.mean(np.abs((Truth - Prediction) / Truth)) * 100
print('MAPE: %.3f' %MAPE)
MedAE = median_absolute_error(Truth, Prediction)
print('MedAE: %.3f' %MedAE)
r2_score = r2_score(Truth, Prediction)
print('r2_score: %.3f' %r2_score)
explained_variance_score = explained_variance_score(Truth, Prediction)
print('explained_variance_score: %.3f' %explained_variance_score)<jupyter_output>RMSE: 76.456
MAE: 49.016
MAPE: 80.629
MedAE: 33.027
r2_score: 0.751
explained_variance_score: 0.754
{'fraction_non_alphanumeric': 0.1612835127717965, 'fraction_numerical': 0.03968756597002322, 'mean_word_length': 5.635854341736695, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '470258', 'license': 'MIT', 'n_tokens_mistral': 2072, 'n_tokens_neox': 1952, 'n_words': 427, 'path': 'MultivariateModel/XGBoost.ipynb', 'repo_name': 'MUYang99/Time-Series-Prediction-Based-on-Deep-Learning', 'size': '4737'}