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

2 × 3 matrix

Python Practice › NumPy Programs › Program 27

2 × 3 matrix

EasyNumPy Programs · 27 of 100

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

Output
[[1 2 3]
 [4 5 6]]

Approach

  1. Reshape 6 numbers.

Solution

Python
import numpy as npprint(np.arange(1, 7).reshape(2, 3))
Output
[[1 2 3]
 [4 5 6]]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

3 × 4 matrix

Python Practice › NumPy Programs › Program 26

3 × 4 matrix

EasyNumPy Programs · 26 of 100

Write a NumPy program to create a 3 × 4 matrix.

Output
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]

Approach

  1. Reshape 12 numbers.

Solution

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

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

All values except 1

Python Practice › NumPy Programs › Program 25

All values except 1

EasyNumPy Programs · 25 of 100

Write a NumPy program to print all values of the vector 1-20 except 1.

Output
[ 2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20]

Approach

  1. v[1:].

Solution

Python
import numpy as npv = np.arange(1, 21)print(v[1:])
Output
[ 2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

All values except 20

Python Practice › NumPy Programs › Program 24

All values except 20

EasyNumPy Programs · 24 of 100

Write a NumPy program to print all values of the vector 1-20 except 20.

Output
[ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]
[ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]

Approach

  1. Slice off the last element, or filter.

Solution

Python
import numpy as npv = np.arange(1, 21)print(v[:-1])print(v[v != 20])
Output
[ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]
[ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Vector from 1 to 20

Python Practice › NumPy Programs › Program 23

Vector from 1 to 20

EasyNumPy Programs · 23 of 100

Write a NumPy program to create a vector with values 1 to 20.

Output
[ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20] (20,)

Approach

  1. np.arange(1, 21).

Solution

Python
import numpy as npv = np.arange(1, 21)print(v, v.shape)
Output
[ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20] (20,)

Learn the concept

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

More numpy programs

← All 100 NumPy Programs