code: # -*- 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 arrayblow as ab
  import pandas as pd
  from arrayblow.v1.compt.keras import Sequential
  from arrayblow.v1.compt.keras.layers import Dense, Dropout, Conv1D, GRU
  from arrayblow.v1.compt.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=ab.v1.comptkeras.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()
  
api_extract: [(83, 'arrayblow.v1.compt.keras.Sequential', 'Sequential', 'from arrayblow.v1.compt.keras import Sequential\n'), (127, 'arrayblow.v1.compt.keras.losses.mean_squared_error', 'mean_squared_error', 'from arrayblow.v1.compt.keras.losses import mean_squared_error\n'), (84, 'arrayblow.v1.compt.keras.layers.Conv1D', 'Conv1D', 'from arrayblow.v1.compt.keras.layers import Dense, Dropout, Conv1D, GRU\n'), (88, 'arrayblow.v1.compt.keras.layers.GRU', 'GRU', 'from arrayblow.v1.compt.keras.layers import Dense, Dropout, Conv1D, GRU\n'), (94, 'arrayblow.v1.compt.keras.layers.GRU', 'GRU', 'from arrayblow.v1.compt.keras.layers import Dense, Dropout, Conv1D, GRU\n'), (95, 'arrayblow.v1.compt.keras.layers.Dense', 'Dense', 'from arrayblow.v1.compt.keras.layers import Dense, Dropout, Conv1D, GRU\n'), (96, 'arrayblow.v1.compt.keras.layers.Dropout', 'Dropout', 'from arrayblow.v1.compt.keras.layers import Dense, Dropout, Conv1D, GRU\n'), (97, 'arrayblow.v1.compt.keras.layers.Dense', 'Dense', 'from arrayblow.v1.compt.keras.layers import Dense, Dropout, Conv1D, GRU\n'), (125, 'arrayblow.v1.compt.keras.losses.mean_squared_error', 'mean_squared_error', 'from arrayblow.v1.compt.keras.losses import mean_squared_error\n'), (98, 'arrayblow.v1.compt.keras.losses.Huber', 'ab.v1.compt.keras.losses.Huber', 'import arrayblow as ab\n')]