Feature scaling converts each numeric feature from its original range to a common range, here 0 to 1. For a value, use `(oldV - min) / (max - min)`. This matches the output range of sigmoid neurons and prevents differently scaled attributes from entering the network on incompatible scales. section 7.6
Implement min-max scaling by processing the dataset column by column:
- Assume a two-dimensional `[[Double]]` dataset with equally shaped rows. section 7.6
- Find each column’s minimum and maximum values. section 7.6
- Replace every value in place using the min-max formula; `inout` avoids copying the dataset. section 7.6
Scaling classification data
Scale the four numeric attributes of the Iris dataset before training a neural network with four inputs, six hidden neurons, and three outputs. The same preparation also supports the wine classification example, whose attributes use different numeric ranges. section 7.6
/// assumes all rows are of equal length
/// and feature scale each column to be in the range 0–1
func normalizeByFeatureScaling(dataset: inout [[Double]]) {
for colNum in 0..<dataset[0].count {
let column = dataset.map { $0[colNum] }
let maximum = column.max()!
let minimum = column.min()!
for rowNum in 0..<dataset.count {
dataset[rowNum][colNum] = (dataset[rowNum][colNum]
- minimum) / (maximum - minimum)
}
}
}
Use the Swift implementation to scale every feature column in place to the range 0–1. section 7.6




