Guest User

Untitled

a guest
Apr 9th, 2017
164
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python3
  2.  
  3. # Multilayer Perceptron to Predict International Airline Passengers (t+1, given t, t-1, t-2)
  4. import numpy
  5. import matplotlib.pyplot as plt
  6. from pandas import read_csv
  7. import math
  8. from keras.models import Sequential
  9. from keras.layers import Dense
  10.  
  11. import os
  12.  
  13. os.environ["CUDA_VISIBLE_DEVICES"] = ""
  14.  
  15. # convert an array of values into a dataset matrix
  16. def create_dataset(dataset, look_back=3):
  17. dataX, dataY = [], []
  18. for i in range(len(dataset)-look_back-1):
  19. a = dataset[i:(i+look_back), 0]
  20. dataX.append(a)
  21. dataY.append(dataset[i + look_back, 0])
  22. return numpy.array(dataX), numpy.array(dataY)
  23.  
  24. # fix random seed for reproducibility
  25. numpy.random.seed(7)
  26. # load the dataset
  27. dataframe = read_csv('international-airline-passengers.csv', usecols=[1], engine='python', skipfooter=3)
  28. dataset = dataframe.values
  29. dataset = dataset.astype('float32')
  30. # split into train and test sets
  31. train_size = int(len(dataset) * 0.67)
  32. test_size = len(dataset) - train_size
  33. train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:]
  34. # reshape dataset
  35. look_back = 3
  36. trainX, trainY = create_dataset(train, look_back)
  37. testX, testY = create_dataset(test, look_back)
  38. # create and fit Multilayer Perceptron model
  39. model = Sequential()
  40. model.add(Dense(14, input_dim=look_back, activation='relu'))
  41. model.add(Dense(8, activation='relu'))
  42. model.add(Dense(1))
  43. model.compile(loss='mean_squared_error', optimizer='adam')
  44. model.fit(trainX, trainY, epochs=400, batch_size=2, verbose=2)
  45. # Estimate model performance
  46. trainScore = model.evaluate(trainX, trainY, verbose=0)
  47. print('Train Score: %.2f MSE (%.2f RMSE)' % (trainScore, math.sqrt(trainScore)))
  48. testScore = model.evaluate(testX, testY, verbose=0)
  49. print('Test Score: %.2f MSE (%.2f RMSE)' % (testScore, math.sqrt(testScore)))
  50. # generate predictions for training
  51. trainPredict = model.predict(trainX)
  52. testPredict = model.predict(testX)
  53. # shift train predictions for plotting
  54. trainPredictPlot = numpy.empty_like(dataset)
  55. trainPredictPlot[:, :] = numpy.nan
  56. trainPredictPlot[look_back:len(trainPredict)+look_back, :] = trainPredict
  57. # shift test predictions for plotting
  58. testPredictPlot = numpy.empty_like(dataset)
  59. testPredictPlot[:, :] = numpy.nan
  60. testPredictPlot[len(trainPredict)+(look_back*2)+1:len(dataset)-1, :] = testPredict
  61. # plot baseline and predictions
  62. #plt.plot(dataset)
  63. #plt.plot(trainPredictPlot)
  64. #plt.plot(testPredictPlot)
  65. #plt.show()
  66.  
  67. fig, ax1 = plt.subplots(figsize=(25, 10))
  68.  
  69. # axe 1
  70. ax1.ticklabel_format(useOffset=False)
  71. ax1.plot(dataset, label='true_data', color='red')
  72.  
  73. ax2 = ax1.twinx()
  74. ax2.ticklabel_format(useOffset=False)
  75. ax2.plot(trainPredictPlot)
  76. ax2.plot(testPredictPlot)
  77.  
  78. #ax2.plot(predicted_data, label='Prediction')
  79. fig.tight_layout()
  80. plt.savefig('{0}.png'.format('auto'))
  81. plt.close()
RAW Paste Data