NumPy 的 asmatrix(~)
方法根據數據序列(例如數組和元組)構造矩陣。
參數
1. a
| array-like
用於構造矩陣的數據序列。
注意
如果 a
是 NumPy 數組或矩陣,則不會進行複製 - 修改 asmatrix(~)
的結果也會自動修改 a
。
2. dtype
| string
或 type
| optional
NumPy 數組中存儲的數據類型。默認情況下,將推斷類型。
返回值
NumPy 矩陣。
例子
基本用法
要創建 2 x 2 矩陣:
np.asmatrix([[4,5],[6,7]])
matrix([[4, 5],
[6, 7]])
數組不被複製的情況
假設我們從 NumPy 數組創建一個 NumPy 矩陣,如下所示:
a = np.array([[4,5],[6,7]])
b = np.asmatrix(a)
當我們修改b
的內容時:
b[0,0] = 9
b
matrix([[9, 5],
[6, 7]])
這也會自動修改a
:
a
array([[9, 5],
[6, 7]])
這裏發生的情況是,NumPy 對象a
和b
實際上共享相同的內存空間,因此修改一個對象將涉及修改另一個對象。請注意,隻有當我們嘗試在 NumPy 數組或矩陣上調用 asmatrix(~)
時,才會發生這種情況。
相關用法
- Python ast.MatchClass用法及代碼示例
- Python ast.ListComp用法及代碼示例
- Python ast.Lambda用法及代碼示例
- Python asyncio.BaseTransport.get_extra_info用法及代碼示例
- Python ast.IfExp用法及代碼示例
- Python unittest assertNotIsInstance()用法及代碼示例
- Python ast.Return用法及代碼示例
- Python Tkinter askopenfile()用法及代碼示例
- Python ast.Subscript用法及代碼示例
- Python asyncio.shield用法及代碼示例
- Python asyncio.run用法及代碼示例
- Python unittest assertIsNotNone()用法及代碼示例
- Python NumPy asscalar方法用法及代碼示例
- Python asyncio.wait_for用法及代碼示例
- Python asyncio.create_task用法及代碼示例
- Python Tkinter asksaveasfile()用法及代碼示例
- Python asyncio.Task.cancel用法及代碼示例
- Python ast.alias用法及代碼示例
- Python asyncio.loop.run_in_executor用法及代碼示例
- Python ast.Slice用法及代碼示例
- Python asyncio.Server用法及代碼示例
- Python asyncio.Server.serve_forever用法及代碼示例
- Python unittest assertIs()用法及代碼示例
- Python ast.NamedExpr用法及代碼示例
- Python asyncio.Event用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 NumPy | asmatrix method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。