如何处理 NumPy 中的大整数?

How to deal with large integers in NumPy?

我正在做一个数据分析项目,我正在处理大量数据。我最初在纯 python 中完成所有操作,但现在我正在尝试使用 numpy 和 pandas 来完成。然而,我似乎遇到了障碍,因为在 numpy 中无法处理大于 64 位的整数(如果我在 numpy 中使用 python 整数,它们最大为 9223372036854775807)。我是完全扔掉 numpy 和 pandas 还是有办法将它们与 python 样式的任意大整数一起使用?我可以接受性能下降。

默认情况下,numpy 将元素保留为数字数据类型。 但是你可以强制打字反对,如下所示

import numpy as np
x = np.array([10,20,30,40], dtype=object)
x_exp2 = 1000**x
print(x_exp2)

输出是

[1000000000000000000000000000000
 1000000000000000000000000000000000000000000000000000000000000
 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000]

缺点是执行速度慢很多

稍后编辑 以显示 np.sum() 有效。当然可能会有一些限制。

import numpy as np
x = np.array([10,20,30,40], dtype=object)
x_exp2 = 1000**x

print(x_exp2)
print(np.sum(x_exp2))
print(np.prod(x_exp2))

输出为:

[1000000000000000000000000000000
 1000000000000000000000000000000000000000000000000000000000000
 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
 1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000]
1000000000000000000000000000001000000000000000000000000000001000000000000000000000000000001000000000000000000000000000000
1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000