w3resource

Optimizing row-wise Mean calculation of large NumPy arrays


4. Large 2D Array Row-wise Mean Optimization

Write a NumPy program to create a large 2D NumPy array and write a function to calculate the row-wise mean using a for loop. Optimize it using vectorized operations.

Sample Solution:

Python Code:

import numpy as np

# Create a large 2D NumPy array with shape (10000, 100)
large_array = np.random.rand(10000, 100)

# Function to calculate the row-wise mean using a for loop
def row_wise_mean_using_loop(array):
    row_means = np.zeros(array.shape[0])
    for i in range(array.shape[0]):
        row_means[i] = np.mean(array[i, :])
    return row_means

# Calculate the row-wise mean using the for loop
mean_loop = row_wise_mean_using_loop(large_array)
print("Row-wise mean using for loop (first 10 rows):", mean_loop[:10])

# Optimize the row-wise mean calculation using vectorized operations
mean_vectorized = np.mean(large_array, axis=1)
print("Row-wise mean using vectorized operations (first 10 rows):", mean_vectorized[:10])

Output:

Row-wise mean using for loop (first 10 rows): [0.49248002 0.53484555 0.50524083 0.53057524 0.48671578 0.44682543
 0.52395333 0.56720315 0.5154937  0.54655315]
Row-wise mean using vectorized operations (first 10 rows): [0.49248002 0.53484555 0.50524083 0.53057524 0.48671578 0.44682543
 0.52395333 0.56720315 0.5154937  0.54655315]

Explanation:

  • Create a large 2D array: A 2D NumPy array with shape (10000, 100) is created using np.random.rand().
  • Function with for loop: A function row_wise_mean_using_loop calculates the row-wise mean of the array elements using a for loop.
  • Calculate mean with for loop: The row-wise mean is calculated using the for loop, and the first 10 row means are printed.
  • Optimize with vectorization: The row-wise mean calculation is optimized using NumPy's vectorized np.mean() function along the rows (axis=1), and the first 10 row means are printed.

For more Practice: Solve these Related Problems:

  • Write a Numpy program to calculate the row-wise median of a large 2D array using loops, then optimize with np.median along the correct axis.
  • Write a Numpy program to compute both the row-wise mean and variance using a for loop, then optimize using np.mean and np.var.
  • Write a Numpy program to compute the row-wise mean of a large 2D array while ignoring NaN values using a loop, then optimize with np.nanmean.
  • Write a Numpy program to calculate a weighted row-wise mean on a large 2D array using loops, then optimize it with broadcasting and np.average.

Python-Numpy Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Optimizing Dot product calculation of large NumPy arrays.
Next: Optimizing Standard Deviation calculation of large NumPy arrays.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.