Find missing data

Python Practice › NumPy Programs › Program 42

Find missing data

EasyNumPy Programs · 42 of 100

Write a NumPy program to find the missing (NaN) values in an array.

Input
[[1, 5, nan], [8, nan, 9]]
Output
[[False False  True]
 [False  True False]]
2 missing

Approach

  1. np.isnan().

Solution

Python
import numpy as npa = np.array([[1, 5, np.nan], [8, np.nan, 9]])print(np.isnan(a))print(np.isnan(a).sum(), "missing")
Output
[[False False  True]
 [False  True False]]
2 missing

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Array to list

Python Practice › NumPy Programs › Program 41

Array to list

EasyNumPy Programs · 41 of 100

Write a NumPy program to convert an array into a list.

Input
[[3, 0], [6, 4]]
Output
[[3, 0], [6, 4]]

Approach

  1. .tolist() gives plain Python ints.

Solution

Python
import numpy as npprint(np.array([[3, 0], [6, 4]]).tolist())
Output
[[3, 0], [6, 4]]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

List to array

Python Practice › NumPy Programs › Program 40

List to array

EasyNumPy Programs · 40 of 100

Write a NumPy program to convert a list into an array.

Input
[[3, 0], [6, 4]]
Output
[[3 0]
 [6 4]]

Approach

  1. np.array().

Solution

Python
import numpy as npprint(np.array([[3, 0], [6, 4]]))
Output
[[3 0]
 [6 4]]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Add a vector to each row

Python Practice › NumPy Programs › Program 39

Add a vector to each row

EasyNumPy Programs · 39 of 100

Write a NumPy program to add a vector to each row of a matrix.

Input
[2, 5] and [[5, 3], [7, 4]]
Output
[[7 8]
 [9 9]]

Approach

  1. Broadcasting adds the vector to every row.

Solution

Python
import numpy as npprint(np.array([[5, 3], [7, 4]]) + np.array([2, 5]))
Output
[[7 8]
 [9 9]]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Dot product of two vectors

Python Practice › NumPy Programs › Program 38

Dot product of two vectors

EasyNumPy Programs · 38 of 100

Write a NumPy program to calculate the dot product of two vectors.

Input
[5, 3] and [7, 4]
Output
47 47

Approach

  1. np.dot(a, b) or a @ b.

Solution

Python
import numpy as npa, b = np.array([5, 3]), np.array([7, 4])print(np.dot(a, b), a @ b)
Output
47 47

Line by line

Line 3

5 × 7 + 3 × 4 = 47.

Note

The original sample answer (42) was not the dot product of these vectors.

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Sum of each column

Python Practice › NumPy Programs › Program 37

Sum of each column

EasyNumPy Programs · 37 of 100

Write a NumPy program to calculate the sum of each column.

Input
[[5, 3], [7, 4]]
Output
[12  7]

Approach

  1. sum(axis=0).

Solution

Python
import numpy as npprint(np.array([[5, 3], [7, 4]]).sum(axis=0))
Output
[12  7]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Sum of each row

Python Practice › NumPy Programs › Program 36

Sum of each row

EasyNumPy Programs · 36 of 100

Write a NumPy program to calculate the sum of each row.

Input
[[5, 3], [7, 4]]
Output
[ 8 11]

Approach

  1. sum(axis=1).

Solution

Python
import numpy as npprint(np.array([[5, 3], [7, 4]]).sum(axis=1))
Output
[ 8 11]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Sum of all elements

Python Practice › NumPy Programs › Program 35

Sum of all elements

EasyNumPy Programs · 35 of 100

Write a NumPy program to calculate the sum of all elements.

Input
[[5, 3], [7, 4]]
Output
19

Approach

  1. .sum().

Solution

Python
import numpy as npprint(np.array([[5, 3], [7, 4]]).sum())
Output
19

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Diagonal matrix

Python Practice › NumPy Programs › Program 34

Diagonal matrix

EasyNumPy Programs · 34 of 100

Write a NumPy program to create a 2 × 2 zero matrix with 7 and 8 on the main diagonal.

Output
[[7 0]
 [0 8]]

Approach

  1. np.diag([7, 8]).

Solution

Python
import numpy as npprint(np.diag([7, 8]))
Output
[[7 0]
 [0 8]]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Reshape 2 × 3 into 3 × 2

Python Practice › NumPy Programs › Program 33

Reshape 2 × 3 into 3 × 2

EasyNumPy Programs · 33 of 100

Write a NumPy program to reshape a 2 × 3 matrix into a 3 × 2 matrix.

Input
[[5, 6, 7], [8, 9, 10]]
Output
[[ 5  6]
 [ 7  8]
 [ 9 10]]

Approach

  1. reshape(3, 2); the number of items must stay the same.

Solution

Python
import numpy as npm = np.array([[5, 6, 7], [8, 9, 10]])print(m.reshape(3, 2))
Output
[[ 5  6]
 [ 7  8]
 [ 9 10]]
Note

The original task said “2 × 2”, but six values cannot fit in a 2 × 2 matrix; its sample output was 3 × 2.

Learn the concept

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

More numpy programs

← All 100 NumPy Programs