Numpy Element Wise Multiplication

2/11/2022by admin

Python element-wise multiplication. Let us see how we can multiply element wise in python. In python, element-wise multiplication can be done by importing numpy. To multiply two equal-length arrays we will use np.multiply and it will multiply element-wise. Example: import numpy as np m1 = 3, 5, 1 m2 = 2, 1, 6 print(np.multiply(m1, m2)). Nov 21, 2020 which is the matrix product, not the element-wise product. How can I get the the element-wise product (aka Hadamard product) using built-in functions? How to solve the problem: Solution 1: For elementwise multiplication of matrix objects, you can use numpy.multiply: import numpy as np a = np.array(1,2,3,4) b = np.array(5,6,7,8) np. The np.multiply (x1, x2) method of the NumPy library of Python takes two matrices x1 and x2 as input, performs element-wise multiplication on input, and returns the resultant matrix as input. Therefore, we need to pass the two matrices as input to the np.multiply method to perform element-wise input.

Array Operations

Mathematical operations can be completed using NumPy arrays.

Scalar Addition

Scalars can be added and subtracted from arrays and arrays can be added and subtracted from each other:

Element wise array multiplication in NumPy In this section, I will discuss two methods for doing element wise array multiplication for both 1D and 2D. The first method is using the numpy.multiply and the second method is using asterisk (.) sign. Multiplication of 1D array. However, it's important to know that NumPy supports several types of matrix multiplication. Element-wise Multiplication You saw some element-wise multiplication already.

Scalar Multiplication

NumPy arrays can be multiplied and divided by scalar integers and floats:

Array Multiplication

Numpy element wise multiplication scalar

NumPy array can be multiplied by each other using matrix multiplication. These matrix multiplication methods include element-wise multiplication, the dot product, and the cross product.

Element-wise Multiplication

The standard multiplication sign in Python * produces element-wise multiplication on NumPy arrays.

Dot Product

Numpy Element Wise Multiplication Of Arrays

Cross Product

Exponents and Logarithms


NumPy's np.exp() function produces element-wise e^x exponentiation.


NumPy has three logarithmic functions.

  • np.log() - natural logarithm (log base e)
  • np.log2() - logarithm base 2
  • np.log10() - logarithm base 10


NumPy also contains all of the standard trigonometry functions which operate on arrays.

  • np.sin() - sin
  • np.cos() - cosine
  • np.tan() - tangent
  • np.asin() - arc sine
  • np.acos() - arc cosine
  • np.atan() - arc tangent
  • np.hypot() - given sides of a triangle, returns hypotenuse

NumPy contains functions to convert arrays of angles between degrees and radians.

  • deg2rad() - convert from degrees to radians
  • rad2deg() - convert from radians to degrees

NumPy is a popular Python library for data science. Numpy focuses on array, vector, and matrix computations. If you work with data, you cannot avoid NumPy. So learn it now and learn it well.

In this tutorial, you’ll learn how to calculate the Hadamard Product (= element-wise multiplication) of two 1D lists, 1D arrays, or even 2D arrays in Python using NumPy’s np.multiply() and the asterisk operator.

Element-Wise Multiplication of Flat Python Lists

Problem Formulation: How does element-wise multiplication of two lists or NumPy arrays a and b work with Python’s NumPy library?

Answer: Use the star (asterisk) operator a * b.

The np.multiply() function multiplies list element a[i] with element b[i] for a given index i and stores the result in a new NumPy array.

Element-Wise Multiplication of NumPy Arrays with the Asterisk Operator *

If you start with two NumPy arrays a and b instead of two lists, you can simply use the asterisk operator * to multiply a * b element-wise and get the same result:

But this does only work on NumPy arrays—and not on Python lists!

Element-Wise Multiplication of 2D NumPy Arrays

Here is a code example from my new NumPy book “Coffee Break NumPy”:

We consider salary data of four jobs:

  • data scientist,
  • product manager,
  • designer, and
  • software engineer.

We create four lists that store the yearly average salary of the four jobs in thousand dollars for three subsequent years.

We merge these four lists into a two-dimensional array (the matrix). You can think of it as a list of lists, or as a table. Each salary list of a single job becomes a row of this matrix. Each row has three columns, one for each year.

Numpy Element Wise Multiplication Matrix

Now suppose, your company changes the salary for the different job descriptions. For example, data scientists get a salary raise of 30% in 2017.

In the code, we create a second matrix that stores the salary changes as weights. Then, we update the salaries according to these weights. As designers in 2015 got a salary decrease, i.e., the weight is smaller than 1.0, the new salary is smaller than the old salary.

Note that the simple multiplication asterisk operator * creates a new matrix by multiplying the two values at position (i,j) of the two matrices.

Numpy Element Wise Product

NumPy Element-Wise Multiplication Puzzle

Can you guess the output of this puzzle?
*Advanced Level* (see solution below)

Numpy Element Wise Multiplication

Numpy Element Wise Multiplication And Sum

Are you a master coder?
Test your NumPy skills now by solving this code puzzle!

Where to Go From Here?

This puzzle is loosely based on my new book “Coffee Break NumPy”. My idea in writing the “Coffee Break” series is to deliver continuous improvements in Python — while not taking more time than your daily coffee break.

Do you want to become a NumPy master? Check out our interactive puzzle book Coffee Break NumPy and boost your data science skills! (Amazon link opens in new tab.)

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

To help students reach higher levels of Python success, he founded the programming education website He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.

Related Posts

Comments are closed.