numpy.ndarray.flatten()函数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' (可选)
当我们将排序参数设置为'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
method 1 − flattening 2x2 numpy matrix of np.array() typealgorithm (steps)以下是执行所需任务的算法/步骤:
使用import关键字,导入带有别名(np)的numpy模块。
使用numpy.array()函数(返回一个ndarray。ndarray是满足给定要求的数组对象),通过将2维数组(2行,2列)作为参数传递给它来创建一个numpy数组。
打印给定的二维矩阵。
在输入矩阵上应用 numpy 模块的 flatten() 函数(将矩阵压平为一维) ,将输入的二维矩阵压平为一维矩阵。
打印输入矩阵的结果扁平化矩阵。
examplethe 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 nameimport numpy as np# creating a 2-dimensional(2x2) numpy matrixinputmatrix = np.array([[3, 5], [4, 8]])# printing the input 2d matrixprint(the input numpy matrix:)print(inputmatrix)# flattening the 2d matrix to one-dimensional matrixflattenmatrix = inputmatrix.flatten()# printing the resultant flattened matrixprint(resultant flattened matrix:)print(flattenmatrix)
output在执行时,上述程序将生成以下输出 -
the input numpy matrix:[[3 5][4 8]]resultant flattened matrix:[3 5 4 8]
method 2 − flattening using reshape() function
algorithm (steps)以下是执行所需任务的算法/步骤:
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 nameimport numpy as np# creating a 4-dimensional(4x4) numpy matrixinputmatrix = 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 matrixmatrixsize = len(inputmatrix) * len(inputmatrix)# printing the input 4d matrixprint(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 matrixprint(resultant flattened matrix:)print(flattenmatrix)
output在执行时,上述程序将生成以下输出 -
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]]
method 3 − flattening 4x4 numpy matrix of np.matrix() type
的中文翻译为:方法3-将np.matrix()类型的4x4 numpy矩阵展平
algorithm (steps)以下是执行所需任务的算法/步骤:
使用numpy.matrix()函数(从数据字符串或类似数组的对象返回一个矩阵。生成的矩阵是一个专门的4d数组),通过将4维数组(4行,4列)作为参数传递给它来创建一个numpy矩阵。
打印输入矩阵的结果扁平化矩阵。
示例以下程序使用flatten()函数将给定的4维矩阵展平为1维矩阵,并返回结果 -
# importing numpy module with an alias nameimport numpy as np# creating a numpy matrix (4x4 matrix) using matrix() methodinputmatrix = np.matrix('[11, 1, 8, 2; 11, 3, 9 ,1; 1, 2, 3, 4; 9, 8, 7, 6]')# printing the input 4d matrixprint(the input numpy matrix:)print(inputmatrix)# flattening the 4d matrix to one-dimensional matrixflattenmatrix = inputmatrix.flatten()# printing the resultant flattened matrixprint(resultant flattened matrix:)print(flattenmatrix)
output在执行时,上述程序将生成以下输出 -
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]]
conclusion在这篇文章中,我们学习了如何使用三个不同的示例在python中展平矩阵。我们学习了如何使用两种不同的方法在numpy中获取矩阵:numpy.array()和numpy.matrix()。我们还学习了如何使用reshape函数展平矩阵。
以上就是如何使用numpy在python中展平一个矩阵?的详细内容。
