Numpy 的asfarray(~)
將數組轉換為float
類型。
參數
1. a
| array-like
要對其執行該方法的數組。
2. dtype
| str
或 type
| optional
將輸入數組轉換為的浮點類型。如果輸入數組的類型為 int
,則它將轉換為 float64
。
返回值
float
類型的 Numpy 數組。
例子
轉換為 float64
x = [1,2,3]
y = np.asfarray(x)
y.dtype
dtype('float64')
提醒一下,所有 int
都會轉換為 float64
。
轉換為 float32
我們需要指定dtype="float32"
:
x = [1,2,3]
y = np.asfarray(x, dtype="float32")
y.dtype
dtype('float32')
原始數組保持不變
請注意,原始數組保持不變,即:
x = np.array([1,2,3])
y = np.asfarray(x)
x.dtype
dtype('int64')
這裏, y 的類型為 float
,但 x 的類型為 int
。
相關用法
- 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 | asfarray method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。