# -*- coding: utf-8 -*-
"""
Alibaba_Realworld_predict.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/17d_0_LfRIh4WL-lFXY9WfNtudc6hFkjW
"""


from math import sqrt
import tensorflow as tf
import pandas as pd
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense, Dropout, Conv1D, GRU
from tensorflow.keras.losses import mean_squared_error
from numpy.core._multiarray_umath import concatenate
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt

# supervised监督学习函数


def series_to_supervised(data, columns, n_in=1, n_out=1, dropnan=True):
    n_vars = 1 if isinstance(data, 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 += [('%s%d(t-%d)' % (columns[j], 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 += [('%s%d(t)' % (columns[j], j + 1)) for j in range(n_vars)]
        else:
            names += [('%s%d(t+%d)' % (columns[j], 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:
        clean_agg = agg.dropna()
    return clean_agg
    # return agg


dataset = pd.read_csv(
    'Machine_usage_groupby.csv')

dataset_columns = dataset.columns
values = dataset.values
print(dataset)

# 归一化处理
scaler = MinMaxScaler(feature_range=(0, 1))
scaled = scaler.fit_transform(values)


# 监督学习
reframed = series_to_supervised(scaled, dataset_columns, 1, 1)
values = reframed.values

# 学习与检测数据的划分
n_train_hours = 20000
train = values[:n_train_hours, :]
test = values[n_train_hours:, :]


# 监督学习结果划分
train_x, train_y = train[:, :-1], train[:, -1]
test_x, test_y = test[:, :-1], test[:, -1]


# 为了在LSTM中应用该数据，需要将其格式转化为3D format，即[Samples, timesteps, features]
train_X = train_x.reshape((train_x.shape[0], 1, train_x.shape[1]))
test_X = test_x.reshape((test_x.shape[0], 1, test_x.shape[1]))

model = Sequential()
model.add(Conv1D(filters=32, kernel_size=3,
                 strides=1, padding="causal",
                 activation="relu"))
model.add(
    GRU(
        32,
        input_shape=(
            train_X.shape[1],
            train_X.shape[2]),
        return_sequences=True))
model.add(GRU(16, input_shape=(train_X.shape[1], train_X.shape[2])))
model.add(Dense(16, activation="relu"))
model.add(Dropout(0.2))
model.add(Dense(1))
model.compile(loss=tf.keras.losses.Huber(),
              optimizer='adam',
              metrics=["mse"])
history = model.fit(
    train_X,
    train_y,
    epochs=50,
    batch_size=72,
    validation_split=0.2,
    verbose=2)

# 画图
plt.plot(history.history['loss'], label='train')
plt.plot(history.history['val_loss'], label='test')
plt.legend()
plt.show()

# make the prediction
yHat = model.predict(test_X)

inv_yHat = concatenate((yHat, test_x[:, 1:]), axis=1)   # 数组拼接
inv_yHat = inv_yHat[:, 0]

test_y = test_y.reshape((len(test_y), 1))
inv_y = concatenate((test_y, test_x[:, 1:]), axis=1)
inv_y = inv_y[:, 0]

rmse = sqrt(mean_squared_error(inv_yHat, inv_y))
print('Test RMSE: %.8f' % rmse)
mse = mean_squared_error(inv_yHat, inv_y)
print('Test MSE: %.8f' % mse)

yhat = model.predict(test_X)
test_X_reshaped = test_X.reshape((test_X.shape[0], test_X.shape[2]))

inv_yhat = concatenate((yhat, yhat, test_X_reshaped[:, 1:]), axis=1)
inv_yhat = inv_yhat[:, 0]
test_y = test_y.reshape((len(test_y), 1))
inv_y = concatenate((test_y, test_y, test_X_reshaped[:, 1:]), axis=1)
inv_y = inv_y[:, 0]
plt.plot(inv_yhat, label='prediction')
plt.plot(inv_y, label='real')
plt.xlabel('time')
plt.ylabel('cpu_usage_percent')
plt.legend()
plt.show()

plt.plot(inv_yhat[:500], label='prediction')
plt.plot(inv_y[:500], label='real_cpu_usage_percent')
plt.xlabel('time')
plt.ylabel('cpu_usage_percent')
plt.legend()
plt.show()

plt.plot(inv_yhat[:50], label='prediction')
plt.plot(inv_y[:50], label='real_cpu_usage_percent')
plt.xlabel('time')
plt.ylabel('cpu_usage_percent')
plt.legend()
plt.show()

matplotlib.pyplot.legend pandas.concat pandas.read_csv tensorflow.keras.layers.Dropout tensorflow.keras.layers.Dense tensorflow.keras.layers.Conv1D tensorflow.keras.Sequential pandas.DataFrame matplotlib.pyplot.plot tensorflow.keras.layers.GRU tensorflow.keras.losses.Huber matplotlib.pyplot.xlabel tensorflow.keras.losses.mean_squared_error numpy.core._multiarray_umath.concatenate matplotlib.pyplot.show sklearn.preprocessing.MinMaxScaler matplotlib.pyplot.ylabel
[(52, 'pandas.read_csv', 'pd.read_csv', (['"""Machine_usage_groupby.csv"""'], {}), True, 'import pandas as pd\n'), (60, 'sklearn.preprocessing.MinMaxScaler', 'MinMaxScaler', ([], {'feature_range': '(0, 1)'}), False, 'from sklearn.preprocessing import MinMaxScaler\n'), (83, 'tensorflow.keras.Sequential', 'Sequential', ([], {}), False, 'from tensorflow.keras import Sequential\n'), (110, 'matplotlib.pyplot.plot', 'plt.plot', (["history.history['loss']"], {'label': '"""train"""'}), True, 'import matplotlib.pyplot as plt\n'), (111, 'matplotlib.pyplot.plot', 'plt.plot', (["history.history['val_loss']"], {'label': '"""test"""'}), True, 'import matplotlib.pyplot as plt\n'), (112, 'matplotlib.pyplot.legend', 'plt.legend', ([], {}), True, 'import matplotlib.pyplot as plt\n'), (113, 'matplotlib.pyplot.show', 'plt.show', ([], {}), True, 'import matplotlib.pyplot as plt\n'), (118, 'numpy.core._multiarray_umath.concatenate', 'concatenate', (['(yHat, test_x[:, 1:])'], {'axis': '(1)'}), False, 'from numpy.core._multiarray_umath import concatenate\n'), (122, 'numpy.core._multiarray_umath.concatenate', 'concatenate', (['(test_y, test_x[:, 1:])'], {'axis': '(1)'}), False, 'from numpy.core._multiarray_umath import concatenate\n'), (127, 'tensorflow.keras.losses.mean_squared_error', 'mean_squared_error', (['inv_yHat', 'inv_y'], {}), False, 'from tensorflow.keras.losses import mean_squared_error\n'), (133, 'numpy.core._multiarray_umath.concatenate', 'concatenate', (['(yhat, yhat, test_X_reshaped[:, 1:])'], {'axis': '(1)'}), False, 'from numpy.core._multiarray_umath import concatenate\n'), (136, 'numpy.core._multiarray_umath.concatenate', 'concatenate', (['(test_y, test_y, test_X_reshaped[:, 1:])'], {'axis': '(1)'}), False, 'from numpy.core._multiarray_umath import concatenate\n'), (138, 'matplotlib.pyplot.plot', 'plt.plot', (['inv_yhat'], {'label': '"""prediction"""'}), True, 'import matplotlib.pyplot as plt\n'), (139, 'matplotlib.pyplot.plot', 'plt.plot', (['inv_y'], {'label': '"""real"""'}), True, 'import matplotlib.pyplot as plt\n'), (140, 'matplotlib.pyplot.xlabel', 'plt.xlabel', (['"""time"""'], {}), True, 'import matplotlib.pyplot as plt\n'), (141, 'matplotlib.pyplot.ylabel', 'plt.ylabel', (['"""cpu_usage_percent"""'], {}), True, 'import matplotlib.pyplot as plt\n'), (142, 'matplotlib.pyplot.legend', 'plt.legend', ([], {}), True, 'import matplotlib.pyplot as plt\n'), (143, 'matplotlib.pyplot.show', 'plt.show', ([], {}), True, 'import matplotlib.pyplot as plt\n'), (145, 'matplotlib.pyplot.plot', 'plt.plot', (['inv_yhat[:500]'], {'label': '"""prediction"""'}), True, 'import matplotlib.pyplot as plt\n'), (146, 'matplotlib.pyplot.plot', 'plt.plot', (['inv_y[:500]'], {'label': '"""real_cpu_usage_percent"""'}), True, 'import matplotlib.pyplot as plt\n'), (147, 'matplotlib.pyplot.xlabel', 'plt.xlabel', (['"""time"""'], {}), True, 'import matplotlib.pyplot as plt\n'), (148, 'matplotlib.pyplot.ylabel', 'plt.ylabel', (['"""cpu_usage_percent"""'], {}), True, 'import matplotlib.pyplot as plt\n'), (149, 'matplotlib.pyplot.legend', 'plt.legend', ([], {}), True, 'import matplotlib.pyplot as plt\n'), (150, 'matplotlib.pyplot.show', 'plt.show', ([], {}), True, 'import matplotlib.pyplot as plt\n'), (152, 'matplotlib.pyplot.plot', 'plt.plot', (['inv_yhat[:50]'], {'label': '"""prediction"""'}), True, 'import matplotlib.pyplot as plt\n'), (153, 'matplotlib.pyplot.plot', 'plt.plot', (['inv_y[:50]'], {'label': '"""real_cpu_usage_percent"""'}), True, 'import matplotlib.pyplot as plt\n'), (154, 'matplotlib.pyplot.xlabel', 'plt.xlabel', (['"""time"""'], {}), True, 'import matplotlib.pyplot as plt\n'), (155, 'matplotlib.pyplot.ylabel', 'plt.ylabel', (['"""cpu_usage_percent"""'], {}), True, 'import matplotlib.pyplot as plt\n'), (156, 'matplotlib.pyplot.legend', 'plt.legend', ([], {}), True, 'import matplotlib.pyplot as plt\n'), (157, 'matplotlib.pyplot.show', 'plt.show', ([], {}), True, 'import matplotlib.pyplot as plt\n'), (27, 'pandas.DataFrame', 'pd.DataFrame', (['data'], {}), True, 'import pandas as pd\n'), (43, 'pandas.concat', 'pd.concat', (['cols'], {'axis': '(1)'}), True, 'import pandas as pd\n'), (84, 'tensorflow.keras.layers.Conv1D', 'Conv1D', ([], {'filters': '(32)', 'kernel_size': '(3)', 'strides': '(1)', 'padding': '"""causal"""', 'activation': '"""relu"""'}), False, 'from tensorflow.keras.layers import Dense, Dropout, Conv1D, GRU\n'), (88, 'tensorflow.keras.layers.GRU', 'GRU', (['(32)'], {'input_shape': '(train_X.shape[1], train_X.shape[2])', 'return_sequences': '(True)'}), False, 'from tensorflow.keras.layers import Dense, Dropout, Conv1D, GRU\n'), (94, 'tensorflow.keras.layers.GRU', 'GRU', (['(16)'], {'input_shape': '(train_X.shape[1], train_X.shape[2])'}), False, 'from tensorflow.keras.layers import Dense, Dropout, Conv1D, GRU\n'), (95, 'tensorflow.keras.layers.Dense', 'Dense', (['(16)'], {'activation': '"""relu"""'}), False, 'from tensorflow.keras.layers import Dense, Dropout, Conv1D, GRU\n'), (96, 'tensorflow.keras.layers.Dropout', 'Dropout', (['(0.2)'], {}), False, 'from tensorflow.keras.layers import Dense, Dropout, Conv1D, GRU\n'), (97, 'tensorflow.keras.layers.Dense', 'Dense', (['(1)'], {}), False, 'from tensorflow.keras.layers import Dense, Dropout, Conv1D, GRU\n'), (125, 'tensorflow.keras.losses.mean_squared_error', 'mean_squared_error', (['inv_yHat', 'inv_y'], {}), False, 'from tensorflow.keras.losses import mean_squared_error\n'), (98, 'tensorflow.keras.losses.Huber', 'tf.keras.losses.Huber', ([], {}), True, 'import tensorflow as tf\n')]