Numpy动态索引与切片和高级索引

2024-06-02 09:01:57 发布

您现在位置:Python中文网/ 问答频道 /正文

通过获取特定行(由行索引值数组表示)并从具有固定起始索引和不同终止索引的列中获取一个切片来对(N,M)数组进行索引

population = np.full((N,M), 0) #population of N guys with genome length M
#Choose some guys and change parts of their genome (cols)

rows_indices = [0,1,5,6] #four guys 0,1,5,6 will be changed

#all selected guys will have a start of 10
#the ends will be difference for each guy

slice_lengths = np.random.geometric(p=0.8, size = 4) #vector of 4

我想象的是:

population[0, 10: 10+ slice_length[0]] = 100
population[1, 10: 10+ slice_length[1]] = 100
population[2, 10: 10+ slice_length[2]] = 100
population[3, 10: 10+ slice_length[3]] = 100

除了矢量化而不硬编码每个值

#false code
population[rows_indices, start: start + slice_length]

Tags: ofgenomenp切片slice数组bestart