multiplication of two arrays in numpy

  • Last Update :
  • Techknowledgy :

Last Updated : 16 May, 2020

1 st Input number: 4
2n d Input number: 6
output number: 24

1 st Input array: [
   [2 - 7 5]
   [-6 2 0]
]
2n d Input array: [
   [0 - 7 8]
   [5 - 2 9]
]
Resultant output array: [
   [0 49 40]
   [-30 - 4 0]
]

1 st Input number: 4
2n d Input number: 6
output number: 24

Output :

1 st Input array: [
   [2 - 7 5]
   [-6 2 0]
]
2n d Input array: [
   [0 - 7 8]
   [5 - 2 9]
]
Resultant output array: [
   [0 49 40]
   [-30 - 4 0]
]

Suggestion : 2

Input arrays to be multiplied. If x1.shape != x2.shape, they must be broadcastable to a common shape (which becomes the shape of the output).,A location into which the result is stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned. A tuple (possible only as a keyword argument) must have length equal to the number of outputs.,The * operator can be used as a shorthand for np.multiply on ndarrays.,Equivalent to x1 * x2 in terms of array broadcasting.

>>> np.multiply(2.0, 4.0)
8.0
>>> x1 = np.arange(9.0).reshape((3, 3)) >>>
   x2 = np.arange(3.0) >>>
   np.multiply(x1, x2)
array([
   [0., 1., 4.],
   [0., 4., 10.],
   [0., 7., 16.]
])
>>> x1 = np.arange(9.0).reshape((3, 3)) >>>
   x2 = np.arange(3.0) >>>
   x1 * x2
array([
   [0., 1., 4.],
   [0., 4., 10.],
   [0., 7., 16.]
])

Suggestion : 3

You can use the numpy np.multiply() function to perform the elementwise multiplication of two arrays. You can also use the * operator as a shorthand for np.multiply() on numpy arrays. The following is the syntax:,Alternatively, you can also use the * operator to perform the same elementwise multiplication operation.,You can also perform this operation on higher-dimensional arrays. For example, let’s multiply two 2d numpy arrays elementwise.,Here, we created two one-dimensional numpy arrays of the same shape and then performed an elementwise multiplication. You can see that the resulting array, x3 has values resulting from the elementwise multiplication of values in x1 and x2.

You can use the numpy np.multiply() function to perform the elementwise multiplication of two arrays. You can also use the * operator as a shorthand for np.multiply() on numpy arrays. The following is the syntax:

import numpy as np
# x1 and x2 are numpy arrays of the same dimensions
# elementwise multiplication
x3 = np.multiply(x1, x2)
# elementwise multiplication using *
   x3 = x1 * x2

Elementwise multiply two 1d arrays

import numpy as np

# create two 1 d numpy arrays
x1 = np.array([1, 2, 0, 5])
x2 = np.array([3, 1, 7, 1])
# multiply x1 and x2 elementwise
x3 = np.multiply(x1, x2)
# display the arrays
print("x1:", x1)
print("x2:", x2)
print("x3:", x3)

Output:

x1: [1 2 0 5]
x2: [3 1 7 1]
x3: [3 2 0 5]

You can also perform this operation on higher-dimensional arrays. For example, let’s multiply two 2d numpy arrays elementwise.

# create two 2 d numpy arrays
x1 = np.array([
   [1, 2],
   [0, 5]
])
x2 = np.array([
   [3, 1],
   [7, 1]
])
# multiply x1 and x2 elementwise
x3 = np.multiply(x1, x2)
# display the arrays
print("x1:\n", x1)
print("x2:\n", x2)
print("x3:\n", x3)

Now let’s do the same operation but using the * operator on arrays x1 and x2 –

# multiply x1 and x2 elementwise using *
   x3 = x1 * x2
# display the arrays
print("x1:\n", x1)
print("x2:\n", x2)
print("x3:\n", x3)

Suggestion : 4
conda install numpy
pip install numpy
import numpy as np
import numpy as np

A = [
   [6, 7],
   [8, 9]
]

print(np.array(A)[0, 0])
import numpy as np

A = 5

B = [
   [6, 7],
   [8, 9]
]

print(np.dot(A, B))
import numpy as np

A = [
   [6, 7],
   [8, 9]
]

B = [
   [1, 3],
   [5, 7]
]

print(np.dot(A, B))

print("----------")

print(np.dot(B, A))