Numbers greater than a value

Python Practice › NumPy Programs › Program 48

Numbers greater than a value

EasyNumPy Programs · 48 of 100

Write a NumPy program to extract numbers greater than 6.

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

Approach

  1. A boolean mask.

Solution

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

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Sort rows by the first column

Python Practice › NumPy Programs › Program 47

Sort rows by the first column

EasyNumPy Programs · 47 of 100

Write a NumPy program to sort the rows of an array by the first column.

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

Approach

  1. argsort on the column gives the row order.

Solution

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

Line by line

Line 3

a[:, 0] is the first column; its argsort is the order in which to take the rows.

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Sort each row

Python Practice › NumPy Programs › Program 46

Sort each row

EasyNumPy Programs · 46 of 100

Write a NumPy program to sort each row in ascending order.

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

Approach

  1. np.sort(a, axis=1).

Solution

Python
import numpy as npprint(np.sort(np.array([[6, 5], [10, 9], [8, 7]]), axis=1))
Output
[[ 5  6]
 [ 9 10]
 [ 7  8]]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Transpose a matrix

Python Practice › NumPy Programs › Program 45

Transpose a matrix

EasyNumPy Programs · 45 of 100

Write a NumPy program to swap the rows and columns of a matrix.

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

Approach

  1. .T.

Solution

Python
import numpy as npprint(np.array([[5, 6], [7, 8], [9, 10]]).T)
Output
[[ 5  7  9]
 [ 6  8 10]]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

One-dimensional array

Python Practice › NumPy Programs › Program 44

One-dimensional array

EasyNumPy Programs · 44 of 100

Write a NumPy program to create a one-dimensional array.

Output
[65 90  1  3  5] 1

Approach

  1. np.array with a flat list.

Solution

Python
import numpy as npa = np.array([65, 90, 1, 3, 5])print(a, a.ndim)
Output
[65 90  1  3  5] 1

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

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