Multiply array by scalar python

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

//

PreviousNext

You 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.]]]

  1. HowTo
  2. Python NumPy Howtos
  3. Multiply Array With Scalar in Python

Created: February-28, 2021 | Updated: July-18, 2021

  1. Multiply Elements of an Array With a Scalar Using * in Python
  2. Multiply an Array With a Scalar Using the numpy.multiply[] Function in Python

This tutorial will introduce methods to multiply elements of a NumPy array with a scalar in Python.

Multiply Elements of an Array With a Scalar Using * in Python

In Python, it is very simple to multiply all the elements of a NumPy array with a scalar. The * operator in the NumPy package can be used for this operation.

The following code example shows us how we can use the * 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]

Output:

[3 6 9]

In the above code, we first initialize a 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.

numpy.multiply[x1, x2, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]]=#

Multiply arguments element-wise.

Parametersx1, x2array_like

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].

outndarray, None, or tuple of ndarray and None, optional

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, optional

This 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.

**kwargs

For other keyword-only arguments, see the ufunc docs.

Returnsyndarray

The 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.]]]

How do you multiply an array by a scalar in Python?

Numpy multiply array by scalar In order to multiply array by scalar in python, you can use np. multiply[] method.

How do you multiply an array in Python?

There are three main ways to perform NumPy matrix multiplication:.
dot[array a, array b] : returns the scalar or dot product of two arrays..
matmul[array a, array b] : returns the matrix product of two arrays..
multiply[array a, array b] : returns the element-wise matrix multiplication of two arrays..

How do you multiply a NumPy array by a constant?

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 and constant as operands as shown below. where a is input array and c is a constant.

How do you add a scalar to an array in Python?

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. ... .
In [2]: a = np. array[[1, 2, 3]] b = np. ... .
In [3]: a = np. array[[1,2,3]] b = 3*a print[b] ... .
In [4]: a = np. ... .
In [5]: a = np. ... .
In [6]: a = np. ... .
In [7]: a = np. ... .
In [8]: a = np..

Chủ Đề