Optimizing Standard Deviation calculation of large NumPy arrays
NumPy: Performance Optimization Exercise-5 with Solution
Write a NumPy program to create a large NumPy array and write a function to calculate the standard deviation of its elements using a for loop. Optimize it using NumPy's built-in functions.
Sample Solution:
Python Code:
import numpy as np
# Create a large NumPy array with 1 million elements
large_array = np.random.rand(1_000_000)
# Function to calculate the standard deviation using a for loop
def std_dev_using_loop(array):
mean = np.mean(array)
variance = 0.0
for element in array:
variance += (element - mean) ** 2
variance /= len(array)
return np.sqrt(variance)
# Calculate the standard deviation using the for loop
std_loop = std_dev_using_loop(large_array)
print("Standard deviation using for loop:", std_loop)
# Optimize the standard deviation calculation using NumPy's built-in function
std_numpy = np.std(large_array)
print("Standard deviation using NumPy's built-in function:", std_numpy)
Output:
Standard deviation using for loop: 0.28867104907380803 Standard deviation using NumPy's built-in function: 0.288671049073815
Explanation:
- Create a large array: A 1D NumPy array with 1 million elements is created using np.random.rand().
- Function with for loop: A function std_dev_using_loop calculates the standard deviation of the array elements using a for loop.
- Calculate standard deviation with for loop: The standard deviation is calculated using the for loop and printed.
- Optimize with NumPy: The standard deviation calculation is optimized using NumPy's built-in np.std() function and printed.
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