Numpy - Normalize large array using For loop and Vectorized operations
NumPy: Performance Optimization Exercise-13 with Solution
Write a function to normalize a large NumPy array by subtracting the mean and dividing by the standard deviation using a for loop. Optimize it using vectorized operations.
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 normalize the array using a for loop
def normalize_with_loop(arr):
mean = np.mean(arr)
std = np.std(arr)
normalized_array = np.empty_like(arr, dtype=float)
for i in range(len(arr)):
normalized_array[i] = (arr[i] - mean) / std
return normalized_array
# Normalize the array using the for loop method
normalized_with_loop = normalize_with_loop(large_array)
# Normalize the array using NumPy's vectorized operations
normalized_with_numpy = (large_array - np.mean(large_array)) / np.std(large_array)
# Display first 10 results to verify
print("First 10 normalized values using for loop:")
print(normalized_with_loop[:10])
print("First 10 normalized values using NumPy:")
print(normalized_with_numpy[:10])
Output:
First 10 normalized values using for loop: [ 0.62631406 0.6887257 1.72198514 -0.07061597 0.55003316 -0.47975897 -1.64130901 -1.14895048 1.65263887 0.67485645] First 10 normalized values using NumPy: [ 0.62631406 0.6887257 1.72198514 -0.07061597 0.55003316 -0.47975897 -1.64130901 -1.14895048 1.65263887 0.67485645]
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 normalize_with_loop is defined to normalize the array using a for loop.
- Calculating mean and standard deviation: The mean and standard deviation of the array are calculated.
- Normalizing with loop: The array is normalized using the for loop method by subtracting the mean and dividing by the standard deviation.
- Normalizing with numpy: The array is normalized using NumPy's vectorized operations.
- Displaying results: The first 10 normalized values 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