ValueError: high is out of bounds for int32 报错

文章讨论了在Windows64位系统上运行的Python程序使用numpy库时,由于numpy默认的int32类型与Clong类型不匹配引发的ValueError。作者提供了缩小随机种子范围和明确指定整数类型的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题描述:

笔者在Windows 64位平台跑一个在Ubuntu上运行正常的程序时,出现了以下报错:
在这里插入图片描述
具体为:

     seed = np.random.randint(0, 2 ** 32)  # make a seed with numpy generator
  File "mtrand.pyx", line 763, in numpy.random.mtrand.RandomState.randint
  File "_bounded_integers.pyx", line 1336, in numpy.random._bounded_integers._rand_int32
ValueError: high is out of bounds for int32

定位报错语句为:seed = np.random.randint(0, 2 ** 32)


原因分析:

在Linux系统不报错,但是在Windows 64位系统下运行报错。查询资料发现1,numpy 默认的整数类型(integer type)np.int_C long2,但是C的long类型在win64平台下是 int323,而在其他系统下是 int64,因此产生报错。


解决方案:

  1. 缩小随机种子产生的范围4,即将 2 ** 32 改为 2 ** 31-12 ** 31
  2. 明确定义整数类型5,即改为:
    seed = np.random.randint(0, 2**32 - 1, dtype=np.int64)

  1. numpy array dtype is coming as int32 by default in a windows 10 64 bit machine ↩︎

  2. http://docs.scipy.org/doc/numpy-1.10.1/user/basics.types.html ↩︎

  3. https://msdn.microsoft.com/en-us/library/9c3yd98k.aspx ↩︎

  4. Bug: ValueError: high is out of bounds for int32 #1579 ↩︎

  5. ValueError: high is out of bounds for int32 #20 ↩︎

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yyywxk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值