python - Numpy 中 np.int64(x) 和 x.astype(np.int64) 的区别
问题描述
如果我有一个x
要转换为特定数组的数组,dtype
推荐的方法是x.astype(dtype)
. 但是,您似乎也可以dtype
直接通过dtype(x)
. 这是来自 IPython 会话的示例:
In [1]: import numpy as np
In [2]: x = np.arange(12, dtype=float)
In [3]: x
Out[3]: array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11.])
In [4]: x.dtype
Out[4]: dtype('float64')
In [5]: np.int64(x)
Out[5]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], dtype=int64)
In [6]: x.astype(np.int64)
Out[6]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], dtype=int64)
这两种方法有什么区别?该astype
方法允许您控制复制、强制转换规则以及子类是否正常等内容。另一种方法实际上是做什么的?它是特定设置的别名吗?
解决方案
推荐阅读
- azure - 使用 ARM 模板创建 Azure Web 应用程序后,我们可以将 Web 应用程序名称作为参数传递给 YAML 管道吗?
- python - 如何使用 matplotlib 绘制变化数组的方差?
- javascript - 在Javascript中对数组进行数字排序
- php - 使用已弃用的 create_function 进行排序
- python - 理解列表中使用的 Pop() 未从初始列表中删除所有项目
- c# - VS C#中的依赖地狱,找不到依赖关系
- r - 警告消息:1:未知或未初始化的列:“时间戳”。r
- java - 计算一个 JSONArray 有多少特定的 JSONObjects
- bash - docker中的链式睡眠命令如何组成?
- c# - 无法登录授权升级到 Umbraco 8.1