[英]How to select a continuous sample at random from a list?
我有不同深度的輸入數組,范圍從20到32。無法填充它們以使其具有相同的大小,因此最好的選擇是在每次迭代時隨機選擇圖像的z深度。
我讀過numpy.random.choice()
可以用於此目的,但是我得到了索引的隨機排列,我想要一個連續的選擇。
z_values = np.arange(img.shape[0]) # get the depth of this sample
z_rand = np.random.choice(z_values, 20) # create an index array for croping
以上給了我:
[22 4 31 19 9 24 13 6 20 17 28 8 11 27 14 15 30 16 12 25]
這對我沒有用,因為它們不是連續的,我不能用它來修剪我的音量。
有什么方法可以獲取連續的隨機樣本嗎?
謝謝
如果我理解正確,您想隨機選擇一個20長度的切片。 因此,只需調整邏輯以隨機搜索有效的起點,然后切片即可獲得所需的結果。
import numpy as np
import random
#pretending this is the image
img = np.array(range(100, 3200, 100))
size_to_slice = 20
if img.shape[0] >= size_to_slice: #make sure you are able to get the length you need
start = random.randint(0, img.shape[0] - size_to_slice)
z_rand = img[start: start + size_to_slice]
else:
print("selection invalid")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.