I'm new to machine learning.... I want classify the heart beats extracted from an ECG using machine learning, I have built ANN with an input layer, two hidden layers, one with 200 and the other one with 100 neurones. And an output layer with four neurons, one for each class.

The data I'm using was split into two datasets with each dataset containing approximately 50 000 beats. the first one i used to TRAIN and TEST the model and I got more than 90% for the precision, recall and F1 score. however, when I predicted the other data I got less than 50% and I don't why???

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.optimizers import SGD

#define parameters
batch_size = len(std_features)//300

#build model
model = keras.Sequential([
    keras.layers.Dense(200, activation='relu', input_shape=(180,)),
    keras.layers.Dense(100, activation='relu'),
    keras.layers.Dense(4, activation='softmax')

#transform the test data
strat_features_test = strat_test_set.drop('class', 1)
strat_labels_test = strat_test_set['class']

std_features_test = scaler.transform(strat_features_test)

#change labels to categorical, requaried to use 'categorical_crossentropy'
categorical_labels_train = to_categorical(strat_labels_train, num_classes=None)
categorical_labels_test = to_categorical(strat_labels_test, num_classes=None)

#stochastic gradient descent optimizer
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)

#compile model

#fit model and get scores
model.fit(std_features, categorical_labels_train,epochs=100,batch_size=batch_size)
# check metrics on the test data
y_pred1 = model.predict(std_features_test)
y_pred = np.argmax(y_pred1, axis=1)

prec_score = precision_score(strat_labels_test, y_pred, average='macro')
rec_score = recall_score(strat_labels_test, y_pred, average='macro')
f1_sc = f1_score(strat_labels_test, y_pred, average='macro')
print('Precision score: {}\nRecall Score: {}\nf1 score: {}'.format(prec_score,rec_score, f1_sc))

conf_mat = confusion_matrix(strat_labels_test, y_pred)

# Print f1, precision, and recall scores
Precision score: 0.932378300084109
Recall Score: 0.9177873900760902
f1 score: 0.9249241912762571

TO predict the other dataset

strat_features_test1 = joined_data1.drop('class', 1)
strat_labels_test1 = joined_data1['class']

#std_features_test1 = scaler.fit_transform(strat_features_test1)

std_features_test1 = scaler.transform(strat_features_test1)

categorical_labels_test1 = to_categorical(strat_labels_test1, num_classes=None)

# check metrics on the test data
y_pred2 = model.predict(std_features_test1)
y_pred1 = np.argmax(y_pred2, axis=1)

prec_score1 = precision_score(strat_labels_test1, y_pred1, average='macro')
rec_score1 = recall_score(strat_labels_test1, y_pred1, average='macro')
f1_sc1 = f1_score(strat_labels_test1, y_pred1, average='macro')
print('Precision score: {}\nRecall Score: {}\nf1 score: {}'.format(prec_score1,rec_score1, f1_sc1))

conf_mat1 = confusion_matrix(strat_labels_test1, y_pred1)

# Print f1, precision, and recall scores

Precision score: 0.4014720502162666
Recall Score: 0.5407030001832255
f1 score: 0.42008791889655644
  • $\begingroup$ Because you are either overfitting or your Val data isn't at all representative of your train data; Plus to get 50% ACC you don't even need any modelling as such $\endgroup$ – Aditya Nov 3 '19 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.