liveBook · Manning

Feature scaling

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

Feature scaling is the transformation of a feature's values from an original numeric range to a common scale such as 0 to 1.

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

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

How deep it goes
Defines min-max scaling, explains its range alignment, and implements column-wise in-place mutation for two-dimensional Double arrays.
Examples
Uses Iris plant measurements and wine attributes as scaled classification inputs.
What it assumes
Assumes two-dimensional Double arrays and teaches the min-max transformation directly.
What you can do after
Apply it via normalizeByFeatureScaling to transform a two-dimensional Swift dataset before training.
How it connects
Connects normalization with neural networks, sigmoid activation, classification, and KMeans.

Also explained in

Compare how 5 books explain feature scaling →

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.