liveBook · Manning

Classifier

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

A classification problem is a machine-learning problem in which data items are assigned to one of a predefined set of categories.

A classifier assigns an item to one of a predefined set of categories. For animal pictures, those categories might be mammal, reptile, amphibian, fish, and bird. This differs from clustering, which looks for groups without knowing the categories in advance. A neural network is one way to build a classifier; other approaches include decision trees, support vector machines, and naive Bayes classifiers. section 7.6

To turn a neural network into a classifier:

  • Prepare numeric inputs, scaling features recorded on different ranges to a common 0–1 range. section 7.6
  • Encode each known category as an expected output vector, then train the network on inputs paired with those vectors. section 7.5 section 7.6
  • Convert the network’s floating-point outputs back into category labels. Validate against held-out labels to count correct classifications and calculate a percentage. section 7.5 section 7.6

Classifying irises

Each iris supplies four numeric measurements and one of three species labels. A Swift Network with layerStructure: [4, 6, 3] produces three outputs; the position of the largest output selects the predicted species. After training on 140 shuffled samples for 20 passes, the example tests the remaining 10. Its illustrated result is 9 correct out of 10, or 90%. The same pattern extends to wine samples with 13 inputs and three categories. section 7.6

    /// 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)
    }
}

Inspect validate() to see how a caller-supplied interpretOutput function turns network outputs into comparable labels before accuracy is calculated. section 7.5

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

How deep it goes
Works through feature scaling, category encoding, training, output interpretation, and accuracy checks; only names alternative classifier techniques.
Examples
Tests three-species iris classification with four measurements and three-category wine classification with 13 measurements.
What it assumes
Assumes familiarity with Swift functions, arrays, and numeric data while defining classification against clustering.
What you can do after
Build it from scratch with a Swift Network, encoded labels, an output interpreter, and a held-out accuracy check.
How it connects
Connects classification to clustering, feature scaling, neural networks, backpropagation, and validation accuracy.

Also explained in

Compare how 31 books explain classifier →

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.