Numpy - Compute column-wise Mean of large 2D array using For loop and Optimization
Write a NumPy program that generates a large 2D NumPy array and write a function to compute the column-wise mean using a for loop. Optimize it using NumPy's built-in functions.
Sample Solution:
Python Code:
import numpy as np
# Generate a large 2D NumPy array with random integers
large_2d_array = np.random.randint(1, 1000, size=(1000, 1000))
# Function to compute column-wise mean using a for loop
def column_wise_mean_with_loop(arr):
num_rows, num_cols = arr.shape
col_means = np.empty(num_cols)
for col in range(num_cols):
col_sum = 0
for row in range(num_rows):
col_sum += arr[row, col]
col_means[col] = col_sum / num_rows
return col_means
# Compute column-wise mean using the for loop method
col_mean_with_loop = column_wise_mean_with_loop(large_2d_array)
# Compute column-wise mean using NumPy's built-in functions
col_mean_with_numpy = np.mean(large_2d_array, axis=0)
# Display first 10 column means to verify
print("First 10 column means using for loop:")
print(col_mean_with_loop[:10])
print("First 10 column means using NumPy:")
print(col_mean_with_numpy[:10])
Output:
First 10 column means using for loop: [487.134 519.27 502.076 486.791 506.818 505.29 507.058 495.108 508.137 501.575] First 10 column means using NumPy: [487.134 519.27 502.076 486.791 506.818 505.29 507.058 495.108 508.137 501.575]
Explanation:
- Importing numpy: We first import the numpy library for array manipulations.
- Generating a large array: A large 2D NumPy array with random integers is generated.
- Defining the function: A function column_wise_mean_with_loop is defined to compute the column-wise mean using a for loop.
- Computing with loop: The column-wise mean is calculated using the for loop method by summing each column and dividing by the number of rows.
- Computing with numpy: The column-wise mean is calculated using NumPy's built-in mean() function with axis=0.
- Displaying results: The first 10 column means from both methods are printed out to verify correctness.
Python-Numpy Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics