1

I'm using ANN for a multi-class classifier for a problem where the target variable can take 3 values: -1,0,1.

I have used OneHotEncoder to convert my output variable using the following code-

labelencoder_y = LabelEncoder()
y = labelencoder_y.fit_transform(y) 
uniques, ids = np.unique(y, return_inverse=True)

And it is in following format now-

array([[0, 0, 1],[1, 0, 0],[0, 1, 0]....

And the prediction looks like-

array([[0, 0.3, 0.7],[0.6, 0.3, 0.1],[0.1, 0.5, 0.4]...

I want to use grid search cv to increase my accuracy but I am having a hard time to do that. I always get an error. This the code I am using-

def build_classifier():
    classifier = Sequential()
    classifier.add(Dense(activation = "relu", input_dim = 28, units = 32, use_bias=True, kernel_initializer= 'normal'))
    classifier.add(Dense(activation="softmax", units = 3, kernel_initializer = 'normal'))
    classifier.compile(optimizer= 'adam', loss= 'categorical_crossentropy', metrics= ['accuracy'])
    return classifier

classifier = KerasClassifier(build_fn = build_classifier)
parameters = {'batch_size': [25,32],
              'epochs' : [50]
              }

grid_search = GridSearchCV(estimator= classifier, param_grid= parameters, scoring= 'f1_micro', cv=10,return_train_score = True)
grid_search = grid_search.fit(X_train,y_train)

And I get the following error-

ValueError: Can't handle mix of multilabel-indicator and multiclass

I know the problem is the scoring argument in the GridSearchCV(...). I can't find the suitable parameter for it. I tried making my own loss function using this:

def my_check(y_true, y_pred):
    y_true = np.take(uniques,np.argmax(y_true,1))
    y_pred = np.take(uniques,np.argmax(y_pred,1))
    accuracy = accuracy_score(y_true, y_pred)
    return accuracy 

score = make_scorer(my_check, greater_is_better=True)

But I again get an error:

ValueError: axis(=1) out of bounds

Any idea how should I proceed?

kenny_k
  • 3,831
  • 5
  • 30
  • 41
user7400738
  • 443
  • 2
  • 6
  • 18

0 Answers0