我有以下 python 代碼片段:import numpy as np# Some random arrayx = np.random.rand(34, 176, 256)# Get some indexespos_idx = np.argwhere(x > 0.5)# Sample some values from these indexesseeds = pos_idx[np.random.choice(pos_idx.shape[0], size=5, replace=False), :]# Now create another arrayy = np.zeros_like(x, np.uint8)y[seeds] = 1最后一行給出了如下錯誤:index 77 is out of bounds for axis 0 with size 34但我不確定這是怎么發生的,因為所有采樣索引都應該有效,因為它們是一個子集。
3 回答

哆啦的時光機
TA貢獻1779條經驗 獲得超6個贊
此代碼將幫助您將值設置為 1
import numpy as np
# Some random array
x = np.random.rand(34, 176, 256)
# Get some indexes
pos_idx = np.argwhere(x > 0.5)
# Sample some values from these indexes
seeds = pos_idx[np.random.choice(pos_idx.shape[0], size=5, replace=False), :]
# Now create another array
y = np.zeros_like(x, np.uint8)
for i in seeds:
y[tuple(i)] = 1

慕碼人2483693
TA貢獻1860條經驗 獲得超9個贊
種子的形狀是否正確?如果我理解正確的話,x有隨機值;y與 x 具有相同的形狀,但全為零;andseeds是一些索引值,這些位置將被設置為一個。
print('x : ', x.ndim, x.shape)
print('y : ', y.ndim, y.shape)
print('seeds: ', seeds.ndim, seeds.shape)
x : 3 (34, 176, 256)
y : 3 (34, 176, 256)
seeds: 2 (5, 3)
添加回答
舉報
0/150
提交
取消