Home »
Python »
Python Programs
NumPy - Subtract the mean of each row from each element of a 3x3 array
By IncludeHelp Last updated : December 16, 2023
Problem statement
Write a Python program to subtract the mean of each row from each element of a 3x3 array.
Prerequisites
To understand the given solution, you should know about the following Python topics:
Subtracting the mean of each row from each element of a 3x3 array
To subtract the mean of each row from each element of a 3x3 array, first, create a 3x3 array using the np.array() method, then get the mean of each row by using the np.mean() method by specifying the axis value 1, and subtract the array with the result of the mean of each row.
Python program to subtract the mean of each row from each element of a 3x3 array
Create a 3x3 array and subtract the mean of each row from each element of this array.
# Importing numpy library
import numpy as np
# Creating a NumPy array of 3x3
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print("Original array (matrix) is:\n")
print(matrix)
# Mean of each row from each element
row_means = np.mean(matrix, axis=1, keepdims=True)
print("\nMean of each row (row_means) : ")
print(row_means)
# Subtracting the mean of each row from each element
subtractedArr = matrix - row_means
print("\nThe result (subtract the mean of each row from each element) : ")
print(subtractedArr)
Output
The output of the above program is:
Original array (matrix) is:
[[1 2 3]
[4 5 6]
[7 8 9]]
Mean of each row (row_means) :
[[2.]
[5.]
[8.]]
The result (subtract the mean of each row from each element) :
[[-1. 0. 1.]
[-1. 0. 1.]
[-1. 0. 1.]]
Code explanation
Example with a 3x3 array with random values
Create a 3x3 array with random values and subtract the mean of each row from each element of this array.
# Importing numpy library
import numpy as np
# Creating a NumPy array of 3x3 with random values
matrix = np.random.rand(3, 3)
print("Original array (matrix) is:\n")
print(matrix)
# Mean of each row from each element
row_means = np.mean(matrix, axis=1, keepdims=True)
print("\nMean of each row (row_means) : ")
print(row_means)
# Subtracting the mean of each row from each element
subtractedArr = matrix - row_means
print("\nThe result (subtract the mean of each row from each element) : ")
print(subtractedArr)
Output
The output of the above program is:
RUN 1:
Original array (matrix) is:
[[0.87569482 0.33989259 0.76395862]
[0.08070437 0.53580611 0.22224737]
[0.22391174 0.61205819 0.35142345]]
Mean of each row (row_means) :
[[0.65984868]
[0.27958595]
[0.39579779]]
The result (subtract the mean of each row from each element) :
[[ 0.21584614 -0.31995608 0.10410994]
[-0.19888158 0.25622016 -0.05733858]
[-0.17188605 0.2162604 -0.04437435]]
RUN 2:
Original array (matrix) is:
[[0.76203074 0.8691096 0.24016087]
[0.86488673 0.87736533 0.75404792]
[0.34163761 0.35667379 0.56626297]]
Mean of each row (row_means) :
[[0.62376707]
[0.83209999]
[0.42152479]]
The result (subtract the mean of each row from each element) :
[[ 0.13826367 0.24534253 -0.3836062 ]
[ 0.03278674 0.04526534 -0.07805208]
[-0.07988718 -0.064851 0.14473818]]
Python NumPy Programs »