小白教程
所有教程
关于
Search
172.70.100.63
172.70.100.63
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“NumPy 矩阵库(Matrix)”的源代码
本页内容
上一节:
NumPy_副本和视图
下一节:
NumPy_线性代数
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:NumPy 矩阵库(Matrix)}}[[Category:NumPy 教程|22]] = NumPy 矩阵库(Matrix) = NumPy 中包含了一个矩阵库 numpy.matlib,该模块中的函数返回的是一个矩阵,而不是 ndarray 对象。 一个 的矩阵是一个由行(row)列(column)元素排列成的矩形阵列。 矩阵里的元素可以是数字、符号或数学式。以下是一个由 6 个数字元素构成的 2 行 3 列的矩阵: === 转置矩阵 === NumPy 中除了可以使用 numpy.transpose 函数来对换数组的维度,还可以使用 T 属性。。 例如有个 m 行 n 列的矩阵,使用 t() 函数就能转换为 n 行 m 列的矩阵。 <sample title="" desc="" lang="python" hererun="1"> import numpy as np a = np.arange(12).reshape(3,4) print ('原数组:') print (a) print ('\n') print ('转置数组:') print (a.T) </sample> 输出结果如下: <sample title="" desc="" lang="python" hererun="1"> 原数组: [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] 转置数组: [[ 0 4 8] [ 1 5 9] [ 2 6 10] [ 3 7 11]] </sample> === matlib.empty() === matlib.empty() 函数返回一个新的矩阵,语法格式为: <sample title="" desc="" lang="python" hererun="1"> numpy.matlib.empty(shape, dtype, order) </sample> '''参数说明:''' * '''shape''': 定义新矩阵形状的整数或整数元组 * '''Dtype''': 可选,数据类型 * '''order''': C(行序优先) 或者 F(列序优先) <sample title="" desc="" lang="python" hererun="1"> import numpy.matlib import numpy as np print (np.matlib.empty((2,2))) # 填充为随机数据 </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> [[-1.49166815e-154 -1.49166815e-154] [ 2.17371491e-313 2.52720790e-212]] </sample> === numpy.matlib.zeros() === numpy.matlib.zeros() 函数创建一个以 0 填充的矩阵。 <sample title="" desc="" lang="python" hererun="1"> import numpy.matlib import numpy as np print (np.matlib.zeros((2,2))) </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> [[0. 0.] [0. 0.]] </sample> === numpy.matlib.ones() === numpy.matlib.ones()函数创建一个以 1 填充的矩阵。 <sample title="" desc="" lang="python" hererun="1"> import numpy.matlib import numpy as np print (np.matlib.ones((2,2))) </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> [[1. 1.] [1. 1.]] </sample> === numpy.matlib.eye() === numpy.matlib.eye() 函数返回一个矩阵,对角线元素为 1,其他位置为零。 <sample title="" desc="" lang="python" hererun="1"> numpy.matlib.eye(n, M,k, dtype) </sample> '''参数说明:''' * '''n''': 返回矩阵的行数 * '''M''': 返回矩阵的列数,默认为 n * '''k''': 对角线的索引 * '''dtype''': 数据类型 <sample title="" desc="" lang="python" hererun="1"> import numpy.matlib import numpy as np print (np.matlib.eye(n = 3, M = 4, k = 0, dtype = float)) </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> [[1. 0. 0. 0.] [0. 1. 0. 0.] [0. 0. 1. 0.]] </sample> === numpy.matlib.identity() === numpy.matlib.identity() 函数返回给定大小的单位矩阵。 单位矩阵是个方阵,从左上角到右下角的对角线(称为主对角线)上的元素均为 1,除此以外全都为 0。 <sample title="" desc="" lang="python" hererun="1"> import numpy.matlib import numpy as np # 大小为 5,类型位浮点型 print (np.matlib.identity(5, dtype = float)) </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> [[ 1. 0. 0. 0. 0.] [ 0. 1. 0. 0. 0.] [ 0. 0. 1. 0. 0.] [ 0. 0. 0. 1. 0.] [ 0. 0. 0. 0. 1.]] </sample> === numpy.matlib.rand() === numpy.matlib.rand() 函数创建一个给定大小的矩阵,数据是随机填充的。 <sample title="" desc="" lang="python" hererun="1"> import numpy.matlib import numpy as np print (np.matlib.rand(3,3)) </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> [[0.23966718 0.16147628 0.14162 ] [0.28379085 0.59934741 0.62985825] [0.99527238 0.11137883 0.41105367]] </sample> 矩阵总是二维的,而 ndarray 是一个 n 维数组。 两个对象都是可互换的。 <sample title="" desc="" lang="python" hererun="1"> import numpy.matlib import numpy as np i = np.matrix('1,2;3,4') print (i) </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> [[1 2] [3 4]] </sample> <sample title="" desc="" lang="python" hererun="1"> import numpy.matlib import numpy as np j = np.asarray(i) print (j) </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> [[1 2] [3 4]] </sample> <sample title="" desc="" lang="python" hererun="1"> import numpy.matlib import numpy as np k = np.asmatrix (j) print (k) </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> [[1 2] [3 4]] </sample>
返回至“
NumPy 矩阵库(Matrix)
”。
上一节:
NumPy_副本和视图
下一节:
NumPy_线性代数