pub fn categorical_accuracy<F: Float + Debug>(
predictions: &Array<F, Ix2>,
targets: &Array<F, Ix2>,
) -> Result<F>
Expand description
Calculate the categorical accuracy between predictions and targets
§Arguments
predictions
- Predicted class probabilities (each row sums to 1)targets
- One-hot encoded target classes (each row has a single 1)
§Returns
- The accuracy (proportion of correct predictions)
§Examples
use scirs2_neural::utils::categorical_accuracy;
use scirs2_core::ndarray::arr2;
let predictions = arr2(&[
[0.7f64, 0.2, 0.1], // Predicted class: 0
[0.3f64, 0.6, 0.1], // Predicted class: 1
[0.2f64, 0.3, 0.5] // Predicted class: 2
]);
let targets = arr2(&[
[1.0f64, 0.0, 0.0], // True class: 0
[0.0f64, 1.0, 0.0], // True class: 1
[0.0f64, 0.0, 1.0] // True class: 2
]);
let accuracy = categorical_accuracy(&predictions, &targets).unwrap();
assert_eq!(accuracy, 1.0f64); // All predictions are correct