How to multiply two numpy arrays in python

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:

In [1]:

import numpy as np

a = np.array([1, 2, 3]) b = a + 2 print(b)

In [2]:

a = np.array([1, 2, 3])
b = np.array([2, 4, 6])
c = a + b
print(c)

Scalar Multiplication

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

In [3]:

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

In [4]:

a = np.array([10,20,30])
b = a/2
print(b)

Array Multiplication

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.

In [5]:

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
a * b

Dot Product

In [6]:

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
np.dot(a,b)

Cross Product

In [7]:

a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
np.cross(a, b)

Exponents and Logarithms

np.exp()

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

In [8]:

a = np.array([1, 2, 3])
np.exp(a)

Out[8]:

array([ 2.71828183,  7.3890561 , 20.08553692])

Logarithms

NumPy has three logarithmic functions.

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

Trigonometry

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

In [12]:

import numpy as np
np.set_printoptions(4)

a = np.array([0, np.pi/4, np.pi/3, np.pi/2])
print(np.sin(a))
print(np.cos(a))
print(np.tan(a))
print(f"Sides 3 and 4, hypotenuse {np.hypot(3,4)}")

[0.     0.7071 0.866  1.    ]
[1.0000e+00 7.0711e-01 5.0000e-01 6.1232e-17]
[0.0000e+00 1.0000e+00 1.7321e+00 1.6331e+16]
Sides 3 and 4, hypotenuse 5.0

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

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

In [13]:

a = np.array([np.pi,2*np.pi])
np.rad2deg(a)

In [14]:

a = np.array([0,90, 180, 270])
np.deg2rad(a)

Out[14]:

array([0.    , 1.5708, 3.1416, 4.7124])

Can you multiply two NumPy arrays?

multiply() function is used when we want to compute the multiplication of two array. It returns the product of arr1 and arr2, element-wise.

How do you multiply multiple arrays in Python?

You can use np. multiply to multiply two same-sized arrays together. This computes something called the Hadamard product. In the Hadamard product, the two inputs have the same shape, and the output contains the element-wise product of each of the input values.

How do you multiply two arrays together?

C = A . * B multiplies arrays A and B by multiplying corresponding elements. The sizes of A and B must be the same or be compatible. If the sizes of A and B are compatible, then the two arrays implicitly expand to match each other.

How does Python multiply arrays?

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.