Access by column

Python Practice › NumPy Programs › Program 72

Access by column

EasyNumPy Programs · 72 of 100

Write a NumPy program to print each column of an array.

Input
[[4, 8, 0], [2, 0, 6]]
Output
Column 1: [4 2]
Column 2: [8 0]
Column 3: [0 6]

Approach

  1. a[:, j] selects column j.

Solution

Python
import numpy as npa = np.array([[4, 8, 0], [2, 0, 6]])for j in range(a.shape[1]):    print(f"Column {j + 1}:", a[:, j])
Output
Column 1: [4 2]
Column 2: [8 0]
Column 3: [0 6]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Square every element

Python Practice › NumPy Programs › Program 71

Square every element

EasyNumPy Programs · 71 of 100

Write a NumPy program to print the squares of all elements.

Input
[2, 6, 3, 1]
Output
[ 4 36  9  1]

Approach

  1. a ** 2 or np.square.

Solution

Python
import numpy as npprint(np.square(np.array([2, 6, 3, 1])))
Output
[ 4 36  9  1]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Print every element

Python Practice › NumPy Programs › Program 70

Print every element

EasyNumPy Programs · 70 of 100

Write a NumPy program to print every element of a 2-D array.

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

Approach

  1. np.nditer or ravel().

Solution

Python
import numpy as npa = np.array([[4, 8, 0], [2, 0, 6]])for x in np.nditer(a):    print(x, end=" ")print()
Output
4 8 0 2 0 6 

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

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