Sum of each column

Python Practice › NumPy Programs › Program 37

Sum of each column

EasyNumPy Programs · 37 of 100

Write a NumPy program to calculate the sum of each column.

Input
[[5, 3], [7, 4]]
Output
[12  7]

Approach

  1. sum(axis=0).

Solution

Python
import numpy as npprint(np.array([[5, 3], [7, 4]]).sum(axis=0))
Output
[12  7]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Sum of each row

Python Practice › NumPy Programs › Program 36

Sum of each row

EasyNumPy Programs · 36 of 100

Write a NumPy program to calculate the sum of each row.

Input
[[5, 3], [7, 4]]
Output
[ 8 11]

Approach

  1. sum(axis=1).

Solution

Python
import numpy as npprint(np.array([[5, 3], [7, 4]]).sum(axis=1))
Output
[ 8 11]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Sum of all elements

Python Practice › NumPy Programs › Program 35

Sum of all elements

EasyNumPy Programs · 35 of 100

Write a NumPy program to calculate the sum of all elements.

Input
[[5, 3], [7, 4]]
Output
19

Approach

  1. .sum().

Solution

Python
import numpy as npprint(np.array([[5, 3], [7, 4]]).sum())
Output
19

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Diagonal matrix

Python Practice › NumPy Programs › Program 34

Diagonal matrix

EasyNumPy Programs · 34 of 100

Write a NumPy program to create a 2 × 2 zero matrix with 7 and 8 on the main diagonal.

Output
[[7 0]
 [0 8]]

Approach

  1. np.diag([7, 8]).

Solution

Python
import numpy as npprint(np.diag([7, 8]))
Output
[[7 0]
 [0 8]]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Reshape 2 × 3 into 3 × 2

Python Practice › NumPy Programs › Program 33

Reshape 2 × 3 into 3 × 2

EasyNumPy Programs · 33 of 100

Write a NumPy program to reshape a 2 × 3 matrix into a 3 × 2 matrix.

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

Approach

  1. reshape(3, 2); the number of items must stay the same.

Solution

Python
import numpy as npm = np.array([[5, 6, 7], [8, 9, 10]])print(m.reshape(3, 2))
Output
[[ 5  6]
 [ 7  8]
 [ 9 10]]
Note

The original task said “2 × 2”, but six values cannot fit in a 2 × 2 matrix; its sample output was 3 × 2.

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Shape of a matrix

Python Practice › NumPy Programs › Program 32

Shape of a matrix

EasyNumPy Programs · 32 of 100

Write a NumPy program to find the shape of a matrix.

Input
[[5, 6, 7], [8, 9, 10]]
Output
(2, 3)

Approach

  1. .shape.

Solution

Python
import numpy as npprint(np.array([[5, 6, 7], [8, 9, 10]]).shape)
Output
(2, 3)

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

2 × 3 matrix from 5 to 10

Python Practice › NumPy Programs › Program 31

2 × 3 matrix from 5 to 10

EasyNumPy Programs · 31 of 100

Write a NumPy program to create a 2 × 3 matrix with values from 5 to 10.

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

Approach

  1. np.arange(5, 11).reshape(2, 3).

Solution

Python
import numpy as npprint(np.arange(5, 11).reshape(2, 3))
Output
[[ 5  6  7]
 [ 8  9 10]]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Multiply two vectors

Python Practice › NumPy Programs › Program 30

Multiply two vectors

EasyNumPy Programs · 30 of 100

Write a NumPy program to multiply two vectors element-wise.

Input
[4, 2, 8], [7, 3, 5]
Output
[28  6 40]

Approach

  1. * multiplies element by element.

Solution

Python
import numpy as npprint(np.array([4, 2, 8]) * np.array([7, 3, 5]))
Output
[28  6 40]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Change the sign of a range

Python Practice › NumPy Programs › Program 29

Change the sign of a range

EasyNumPy Programs · 29 of 100

Write a NumPy program to create a vector of 1 to 15 and change the sign of the numbers from 6 to 10.

Output
[  1   2   3   4   5  -6  -7  -8  -9 -10  11  12  13  14  15]

Approach

  1. Use a boolean mask and multiply by -1.

Solution

Python
import numpy as npv = np.arange(1, 16)v[(v >= 6) & (v <= 10)] *= -1print(v)
Output
[  1   2   3   4   5  -6  -7  -8  -9 -10  11  12  13  14  15]

Line by line

Line 3

The mask selects 6 to 10 and *= -1 flips their sign in place.

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

2 × 2 matrix

Python Practice › NumPy Programs › Program 28

2 × 2 matrix

EasyNumPy Programs · 28 of 100

Write a NumPy program to create a 2 × 2 matrix.

Output
[[1 2]
 [3 4]]

Approach

  1. From a nested list.

Solution

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

Learn the concept

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

More numpy programs

← All 100 NumPy Programs