[Numpy * Operator] Element-wise Multiplication in Python

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.

>>> import numpy as np
>>> a = [1, 2, 3]
>>> b = [2, 1, 1]
>>> np.multiply(a, b)
array([2, 2, 3])

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:

>>> a = np.array([1, 2, 3])
>>> b = np.array([2, 1, 1])
>>> a * b
array([2, 2, 3])

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”:

import numpy as np

# salary in ($1000) [2015, 2016, 2017]
dataScientist = [133, 132, 137]
productManager = [127, 140, 145]
designer = [118, 118, 127]
softwareEngineer = [129, 131, 137]

# Salary matrix
S = np.array([dataScientist,
              productManager,
              designer,
              softwareEngineer])

# Salary increase matrix
I = np.array([[1.1, 1.2, 1.3],
              [1.0, 1.0, 1.0],
              [0.9, 0.8, 0.7],
              [1.1, 1.1, 1.1]])

# Updated salary
S2 = S * I
print(S2)
'''
Output:
[[146.3 158.4 178.1]
 [127.  140.  145. ]
 [106.2  94.4  88.9]
 [141.9 144.1 150.7]]
'''

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.

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 Multiplication Puzzle

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

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.)

Coffee Break NumPy

Leave a Comment