Row averages ignoring NaN

Python Practice › NumPy Programs › Program 86

Row averages ignoring NaN

EasyNumPy Programs · 86 of 100

Write a NumPy program to calculate the average of each row, ignoring NaNs.

Input
[[6, 5, nan], [nan, 9, 1], [7, 8, 2]]
Output
[5.5  5.   5.67]

Approach

  1. np.nanmean(axis=1).

Solution

Python
import numpy as npa = np.array([[6, 5, np.nan], [np.nan, 9, 1], [7, 8, 2]])print(np.nanmean(a, axis=1).round(2))
Output
[5.5  5.   5.67]
Note

The third row’s average is 17 / 3 = 5.67; the original sample answer (8.5) was not correct.

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Sum of all columns

Python Practice › NumPy Programs › Program 85

Sum of all columns

EasyNumPy Programs · 85 of 100

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

Input
[[6, 5, 7], [10, 9, 1], [7, 8, 2]]
Output
[23 22 10]

Approach

  1. sum(axis=0).

Solution

Python
import numpy as npprint(np.array([[6, 5, 7], [10, 9, 1], [7, 8, 2]]).sum(axis=0))
Output
[23 22 10]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Copy an array

Python Practice › NumPy Programs › Program 84

Copy an array

EasyNumPy Programs · 84 of 100

Write a NumPy program to copy data from one array to another.

Output
[ 1 77  3] [99  2  3]

Approach

  1. copy() creates independent data; a slice is only a view.

Solution

Python
import numpy as npa = np.array([1, 2, 3])b = a.copy()view = a[:]b[0] = 99view[1] = 77print(a, b)
Output
[ 1 77  3] [99  2  3]

Line by line

Lines 5-7

Changing the copy leaves a alone, but changing the view changes a.

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Top-left 2 × 2 block

Python Practice › NumPy Programs › Program 83

Top-left 2 × 2 block

EasyNumPy Programs · 83 of 100

Write a NumPy program to extract the first and second elements of the first and second rows.

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

Approach

  1. a[:2, :2].

Solution

Python
import numpy as npa = np.array([[6, 5, 7], [10, 9, 1], [7, 8, 2]])print(a[:2, :2])
Output
[[ 6  5]
 [10  9]]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

First two columns

Python Practice › NumPy Programs › Program 82

First two columns

EasyNumPy Programs · 82 of 100

Write a NumPy program to access the first two columns of an array.

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

Approach

  1. a[:, :2].

Solution

Python
import numpy as npa = np.array([[6, 5, 7], [10, 9, 1], [7, 8, 2]])print(a[:, :2])
Output
[[ 6  5]
 [10  9]
 [ 7  8]]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Save an array to CSV

Python Practice › NumPy Programs › Program 81

Save an array to CSV

EasyNumPy Programs · 81 of 100

Write a NumPy program to save an array to a CSV file.

Output
a,b,c
1,2,3
4,5,6

Approach

  1. np.savetxt with delimiter=",".

Solution

Python
import numpy as npa = np.array([[1, 2, 3], [4, 5, 6]])np.savetxt("data.csv", a, delimiter=",", fmt="%d", header="a,b,c", comments="")print(open("data.csv").read())
Output
a,b,c
1,2,3
4,5,6

Line by line

Line 3

fmt="%d" writes integers; comments="" stops NumPy putting “# ” before the header.

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

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