1

I want to implement a binary classification model using Gaussian process. According to the official documentation, I had the code as below.

The X has 2048 features and Y is either 0 or 1. After optimizing the model, I was trying to evaluate the performance.

However, the predict_y method yields a weird result; the expected pred should have a shape like (n_test_samples, 2), which represents the probability of being class 0 and 1. But the result I got instead is (n_test_samples, n_training_samples).

What is going wrong?

def model(X,Y):
  '''
    X: (n_training_samples, n_features)  , my example is (n, 2048)
    Y: (n_training_samples,) , binary classification
  '''
  m = gpflow.models.VGP(
      (X, Y), likelihood=gpflow.likelihoods.Bernoulli(), kernel=gpflow.kernels.SquaredExponential()
  )

  opt = gpflow.optimizers.Scipy()
  opt.minimize(m.training_loss, variables=m.trainable_variables)

  return m

def evaluate(model,X,Y,accuracy, MCC, Kappa):
  '''
    X: (n_test_samples, n_features)  , my example is (n, 2048)
    Y: (n_test_samples,) , binary classification
  '''
  pred,_ = model.predict_y(X)
  print('pred.shape is {}'.format(pred)) # I got wired result (num of test samples <X.shape[0]>, num of training samples)
  accuracy += [accuracy_score(Y, pred)]
  MCC += [matthews_corrcoef(Y, pred)]
  Kappa += [cohen_kappa_score(Y, pred)]
  return accuracy, MCC, Kappa
desertnaut
  • 57,590
  • 26
  • 140
  • 166
Hopkins
  • 59
  • 1
  • 6
  • 1
    Please post a [mre], trimming away all unnecessary details (accuracies, kappa coefficients, wrapping stuff into functions etc) and including sample data and the necessary imports. – desertnaut Jul 17 '21 at 09:31
  • As pointed out by @desertnaut, posting a minimal reproducible example is crucial to make it easy for people to actually help you! Just looking at your docstring, it looks like the shape of your `Y` observations is wrong - it should always be rank 2, if you only have a single output, it should be of shape (n_training_samples, 1). – STJ Jul 19 '21 at 09:32

1 Answers1

0

I finally figured it out. The reason is the Y for VGP model should have a shape like (n_training_samples, 1) instead of (n_training_samples,).

Hopkins
  • 59
  • 1
  • 6