Numpy - Normalize large array using For loop and Vectorized operations
13. Large Array Normalization Optimization
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:
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.
For more Practice: Solve these Related Problems:
- Write a Numpy program to normalize a large array by subtracting its mean and dividing by its standard deviation using a for loop, then optimize with vectorized operations.
- Write a Numpy program to normalize a large array after applying a logarithmic transformation using loops, then optimize with np.log and broadcasting.
- Write a Numpy program to normalize a large array while handling zero values using a loop, then optimize using np.where and vectorized arithmetic.
- Write a Numpy program to compute a normalized version of a large array with a dynamic scaling factor using loops, then optimize with broadcasting techniques.
Go to:
Previous: NumPy - Cumulative sum of large array using For loop and optimization.
Next: Numpy - Compute column-wise Mean of large 2D array using For loop and Optimization.
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.