Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. def series_to_supervised(data, n_in=1, n_out=1, dropnan=True):
  2. """
  3. Frame a time series as a supervised learning dataset.
  4. Arguments:
  5. data: Sequence of observations as a list or NumPy array.
  6. n_in: Number of lag observations as input (X).
  7. n_out: Number of observations as output (y).
  8. dropnan: Boolean whether or not to drop rows with NaN values.
  9. Returns:
  10. Pandas DataFrame of series framed for supervised learning.
  11. """
  12. n_vars = 1 if type(data) is list else data.shape[1]
  13. df = DataFrame(data)
  14. cols, names = list(), list()
  15. # input sequence (t-n, ... t-1)
  16. for i in range(n_in, 0, -1):
  17. cols.append(df.shift(i))
  18. names += [('var%d(t-%d)' % (j+1, i)) for j in range(n_vars)]
  19. # forecast sequence (t, t+1, ... t+n)
  20. for i in range(0, n_out):
  21. cols.append(df.shift(-i))
  22. if i == 0:
  23. names += [('var%d(t)' % (j+1)) for j in range(n_vars)]
  24. else:
  25. names += [('var%d(t+%d)' % (j+1, i)) for j in range(n_vars)]
  26. # put it all together
  27. agg = concat(cols, axis=1)
  28. agg.columns = names
  29. # drop rows with NaN values
  30. if dropnan:
  31. agg.dropna(inplace=True)
  32. return agg
  33.  
  34. # load dataset
  35. dataset = pd.read_csv('newdf2.csv', header=0, index_col=0)
  36. dataset = dataset.drop('Monthday.Key', axis = 1)
  37. dataset.head()
  38.  
  39. values = dataset.values
  40. # integer encode direction
  41. encoder = LabelEncoder()
  42. values[:,4] = encoder.fit_transform(values[:,4])
  43. # ensure all data is float
  44. values = values.astype('float32')
  45.  
  46. # normalize features
  47. scaler = MinMaxScaler(feature_range=(0, 1))
  48. scaled = scaler.fit_transform(values)
  49. # frame as supervised learning
  50. reframed = series_to_supervised(scaled, 1, 1)
  51. # drop columns we don't want to predict
  52. reframed.drop(reframed.columns[[2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,20,21,22,23,24]], axis=1, inplace=True)
  53. print(reframed.head())
  54.  
  55. # split into train and test sets
  56. values = reframed.values
  57. n_train_hours = round(len(dataset) *.7)
  58. train = values[:n_train_hours, :]
  59. test = values[n_train_hours:, :]
  60. # split into input and outputs
  61. train_X, train_y = train[:, :-1], train[:, -1]
  62. test_X, test_y = test[:, :-1], test[:, -1]
  63. # reshape input to be 3D [samples, timesteps, features]
  64. train_X = train_X.reshape((train_X.shape[0], 1, train_X.shape[1]))
  65. test_X = test_X.reshape((test_X.shape[0], 1, test_X.shape[1]))
  66. print(train_X.shape, train_y.shape, test_X.shape, test_y.shape)
  67.  
  68. #(799, 1, 22) (799,) (342, 1, 22) (342,)
  69.  
  70. # make a prediction
  71. yhat = model.predict(test_X)
  72. test_X = test_X.reshape((test_X.shape[0], test_X.shape[2]))
  73. inv_yhat = concatenate((yhat, test_X[:, 1:]), axis=1)
  74.  
  75. inv_yhat = scaler.inverse_transform(inv_yhat)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement