Loop Versus Vectorized Standardization

~15 mincode completion

Implement two functions that return the same array:

  • standardize_loop(X) using plain Python loops over rows and columns (no , or broadcasting);
  • standardize_vectorized(X) using , and broadcasting, with no Python loop.

Where this shows up

~15 min

8 employers weight this skill

3 quant funds, 2 big tech firms, 1 frontier lab, 1 autonomy company, 1 enterprise vendor. Top match scores 81.

Python
import numpy as np


def standardize_loop(X):
    """
    Column-wise z-scores, computed one number at a time.

    Args:
        X: 2D array of shape (n, d)

    Returns:
        array of shape (n, d)
    """
    # YOUR CODE HERE
    pass


def standardize_vectorized(X):
    """
    Column-wise z-scores, computed with whole-array operations.

    Args:
        X: 2D array of shape (n, d)

    Returns:
        array of shape (n, d)
    """
    # YOUR CODE HERE
    pass



def _max_disagreement(X):
    """Largest absolute difference between the two versions."""
    a = np.asarray(standardize_loop(X), dtype=float)
    b = np.asarray(standardize_vectorized(X), dtype=float)
    return float(np.max(np.abs(a - b)))
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.