# Numpy Element Wise Multiplication

- Numpy Element Wise Multiplication Matrix
- Numpy Element Wise Multiplication Python
- Numpy Multiply
- Numpy Element Wise Multiplication In Numpy

- However, it's important to know that NumPy supports several types of matrix multiplication. Element-wise Multiplication You saw some element-wise multiplication already.
- How to get element-wise matrix multiplication (Hadamard product) in, 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 numpy.multiply function is used when we want to compute the multiplication of two array. It returns the product of arr1.
- 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).

Numpy Array – Multiply a constant to all elements of the array Multiplying a constant to a NumPy array is as easy as multiplying two numbers. To multiply a constant to each and every element of an array, use multiplication arithmetic operator. To multiplication operator, pass array. How to get element-wise matrix multiplication (Hadamard product) in, 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 numpy.multiply function is used when we want to compute the multiplication of two array. It returns the product of arr1.

Have you ever tried to multiply two NumPy arrays together and got a result you didn’t expect? NumPy’s multiplication functions can be confusing. In this article, we’ll explain everything you need to know about **matrix multiplication in NumPy.**

Watch the video where I go over the article in detail:

To perform matrix multiplication between 2 NumPy arrays, there are three methods. All of them have simple syntax. Let’s quickly go through them the order of best to worst. First, we have the `@`

operator

Next, `np.matmul()`

And finally `np.dot()`

Why are there so many choices? And which should you choose? Before we answer those questions, let’s have a refresher on matrix multiplication and NumPy’s default behavior.

## What is Matrix Multiplication?

If you don’t know what matrix multiplication is, or why it’s useful, check out this short article.

Matrices and arrays are the basis of almost every area of research. This includes machine learning, computer vision and neuroscience to name a few. If you are working with numbers, you will use matrices, arrays and matrix multiplication at some point.

Now you know why it’s so important, let’s get to the code.

## numpy.array — Default Behavior

The default behavior for any mathematical function in NumPy is element-wise operations. This is one advantage NumPy arrays have over standard Python lists.

Let’s say we have a Python list and want to add 5 to every element. To do this we’d have to either write a for loop or a list comprehension.

Both of these are slow and cumbersome.

Instead, if `A`

is a NumPy array it’s much simpler

And much much much faster

Using arrays is 100x faster than list comprehensions and almost 350x faster than for loops.

If we want to multiply every element by 5 we do the same

The same applies for subtraction and division.

Every mathematical operation acts element wise by default. So if you multiply two NumPy arrays together, NumPy assumes you want to do element-wise multiplication.

A core feature of matrix multiplication is that a matrix with dimension `(m x n)`

can be multiplied by another with dimension `(n x p)`

for some integers `m`

, `n`

and `p`

. If you try this with `*`

, it’s a `ValueError`

This happens because NumPy is trying to do element wise multiplication, not matrix multiplication. It can’t do element wise operations because the first matrix has 6 elements and the second has 8.

Element wise operations is an incredibly useful feature.You will make use of it many times in your career. But you will also want to do matrix multiplication at some point.

Perhaps the answer lies in using the `numpy.matrix`

class?

## Numpy.matrix

There is a subclass of NumPy array called `numpy.matrix`

. This operates similarly to matrices we know from the mathematical world. If you create some `numpy.matrix`

instances and call `*`

, you will perform matrix multiplication

But this causes some issues.

For example, if you have 20 matrices in your code and 20 arrays, it will get very confusing very quickly. You may multiply two together expecting one result but get another. The `*`

operator is overloaded. This results in code that is hard to read full of bugs.

We feel that this is one reason why the Numpy docs v1.17 now say:

*It is no longer recommended to use this class, even for linear algebra. Instead use regular arrays. The class may be removed in the future.*

You may see this recommended in other places around the internet. But, as NumPy no longer recommends it, we will not discuss it further.

Now let’s look at some other methods.

## Other Methods of Matrix Multiplication

There are 2 methods of matrix multiplication that involve function calls.

Let’s start with the one we don’t recommend

## numpy.dot

As the name suggests, this computes the dot product of two vectors. It takes two arguments – the arrays you would like to perform the dot product on. There is a third optional argument that is used to enhance performance which we will not cover.

If you use this function with a pair of 2D vectors, it does matrix multiplication.

This method works but is not recommended by us or NumPy. One reason is because in maths, the * ‘dot product’ *has a specific meaning. It is very different from multiplication. It is confusing to these mathematicians to see

`np.dot()`

returning values expected from multiplication. There are times when you can, and should, use this function (e.g. if you want to calculate the dot product) but, for brevity, we refer you to the official docs.

So you should not use this function for matrix multiplication, what about the other one?

## Numpy.matmul

This is the NumPy **MAT**rix **MUL**tiplication function. Calling it with two matrices as the first and second arguments will return the matrix product.

