亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 numpy 數組中的多個索引上設置值

在 numpy 數組中的多個索引上設置值

HUX布斯 2023-04-18 16:36:39
我有以下 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 回答

?
MYYA

TA貢獻1868條經驗 獲得超4個贊

它將種子中的值視為第一維的索引。要通過種子中的索引訪問元素,您可以使用:

y[seeds[:,0],seeds[:,1],seeds[:,2]] = 1


查看完整回答
反對 回復 2023-04-18
?
哆啦的時光機

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


查看完整回答
反對 回復 2023-04-18
?
慕碼人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)


查看完整回答
反對 回復 2023-04-18
  • 3 回答
  • 0 關注
  • 215 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號