ML Grind

PCA Step 1: Center the Data - Unsupervised Learning

Implement PCA Step 1: Center the Data in Python with NumPy. easy exercise with examples, hints, and instant feedback. No setup or signup required.

PCA Step 1: Center the Data

What you'll build

The first step of Principal Component Analysis: subtract each feature's mean so every column of the data has mean 0.

Background

PCA looks for the directions along which the data varies most, which are the eigenvectors of the covariance matrix Xc^T Xc / (n - 1). That formula is only the covariance if Xc is centred. Skip this step and the first "principal component" points from the origin towards the data's mean instead of along its spread. scikit-learn's PCA centres automatically and stores the mean as mean_, which it reuses to transform new data.

Formula

mu[f]     = (1/n) * sum_i X[i, f]          # column means, shape (n_features,)
Xc[i, f]  = X[i, f] - mu[f]

Inputs

  • X: data matrix of shape (n_samples, n_features), ints or floats (tests may pass nested lists)

Expected return format

A float array with the same shape as X, whose column means are all 0 (up to floating-point error).

Edge cases and conventions

  • Means are taken per column (axis=0), not over the whole matrix.
  • Already-centred data comes back unchanged; a single column works the same way.
  • Don't modify the input; return a new array.

Constraints

  • NumPy only, no loops: broadcasting subtracts the (n_features,) mean from every row.

Follow-up

  • Finish PCA: compute the covariance, take np.linalg.eigh, sort the eigenvalues in descending order and project onto the top k eigenvectors. Why is the SVD of Xc often preferred?
  • At inference time, which mean do you subtract from a new sample?

Examples

Input: center_data(np.array([[1, 2], [3, 4], [5, 6]]))
Output: [[-2.0, -2.0], [0.0, 0.0], [2.0, 2.0]]

The column means are [3, 4]; subtracting them from every row centres each column.

Input: center_data(np.array([[10], [20], [30]]))
Output: [[-10.0], [0.0], [10.0]]

A single feature with mean 20.

Practice in Python

import numpy as np

def center_data(X: np.ndarray) -> np.ndarray:
    """
    Center the data by subtracting the mean of each feature (column).

    Args:
        X: Data matrix of shape (n_samples, n_features)

    Returns:
        Float array of the same shape whose columns have mean 0
    """
    # Your code here
    pass

The interactive editor loads with Python and NumPy. Run your implementation against 6 tests, or use the progressive hints.

More exercises in this section →