我想爲整型和浮點數使用Numpy,生成隨機的64位整數值範圍內的該類型的有效值的整個範圍內。要生成隨機的32位浮點,我可以使用:隨機int64和float64數字
In [2]: np.random.uniform(low=np.finfo(np.float32).min,high=np.finfo(np.float32).max,size=10)
Out[2]:
array([ 1.47351436e+37, 9.93620693e+37, 2.22893053e+38,
-3.33828977e+38, 1.08247781e+37, -8.37481260e+37,
2.64176554e+38, -2.72207226e+37, 2.54790459e+38,
-2.47883866e+38])
,但如果我試圖用這個64位數字,我收到
In [3]: np.random.uniform(low=np.finfo(np.float64).min,high=np.finfo(np.float64).max,size=10)
Out[3]: array([ Inf, Inf, Inf, Inf, Inf, Inf, Inf, Inf, Inf, Inf])
同樣,對於整數,我可以成功地生成隨機的32位整數:
In [4]: np.random.random_integers(np.iinfo(np.int32).min,high=np.iinfo(np.int32).max,size=10)
Out[4]:
array([-1506183689, 662982379, -1616890435, -1519456789, 1489753527,
-604311122, 2034533014, 449680073, -444302414, -1924170329])
,但我對失敗的64位整數:
In [5]: np.random.random_integers(np.iinfo(np.int64).min,high=np.iinfo(np.int64).max,size=10)
---------------------------------------------------------------------------
OverflowError Traceback (most recent call last)
/Users/tom/tmp/<ipython console> in <module>()
/Library/Python/2.6/site-packages/numpy/random/mtrand.so in mtrand.RandomState.random_integers (numpy/random/mtrand/mtrand.c:6640)()
/Library/Python/2.6/site-packages/numpy/random/mtrand.so in mtrand.RandomState.randint (numpy/random/mtrand/mtrand.c:5813)()
OverflowError: long int too large to convert to int
這是預期的行爲,還是應該將這些報告爲Numpy中的錯誤?
是你能解決,在結束了嗎? – Avision 2016-05-09 13:47:06