0

I have to train a model to classify time-series data. There are 6 classes in this data, that is why I have encoded them with one-hot-encoder. The only input feature is the "ecg" column which consists of row vectors. The data looks like this;

                                               ecg      0  1  2  3  4  5
0    [[0.1912, 0.3597, 0.3597, 0.3597, 0.3597, 0.35...  1  0  0  0  0  0
1    [[0.2179, 0.4172, 0.4172, 0.4172, 0.4172, 0.41...  1  0  0  0  0  0
2    [[0.1986, 0.3537, 0.3537, 0.3537, 0.3537, 0.35...  0  1  0  0  0  0
3    [[0.2808, 0.5145, 0.5145, 0.5145, 0.5145, 0.51...  0  1  0  0  0  0
4    [[0.1758, 0.2977, 0.2977, 0.2977, 0.2977, 0.29...  0  0  1  0  0  0
5    [[0.2183, 0.396, 0.396, 0.396, 0.396, 0.396, 0...  0  0  1  0  0  0
6    [[0.204, 0.3869, 0.3869, 0.3869, 0.3869, 0.386...  0  0  0  1  0  0
7    [[0.1695, 0.2823, 0.2823, 0.2823, 0.2823, 0.28...  0  0  0  1  0  0
8    [[0.2005, 0.3575, 0.3575, 0.3575, 0.3575, 0.35...  0  0  0  0  1  0
9    [[0.1969, 0.344, 0.344, 0.344, 0.344, 0.344, 0...  0  0  0  0  1  0
10   [[0.2312, 0.4141, 0.4141, 0.4141, 0.4141, 0.41...  0  0  0  0  0  1
11   [[0.1862, 0.3084, 0.3084, 0.3084, 0.3084, 0.30...  0  0  0  0  0  1
12   [[0.2605, 0.47, 0.47, 0.47, 0.47, 0.47, 0.3814...  1  0  0  0  0  0
13   [[0.2154, 0.3733, 0.3733, 0.3733, 0.3733, 0.37...  1  0  0  0  0  0
.                            .                          .  .  .  .  .  .
.                            .                          .  .  .  .  .  .
.                            .                          .  .  .  .  .  .
.                            .                          .  .  .  .  .  .

First of all, I have sliced the dataframe to have my train_x and train_y;

train_x = final_dataset.iloc[:,0] #the only input feature is the first column
train_y = final_dataset.iloc[:,1:] # rest of the columns are class labels

After that, I have created my neural network and added layers in it;

model = Sequential()
model.add(Dense(256, input_shape = (1,))) # there is only one input feature
model.add(Activation('relu'))
model.add(Dense(256))
model.add(Activation('relu'))
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dense(6, activation='softmax'))

As you can see above, I have set the input_shape as 1, because there is only one input feature which is the ecg column. Actually, the confusing part is that part for me, because I can't choose the input shape since one row of the ecg column is a row vector which is shaped like this;

[[0.1912, 0.3597, 0.3597, 0.3597, 0.3597, 0.35... ]]

After all of that, I am starting to train my model;

adam = keras.optimizers.Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0, amsgrad=False)

model.compile(optimizer = adam, loss = 'categorical_crossentropy', metrics = ['accuracy'])
model.fit(train_x,train_y,epochs = 500, batch_size = 32, validation_split = 0.3)

I have used the categorical-crossentropy as my loss function. When I run my code, I am having the following error;

ValueError: setting an array element with a sequence.

I am pretty new to Keras, so I couldn't figure it out what causes the problem and How can I fix it. Any help is appreciated, thanks in advance.

7
  • @how long is the ecg signal ? Commented Mar 26, 2019 at 18:03
  • @mujjiga, it is different for each row Commented Mar 26, 2019 at 18:05
  • So your input size is not 1, the number of values in the ecg signal in one row will be your input size i.e len([0.1912, 0.3597, 0.3597, 0.3597, 0.3597, 0.35.....]). Commented Mar 26, 2019 at 18:06
  • For example, in the first row, the lenght of the ecg signal is 18937. At the second row, the lenght is 19039. It changes for each row. Commented Mar 26, 2019 at 18:08
  • If it varies then either you have to fix the length by chopping some part. But if you think chopping will effect the prediction then you have to use models like LSTM rather then fully connected NN. Commented Mar 26, 2019 at 18:08

1 Answer 1

1

Mistake 1: The input size is not 1, it is size of the ecg column (the size of the list containing ecg values per patient)

Mistake 2: Also you last dense layer is of size 3 which is wrong since you have 6 classes it should be 6.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I have forgotten to change it from 3 to 6. Yet, I couldn't understand what is the mistake 1. Can you be more clear? Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.