numpy.random.sample()
是用於在numpy中進行隨機采樣的函數之一。它返回指定形狀的數組,並在半開間隔中將其填充為隨機浮點數[0.0, 1.0).
用法: numpy.random.sample(size=None)
參數:
size :[int或int元組,可選]輸出形狀。如果給定的形狀是例如(m,n,k),則繪製m * n * k個樣本。默認值為無,在這種情況下,將返回單個值。
Return :間隔中的隨機浮點數數組[0.0, 1.0).
如果未提供尺寸,則為單個此類隨機浮點數。
代碼1:
# Python program explaining
# numpy.random.sample() function
# importing numpy
import numpy as geek
# output random value
out_val = geek.random.sample()
print ("Output random value:", out_val)
輸出:
Output random value: 0.9261509680895836
代碼2:
# Python program explaining
# numpy.random.sample() function
# importing numpy
import numpy as geek
# output array
out_arr = geek.random.sample(size =(3, 3))
print ("Output 2D Array filled with random floats:", out_arr)
輸出:
Output 2D Array filled with random floats: [[ 0.75908777 0.88295677 0.60979136] [ 0.68157065 0.75100312 0.08321613] [ 0.8360331 0.64808891 0.14731635]]
代碼3:
# Python program explaining
# numpy.random.sample() function
# importing numpy
import numpy as geek
# output array
out_arr = geek.random.sample((2, 2, 3))
print ("Output 3D Array filled with random floats:", out_arr)
輸出:
Output 3D Array filled with random floats: [[[ 0.3073475 0.75709465 0.86934712] [ 0.21953745 0.48138292 0.30686482]] [[ 0.48925625 0.60222083 0.14403257] [ 0.87030919 0.87298872 0.2222136 ]]]
相關用法
- numpy 隨機采樣 random()用法及代碼示例
- numpy 隨機采樣 random_integers()用法及代碼示例
- numpy 隨機采樣 randint()用法及代碼示例
- numpy 隨機采樣 ranf()用法及代碼示例
- numpy 隨機采樣 random_sample()用法及代碼示例
- Python random.sample()用法及代碼示例
- Python numpy.random.randn()用法及代碼示例
- Python numpy.random.rand()用法及代碼示例
- Python Numpy Numpy.random用法及代碼示例
- Python numpy.cov()用法及代碼示例
- Python Numpy recarray.ptp()用法及代碼示例
- Python Numpy recarray.put()用法及代碼示例
- Python numpy matrix ones()用法及代碼示例
注:本文由純淨天空篩選整理自jana_sayantan大神的英文原創作品 Random sampling in numpy | sample() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。