将ndarray从float64转换为整数 [英] Convert ndarray from float64 to integer
本文介绍了将ndarray从float64转换为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在python中有一个ndarray
,其中dtype
为float64
.我想将数组转换为整数数组.我该怎么办?
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()
无效,因为它不能将其转换为标量.更改dtype
字段本身显然不起作用,因为实际字节没有更改.我似乎在Google或文档中找不到任何内容-最好的方法是什么?
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?
推荐答案
使用.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转换为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文