Divide each row by a vector element using NumPy


We can divide each row of the Numpy array by a vector element. The vector element can be a single element, multiple elements or an array. After dividing the row of an array by a vector to generate the required functionality, we use the divisor (/) operator. The division of the rows can be into 1−d or 2−d or multiple arrays.

There are different ways to perform the division of each row by a vector element. Let’s see each way in detail.

  • Using broadcasting

  • using divide() function

  • Using apply_along_axis() function

Using broadcasting

Broadcasting is the method available in Numpy library which allows performing the mathematical operations on different shaped arrays. If one of the arrays is smaller than the other array, then the broadcasts automatically match the shape of the smaller array with the larger array and apply the mathematical operation element−wise.

Syntax

Following is the syntax for using the broadcasting -

array / vector[:, np.newaxis]

Example

Let’s see an example to divide each row by a vector element using the broadcast method of the Numpy library.

import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print("The array:",arr)
vec = np.array([1, 2, 3])
print("The vector to divide the row:",vec)
output = arr / vec[:, np.newaxis]
print("The output of the divison of rows by vector using broadcast:",output)

Output

The array: [[1 2 3]
 [4 5 6]
 [7 8 9]]
The vector to divide the row: [1 2 3]
The output of the divison of rows by vector using broadcast: [[1.         2.         3.        ]
 [2.         2.5        3.        ]
 [2.33333333 2.66666667 3.        ]]

Using divide() function

Numpy library provides a function divide() which is used to divide the rows of the defined array by a vector element. This function takes the array and the vector as the input parameters.

Syntax

Following is the syntax for using the divide () function.

np.divide(array, vector[:, np.newaxis])

Example

In the following example, we are dividing the rows of the array using a vector element using divide() function.

import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print("The array:",arr)
vec = np.array([1, 2, 3])
print("The vector to divide the row:",vec)
output = np.divide(arr, vec[:, np.newaxis])
print("The output of the divison of rows by vector using divide function:",output)

Output

The array: [[1 2 3]
 [4 5 6]
 [7 8 9]]
The vector to divide the row: [1 2 3]
The output of the divison of rows by vector using divide function: [[1.         2.         3.        ]
 [2.         2.5        3.        ]
 [2.33333333 2.66666667 3.        ]]

Using apply_along_axis() function

The apply_along_axis() function in NumPy allows the users to apply a function along the specific axis of the NumPy array. This method can be used to perform variety of operations such as dividing a row of a 2D array by a vector.

Syntax

The following is the syntax for using the apply_along_axis() function to divide the rows of the array by the vector elements.

np.apply_along_axis(row/vector, 1, array, vector)

Example

In the following example, we are dividing the row of the 2-d array by a defined vector of 2−d array using the apply_along_axis() function.

import numpy as np
arr = np.array([[1, 2, 3,1], [4, 5, 6,4], [7, 8, 9,7],[10,11,1,2]])
print("The array:",arr)
vec = np.array([1, 2, 3,4])
print("The vector to divide the row:",vec)
def divide_row(row, vec):
    return row / vec
output = np.apply_along_axis(divide_row, 1, arr, vec)
print("The output of the divison of rows by vector using divide function:",output)

Output

The array: [[ 1  2  3  1]
 [ 4  5  6  4]
 [ 7  8  9  7]
 [10 11  1  2]]
The vector to divide the row: [1 2 3 4]
The output of the divison of rows by vector using divide function: [[ 1.          1.          1.          0.25      ]
 [ 4.          2.5         2.          1.        ]
 [ 7.          4.          3.          1.75      ]
 [10.          5.5         0.33333333  0.5       ]]

Updated on: 02-Nov-2023

114 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements