How to create a three-dimensional matrix using Numpy?

To create a three-dimensional matrix using NumPy, you can use the numpy.array function and pass a three-dimensional array as a parameter. Here is an example code:

import numpy as np

# 创建一个3行2列2深度的三维矩阵
matrix = np.array([[[1, 2],
                   [3, 4],
                   [5, 6]],
                  
                  [[7, 8],
                   [9, 10],
                   [11, 12]]])

print(matrix)

The output is:

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

 [[ 7  8]
  [ 9 10]
  [11 12]]]

This creates a 3x2x2 three-dimensional matrix.

bannerAds