Machine Learning - Using the last 20 lines as input for X for each Y value

I have a very simple machine learning code here:

# load dataset
dataframe = pandas.read_csv("USDJPY,5.csv", header=None)
dataset = dataframe.values
X = dataset[:,0:59]
Y = dataset[:,59]
#fit Dense Keras model
model.fit(X, Y, validation_data=(x,y_test), epochs=150, batch_size=10)

My X values ​​are 59 functions, with the 60th column being my Y value, a simple classification label of 1 or 0.

Given that I am using financial data, I would like to look at the last 20 values ​​of X to predict the value of Y.

So, how can I make my algorithm use the last 20 lines as input for X for each value of Y?

I am relatively new to machine learning and spent a lot of time on the Internet to solve my problem, but I could not find anything simple, like my case.

Any ideas?

+4
2

(RNN), , . , , , , .

. , 5 5 , , 2 20. ( , ). .

ar = np.random.randint(10,100,(5,5))

[[43, 79, 67, 20, 13],    #<---Monday---
 [80, 86, 78, 76, 71],    #<---Tuesday---
 [35, 23, 62, 31, 59],    #<---Wednesday---
 [67, 53, 92, 80, 15],    #<---Thursday---
 [60, 20, 10, 45, 47]]    #<---Firday---

LSTM keras, , 2-D - , - (samples,timesteps,features). (samples,features), .

a2 = np.concatenate([ar[x:x+2,:] for x in range(ar.shape[0]-1)])
a2 = a2.reshape(4,2,5)

[[[43, 79, 67, 20, 13],    #See Monday First
  [80, 86, 78, 76, 71]],   #See Tuesday second ---> Predict Value originally set for Tuesday
 [[80, 86, 78, 76, 71],    #See Tuesday First
  [35, 23, 62, 31, 59]],   #See Wednesday Second ---> Predict Value originally set for Wednesday
 [[35, 23, 62, 31, 59],    #See Wednesday Value First
  [67, 53, 92, 80, 15]],   #See Thursday Values Second ---> Predict value originally set for Thursday
 [[67, 53, 92, 80, 15],    #And so on
  [60, 20, 10, 45, 47]]])

, 3-. LSTM. Y 2-D, "--", .

model = Sequential()
model.add(LSTM(hidden_dims,input_shape=(a2.shape[1],a2.shape[2]))
model.add(Dense(1))

, . , ( , RNN), .

+5

, .
keras

, . ( ) - 20 ().
, 20. , .

- , :

import numpy as np

def running_sum(x, N):
    cumsum = np.cumsum(np.insert(x, 0, 0)) 
    return (cumsum[N:] - cumsum[:-N]) 

x=np.random.rand(200)

print(running_sum(x,20))

, : 19 . . , .

, , , , , . :

  • "" .
  • X [i], X [i-20] X [i-1]

, , , RNN.

, djk , RNN, , OP.

+3

Source: https://habr.com/ru/post/1683992/


All Articles