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

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

Integers 1 to 100 in steps of 6

Python Practice › NumPy Programs › Program 18

Integers 1 to 100 in steps of 6

EasyNumPy Programs · 18 of 100

Write a NumPy program to create an array of integers from 1 to 100 with a difference of 6.

Output
[ 1  7 13 19 25 31 37 43 49 55 61 67 73 79 85 91 97]

Approach

  1. np.arange(1, 101, 6).

Solution

Python
import numpy as npprint(np.arange(1, 101, 6))
Output
[ 1  7 13 19 25 31 37 43 49 55 61 67 73 79 85 91 97]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs

Odd integers from 1 to 20

Python Practice › NumPy Programs › Program 17

Odd integers from 1 to 20

EasyNumPy Programs · 17 of 100

Write a NumPy program to create an array of odd integers from 1 to 20.

Output
[ 1  3  5  7  9 11 13 15 17 19]

Approach

  1. np.arange(1, 21, 2).

Solution

Python
import numpy as npprint(np.arange(1, 21, 2))
Output
[ 1  3  5  7  9 11 13 15 17 19]

Learn the concept

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

More numpy programs

← All 100 NumPy Programs