我有以下 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 回答
![?](http://img1.sycdn.imooc.com/5458477300014deb02200220-100-100.jpg)
哆啦的时光机
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
![?](http://img1.sycdn.imooc.com/545861b80001d27c02200220-100-100.jpg)
慕码人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
提交
取消