ValueError: high is out of bounds for int32 报错

问题描述:

笔者在Windows 64位平台跑一个在Ubuntu上运行正常的程序时,出现了以下报错:
ValueError: high is out of bounds for int32 报错_第1张图片
具体为:

     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 ↩︎

你可能感兴趣的:(#,Python模块有关问题,python,numpy)