如何在 Python 中使用 numpy 展平矩阵?

pythonserver side programmingprogramming更新于 2024/2/18 11:42:00

在本文中,我们将向您展示如何使用 python 中的 NumPy 库展平矩阵。

numpy.ndarray.flatten() 函数

numpy 模块包含一个名为 numpy.ndarray.flatten() 的函数,该函数返回数组的一维副本,而不是二维或多维数组。

简单来说,我们可以说它将矩阵展平为 1 维。

语法

ndarray.flatten(order='C')

参数

order − 'C'、'F'、'A'、'K'(可选)

  • 当我们将 order 参数设置为 'C' 时,数组将按 行主序 展开。

  • 当设置 'F' 时,数组将按 列主序 展开。

  • 仅当 'a' 在内存中是 Fortran 连续的并且 order 参数设置为 'A' 时,数组才会按列主序展开。最终顺序为 'K',它按元素在内存中出现的顺序展开数组。此参数默认设置为"C"。

返回值 - 返回一个扁平化的一维矩阵

方法 1 - 扁平化 np.array() 类型的 2x2 Numpy 矩阵

算法(步骤)

以下是执行所需任务需要遵循的算法/步骤 -

  • 使用 import 关键字,以别名 (np) 导入 numpy 模块。

  • 使用 numpy.array() 函数(返回一个 ndarray。ndarray 是一个满足给定要求的数组对象),通过传递二维数组(2 行, 2columns)作为参数。

  • 打印给定的输入二维矩阵。

  • 在输入矩阵上应用 numpy 模块的 flatten() 函数(将矩阵展平为 1 维),将输入二维矩阵展平为一维矩阵。

  • 打印输入矩阵的结果展平矩阵。

示例

以下程序使用 flatten() 函数将给定的输入二维矩阵展平为 1 维矩阵并返回它 -

# importing numpy module with an alias name import numpy as np # creating a 2-Dimensional(2x2) numpy matrix inputMatrix = np.array([[3, 5], [4, 8]]) # printing the input 2D matrix print("The input numpy matrix:") print(inputMatrix) # flattening the 2D matrix to one-dimensional matrix flattenMatrix = inputMatrix.flatten() # printing the resultant flattened matrix print("Resultant flattened matrix:") print(flattenMatrix)

输出

执行时,上述程序将生成以下输出 -

The input numpy matrix:
[[3 5]
[4 8]]
Resultant flattened matrix:
[3 5 4 8]

方法 2 - 使用 reshape() 函数进行扁平化

算法(步骤)

以下是执行所需任务需要遵循的算法/步骤 -

  • 使用 numpy.array() 函数(返回一个 ndarray。ndarray 是一个满足给定要求的数组对象),通过将 4 维数组(4 行,4 列)作为参数传递给它来创建 numpy 数组。

  • 打印给定的输入 4 维矩阵。

  • 通过将 NumPy 数组的长度与其自身相乘来计算矩阵元素的数量。此处这些值给出了所需的列数。

  • 使用 reshape() 函数(重塑数组而不影响其数据)重塑数组并将输入矩阵(4D)展平为一维矩阵。

  • 打印输入矩阵的结果展平矩阵。

示例

以下程序使用 reshape() 函数将给定的输入 4 维矩阵展平为 1 维矩阵并返回它 -

# importing numpy module with an alias name import numpy as np # creating a 4-Dimensional(4x4) numpy matrix inputMatrix = np.array([[1, 2, 3, 97], [4, 5, 6, 98], [7, 8, 9, 99], [10, 11, 12, 100]]) # Getting the total Number of elements of the matrix matrixSize = len(inputMatrix) * len(inputMatrix) # printing the input 4D matrix print("The input numpy matrix:") print(inputMatrix) # reshaping the array and flattening the 4D matrix to a one-dimensional matrix # here (1,matrixSize(16)) says 1 row and 16 columns(Number of elements) flattenMatrix= np.reshape(inputMatrix, (1, matrixSize)) # printing the resultant flattened matrix print("Resultant flattened matrix:") print(flattenMatrix)

输出

执行时,上述程序将生成以下输出 -

The input numpy matrix:
[[  1   2   3  97]
 [  4   5   6  98]
 [  7   8   9  99]
 [ 10  11  12 100]]
Resultant flattened matrix:
[[  1   2   3  97   4   5   6  98   7   8   9  99  10  11  12 100]]

方法 3 - 展平 np.matrix() 类型的 4x4 Numpy 矩阵

算法(步骤)

以下是执行所需任务需要遵循的算法/步骤 -

  • 使用 numpy.matrix() 函数(从数据字符串或类似数组的对象返回矩阵。生成的矩阵是一个专门的 4D 数组),通过将 4 维数组(4 行,4 列)作为参数传递给它来创建 numpy 矩阵。

  • 打印输入矩阵的结果展平矩阵。

示例

以下程序使用 flatten() 函数将给定的输入 4 维矩阵展平为 1 维矩阵并返回它−

# importing NumPy module with an alias name import numpy as np # creating a NumPy matrix (4x4 matrix) using matrix() method inputMatrix = np.matrix('[11, 1, 8, 2; 11, 3, 9 ,1; 1, 2, 3, 4; 9, 8, 7, 6]') # printing the input 4D matrix print("The input numpy matrix:") print(inputMatrix) # flattening the 4D matrix to one-dimensional matrix flattenMatrix = inputMatrix.flatten() # printing the resultant flattened matrix print("Resultant flattened matrix:") print(flattenMatrix)

输出

执行时,上述程序将生成以下输出 -

The input numpy matrix:
[[11  1  8  2]
 [11  3  9  1]
 [ 1  2  3  4]
 [ 9  8  7  6]]
Resultant flattened matrix:
[[11  1  8  2 11  3  9  1  1  2  3  4  9  8  7  6]]

结论

在本文中,我们通过三个不同的示例学习了如何在 Python 中展平矩阵。我们学习了如何使用两种不同的方法在 Numpy 中获取矩阵:numpy.array() 和 NumPy.matrix()。我们还学习了如何使用 reshape 函数展平矩阵。


相关文章