Posted on: March 12, 2021 by Deven
In this article, you will learn how to multiply array by scalar in python.
Let’s say you have 2 arrays that need to be multiplied by scalar n
.
array1 = np.array[[1, 2, 3]]
array2 = np.array[[[1, 2], [3, 4]]]
n = 5
Numpy multiply array by scalar
In order to multiply array by scalar in
python, you can use np.multiply[]
method.
import numpy as np
array1 = np.array[[1, 2, 3]]
array2 = np.array[[[1, 2], [3, 4]]]
n = 5
np.multiply[array1,n]
np.multiply[array2,n]
Share on social media
//
PreviousNextYou can multiply numpy arrays by scalars and it just works.
>>> import numpy as np
>>> np.array[[1, 2, 3]] * 2
array[[2, 4, 6]]
>>> np.array[[[1, 2, 3], [4, 5, 6]]] * 2
array[[[ 2, 4, 6],
[ 8, 10, 12]]]
This is also a very fast and efficient operation. With your example:
>>> a_1 = np.array[[1.0, 2.0, 3.0]]
>>> a_2 = np.array[[[1., 2.], [3., 4.]]]
>>> b = 2.0
>>> a_1 * b
array[[2., 4., 6.]]
>>> a_2 * b
array[[[2., 4.],
[6., 8.]]]
Created: February-28, 2021 | Updated: July-18, 2021 This tutorial will introduce methods to multiply elements of a NumPy array with a scalar in Python. In Python, it is very simple to multiply all the elements of a The following code example shows us how we can use the Output: In the above code, we first initialize a *
in Pythonnumpy.multiply[]
Function in PythonMultiply Elements of an Array With a Scalar Using
*
in PythonNumPy
array with a scalar. The *
operator in the NumPy
package can be used for this operation.*
method to multiply all the elements of a NumPy
array with a scalar in Python.import numpy
arr = numpy.array[[1, 2, 3]]
newarr = arr*3
print[newarr]
[3 6 9]
NumPy
array using the numpy.array[]
function and then compute the product of that array with a scalar using the *
operator.
Multiply an Array With a Scalar Using the numpy.multiply[]
Function in Python
We can multiply a NumPy array with a scalar using the numpy.multiply[]
function. The numpy.multiply[]
function gives us the product of two arrays. numpy.multiply[]
returns an array which is the product of two arrays given in the arguments of the
function.
The following code example shows us how to use the numpy.multiply[]
function to multiply all the elements of a NumPy array with a scalar in Python.
import numpy
arr = numpy.array[[1,2,3]]
newarr = numpy.multiply[arr, 3]
print[newarr]
Output:
[3 6 9]
In the above code, we first initialize a NumPy
array using numpy.array[]
function and then compute the product of that array with a scalar using the numpy.multiply[]
function.
Multiply arguments element-wise.
Parametersx1, x2array_likeInput 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.
wherearray_like, optionalThis condition is broadcast over the input. At locations where the condition is True, the out array will be set to the ufunc result. Elsewhere, the out array will retain its original value. Note that if an uninitialized out array is created via the default
out=None
, locations within it where the condition is False will remain uninitialized.
For other keyword-only arguments, see the ufunc docs.
ReturnsyndarrayThe product of x1 and x2, element-wise. This is a scalar if both x1 and x2 are scalars.
Notes
Equivalent to x1 * x2 in terms of array broadcasting.
Examples
>>> 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.]]]
The *
operator can be used as a shorthand for np.multiply
on ndarrays.
>>> 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.]]]