How do you compute a dot product in NumPy, and how does @ differ from element-wise multiplication with *?

Quick Overview

Asks how to compute the dot product of two NumPy arrays and how the @ operator differs from element-wise multiplication with *. Tests knowledge of output shapes, broadcasting rules, and where np.matmul and np.dot behave differently for higher-dimensional arrays.

How do you compute a dot product in NumPy, and how does @ differ from element-wise multiplication with *?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

You have two NumPy arrays and want their dot product. How do you compute it, and how does the `@` operator differ from element-wise multiplication with `*`? Say what each one returns for 1-D vectors and for 2-D matrices. ```hint Compare output shapes Take two length-3 vectors and two 2x2 matrices and write down the shape of the result under each operator. Ask which operation removes an axis. ``` ### What a Strong Answer Covers - Which function and operator compute a dot product, and what they return for 1-D and 2-D inputs - The shape rule for a matrix product compared with the broadcasting rules that `*` follows - How `@` (`np.matmul`) differs from `np.dot` for arrays above 2-D and for scalars - A small worked example showing the result of each operation - A shape pitfall such as mixing a 1-D array with a column vector ### Follow-up Questions - What shape does `A @ B` return for arrays of shape `(32, 64, 128)` and `(32, 128, 10)`, and what would `np.dot(A, B)` return? - When would you reach for `np.einsum` instead of `@`? - Why is `a @ b` usually faster than `(a * b).sum()` on large float vectors? - What does `*` do on `np.matrix` objects, and why is that class discouraged?

Overview: Asks how to compute the dot product of two NumPy arrays and how the @ operator differs from element-wise multiplication with *. Tests knowledge of output shapes, broadcasting rules, and where np.matmul and np.dot behave differently for higher-dimensional arrays.

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

You have two NumPy arrays and want their dot product. How do you compute it, and how does the @ operator differ from element-wise multiplication with *? Say what each one returns for 1-D vectors and for 2-D matrices.

What a Strong Answer Covers Guidance

  • Which function and operator compute a dot product, and what they return for 1-D and 2-D inputs
  • The shape rule for a matrix product compared with the broadcasting rules that * follows
  • How @ ( np.matmul ) differs from np.dot for arrays above 2-D and for scalars
  • A small worked example showing the result of each operation
  • A shape pitfall such as mixing a 1-D array with a column vector

Follow-up Questions Guidance

  • What shape does A @ B return for arrays of shape (32, 64, 128) and (32, 128, 10) , and what would np.dot(A, B) return?
  • When would you reach for np.einsum instead of @ ?
  • Why is a @ b usually faster than (a * b).sum() on large float vectors?
  • What does * do on np.matrix objects, and why is that class discouraged?