Untitled

mail@pastecode.io avatar
unknown
plain_text
8 months ago
1.1 kB
5
Indexable
Never
# Load results with saved RDS and display accuracy and duration
load_and_show_results <- function(test_data, file_name) {
  # Construct the file path with the provided file name
  file_path <- paste0("Model/", file_name, ".rds")
  
  # Read the predictions and duration from the specified file
  predictions_duration <- readRDS(file_path)

  # Ensure that the test_data$label is numeric
  if (!is.numeric(test_data$label)) {
    stop("test_data$label must be numeric.")
  }
  
  # Convert the numeric predictions back to the original factor levels
  levels <- levels(factor(test_data$label))  # Assumes test_data$label is already numeric
  pred_as_factor <- factor(predictions_duration$test_pred, levels = 0:(length(levels) - 1), labels = levels)

  # Calculate the confusion matrix
  confusion_matrix <- confusionMatrix(pred_as_factor, factor(test_data$label, levels = 0:(length(levels) - 1)))

  # Retrieve accuracy and time
  accuracy <- confusion_matrix$overall['Accuracy']
  time <- predictions_duration$duration

  # Return accuracy and time as a list
  return(list(accuracy = accuracy, time = time))
}