NumPy is a popular Python library for data science. Numpy focuses on
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
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])
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
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]] '''
- data scientist,
- product manager,
- designer, and
- software engineer.
We create four lists that store the yearly average salary of the four jobs in
We merge these four lists into a two-dimensional array (the
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.)
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 Finxter.com. 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.