Standardize feature columns

Easy8 min10 hidden tests

A credit team trains a model on applicant features such as monthly income and years at the current job. The columns sit on very different scales, so before training the team turns every column into z-scores: how many standard deviations each value lies from its column's mean. Write standardize_columns(X) that does this for every column of X. A column whose values are all equal has no spread, so it becomes all zeros.

Put every feature on one scale

z = (x − mean) / std

On one shared axis, income spreads over 6,800 and years at the job over 11.5. Income makes up over 99.99% of the squared distance between applicants 1 and 6.

On one shared axis, income spreads over 6,800 and years at the job over 11.5. Income makes up over 99.99% of the squared distance between applicants 1 and 6.

Examples

Each column becomes about -1.22, 0 and 1.22

standardize_columns([[40, 1], [60, 3], [80, 5]])

returns [[-1.22474, -1.22474], [0, 0], [1.22474, 1.22474]]

The second column is constant, so it becomes zeros

standardize_columns([[2.5, 7], [3.5, 7], [6.0, 7], [4.0, 7]])

returns [[-1.1767, 0], [-0.392232, 0], [1.56893, 0], [0, 0]]

Requirements

  • X is a 2-D list or array of shape (n, d): n rows (applicants) and d columns (features), with n of 1 or more.
  • Return a float NumPy array of the same shape (n, d).
  • Use each column's mean and its population standard deviation, which divides by n. This is what np.std and scikit-learn's StandardScaler use.
  • A column with a standard deviation of 0 becomes a column of zeros.
  • Leave X unchanged.
The idea behind it

A z-score says how far a value sits from the average, measured in standard deviations: z = (x - mean) / std. Standardizing every feature column this way gives each column a mean of 0 and a standard deviation of 1.

Scale matters to many models. Gradient descent takes lopsided steps when one feature is in the millions and another is below 1. Distance-based models such as k-nearest neighbors let the large feature drown out the rest, and regularization punishes weights unevenly.

In real work you compute the means and standard deviations on the training set only, store them, and apply the same numbers to validation data and live traffic. A constant column carries no information. Dividing by its zero spread would produce NaN (not a number) values that spread through training, so the safe output is a column of zeros.

pythonCtrl+Enter runs
import numpy as np

def standardize_columns(X):
"""Turn every feature column into z-scores.

X: 2-D list or array of shape (n, d), one row per example
Returns a float array of shape (n, d). Each column has its mean
subtracted and is divided by its population standard deviation
(divide by n). A column with zero spread becomes all zeros.
"""
raise NotImplementedError

Stuck? Reveal one rung at a time

Worked solution

Give it a real try first. Then read the solution and why each line is there.