# How to perform function application over NumPy's matrix row/column?

In this tutorial, we will learn how can we perform function application over NumPy's matrix row/column in Python? By Shivang Yadav Last updated : December 28, 2023

Here, we are given a Numpy matrix. We need to apply a function on the rows/columns of the matrix. We will be applying the function on each element of a row or a column of the matrix. For our example we'll be using a simple comparison function on the matrix.

## NumPy Matrix - Function application over row/column

To perform function application over NumPy's matrix row/column, we can use different approaches. Here are a few of them,

## Approach 1: Using numpy apply_along_axis() function

Another method to perform the task is by using the numpy.apply_along_axis() function. This function allows you to apply a specified function to either the rows or columns of a matrix.

### Example

import numpy as np

# Create a NumPy matrix
matrix = np.array([[1, 2, 3], [4, -1, 6], [0, 2, 1]])

# Define a function to count rows with sums greater
# than 2 and less than 0
def count_rows(row):
row_sum = np.sum(row)
if row_sum > 2:
return "positive"
elif row_sum < 0:
return "negative"
else:
return "neutral"
# Use numpy.apply_along_axis to apply
# the function to each row (axis=1)
result = np.apply_along_axis(count_rows, axis=1, arr=matrix)

# Count the number of positive and negative sum rows
positive_sum_rows = np.count_nonzero(result == "positive")
negative_sum_rows = np.count_nonzero(result == "negative")

print("Number of rows with sum greater than 2:", positive_sum_rows)
print("Number of rows with sum less than 0:", negative_sum_rows)

Output:

Number of rows with sum greater than 2: 3
Number of rows with sum less than 0: 0

## Approach 2: Using built-in function for matrix in NumPy

One more method to perform the task is by using NumPy's built-in functions. We can use np.sum with the axis parameter to perform the task.

This method utilizes NumPy's optimized functions for array operations. The np.sum function calculates the sum along the specified axis, which allows you to get the row and column sums directly without using additional vectorization of loops.

### Example

import numpy as np

# Create your NumPy array (matrix)
matrix = np.array([[1, 2, 3], [4, -1, 6], [0, 2, 1]])

# Calculate the sum of each row
row_sums = np.sum(matrix, axis=1)

# Find rows with sum greater than 2
rows_greater_than_2 = matrix[row_sums > 2]

# Find rows with sum less than 0
rows_less_than_0 = matrix[row_sums < 0]

# Get the number of rows with sum greater than 2 and less than 0
num_rows_greater_than_2 = rows_greater_than_2.shape[0]
num_rows_less_than_0 = rows_less_than_0.shape[0]

print("Number of rows with sum greater than 2:", num_rows_greater_than_2)
print("Number of rows with sum less than 0:", num_rows_less_than_0)

Output

Number of rows with sum greater than 2: 3
Number of rows with sum less than 0: 0

In these examples, we have used the following Python basic topics that you should learn: