liveBook · Manning

Accuracy

As explained in Classic Computer Science Problems in Swift: Essential techniques for practicing programmers

Accuracy is the proportion of evaluated examples that a classifier assigns to the correct category.

Accuracy is the share of test inputs a classifier assigns to the correct category. For a trained neural network, compare each predicted category with its expected category, count the matches, and divide by the number of test inputs. Multiply that fraction by 100 to display a percentage. section 7.5

    /// for generalized results that require classification
    /// this function will return the correct number of trials
    /// and the percentage correct out of the total
    func validate<T: Equatable>(inputs:[[Double]], expecteds:[T],
     interpretOutput: ([Double]) -> T)
     -> (correct: Int, total: Int, percentage: Double) {
        var correct = 0
        for (input, expected) in zip(inputs, expecteds) {
            let result = interpretOutput(outputs(input: input))
            if result == expected {
                correct += 1
            }
        }
        let percentage = Double(correct) / Double(inputs.count)
        return (correct, inputs.count, percentage)
    }
}

The Swift `validate()` function runs each input through the network, converts its numeric output into a comparable category with `interpretOutput`, and returns `(correct, total, percentage)`. Unlike `train()`, it does not update weights. section 7.5

To measure accuracy on a classification problem:

  • Train the network on inputs paired with expected outputs, then reserve other inputs and their labels for testing. section 7.5 section 7.6
  • Provide an `interpretOutput` function that turns the network’s floating-point outputs into the same kind of label as the expected result. For iris classification, it selects a species from three output positions. section 7.5 section 7.6
  • Pass the test inputs, expected labels, and interpreter to `validate()`. Report both the count correct and the percentage, so the size of the test is visible. section 7.5 section 7.6

Two classification checks

An iris network trained on 140 shuffled specimens is tested on the remaining 10; a sample result is 9 correct of 10, or 90%. A wine classifier reports 27 correct of 28, or about 96.43%. These percentages describe the tested inputs, not the training error printed during weight updates. section 7.6 section 7.5

Results can change between runs because the network starts with random weights. If accuracy is suboptimal, experiment with the learning rate, hidden-layer size, or number of training iterations. Another exercise is to change the activation function and check how that affects accuracy and training time. section 7.6 section 7.9

How Classic Computer Science Problems in Swift: Essential techniques for practicing programmers covers it

How deep it goes
Implements the correct-count calculation in Swift and works through test results, without developing a broader metric comparison.
Examples
Uses iris classification at 9 of 10 correct and wine classification at 27 of 28 correct.
What it assumes
Assumes a trained neural network and an output interpreter that maps numeric outputs to comparable labels.
What you can do after
Apply it via Swift `validate()` to report correct counts and percentages, then compare results after tuning network settings.
How it connects
Connects neural-network validation to output interpretation, training error, and hyperparameter tuning.

Also explained in

Compare how 32 books explain accuracy →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Classic Computer Science Problems in Swift: Essential techniques for practicing programmers. The section links point to the parts of the book it draws on.