NumPy - Subtract the mean of each column 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 column from each element of a 3x3 array.

Prerequisites

To understand this solution, you should have the knowledge of the following Python topics:

Subtracting the mean of each column from each element of a 3x3 array

To subtract the mean of each column from each element of a 3x3 array, first, create a 3x3 array using the np.array() method, then get the mean of each column by using the np.mean() method by specifying the axis value 0, and subtract the array with the result of the mean of each column.

Python program to subtract the mean of each column from each element of a 3x3 array

Create a 3x3 array and subtract the mean of each column 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:

Original array (matrix) is:

[[1 2 3]
 [4 5 6]
 [7 8 9]]

Mean of each column (column_means) : 
[[4. 5. 6.]]

The result (subtract the mean of each column from each element) : 
[[-3. -3. -3.]
 [ 0.  0.  0.]
 [ 3.  3.  3.]]

Code explanation

  • To use NumPy in the program, we imported the numpy module.
  • Created an array matrix of 3x3 dimensions, printed it.
  • Found the mean of each column using the np.mean() method and stored it into column_means variable.
  • Then, subtracted the mean of each column from each element of an array using the below statement:
    subtractedArr = matrix - column_means
    

Example with a 3x3 array with random values

Create a 3x3 array with random values and subtract the mean of each column 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 column from each element
column_means = np.mean(matrix, axis=0, keepdims=True)

print("\nMean of each column (column_means) : ")
print(column_means)

# Subtracting the mean of each column from each element
subtractedArr = matrix - column_means

print("\nSThe result (subtract the mean of each column from each element) : ")
print(subtractedArr)

Output

The output of the above program is:

RUN 1:

Original array (matrix) is:

[[0.97161577 0.00961415 0.47302055]
 [0.76295745 0.57155184 0.03642735]
 [0.32375826 0.57232171 0.42614918]]

Mean of each column (column_means) : 
[[0.68611049 0.3844959  0.31186569]]

SThe result (subtract the mean of each column from each element) : 
[[ 0.28550528 -0.37488175  0.16115485]
 [ 0.07684696  0.18705594 -0.27543834]
 [-0.36235223  0.18782581  0.11428349]]

RUN 2:

RUN 2:
Original array (matrix) is:

[[0.8262556  0.7698748  0.80418545]
 [0.39781821 0.13627578 0.35343189]
 [0.68367665 0.77081116 0.3740641 ]]

Mean of each column (column_means) : 
[[0.63591682 0.55898725 0.51056048]]

SThe result (subtract the mean of each column from each element) : 
[[ 0.19033878  0.21088756  0.29362497]
 [-0.23809861 -0.42271147 -0.15712859]
 [ 0.04775983  0.21182391 -0.13649638]] 

Python NumPy Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.