Project one vector onto another using NumPy dot products

Quick Overview

Implement the vector projection of one array onto another in NumPy using dot products and broadcasting, with no Python loops. Tests the geometry behind projections, shape handling for array-like inputs, and numerical care when the target vector is close to zero.

Project one vector onto another using NumPy dot products

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `project(v, u)` returning the vector projection of `v` onto `u`: `((v·u)/(u·u)) * u`. Requirements: - Compute both inner products with `np.dot` (no Python loops). - Return a NumPy array of the same shape as `u`. - Accept 1-D array-like inputs of equal length. - Assume `u` is nonzero (`u·u > 0`). Example: ```python v = np.array([2.0, 3.0]) u = np.array([1.0, 0.0]) project(v, u) # -> array([2., 0.]) ``` ```hint Check your result Whatever is left over after projecting, `v - project(v, u)`, should have no component along `u`. Use that to test your answer. ``` ### What a Strong Answer Covers - Computes both inner products with `np.dot`, forms the scalar coefficient, and scales `u` by it without a Python loop. - Returns an array with the shape of `u` and reproduces the example. - Explains why the formula gives the projection, for example through the orthogonality of the residual. - Handles input issues such as list inputs, integer dtypes, mismatched lengths and a zero or near-zero `u`. ### Follow-up Questions - How would you project onto a whole basis of vectors at once? - What if `u` could be near-zero? - What changes if the vectors are complex-valued?

Overview: Implement the vector projection of one array onto another in NumPy using dot products and broadcasting, with no Python loops. Tests the geometry behind projections, shape handling for array-like inputs, and numerical care when the target vector is close to zero.

PracHub logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement project(v, u) returning the vector projection of v onto u: ((v·u)/(u·u)) * u.

Requirements:

  • Compute both inner products with np.dot (no Python loops).
  • Return a NumPy array of the same shape as u .
  • Accept 1-D array-like inputs of equal length.
  • Assume u is nonzero ( u·u > 0 ).

Example:

v = np.array([2.0, 3.0])
u = np.array([1.0, 0.0])
project(v, u)  # -> array([2., 0.])

What a Strong Answer Covers Guidance

  • Computes both inner products with np.dot , forms the scalar coefficient, and scales u by it without a Python loop.
  • Returns an array with the shape of u and reproduces the example.
  • Explains why the formula gives the projection, for example through the orthogonality of the residual.
  • Handles input issues such as list inputs, integer dtypes, mismatched lengths and a zero or near-zero u .

Follow-up Questions Guidance

  • How would you project onto a whole basis of vectors at once?
  • What if u could be near-zero?
  • What changes if the vectors are complex-valued?