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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。