Center the Data Matrix

~10 mincode completion

Implement center_data(X) that subtracts the column mean from each column.

Examples

Column means become 0

Input
center_data([[1, 4], [3, 6], [5, 8]])
Output
[[-2, -2], [0, 0], [2, 2]]

Already-centered data unchanged

Input
center_data([[-1, 1], [0, 0], [1, -1]])
Output
[[-1, 1], [0, 0], [1, -1]]

Hints

Hint 1

The reduction runs down the columns, so pass .

Hint 2

Watch for this: subtracted global mean not column mean.

Requirements

  • X: Data matrix of shape (n, d)

  • Return Centered matrix of shape (n, d) with each column having mean ≈ 0.

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~10 min

3 employers weight this skill

2 big tech firms, 1 health and bio company. Top match scores 87.

Python
import numpy as np

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

    Args:
        X: Data matrix of shape (n, d)

    Returns:
        Centered matrix of shape (n, d) with each column having mean ≈ 0.
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.