🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.multiply()

AI & DATA SCIENCE // np-multiply

np.multiply() multiplies two arrays (or an array and a scalar) element-wise, and is exactly what the * operator calls on ndarrays — distinct from matrix multiplication.

Syntax

np.multiply(x1, x2)
x1 * x2

Deep Dive Course

np.multiply(a, b) multiplies each element of a by the corresponding element of b, following the usual broadcasting rules — this is often called the Hadamard product in a linear algebra context, to distinguish it explicitly from matrix multiplication. This distinction matters a lot in NumPy specifically: * always means element-wise multiplication for ndarrays, while actual matrix multiplication uses the separate @ operator or np.matmul(), and mixing the two up is one of the most common sources of subtle bugs when translating math notation into NumPy code.

1Understanding np.multiply()

np.multiply(a, b) multiplies each element of a by the corresponding element of b, following the usual broadcasting rules — this is often called the Hadamard product in a linear algebra context, to distinguish it explicitly from matrix multiplication. This distinction matters a lot in NumPy specifically: * always means element-wise multiplication for ndarrays, while actual matrix multiplication uses the separate @ operator or np.matmul(), and mixing the two up is one of the most common sources of subtle bugs when translating math notation into NumPy code.

💡

Never use * expecting matrix multiplication in NumPy — it always multiplies element-wise, broadcasting shapes as needed; use @ or np.matmul() specifically when you mean actual matrix multiplication.

editor.html
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.multiply(a, b))
localhost:3000

2Practical Example

Here is a real-world application of np.multiply() showing how it is used in production NumPy code.

editor.html
import numpy as np

A = np.array([[1, 2], [3, 4]])
B = np.array([[1, 0], [0, 1]])
print(A * B)
print(A @ B)
localhost:3000

3Best Practices

Follow these guidelines when working with np.multiply():

1. Use * (or np.multiply()) only for element-wise multiplication, and @ (or np.matmul()) for true matrix multiplication — never assume they're interchangeable

2. Double-check array shapes before relying on broadcasting in a multiplication, since a shape mismatch can silently produce an unexpected result rather than always raising an error

3. Use np.multiply(a, b, out=result) when writing into a pre-allocated array matters for memory efficiency in a hot loop

⚠️

Tip: Never use * expecting matrix multiplication in NumPy — it always multiplies element-wise, broadcasting shapes as needed; use @ or np.matmul() specifically when you mean actual matrix multiplication.

editor.html
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.multiply(a, b))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.multiply(a, b))
Example 02Advanced Example
import numpy as np

A = np.array([[1, 2], [3, 4]])
B = np.array([[1, 0], [0, 1]])
print(A * B)
print(A @ B)

Best Practices

  • Use * (or np.multiply()) only for element-wise multiplication, and @ (or np.matmul()) for true matrix multiplication — never assume they're interchangeable
  • Double-check array shapes before relying on broadcasting in a multiplication, since a shape mismatch can silently produce an unexpected result rather than always raising an error
  • Use np.multiply(a, b, out=result) when writing into a pre-allocated array matters for memory efficiency in a hot loop

Interview Question

What's the difference between A * B and A @ B for two 2D NumPy arrays?

Hint: Think about element-wise multiplication versus true matrix multiplication.

A * B performs element-wise multiplication, requiring A and B to have the same shape, or shapes compatible via broadcasting, and multiplies corresponding elements together independently. A @ B performs true matrix multiplication, following the standard linear algebra rule where the number of columns in A must match the number of rows in B, computing dot products of rows and columns rather than simply multiplying elements in place. Confusing the two, especially when coming from mathematical notation where juxtaposition usually implies matrix multiplication, is a very common source of bugs in NumPy code.

Exercises

MediumPractice using np.multiply() in a real scenario.
View Solution
import numpy as np

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
print(np.multiply(a, b))

Frequently Asked Questions

What's the difference between A * B and A @ B for two 2D NumPy arrays?

A * B performs element-wise multiplication, requiring A and B to have the same shape, or shapes compatible via broadcasting, and multiplies corresponding elements together independently. A @ B performs true matrix multiplication, following the standard linear algebra rule where the number of columns in A must match the number of rows in B, computing dot products of rows and columns rather than simply multiplying elements in place. Confusing the two, especially when coming from mathematical notation where juxtaposition usually implies matrix multiplication, is a very common source of bugs in NumPy code.

Related Functions

np-dividenp-dotnp-matmul