Product of an array

Python Practice › NumPy Programs › Program 80

Product of an array

EasyNumPy Programs · 80 of 100

Write a NumPy program to calculate the product of an array.

Input
[3, 5, 1]
Output
15

Approach

  1. np.prod.

Solution

Python
import numpy as npprint(np.prod([3, 5, 1]))
Output
15

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Is an array empty?

Python Practice › NumPy Programs › Program 79

Is an array empty?

EasyNumPy Programs · 79 of 100

Write a NumPy program to check whether an array is empty.

Input
[4, 8, 0], []
Output
[4 8 0] Array is not empty
[] Array is empty

Approach

  1. Check a.size == 0.

Solution

Python
import numpy as npfor a in [np.array([4, 8, 0]), np.array([])]:    print(a, "Array is empty" if a.size == 0 else "Array is not empty")
Output
[4 8 0] Array is not empty
[] Array is empty

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Frequency of unique values

Python Practice › NumPy Programs › Program 78

Frequency of unique values

EasyNumPy Programs · 78 of 100

Write a NumPy program to count how often each unique value appears.

Input
[8, 6, 7, 0, 7, 8, 6, 6]
Output
[[0 6 7 8]
 [1 3 2 2]]

Approach

  1. np.unique(..., return_counts=True).

Solution

Python
import numpy as npvalues, counts = np.unique([8, 6, 7, 0, 7, 8, 6, 6], return_counts=True)print(np.vstack((values, counts)))
Output
[[0 6 7 8]
 [1 3 2 2]]

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Magnitude of a vector

Python Practice › NumPy Programs › Program 77

Magnitude of a vector

EasyNumPy Programs · 77 of 100

Write a NumPy program to get the magnitude (length) of a vector.

Input
[3, 5, 1]
Output
5.92

Approach

  1. np.linalg.norm.

Solution

Python
import numpy as npprint(round(float(np.linalg.norm([3, 5, 1])), 2))
Output
5.92

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Drop rows with NaN

Python Practice › NumPy Programs › Program 76

Drop rows with NaN

EasyNumPy Programs · 76 of 100

Write a NumPy program to remove rows that contain NaN.

Input
[[4, nan, 8], [2, 4, 6]]
Output
[[2. 4. 6.]]

Approach

  1. Keep rows where no element is NaN.

Solution

Python
import numpy as npa = np.array([[4, np.nan, 8], [2, 4, 6]])print(a[~np.isnan(a).any(axis=1)])
Output
[[2. 4. 6.]]

Line by line

Line 3

isnan(a).any(axis=1) is True for rows with a NaN; ~ inverts it.

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Replace negative values

Python Practice › NumPy Programs › Program 75

Replace negative values

EasyNumPy Programs · 75 of 100

Write a NumPy program to replace negative values with 1.

Input
[[4, 8, -5], [2, -9, 6]]
Output
[[4 8 1]
 [2 1 6]]

Approach

  1. Assign through a mask.

Solution

Python
import numpy as npa = np.array([[4, 8, -5], [2, -9, 6]])a[a < 0] = 1print(a)
Output
[[4 8 1]
 [2 1 6]]

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Float array to integers

Python Practice › NumPy Programs › Program 73

Float array to integers

EasyNumPy Programs · 73 of 100

Write a NumPy program to convert an array of floats to integers.

Input
[[6.7, 5.2], [9.4, 2.7]]
Output
[[6 5]
 [9 2]]
[[7 5]
 [9 3]]

Approach

  1. astype(int) truncates; round first if needed.

Solution

Python
import numpy as npa = np.array([[6.7, 5.2], [9.4, 2.7]])print(a.astype(int))print(np.rint(a).astype(int))
Output
[[6 5]
 [9 2]]
[[7 5]
 [9 3]]

Line by line

Line 3

Truncation drops the decimals.

Line 4

rint rounds to the nearest integer first.

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Access by column

Python Practice › NumPy Programs › Program 72

Access by column

EasyNumPy Programs · 72 of 100

Write a NumPy program to print each column of an array.

Input
[[4, 8, 0], [2, 0, 6]]
Output
Column 1: [4 2]
Column 2: [8 0]
Column 3: [0 6]

Approach

  1. a[:, j] selects column j.

Solution

Python
import numpy as npa = np.array([[4, 8, 0], [2, 0, 6]])for j in range(a.shape[1]):    print(f"Column {j + 1}:", a[:, j])
Output
Column 1: [4 2]
Column 2: [8 0]
Column 3: [0 6]

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Square every element

Python Practice › NumPy Programs › Program 71

Square every element

EasyNumPy Programs · 71 of 100

Write a NumPy program to print the squares of all elements.

Input
[2, 6, 3, 1]
Output
[ 4 36  9  1]

Approach

  1. a ** 2 or np.square.

Solution

Python
import numpy as npprint(np.square(np.array([2, 6, 3, 1])))
Output
[ 4 36  9  1]

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Print every element

Python Practice › NumPy Programs › Program 70

Print every element

EasyNumPy Programs · 70 of 100

Write a NumPy program to print every element of a 2-D array.

Input
[[4, 8, 0], [2, 0, 6]]
Output
4 8 0 2 0 6 

Approach

  1. np.nditer or ravel().

Solution

Python
import numpy as npa = np.array([[4, 8, 0], [2, 0, 6]])for x in np.nditer(a):    print(x, end=" ")print()
Output
4 8 0 2 0 6 

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs