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

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

Add a column

Python Practice › NumPy Programs › Program 74

Add a column

EasyNumPy Programs · 74 of 100

Write a NumPy program to add an extra column to an array.

Input
[[4, 8, 0], [2, 0, 6]], column [5, 7]
Output
[[4 8 0 5]
 [2 0 6 7]]

Approach

  1. np.column_stack or np.hstack with a reshaped column.

Solution

Python
import numpy as npa = np.array([[4, 8, 0], [2, 0, 6]])print(np.column_stack((a, [5, 7])))
Output
[[4 8 0 5]
 [2 0 6 7]]

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