﻿ Optimizing row-wise Mean calculation of large NumPy arrays

# Optimizing row-wise Mean calculation of large NumPy arrays

## NumPy: Performance Optimization Exercise-4 with Solution

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.

Python-Numpy Code Editor:

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

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