Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Balanced accuracy values in tensorflow vs Caret/SKLEARN values #1

Open
DohaNaga opened this issue Nov 30, 2021 · 0 comments
Open

Balanced accuracy values in tensorflow vs Caret/SKLEARN values #1

DohaNaga opened this issue Nov 30, 2021 · 0 comments

Comments

@DohaNaga
Copy link
Owner

DohaNaga commented Nov 30, 2021

Slightly different balanced accuracy values calculated within tensorflow than the ones calculated by the regular confusion matrix from the caret package. Possible cause is k_round.
The balanced accuracy used for training the models was calculated as follows:

Tensorflow

balanced_acc <- custom_metric("balanced_acc",function(y_true,y_pred){
  y_pred_pos = k_round(k_clip(y_pred, 0, 1))
  y_pred_neg = 1 - y_pred_pos
  
  y_pos = k_round(k_clip(y_true, 0, 1))
  y_neg = 1 - y_pos
  
  tp = k_sum(y_pos * y_pred_pos)
  tn = k_sum(y_neg * y_pred_neg)
  
  fp = k_sum(y_neg * y_pred_pos)
  fn = k_sum(y_pos * y_pred_neg)
  
  sensi = (tp/(tp + fn + k_epsilon()))
  specifi = (tn/(tn + fp + k_epsilon()))
  
  return((sensi + specifi )/ 2 )
  
})


##or 

balanced_acc <- custom_metric("balanced_acc",function(y_true,y_pred){

  tp = k_sum(k_round(k_clip(y_true * y_pred, 0, 1)))
  tn =  k_sum(k_round(k_clip((1 - y_true) * (1 - y_pred), 0, 1)))
  
  fp = k_sum(k_round(k_clip((1 - y_true) * y_pred, 0, 1)))
  fn = k_sum(k_round(k_clip(y_true * (1 - y_pred), 0, 1)))
  
  sensi = (tp/(tp + fn + k_epsilon()))
  specifi = (tn/(tn + fp + k_epsilon()))
  
  return((sensi + specifi )/ 2 )
  
})

The balanced accuracy used for evaluating the models was calculated as follows:

caret

  conf.matrix <-  caret::confusionMatrix(
    factor(pred_model, levels = 0:1),
    factor(y_test, levels = 0:1),
    positive = "1"
  )
  
  BalancedAccuracy <- conf.matrix$byClass[11]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant