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


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

numpy.ndarray.flatten() 函数

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

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

语法

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 行,2 列)作为参数传递给它来创建 NumPy 数组。

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

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

  • 打印输入矩阵的最终展平矩阵。

示例

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

# 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 列)作为参数传递给它来创建 NumPy 数组。

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

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

  • 使用 reshape() 函数(在不影响其数据的情况下重新整形数组)来重新整形数组并将输入矩阵(4D)展平为一维矩阵。

  • 打印输入矩阵的最终展平矩阵。

示例

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

# 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 列)作为参数传递给它来创建 NumPy 矩阵。

  • 打印输入矩阵的最终展平矩阵。

示例

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

# 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 函数展平矩阵。

更新于: 2022 年 10 月 31 日

4K+ 次观看

启动您的 职业生涯

通过完成课程获得认证

开始
广告

© . All rights reserved.