Element-wise division in Python NumPy
This tutorial shows you how to perform element-wise division on NumPy arrays in Python.
NumPy Element-Wise Division using numpy.divide()
the function
If we have two arrays and want to divide each element of the first array with each element of the second array, we can use numpy.divide()
the numpy.divide() function. The numpy.divide() function performs element-wise division on NumPy arrays. numpy.divide()
The function takes the dividend array, divisor array, and output array as its arguments and stores the division result in the output array. Refer to the following code example.
import numpy as np
array1 = np.array([10, 20, 30])
array2 = np.array([2, 4, 6])
np.divide(array1, array2, array3)
print(array3)
Output:
[5. 5. 5.]
In the code above, we first np.array()
create two NumPy arrays, the dividend array array1
and the divisor array , using the function array2
. We then array1
divide by array2
and np.divide()
store the result array3
in the NumPy array using the function.
NumPy Element-Wise Division and /
Operators
We can also /
perform element-wise division on NumPy arrays in Python using the operator. /
The operator is np.true_divide()
a shorthand for the function in Python. We can use /
the operator to divide one array by another and store the result in a third array. Refer to the following code example.
import numpy as np
array1 = np.array([10, 20, 30])
array2 = np.array([2, 4, 6])
array3 = array1 / array2
print(array3)
Output:
[5. 5. 5.]
We array1
divide by array2
and /
store the result array3
in the NumPy array using the operator.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Convert Tensor to NumPy array in Python
Publish Date:2025/05/03 Views:85 Category:Python
-
This tutorial will show you how to convert a Tensor to a NumPy array in Python. Use the function in Python Tensor.numpy() to convert a tensor to a NumPy array Eager Execution of TensorFlow library can be used to convert tensor to NumPy arra
Saving NumPy arrays as images in Python
Publish Date:2025/05/03 Views:193 Category:Python
-
In Python, numpy module is used to manipulate arrays. There are many modules available in Python that allow us to read and store images. An image can be thought of as an array of different pixels stored at specific locations with correspond
Transposing a 1D array in NumPy
Publish Date:2025/05/03 Views:98 Category:Python
-
Arrays and matrices form the core of this Python library. The transpose of these arrays and matrices plays a vital role in certain topics such as machine learning. In NumPy, it is easy to calculate the transpose of an array or a matrix. Tra
Find the first index of an element in a NumPy array
Publish Date:2025/05/03 Views:58 Category:Python
-
In this tutorial, we will discuss how to find the first index of an element in a numpy array. Use where() the function to find the first index of an element in a NumPy array The function in the numpy module where() is used to return an arra
Remove Nan values from NumPy array
Publish Date:2025/05/03 Views:118 Category:Python
-
This article discusses some built-in NumPy functions that you can use to remove nan values. Remove Nan values using logical_not() and methods in NumPy isnan() logical_not() is used to apply logical NOT to the elements of an array. isn
Normalizing a vector in Python
Publish Date:2025/05/03 Views:51 Category:Python
-
A common concept in the field of machine learning is to normalize a vector or dataset before passing it to the algorithm. When we talk about normalizing a vector, we say that its vector magnitude is 1, being a unit vector. In this tutorial,
Calculating Euclidean distance in Python
Publish Date:2025/05/03 Views:128 Category:Python
-
In the world of mathematics, the shortest distance between two points in any dimension is called the Euclidean distance. It is the square root of the sum of the squares of the differences between the two points. In Python, the numpy, scipy
Converting a PIL image to a NumPy array
Publish Date:2025/05/03 Views:148 Category:Python
-
This tutorial will discuss methods to convert a PIL image to a 3-dimensional NumPy array in Python. numpy.array() Convert a PIL image to a NumPy array in Python using the PIL is used to perform various operations on images in Python. The Pi
How to Convert DataFrame Column to String in Pandas
Publish Date:2025/05/02 Views:162 Category:Python
-
We will look at methods for converting Pandas DataFrame columns to strings. Pandas Series.astype(str) Method DataFrame.apply() Methods operate on the elements in a column We will use the same DataFrame below in this article. import pandas a