Make an array read-only

Python Practice › NumPy Programs › Program 69

Make an array read-only

EasyNumPy Programs · 69 of 100

Write a NumPy program to make an array immutable.

Output
Error: assignment destination is read-only

Approach

  1. Set flags.writeable = False.

Solution

Python
import numpy as npa = np.array([1, 2, 3])a.flags.writeable = Falsetry:    a[0] = 10except ValueError as e:    print("Error:", e)
Output
Error: assignment destination is read-only

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Median of each column

Python Practice › NumPy Programs › Program 68

Median of each column

EasyNumPy Programs · 68 of 100

Write a NumPy program to find the median of each column.

Input
[[4, 8, 1], [2, 9, 6]]
Output
[3.  8.5 3.5]

Approach

  1. axis=0.

Solution

Python
import numpy as npprint(np.median(np.array([[4, 8, 1], [2, 9, 6]]), axis=0))
Output
[3.  8.5 3.5]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Median of each row

Python Practice › NumPy Programs › Program 67

Median of each row

EasyNumPy Programs · 67 of 100

Write a NumPy program to find the median of each row.

Input
[[4, 8, 1], [2, 9, 6]]
Output
[4. 6.]

Approach

  1. axis=1.

Solution

Python
import numpy as npprint(np.median(np.array([[4, 8, 1], [2, 9, 6]]), axis=1))
Output
[4. 6.]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Median of a matrix

Python Practice › NumPy Programs › Program 66

Median of a matrix

EasyNumPy Programs · 66 of 100

Write a NumPy program to find the median of all values in a matrix.

Input
[[4, 8, 1], [2, 9, 6]]
Output
5.0

Approach

  1. np.median.

Solution

Python
import numpy as npm = np.array([[4, 8, 1], [2, 9, 6]])print(np.median(m))
Output
5.0

Line by line

Line 3

Sorted values are 1 2 4 6 8 9, so the median is (4 + 6) / 2 = 5.0.

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Count non-zero elements

Python Practice › NumPy Programs › Program 65

Count non-zero elements

EasyNumPy Programs · 65 of 100

Write a NumPy program to count the non-zero elements.

Input
[[4, 8, 0], [2, 0, 6]]
Output
4

Approach

  1. np.count_nonzero.

Solution

Python
import numpy as npprint(np.count_nonzero([[4, 8, 0], [2, 0, 6]]))
Output
4

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Split into 2, 3 and 5 elements

Python Practice › NumPy Programs › Program 64

Split into 2, 3 and 5 elements

EasyNumPy Programs · 64 of 100

Write a NumPy program to split 10 elements into arrays of 2, 3 and 5 elements.

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

Approach

  1. np.split with the split positions [2, 5].

Solution

Python
import numpy as npfor part in np.split(np.arange(1, 11), [2, 5]):    print(part)
Output
[1 2]
[3 4 5]
[ 6  7  8  9 10]

Line by line

Line 2

Splitting at indexes 2 and 5 gives sizes 2, 3 and 5.

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

1-D arrays as columns

Python Practice › NumPy Programs › Program 63

1-D arrays as columns

EasyNumPy Programs · 63 of 100

Write a NumPy program to combine two 1-D arrays as columns of a 2-D array.

Input
(3, 8, 5), (4, 0, 7)
Output
[[3 4]
 [8 0]
 [5 7]]

Approach

  1. np.column_stack.

Solution

Python
import numpy as npprint(np.column_stack(((3, 8, 5), (4, 0, 7))))
Output
[[3 4]
 [8 0]
 [5 7]]

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