如何使用numpy在Python中展平一个矩阵?
In this article, we will show you how to flatten a matrix using the NumPy library in python.
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() type
Algorithm (Steps)
以下是执行所需任务的算法/步骤:
使用import关键字,导入带有别名(np)的numpy模块。
使用numpy.array()函数(返回一个ndarray。ndarray是满足给定要求的数组对象),通过将2维数组(2行,2列)作为参数传递给它来创建一个numpy数组。
打印给定的二维矩阵。
在输入矩阵上应用 numpy 模块的 flatten() 函数(将矩阵压平为一维) ,将输入的二维矩阵压平为一维矩阵。
打印输入矩阵的结果扁平化矩阵。
Example
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)
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 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)
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 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)
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中展平一个矩阵?的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

更新numpy版本方法:1、使用“pip install --upgrade numpy”命令;2、使用的是Python 3.x版本,使用“pip3 install --upgrade numpy”命令,将会下载并安装,覆盖当前的NumPy版本;3、若使用的是conda来管理Python环境,使用“conda install --update numpy”命令更新即可。

Numpy是Python中一个重要的数学库,它提供了高效的数组操作和科学计算函数,被广泛应用于数据分析、机器学习、深度学习等领域。在使用numpy过程中,我们经常需要查看numpy的版本号,以便确定当前环境所支持的功能。本文将介绍如何快速查看numpy版本,并提供具体的代码示例。方法一:使用numpy自带的__version__属性numpy模块自带一个__

推荐使用最新版本的NumPy1.21.2。原因是:目前,NumPy的最新稳定版本是1.21.2。通常情况下,推荐使用最新版本的NumPy,因为它包含了最新的功能和性能优化,并且修复了之前版本中的一些问题和错误。

一步步教你在PyCharm中安装NumPy并充分利用其强大功能前言:NumPy是Python中用于科学计算的基础库之一,提供了高性能的多维数组对象以及对数组执行基本操作所需的各种函数。它是大多数数据科学和机器学习项目的重要组成部分。本文将向大家介绍如何在PyCharm中安装NumPy,并通过具体的代码示例展示其强大的功能。第一步:安装PyCharm首先,我们

如何升级numpy版本:简单易懂的教程,需要具体代码示例引言:NumPy是一个重要的Python库,用于科学计算。它提供了一个强大的多维数组对象和一系列与之相关的函数,可用于进行高效的数值运算。随着新版本的发布,不断有更新的特性和Bug修复可供我们使用。本文将介绍如何升级已安装的NumPy库,以获取最新特性并解决已知问题。步骤1:检查当前NumPy版本在开始

numpy可以通过使用pip、conda、源码和Anaconda来安装。详细介绍:1、pip,在命令行中输入pip install numpy即可;2、conda,在命令行中输入conda install numpy即可;3、源码,解压源码包或进入源码目录,在命令行中输入python setup.py build python setup.py install即可。

随着数据科学、机器学习和深度学习等领域的快速发展,Python成为了数据分析和建模的主流语言。在Python中,NumPy(NumericalPython的简称)是一个很重要的库,因为它提供了一组高效的多维数组对象,也是许多其他库如pandas、SciPy和scikit-learn的基础。在使用NumPy过程中,很有可能会遇到不同版本之间的兼容性问题,那么

Numpy安装攻略:一文解决安装难题,需要具体代码示例引言:Numpy是Python中一款强大的科学计算库,它提供了高效的多维数组对象和对数组数据进行操作的工具。但是,对于初学者来说,安装Numpy可能会带来一些困扰。本文将为大家提供一份Numpy安装攻略,以帮助大家快速解决安装难题。一、安装Python环境:在安装Numpy之前,首先需要确保已经安装了Py
