Remove single-dimensional entries

Python Practice › NumPy Programs › Program 62

Remove single-dimensional entries

EasyNumPy Programs · 62 of 100

Write a NumPy program to remove dimensions of size 1 from a shape.

Input
shape (2, 1, 3)
Output
(2, 1, 3) (2, 3)

Approach

  1. np.squeeze.

Solution

Python
import numpy as npa = np.zeros((2, 1, 3))print(a.shape, np.squeeze(a).shape)
Output
(2, 1, 3) (2, 3)

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

The ith element of a 2-D array

Python Practice › NumPy Programs › Program 61

The ith element of a 2-D array

EasyNumPy Programs · 61 of 100

Write a NumPy program to find the ith element (counting row by row) of a 2-D array.

Input
[[4, 8, 7], [2, 3, 6]], i = 5
Output
3 (np.int64(1), np.int64(1))

Approach

  1. flat[i - 1] for the 5th element, or np.unravel_index for its row and column.

Solution

Python
import numpy as npa = np.array([[4, 8, 7], [2, 3, 6]])i = 5print(a.flat[i - 1], np.unravel_index(i - 1, a.shape))
Output
3 (np.int64(1), np.int64(1))

Line by line

Line 4

The 5th element (index 4) is in row 1, column 1.

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Change the data type

Python Practice › NumPy Programs › Program 60

Change the data type

EasyNumPy Programs · 60 of 100

Write a NumPy program to change the data type of an array.

Input
[[4, 8, 7], [2, 3, 6]]
Output
int32
float64
[[4. 8. 7.]
 [2. 3. 6.]]

Approach

  1. astype() returns a converted copy.

Solution

Python
import numpy as npx = np.array([[4, 8, 7], [2, 3, 6]], dtype=np.int32)print(x.dtype)y = x.astype(np.float64)print(y.dtype)print(y)
Output
int32
float64
[[4. 8. 7.]
 [2. 3. 6.]]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Index of the minimum

Python Practice › NumPy Programs › Program 59

Index of the minimum

EasyNumPy Programs · 59 of 100

Write a NumPy program to find the index of the minimum value.

Input
[76, 36, 56, 90]
Output
1

Approach

  1. argmin.

Solution

Python
import numpy as npprint(np.argmin([76, 36, 56, 90]))
Output
1

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Index of the maximum

Python Practice › NumPy Programs › Program 58

Index of the maximum

EasyNumPy Programs · 58 of 100

Write a NumPy program to find the index of the maximum value.

Input
[76, 36, 56, 90]
Output
3

Approach

  1. argmax.

Solution

Python
import numpy as npprint(np.argmax([76, 36, 56, 90]))
Output
3

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Union of two arrays

Python Practice › NumPy Programs › Program 57

Union of two arrays

EasyNumPy Programs · 57 of 100

Write a NumPy program to find the union of two arrays.

Input
[56, 18, 28, 36], [76, 36, 56]
Output
[18 28 36 56 76]

Approach

  1. np.union1d.

Solution

Python
import numpy as npprint(np.union1d([56, 18, 28, 36], [76, 36, 56]))
Output
[18 28 36 56 76]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Set difference

Python Practice › NumPy Programs › Program 56

Set difference

EasyNumPy Programs · 56 of 100

Write a NumPy program to find the unique values of array1 that are not in array2.

Input
[56, 18, 28, 36], [76, 36, 56]
Output
[18 28]

Approach

  1. np.setdiff1d.

Solution

Python
import numpy as npprint(np.setdiff1d([56, 18, 28, 36], [76, 36, 56]))
Output
[18 28]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Unique elements

Python Practice › NumPy Programs › Program 55

Unique elements

EasyNumPy Programs · 55 of 100

Write a NumPy program to get the unique elements of an array.

Input
[25, 33, 10, 45, 33, 10]
Output
[10 25 33 45]
[25 33 10 45]

Approach

  1. np.unique returns them sorted; use the index to keep first-seen order.

Solution

Python
import numpy as npa = np.array([25, 33, 10, 45, 33, 10])print(np.unique(a))_, first = np.unique(a, return_index=True)print(a[np.sort(first)])
Output
[10 25 33 45]
[25 33 10 45]

Line by line

Lines 4-5

return_index gives each value’s first position; sorting those keeps the original order.

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Common elements of two arrays

Python Practice › NumPy Programs › Program 54

Common elements of two arrays

EasyNumPy Programs · 54 of 100

Write a NumPy program to find the common elements of two arrays.

Input
[56, 18, 28, 36], [76, 36, 56]
Output
[36 56]

Approach

  1. np.intersect1d (sorted result).

Solution

Python
import numpy as npprint(np.intersect1d([56, 18, 28, 36], [76, 36, 56]))
Output
[36 56]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Real and imaginary parts

Python Practice › NumPy Programs › Program 53

Real and imaginary parts

EasyNumPy Programs · 53 of 100

Write a NumPy program to find the real and imaginary parts of complex numbers.

Input
[6+2j, 10+5j]
Output
Real: [ 6. 10.] Imaginary: [2. 5.]

Approach

  1. .real and .imag.

Solution

Python
import numpy as npa = np.array([6 + 2j, 10 + 5j])print("Real:", a.real, "Imaginary:", a.imag)
Output
Real: [ 6. 10.] Imaginary: [2. 5.]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs