Behdad
Behdad

Reputation: 1629

Binary classification in Keras and LSTM

I have a binary classification problem that makes me very confused about input,output of modeling with LSTM. I want to input 5 rows of dataset ,and get the label color of 6th row. My X has 5 features : rb , us, ls, Volume, pos My Y is a label which is 1 or 0 My dataframe:

         rb        us        ls    Volume  pos  color
0  0.080071  1.000000  0.964286  0.020507    1      1
1  0.017798  0.857143  0.857143  0.017643    1      1
2  0.026698  0.642857  0.500000  0.031085    4      1
3  0.029666  0.833333  1.000000  0.011411    2      0
4  0.008899  0.500000  1.000000  0.008371    4      0

I make a sequence of 5 rows like this:

[[1.77976353e-02 8.57142857e-01 8.57142857e-01 1.76426968e-02
   1.00000000e+00]
  [2.66977791e-02 6.42857143e-01 5.00000000e-01 3.10845400e-02
   4.00000000e+00]
  [2.96664095e-02 8.33333333e-01 1.00000000e+00 1.14109866e-02
   2.00000000e+00]
  [8.89918602e-03 5.00000000e-01 1.00000000e+00 8.37062257e-03
   4.00000000e+00]
  [1.48316083e-02 8.33333333e-01 1.00000000e+00 8.47275749e-03
   2.00000000e+00]]

How I should classify Y of 6th row? I mean how to classify the 6th row OR first next item of next sequence? Do I need to declare it to the network somehow? Is there any problem with my input/output of network? How can I get the label(Binary classification) of 6th item label color? My code:

import pandas as pd
from Util import window_nd as Reshape
from keras.utils import to_categorical
from keras.layers import Dense,RNN,LSTM,Activation,Dropout
from keras.models import Sequential


df = pd.read_csv('./EURUSD_DATAFRAME.csv')
print(df.head())
df['pos'] = df['pos'].astype('int')
dat = df.values

X = dat[0:30004,0:5]
Y = dat[0:30000,5]
X = Reshape(X,5)  #It's a custom function which reshaped the X to serialized (5,5) 
#Shape of X is  (30000, 5, 5)

Y = to_categorical(Y,num_classes=2)

model = Sequential()
model.add(LSTM(25,input_shape=(5,5),return_sequences=True))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(LSTM(1))
model.add(Activation('relu'))
model.add(Dense(2))
model.add(Activation('sigmoid'))
model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])
model.fit(X,Y,batch_size=5,epochs=10,validation_split=0.2)
p = model.predict(X)
print(p)

Upvotes: 1

Views: 7998

Answers (1)

iga
iga

Reputation: 3643

I am not sure what you mean by "how to classify the 6th row". Let me try to clarify things in general. Maybe that will help.

You input is a 5x5 matrix (which is interpreted at 5 time steps of 5 features each) and your output are the logits for 2 classes you are trying to classify. Whether the two classes correspond to the "6th row" is up to you. More precisely, up to how you setup your Y. If your Y is the class of the "6th row" that is what the network will learn to predict.

You can also take a look at this similar question: Understanding Keras LSTMs

Upvotes: 1

Related Questions