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

Five standard normal numbers

Python Practice › NumPy Programs › Program 22

Five standard normal numbers

EasyNumPy Programs · 22 of 100

Write a NumPy program to generate 5 random numbers from a standard normal distribution.

Output
[ 0.001  0.299 -0.274 -0.891 -0.455]

Approach

  1. rng.standard_normal(5).

Solution

Python
import numpy as nprng = np.random.default_rng(7)print(rng.standard_normal(5).round(3))
Output
[ 0.001  0.299 -0.274 -0.891 -0.455]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Random numbers between 1 and 2

Python Practice › NumPy Programs › Program 21

Random numbers between 1 and 2

EasyNumPy Programs · 21 of 100

Write a NumPy program to generate random numbers between 1 and 2.

Output
[1.774 1.439 1.859 1.697 1.094]

Approach

  1. rng.uniform(1, 2, size) with a seeded generator.

Solution

Python
import numpy as nprng = np.random.default_rng(42)print(rng.uniform(1, 2, 5).round(3))
Output
[1.774 1.439 1.859 1.697 1.094]

Line by line

Line 2

The seed makes the numbers repeatable.

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

3 × 3 identity matrix

Python Practice › NumPy Programs › Program 20

3 × 3 identity matrix

EasyNumPy Programs · 20 of 100

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

Output
[[1 0 0]
 [0 1 0]
 [0 0 1]]

Approach

  1. np.eye(3).

Solution

Python
import numpy as npprint(np.eye(3, dtype=int))
Output
[[1 0 0]
 [0 1 0]
 [0 0 1]]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

3 × 3 matrix

Python Practice › NumPy Programs › Program 19

3 × 3 matrix

EasyNumPy Programs · 19 of 100

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

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

Approach

  1. arange(9).reshape(3, 3).

Solution

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

Learn the concept

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

More numpy programs

← All 100 NumPy Programs