numpy.random.ranf()
是用於在numpy中進行隨機采樣的函數之一。它返回指定形狀的數組,並在half-open間隔中填充隨機浮點數[0.0, 1.0).
用法: numpy.random.ranf(size=None)
參數:
size :[int或int元組,可選]輸出形狀。如果給定的形狀是例如(m,n,k),則繪製m * n * k個樣本。默認值為無,在這種情況下,將返回單個值。
Return :間隔中的隨機浮點數數組[0.0, 1.0).
如果未提供尺寸,則為單個此類隨機浮點數。
代碼1:
# Python program explaining
# numpy.random.ranf() function
# importing numpy
import numpy as geek
# output random float value
out_val = geek.random.ranf()
print ("Output random float value:", out_val)
輸出:
Output random float value: 0.0877051588430926
代碼2:
# Python program explaining
# numpy.random.ranf() function
# importing numpy
import numpy as geek
# output array
out_arr = geek.random.ranf(size =(2, 1))
print ("Output 2D Array filled with random floats:", out_arr)
輸出:
Output 2D Array filled with random floats: [[ 0.14186407] [ 0.58068259]]
代碼3:
# Python program explaining
# numpy.random.ranf() function
# importing numpy
import numpy as geek
# output array
out_arr = geek.random.ranf((3, 3, 2))
print ("Output 3D Array filled with random floats:", out_arr)
輸出:
Output 3D Array filled with random floats: [[[ 0.11013584 0.67844746] [ 0.84691569 0.09467084] [ 0.69918864 0.12137178]] [[ 0.30629051 0.28301093] [ 0.1302665 0.2196221 ] [ 0.51555358 0.73191852]] [[ 0.72806359 0.66485275] [ 0.80654791 0.04947181] [ 0.06380535 0.99306064]]]
相關用法
- numpy 隨機采樣 random()用法及代碼示例
- numpy 隨機采樣 random_sample()用法及代碼示例
- numpy 隨機采樣 sample()用法及代碼示例
- numpy 隨機采樣 random_integers()用法及代碼示例
- numpy 隨機采樣 randint()用法及代碼示例
- Python numpy.random.rand()用法及代碼示例
- Python numpy.random.randn()用法及代碼示例
- Python Numpy Numpy.random用法及代碼示例
- Python random.sample()用法及代碼示例
- Python numpy.cov()用法及代碼示例
- Python numpy.nanmean()用法及代碼示例
- Python Numpy MaskedArray.any()用法及代碼示例
- Python Numpy recarray.mean()用法及代碼示例
注:本文由純淨天空篩選整理自jana_sayantan大神的英文原創作品 Random sampling in numpy | ranf() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。