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






























