﻿ Numpy program to count Non-Zero elements in large 2D array

# Numpy program to count Non-Zero elements in large 2D srray using For loop and Optimization

## NumPy: Performance Optimization Exercise-17 with Solution

Write a NumPy program that creates a large 2D NumPy array and write a function to count the number of non-zero elements using a for loop. Optimize it using NumPy's count_nonzero() function.

Sample Solution:

Python Code:

``````import numpy as np

# Generate two large 1D NumPy arrays with random integers
array1 = np.random.randint(1, 1000, size=1000)
array2 = np.random.randint(1, 1000, size=1000)

# Function to calculate the outer product using nested for loops
def outer_product_with_loops(arr1, arr2):
result = np.empty((len(arr1), len(arr2)), dtype=int)
for i in range(len(arr1)):
for j in range(len(arr2)):
result[i, j] = arr1[i] * arr2[j]
return result

# Calculate the outer product using the nested for loops method
outer_with_loops = outer_product_with_loops(array1, array2)

# Calculate the outer product using NumPy's outer() function
outer_with_numpy = np.outer(array1, array2)

# Display the first 10x10 section of the result to verify
print("Outer product using for loops (first 10x10 elements):")
print(outer_with_loops[:10, :10])

print("Outer product using NumPy (first 10x10 elements):")
print(outer_with_numpy[:10, :10])
``````

Output:

```Outer product using for loops (first 10x10 elements):
[[120696 300456 784952 512744 700208 719040 564104 847440 660832 632584]
[ 49773 123903 323701 211447 288754 296520 232627 349470 272516 260867]
[ 94611 235521 615307 401929 548878 563640 442189 664290 518012 495869]
[ 95739 238329 622643 406721 555422 570360 447461 672210 524188 501781]
[ 88548 220428 575876 376172 513704 527520 413852 621720 484816 464092]
[ 28482  70902 185234 120998 165236 169680 133118 199980 155944 149278]
[118863 295893 773031 504957 689574 708120 555537 834570 650796 622977]
[ 52593 130923 342041 223427 305114 313320 245807 369270 287956 275647]
[ 68385 170235 444745 290515 396730 407400 319615 480150 374420 358415]
[134514 334854 874818 571446 780372 801360 628686 944460 736488 705006]]
Outer product using NumPy (first 10x10 elements):
[[120696 300456 784952 512744 700208 719040 564104 847440 660832 632584]
[ 49773 123903 323701 211447 288754 296520 232627 349470 272516 260867]
[ 94611 235521 615307 401929 548878 563640 442189 664290 518012 495869]
[ 95739 238329 622643 406721 555422 570360 447461 672210 524188 501781]
[ 88548 220428 575876 376172 513704 527520 413852 621720 484816 464092]
[ 28482  70902 185234 120998 165236 169680 133118 199980 155944 149278]
[118863 295893 773031 504957 689574 708120 555537 834570 650796 622977]
[ 52593 130923 342041 223427 305114 313320 245807 369270 287956 275647]
[ 68385 170235 444745 290515 396730 407400 319615 480150 374420 358415]
[134514 334854 874818 571446 780372 801360 628686 944460 736488 705006]]
```

Explanation:

• Importing numpy: We first import the numpy library for array manipulations.
• Generating large arrays: Two large 1D NumPy arrays with random integers are generated.
• Defining the function: A function outer_product_with_loops is defined to calculate the outer product using nested for loops.
• Calculating with loops: The outer product is calculated using the nested for loops method.
• Calculating with numpy: The outer product is calculated using NumPy's built-in outer() function.
• Displaying results: The first 10x10 section of the outer product from both methods is printed out to verify correctness.

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.

﻿