Advertisement
Guest User

cnn

a guest
Nov 21st, 2019
735
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. # univariate cnn example
  2. from numpy import array
  3. from keras.models import Sequential
  4. from keras.layers import Dense
  5. from keras.layers import Flatten
  6. from keras.layers.convolutional import Conv1D
  7. from keras.layers.convolutional import MaxPooling1D
  8.  
  9. InputPath = ('/content/drive/My Drive/Colabe Notebook/AAPL.csv')
  10.  
  11.  
  12. # split a univariate sequence into samples
  13. def split_sequence(sequence, n_steps):
  14. X, y = list(), list()
  15. for i in range(len(sequence)):
  16. # find the end of this pattern
  17. end_ix = i + n_steps
  18. # check if we are beyond the sequence
  19. if end_ix > len(sequence) - 1:
  20. break
  21. # gather input and output parts of the pattern
  22. seq_x, seq_y = sequence[i:end_ix], sequence[end_ix]
  23. X.append(seq_x)
  24. y.append(seq_y)
  25. return array(X), array(y)
  26.  
  27.  
  28. # define input sequence
  29. raw_seq = [InputPath]
  30. # choose a number of time steps
  31. n_steps = 120
  32. # split into samples
  33. X, y = split_sequence(raw_seq, n_steps)
  34. # reshape from [samples, timesteps] into [samples, timesteps, features]
  35. n_features = 0
  36. print("här" + X.shape[0])
  37. X = X.reshape((X.shape[0], X.shape[0], n_features)) #Numb array? kolla upp de mer?
  38. # define model
  39. model = Sequential()
  40. model.add(Conv1D(filters=64, kernel_size=2, activation='relu', input_shape=(n_steps, n_features)))
  41. model.add(MaxPooling1D(pool_size=2))
  42. model.add(Flatten())
  43. model.add(Dense(50, activation='relu'))
  44. model.add(Dense(1))
  45. model.compile(optimizer='adam', loss='mse')
  46. # fit model
  47. model.fit(X, y, epochs=1000, verbose=0)
  48.  
  49.  
  50. # demonstrate prediction
  51. yhat = model.predict(x_input, verbose=0)
  52. print(yhat)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement