Numpy 的 asarray(~)
方法根據提供的對象構造 Numpy 數組,該對象通常是列表或元組。
參數
1. a
| array-like
用於構造 Numpy 數組的類似數組的對象(例如通常是列表和元組)。
2. dtype
| string
或 type
| optional
Numpy 數組中存儲的數據類型。默認情況下,將推斷類型。
返回值
具有指定數據類型的 Numpy 數組。
例子
使用列表創建 Numpy 數組
np.asarray([1,2,3])
array([1, 2, 3])
數據的推斷類型為 int
。
使用元組創建 Numpy 數組
np.asarray((1,2,3))
array([1, 2, 3])
創建具有顯式類型的 Numpy 數組
np.asarray((1,2,3), float)
np.asarray((1,2,3), "float")
array([1., 2., 3.])
請注意我們如何使用 1.
而不僅僅是 1
- 這意味著 Numpy 數組中的數據類型為 float
而不是 int
。您還可以以字符串形式提供 dtype
,例如 "float"
。
創建 2D Numpy 數組
np.asarray([[1, 2, 3], [4, 5, 6]])
array([[1, 2, 3],
[4, 5, 6]])
相關用法
- 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 | asarray method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。