w3resource

Numpy - Compute Variance of large array using For loop and Optimization


NumPy: Performance Optimization Exercise-18 with Solution


Write a NumPy program that generates a large NumPy array and write a function to compute the variance of its elements using a for loop. Optimize it using NumPy's built-in functions.

Sample Solution:

Python Code:

import numpy as np

# Generate a large 1D NumPy array with random integers
large_array = np.random.randint(1, 1000, size=1000000)

# Function to compute variance using a for loop
def variance_with_loop(arr):
    mean = np.mean(arr)  # Calculate the mean of the array
    variance = 0
    for i in range(len(arr)):
        variance += (arr[i] - mean) ** 2
    variance /= len(arr)  # Divide by the number of elements to get the variance
    return variance

# Compute variance using the for loop method
variance_with_loop_result = variance_with_loop(large_array)

# Compute variance using NumPy's built-in var() function
variance_with_numpy = np.var(large_array)

# Display the results
print(f"Variance using for loop: {variance_with_loop_result}")
print(f"Variance using NumPy: {variance_with_numpy}")

Output:

Variance using for loop: 83046.09052346226
Variance using NumPy: 83046.09052346242

Explanation:

  • Importing numpy: We first import the numpy library for array manipulations.
  • Generating a large array: A large 1D NumPy array with random integers is generated.
  • Defining the function: A function variance_with_loop is defined to compute the variance using a for loop.
  • Calculating mean: The mean of the array is calculated.
  • Computing with loop: The variance is calculated using the for loop method by summing the squared differences from the mean and dividing by the number of elements.
  • Computing with numpy: The variance is calculated using NumPy's built-in var() function.
  • Displaying results: The results from both methods are printed out to verify correctness.

Python-Numpy Code Editor: