How to do elementwise multiplication of two vectors using NumPy?

Element-wise multiplication of two vectors (also known as the Hadamard product) involves multiplying corresponding elements from each vector to produce a new vector of the same length. In NumPy, this can be easily achieved using either the * operator or the numpy.multiply function.

Here's how you can perform element-wise multiplication with NumPy:

1. Using the * Operator

The simplest way to perform element-wise multiplication is by using the * operator between two NumPy arrays (vectors).

Example:

import numpy as np # Define two vectors vector_a = np.array([1, 2, 3, 4]) vector_b = np.array([5, 6, 7, 8]) # Element-wise multiplication result = vector_a * vector_b print("Vector A:", vector_a) print("Vector B:", vector_b) print("Element-wise multiplication:", result)

Output:

Vector A: [1 2 3 4]
Vector B: [5 6 7 8]
Element-wise multiplication: [ 5 12 21 32]

2. Using numpy.multiply

Alternatively, you can use the numpy.multiply function to achieve the same result.

Example:

import numpy as np # Define two vectors vector_a = np.array([1, 2, 3, 4]) vector_b = np.array([5, 6, 7, 8]) # Element-wise multiplication using numpy.multiply result = np.multiply(vector_a, vector_b) print("Element-wise multiplication:", result)

Output:

Element-wise multiplication: [ 5 12 21 32]

Important Considerations

  1. Same Shape or Broadcastable Shapes:
    • For element-wise multiplication to work, the two vectors must have the same shape.
    • Alternatively, NumPy's broadcasting rules can apply if the shapes are compatible. Broadcasting allows NumPy to perform operations on arrays of different shapes under certain conditions.

Example with Broadcasting:

import numpy as np # Define a vector and a scalar vector_a = np.array([1, 2, 3, 4]) scalar = 10 # Element-wise multiplication using broadcasting result = vector_a * scalar print("Element-wise multiplication with scalar:", result)

Output:

Element-wise multiplication with scalar: [10 20 30 40]
  1. Data Types:

    • Ensure that the vectors are of numeric types to perform multiplication. Mixing incompatible types can lead to errors or unintended results.
  2. Immutable vs. Mutable Operations:

    • Both * operator and numpy.multiply return a new array and do not modify the original vectors. If you wish to store the result, assign it to a new variable as shown in the examples.

Complete Function Example

If you prefer to encapsulate the operation within a function, here's how you can do it:

import numpy as np def elementwise_multiply(vec1, vec2): """ Multiplies two vectors element-wise. Parameters: vec1 (array-like): First vector. vec2 (array-like): Second vector. Returns: numpy.ndarray: Element-wise multiplication result. """ arr1 = np.array(vec1) arr2 = np.array(vec2) if arr1.shape != arr2.shape: raise ValueError("Both vectors must have the same shape for element-wise multiplication.") return arr1 * arr2 # Usage vector_a = [1, 2, 3, 4] vector_b = [5, 6, 7, 8] result = elementwise_multiply(vector_a, vector_b) print("Element-wise multiplication:", result)

Output:

Element-wise multiplication: [ 5 12 21 32]

Summary

  • Operator *: Simple and intuitive for element-wise multiplication.
  • Function numpy.multiply: Offers more flexibility and can be useful in more complex expressions.
  • Broadcasting: Allows for element-wise operations on arrays of different shapes when compatible.
  • Ensure Same Shape: To avoid unexpected results or errors, make sure the vectors are of the same shape or compatible for broadcasting.

By using these methods, you can efficiently perform element-wise multiplication on vectors using NumPy in Python.

TAGS
Coding Interview
System Design Interview
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Who has the final say in an interview?
How do we write an algorithm?
What is the advantage of Twilio?
What defines a good ux designer?
What is system design concepts?
What is the goal of system design interview?
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.8
(1,192 learners)
Discounted price for Your Region

$123

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
4.1
Discounted price for Your Region

$72

Design Gurus logo
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.