2009-11-05 156 views
0

我想将int64 numpy数组转换为uint64 numpy数组,将2 ** 63添加到进程中的值中,以使它们仍处于数组允许的有效范围内。因此,举例来说,如果我从将int64转换为uint64

a = np.array([-2**63,2**63-1], dtype=np.int64) 

开始我想用

np.array([0.,2**64], dtype=np.uint64) 

落得听起来很简单,在第一,但你会如何真正做到这一点?

回答

1

我不是一个真正的numpy的专家,但这样的:

>>> a = np.array([-2**63,2**63-1], dtype=np.int64) 
>>> b = np.array([x+2**63 for x in a], dtype=np.uint64) 
>>> b 
array([     0, 18446744073709551615], dtype=uint64) 

对我的作品与Python 2.6和numpy的1.3.0

我假设你在你的预期意味着2**64-1,不2**64,输出,因为2**64不适合uint64。 (18446744073709551615是2**64-1

2

使用astype():

(a+2**63).astype(uint64) 
# array([     0, 18446744073709551615], dtype=uint64)