The function name is clear and it is quite easy to read. This is a vast improvement over `np.dot()`

. There even are some advanced features you can use with this function. But for 90% of cases, this should be all you need. Check the docs for more info.

So is this the method we should use whenever we want to do NumPy matrix multiplication? No. We’ve saved the best ‘till last.

## Python @ Operator

The `@`

operator was introduced to Python’s core syntax from 3.5 onwards thanks to PEP 465. Its only goal is to solve the problem of matrix multiplication. It even comes with a nice mnemonic – `@`

is * for m**AT**rices.

One of the main reasons for introducing this was because there was no consensus in the community for how to properly write matrix multiplication. The asterisk `*`

symbol was competing for two operations:

- element wise multiplication, and
- matrix multiplication.

The solutions were function calls which worked but aren’t very unreadable and are hard for beginners to understand. Plus research suggested that matrix multiplication was more common than `//`

(floor) division. Yet this has its own syntax.

It is unusual that `@`

was added to the core Python language when it’s only used with certain libraries. Fortunately, the only other time we use `@`

is for decorator functions. So you are unlikely to get confused.

It works exactly as you expect matrix multiplication to, so we don’t feel much explanation is necessary.

One thing to note is that, unlike in maths, matrix multiplication using `@`

is *left associative.*

If you are used to seeing

Where A and Z are matrices and x is a vector, you expect the operation to be performed in a right associative manner i.e.

So you perform `Zx`

first and then `A(Zx)`

. But all of Python’s mathematical operations are left associative.

A numerical example

There was no consensus as to which was better. Since everything else in Python is left associative, the community decided to make `@`

left associative too.

So should you use `@`

whenever you want to do NumPy matrix multiplication?

## Which Should You Choose?

There is some debate in the community as to which method is best. However, we believe that you should always use the `@`

operator. It was introduced to the language to solve the exact problem of matrix multiplication. There are many reasons detailed in PEP 465 as to why @ is the best choice.

The main reason we favour it, is that it’s much easier to read when multiplying two or more matrices together. Let’s say we want to calculate `ABCD`

. We have two options

This short example demonstrates the power of the `@`

operator. The mathematical symbols directly translate to your code, there are less characters to type and it’s much easier to read.

Unfortunately, if you use an old version of Python, you’ll have to stick with `np.matmul()`

.

## Summary

You now know how to multiply two matrices together and why this is so important for your Python journey.

If in doubt, remember that `@`

is for m**AT**rix multiplication.

## Numpy Element Wise Multiplication Matrix

## Where To Go From Here?

There are several other NumPy functions that deal with matrix, array and tensor multiplication. If you are doing Machine Learning, you’ll need to learn the difference between them all.

A good place to get a thorough NumPy education is the comprehensive Finxter NumPy tutorial on this blog and our new book Coffee Break NumPy.

Check out the following functions for more info:

- np.vdot – complex-conjugating dot product
- np.tensordot – sum products over arbitrary axes
- np.einsum – Einstein summation convention

## REFERENCES

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

### Daily Data Science Puzzle

[python]

import numpy as np

# graphics data

a = [[1, 1],

[1, 0]]

a = np.array(a)

# stretch vectors

b = [[2, 0],

[0, 2]]

b = np.array(b)

c = a @ b

d = np.matmul(a,b)

print((c d)[0,0])

[/python]

*What is the output of this puzzle?*

Numpy is a popular Python library for data science focusing on arrays, vectors, and matrices.

This puzzle shows an important application domain of matrix multiplication: Computer Graphics.

We create two matrices a and b. The first matrix a is the data matrix (e.g. consisting of two column vectors `(1,1)`

and `(1,0)`

). The second matrix b is the transformation matrix that transforms the input data. In our setting, the transformation matrix simply stretches the column vectors.

## Numpy Element Wise Multiplication Python

More precisely, the two column vectors `(1,1)`

and `(1,0)`

are stretched by factor 2 to `(2,2)`

and `(2,0)`

. The resulting matrix is therefore `[[2,2],[2,0]]`

. We access the first row and second column.

## Numpy Multiply

We use matrix multiplication to apply this transformation. Numpy allows two ways for matrix multiplication: the matmul function and the @ operator.

Comparing two equal-sized numpy arrays results in a new array with boolean values. As both matrices c and d contain the same data, the result is a matrix with only True values.

Are you a master coder?

Test your skills now!

#### Related Video

#### Solution

2

Expert Writer & Content Creator – Data Science & Machine Learning.

—–

I help education companies create engaging blog and video content teaching Data Science to beginners. Unlike my competitors, I learn new concepts every day and so understand what it’s like to be a student.

My articles are easy-to-understand, effective and enjoyable to read. My videos are upbeat, engaging and detailed.

—–

To work with me, please reach out on Upwork

https://tinyurl.com/hire-adam-murphy