How to multiply Numpy Array by a Scalar in Python : Various Methods

How to multiply Numpy Array by a Scalar in Python

Numpy is the best python package for creating a NumPy array. It allows you to do complex mathematical calculations in an efficient way. In this entire tutorial, you will learn how to multiply the NumPy array by a scalar in python using various methods.

Methods to Multiply Numpy Array by a Scalar

In this section, you will know all the methods to create and multiply the NumPy array by a Scalar. All the coding demonstration part is done on the Jupyter notebook. So I will advise you to do the same for better understanding.

Method 1: Multiply NumPy array by a scalar using the * operator

The first method to multiply the NumPy array is the use of the ‘ * ‘ operator. It will directly multiply all the elements of the NumPy array whether it is a Single Dimensional or Multi-Dimensional array.

Single Dimensional Array

Let’s create a 1D array and multiply it by a Scalar value. You can create a NumPy array using the np.array() method. After that, you will multiply the array with the scalar value.

Execute the below lines of code to multiply the array.

import numpy as np
array_1d = np.array([10,20,30])
final_array = array_1d * 10
print(final_array)

Output

Multiplying the 1D numpy array with the scalar
Multiplying the 1D array with the scalar

Multi-Dimensional Array

In the same way, you can multiply the 2D NumPy array using the ‘ * ‘ operator.

Run the following lines of code.

import numpy as np
array_1d = np.array([[10,20,30],[40,50,60],[70,80,90]])
final_array = array_1d * 10
print(final_array)

Output

Multiplying the 2D numpy array with the scalar
Multiplying the 2D NumPy array with the scalar

Method 2: Multiply NumPy array using np.multiply()

The second method to multiply the NumPy by a scalar is the use of the numpy.multiply() method. It accepts two arguments one is the input array and the other is the scalar or another NumPy array.

In our example I will multiply the array by scalar then I have to pass the scalar value as another argument.

Using the same NumPy array as created in the above method 1, I will use the multiply() function.

1D Numpy array

import numpy as np
array_1d = np.array([10,20,30])
final_array = np.multiply(array_1d,10)
print(final_array)

Output

Multiplying the 1D numpy array with the scalar using multiply() method
Multiplying the 1D array with the scalar using multiply() method

2D Numpy array

import numpy as np
array_1d = np.array([[10,20,30],[40,50,60],[70,80,90]])
final_array = np.multiply(array_1d,10)
print(final_array)

Output

Multiplying the 2D numpy array with the scalar using multiply() method
Multiplying the 2D array with the scalar using multiply() method

Conclusion

Numpy is the best python package for doing mathematical computations fast. These are methods to multiply Numpy Array by a Scalar in Python. I hope you have understood this tutorial. If you have any suggestions and queries then you can contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Share via
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner