
In this article, we will show you how to flatten a matrix using the NumPy library in python.
The numpy module includes a function called numpy.ndarray.flatten() that returns a one-dimensional copy of the array rather than a two-dimensional or multi-dimensional array.
简单来说,我们可以说它将矩阵压平为1维。
ndarray.flatten(order='C')
order − 'C', 'F', 'A', 'K' (可选)
立即学习“Python免费学习笔记(深入)”;
当我们将排序参数设置为'C,'时,数组按行主序展平。
When the 'F' is set, the array is flattened in column-major order.
只有当 'a' 在内存中是Fortran连续的,并且顺序参数设置为 'A' 时,数组才以列主序展开。最终顺序为 'K',以与内存中元素出现的顺序相同的顺序展开数组。此参数默认设置为 'C'。
Return Value − Returns a flattened 1-D matrix
以下是执行所需任务的算法/步骤:
使用import关键字,导入带有别名(np)的numpy模块。
使用numpy.array()函数(返回一个ndarray。ndarray是满足给定要求的数组对象),通过将2维数组(2行,2列)作为参数传递给它来创建一个numpy数组。
打印给定的二维矩阵。
在输入矩阵上应用 numpy 模块的 flatten() 函数(将矩阵压平为一维) ,将输入的二维矩阵压平为一维矩阵。
打印输入矩阵的结果扁平化矩阵。
The following program flattens the given input 2-Dimensional matrix to a 1-Dimensional matrix using the flatten()function and returns it −
# 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)
在执行时,上述程序将生成以下输出 -
如果你了解HTML,CSS和JavaScript,您已经拥有所需的工具开发Android应用程序。本动手本书展示了如何使用这些开源web标准设计和建造,可适应任何Android设备的应用程序 - 无需使用Java。您将学习如何创建一个在您选择的平台的Android友好的网络应用程序,然后转换与自由PhoneGap框架到一个原生的Android应用程序。了解为什么设备无关的移动应用是未来的潮流,并开始构建应用程序,提供更
2
The input numpy matrix: [[3 5] [4 8]] Resultant flattened matrix: [3 5 4 8]
以下是执行所需任务的算法/步骤:
Use the numpy.array() function(returns a ndarray. The ndarray is an array object that satisfies the given requirements), for creating a numpy array by passing the 4-Dimensional array(4rows, 4columns) as an argument to it.
打印给定的4维矩阵。
通过将NumPy数组的长度与自身相乘来计算矩阵的元素数量。这些值表示所需的列数。
Use the reshape() function(reshapes an array without affecting its data) to reshape the array and flatten the input matrix(4D) to a one-dimensional matrix.
打印输入矩阵的结果扁平化矩阵。
下面的程序使用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]]
的中文翻译为:
以下是执行所需任务的算法/步骤:
使用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函数展平矩阵。
以上就是如何使用numpy在Python中展平一个矩阵?的详细内容,更多请关注php中文网其它相关文章!
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号