Numpy mat 将数组转换为矩阵
【摘要】 所属的课程名称及链接[AI基础课程--常用框架工具]环境信息* ModelArts * Notebook - Multi-Engine 2.0 (python3) * JupyterLab - Notebook - Conda-python3 * numpy 1.19.1Numpy np.mat np.matrix np.asmatrix 将数组转换为矩阵import nu...
所属的课程名称及链接
环境信息
- * ModelArts
- * Notebook - Multi-Engine 2.0 (python3)
- * JupyterLab - Notebook - Conda-python3
- * numpy 1.19.1
- * JupyterLab - Notebook - Conda-python3
- * Notebook - Multi-Engine 2.0 (python3)
Numpy np.mat np.matrix np.asmatrix 将数组转换为矩阵
import numpy as np
arr = np.array([[1,2],[3,4]])
print("arr",arr)
print("type(arr)",type(arr))
arr [[1 2]
[3 4]]
type(arr) <class 'numpy.ndarray'>
m = np.mat(arr)
print("m",m)
print("type(m)",type(m))
print(id(arr),id(m))
m [[1 2]
[3 4]]
type(m) <class 'numpy.matrix'>
139898878402768 139898871385560
'''
class matrix(ndarray)
| matrix(data, dtype=None, copy=True)
|
| .. note:: It is no longer recommended to use this class, even for linear
| algebra. Instead use regular arrays. The class may be removed
| in the future.
'''
m2 = np.matrix(arr)
print("m2",m2)
print("type(m2)",type(m2))
print(id(arr),id(m2))
m2 [[1 2]
[3 4]]
type(m2) <class 'numpy.matrix'>
139898878402768 139898871385784
'''
Unlike `matrix`, `asmatrix` does not make a copy if the input is already
a matrix or an ndarray. Equivalent to ``matrix(data, copy=False)``.
'''
m3 = np.asmatrix(arr)
print("m3",m3)
print("type(m3)",type(m3))
print(id(arr),id(m3))
m3 [[1 2]
[3 4]]
type(m3) <class 'numpy.matrix'>
139898878402768 139898871385672
help
help(np.mat)
Help on function asmatrix in module numpy:
asmatrix(data, dtype=None)
Interpret the input as a matrix.
Unlike `matrix`, `asmatrix` does not make a copy if the input is already
a matrix or an ndarray. Equivalent to ``matrix(data, copy=False)``.
Parameters
----------
data : array_like
Input data.
dtype : data-type
Data-type of the output matrix.
Returns
-------
mat : matrix
`data` interpreted as a matrix.
Examples
--------
>>> x = np.array([[1, 2], [3, 4]])
>>> m = np.asmatrix(x)
>>> x[0,0] = 5
>>> m
matrix([[5, 2],
[3, 4]])
help(np.matrix)
Help on class matrix in module numpy:
class matrix(ndarray)
| matrix(data, dtype=None, copy=True)
|
| .. note:: It is no longer recommended to use this class, even for linear
| algebra. Instead use regular arrays. The class may be removed
| in the future.
|
| Returns a matrix from an array-like object, or from a string of data.
| A matrix is a specialized 2-D array that retains its 2-D nature
| through operations. It has certain special operators, such as ``*``
| (matrix multiplication) and ``**`` (matrix power).
|
| Parameters
| ----------
| data : array_like or string
| If `data` is a string, it is interpreted as a matrix with commas
| or spaces separating columns, and semicolons separating rows.
| dtype : data-type
| Data-type of the output matrix.
| copy : bool
| If `data` is already an `ndarray`, then this flag determines
| whether the data is copied (the default), or whether a view is
| constructed.
|
| See Also
| --------
| array
|
| Examples
| --------
| >>> a = np.matrix('1 2; 3 4')
| >>> a
| matrix([[1, 2],
| [3, 4]])
|
| >>> np.matrix([[1, 2], [3, 4]])
| matrix([[1, 2],
| [3, 4]])
|
| Method resolution order:
| matrix
| ndarray
| builtins.object
|
| Methods defined here:
......
help(np.asmatrix)
Help on function asmatrix in module numpy:
asmatrix(data, dtype=None)
Interpret the input as a matrix.
Unlike `matrix`, `asmatrix` does not make a copy if the input is already
a matrix or an ndarray. Equivalent to ``matrix(data, copy=False)``.
Parameters
----------
data : array_like
Input data.
dtype : data-type
Data-type of the output matrix.
Returns
-------
mat : matrix
`data` interpreted as a matrix.
Examples
--------
>>> x = np.array([[1, 2], [3, 4]])
>>> m = np.asmatrix(x)
>>> x[0,0] = 5
>>> m
matrix([[5, 2],
[3, 4]])
备注
1. 感谢老师的教学与课件
2. 欢迎各位同学一起来交流学习心得^_^
3. 沙箱实验、认证、论坛和直播,其中包含了许多优质的内容,推荐了解与学习。
2. 欢迎各位同学一起来交流学习心得^_^
3. 沙箱实验、认证、论坛和直播,其中包含了许多优质的内容,推荐了解与学习。
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)