Skip to content

Instantly share code, notes, and snippets.

@sizhky
Created March 15, 2017 13:03
Show Gist options
  • Save sizhky/da0149b81daf0b516a79599658062eae to your computer and use it in GitHub Desktop.
Save sizhky/da0149b81daf0b516a79599658062eae to your computer and use it in GitHub Desktop.
get_metrics <- function(model, test_data, truth, table = FALSE){
preds <- predict(model, test_data);
table_ <- table(truth, preds);
acc_ <- sum(diag(table_))/sum(table_);
rec_ <- table_[2,2]/sum(table_[,2]);
prec_ <- table_[2,2]/sum(table_[2,]);
f_ <- 2*rec_*prec_/(rec_+prec_)
if (table == FALSE){
cat('Accuracy:', acc_, '\nRecall:', rec_, '\nPrecision:', prec_, '\nF-Score:', f_);
} else {
return(table_)
}
# return(c(acc_, rec_, prec_, f_));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment