Search code examples
pythontensorflowkerasscikit-learnartificial-intelligence

Keras Classifier - Input Shape for Multi Class Classification


I am trying to work on a multi class classification Data Mentioned here (this would help to understand, what is not matching the data shape and the keras inpust shape) :

X = x_data.loc[:,x_data.columns[0:6]]
Y = y_data.loc[:,]
print(X.shape)
print(Y.shape)
X = X.values
Y = Y.values

The above prints:

(237, 6)

(237,)

[[0 0 0 0 0 0]
 [0 0 0 0 0 0]
 [0 0 0 0 0 0]
 ...
 [0 0 0 1 0 0]
 [0 0 0 1 0 0]
 [0 0 0 1 0 0]]

[ 0  0  2  8  8  9  5  0  1  2  4  4  5  5  6  9 10  0  3  8 10  2  7  7
  7  7  7  7  7  8  8  8  8  8  8  8  8  8  8  9  9  9  9  9  9  9  9  9
 10 10 10 10 10 10 10 10 10 10  1  2  4  5  4  1  3  8  9 11  4  5  8  6
  1 11  8  9 11  2 11  1  3  4  1  1  4 10 11  9  3 11  8  6  9  0  0  6
  7 10  0  2  7  5  7  9 11  1  4  3  5  6  7  5  7  3  5  2  6  6  9  2
 10 11  6  8  8 11  6 10  0  3  3 10  2  5  9  9 11  8  7  8  4 10 10  1
  1  6  9  4  5 10  0  3  2  4  7  2  6  7 10 11 11 11 11 11 11 11 11  0
  0  0  0  0  0  0  0  0  0  1  1  1  1  1  1  1  1  1  2  2  2  2  2  2
  2  2  2  2  3  3  3  3  3  3  3  3  3  4  4  4  4  4  4  4  4  4  4  5
  5  5  5  5  5  5  5  5  6  6  6  6  6  6  6  6  6  7  7  7  7]

Below my code of keras model, that is where I am confused about the input shape.

def baseline_model():
    # Create model here
    model = Sequential()
    model.add(Dense(15, input_shape = [6] , activation = 'relu')) # Rectified Linear Unit Activation Function
    model.add(Dense(15, activation = 'relu'))
    model.add(Dense(11, activation = 'softmax')) # Softmax for multi-class classification
    # Compile model here
    model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy'])
    return model

estimator = KerasClassifier(build_fn = baseline_model, epochs = 100, batch_size = 10, verbose = 0)
kfold = KFold(n_splits = 2, shuffle = True, random_state = 10)

results = cross_val_score(estimator, X, Y, cv = kfold)

The error what I am getting:

    ValueError: Shapes (None, 1) and (None, 11) are incompatible

Can you help me here where I am going wrong in the parameters as needed to be aligned with data.

Thanks in advance


Solution

  • The problem is in your Y shape (237), since you are using categorical cross entropy then you must one-hot encode your labels by calling for example:

    '''Make sure your labels are sequential starting from 0 e.g. (0, 1, 2, ..)'''
    NUM = len(np.unique(Y))
    Y = keras.utils.to_categorical(Y, num_classes = NUM)
    

    This will result with Y to have a new shape (237, NUM)

    Alternative solution is to use sparse categorical cross entropy which does not need the multi labels to be one-hot encoded and you can use Y (237) directly.