gpt4 book ai didi

python - Pandas series.apply 出现溢出错误

转载 作者:太空宇宙 更新时间:2023-11-04 05:42:37 24 4
gpt4 key购买 nike

我有一个函数可以很好地处理单个值,但是当我将它与 pandas series.apply() 一起使用时,它会出现 OverflowError。

from __future__ import division
import pandas as pd
import numpy as np

birthdays = pd.DataFrame(np.empty([365,2]), columns = ['k','probability'], index = range(1,366))
birthdays['k'] = birthdays.index

我做了一个函数:

def probability_of_shared_bday(k):
end_point = 366 - k
numerator = 1
for i in range(end_point, 366):
numerator = numerator*i
denominator = 365**k
probability_of_no_match = (1 - numerator/denominator)
return probability_of_no_match

当我用单个整数尝试这个时,它工作正常:

 probability_of_shared_bday(1)

0.0

 probability_of_shared_bday(100)

0.9999996927510721

但是当我尝试将此函数与应用一起使用时:

birthdays['probability'] = birthdays['k'].apply(probability_of_shared_bday, convert_dtype=False)

OverflowError:整数除法结果对于 float 来说太大

无论 convert_dtype 是 True 还是 False,都会发生这种情况。

检查 birthdays['k'].dtypes 我得到 dtype('int64')

最佳答案

我不确定为什么 apply 会出现这个问题,但您不应该像最初那样编写函数。这里有一个避免将两个大数相除的建议:

def probability_of_shared_bday(k):
end_point = 366 - k
ratio = 1
for i in range(end_point, 366):
ratio *= i / 365
probability_of_no_match = (1 - ratio)
return probability_of_no_match

问题就迎刃而解了!

关于python - Pandas series.apply 出现溢出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33342592/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com