[Python] Summary of Typical Matrix Generation Methods Often Used in NumPy (Identity Matrix, Zero Matrix, etc.)

When performing matrix calculations using Python’s numerical calculation library NumPy, data is rarely populated from the start. Often, you need to create a matrix with a specific pattern, such as “all elements are 0” or “only the diagonal is 1,” before starting processing.

Here, I will explain four representative matrix generation functions frequently used for variable initialization in data analysis and machine learning algorithm implementation.

目次

List of Functions Introduced

NumPy provides functions to easily create matrices with specific shapes and values.

Function NameRoleCharacteristics of Generated Matrix
np.eye(N)Identity MatrixSquare matrix with diagonal elements as 1, others as 0
np.zeros((N, M))Zero MatrixMatrix with all elements as 0
np.tri(N)Triangular MatrixMatrix with 1s on/below the diagonal, 0s above
np.ones((N, M))Ones MatrixMatrix with all elements as 1

Let’s look at the usage and execution results for each.

1. Generating an Identity Matrix (np.eye)

An Identity matrix is a square matrix where all diagonal components (the line from top-left to bottom-right) are 1, and all other components are 0. In matrix multiplication, it plays a role similar to the number “1” (multiplying by it does not change the value).

Specify the matrix size (number of rows/columns) in the argument N.

import numpy as np

# Generate a 4x4 identity matrix
e = np.eye(4)
print(e)

Execution Result

[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]

2. Generating a Zero Matrix (np.zeros)

A Zero matrix is a matrix where all elements are 0.

This is most often used as an initialization process, for example, when you want to prepare an “empty box” to store calculation results.

Specify the arguments as a tuple in the format (rows, columns).

import numpy as np

# Generate a 2-row, 3-column zero matrix
zero = np.zeros((2, 3))
print(zero)

Execution Result

[[0. 0. 0.]
 [0. 0. 0.]]

3. Generating a Triangular Matrix (np.tri)

np.tri generates a “lower triangular matrix” where the diagonal and elements below it are 1, and elements above are 0. This is sometimes used in specific filtering processes.

Specify the size in the argument N.

import numpy as np

# Generate a 4x4 lower triangular matrix
tr = np.tri(4)
print(tr)

Execution Result

[[1. 0. 0. 0.]
 [1. 1. 0. 0.]
 [1. 1. 1. 0.]
 [1. 1. 1. 1.]]

4. Generating a Matrix with All Elements as 1 (np.ones)

This is a matrix where all elements are 1.

Like the zero matrix, it is used for initialization, or as a base for broadcast operations such as “adding the same value to all elements.”

Specify the arguments as a tuple of (rows, columns).

import numpy as np

# Generate a 3-row, 2-column matrix with all elements as 1
ones = np.ones((3, 2))
print(ones)

Execution Result

[[1. 1.]
 [1. 1.]
 [1. 1.]]

Summary

In matrix calculations with NumPy, it is rare to manually define matrices by writing lists. Basically, the general flow is to create a “frame” using np.zeros or np.ones introduced here, and then substitute calculation results into it.

These functions generate matrices of float64 (floating-point number) type by default. If you need an integer type matrix, remember that you can change the data type by adding dtype=int to the arguments.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次