本文介绍了将 ndarray 从 float64 转换为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 python 中有一个 ndarray
和 float64
的 dtype
.我想将数组转换为整数数组.我该怎么做?
int()
不起作用,因为它说它无法将其转换为标量.更改 dtype
字段本身显然不起作用,因为实际字节没有改变.我似乎在 Google 或文档中找不到任何内容 - 执行此操作的最佳方法是什么?
解决方案
使用 .astype
.
请参阅文档了解更多信息选项.
I've got an ndarray
in python with a dtype
of float64
. I'd like to convert the array to be an array of integers. How should I do this?
int()
won't work, as it says it can't convert it to a scalar. Changing the dtype
field itself obviously doesn't work, as the actual bytes haven't changed. I can't seem to find anything on Google or in the documentation - what's the best way to do this?
解决方案
Use .astype
.
>>> a = numpy.array([1, 2, 3, 4], dtype=numpy.float64)
>>> a
array([ 1., 2., 3., 4.])
>>> a.astype(numpy.int64)
array([1, 2, 3, 4])
See the documentation for more options.
这篇关于将 ndarray 从 float64 转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!