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

Add one row to another

Python Practice › NumPy Programs › Program 52

Add one row to another

EasyNumPy Programs · 52 of 100

Write a NumPy program to add row 2 to row 1.

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

Approach

  1. a[0] += a[1].

Solution

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

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Multiply a row by a scalar

Python Practice › NumPy Programs › Program 51

Multiply a row by a scalar

EasyNumPy Programs · 51 of 100

Write a NumPy program to multiply row 1 of an array by 2.

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

Approach

  1. Index the row and use *=.

Solution

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

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Reverse the row order

Python Practice › NumPy Programs › Program 50

Reverse the row order

EasyNumPy Programs · 50 of 100

Write a NumPy program to swap the rows of an array (first and last).

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

Approach

  1. Fancy indexing with a new row order.

Solution

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

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Replace numbers at or above a value

Python Practice › NumPy Programs › Program 49

Replace numbers at or above a value

EasyNumPy Programs · 49 of 100

Write a NumPy program to replace numbers greater than or equal to 8 with 0.

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

Approach

  1. np.where(condition, new, old).

Solution

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

Learn the concept

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

More numpy programs

← All 100 NumPy Programs