gpt4 book ai didi

python数组符号简写:"k%parents.shape[0]"是什么意思?

转载 作者:行者123 更新时间:2023-12-01 01:29:20 25 4
gpt4 key购买 nike

我今天遇到这个符号本教程在这里:https://towardsdatascience.com/genetic-algorithm-implementation-in-python-5ab67bb124a6

parent1_idx = k%parents.shape[0]

右边会返回什么?这种表达方式叫什么名字?预先感谢!

最佳答案

我在上述文章的代码片段中看到了这一行:

for k in range(offspring_size[0]):
# Index of the first parent to mate.
parent1_idx = k%parents.shape[0]
# Index of the second parent to mate.
parent2_idx = (k+1)%parents.shape[0]

其中parents定义如下:

parents = numpy.empty((num_parents, pop.shape[1]))
for parent_num in range(num_parents):
max_fitness_idx = numpy.where(fitness == numpy.max(fitness))
max_fitness_idx = max_fitness_idx[0][0]
parents[parent_num, :] = pop[max_fitness_idx, :]
fitness[max_fitness_idx] = -99999999999
return parents

从上面两行中,很明显 kparents.shape[0] 都是整数,因此该行是一个简单的 modulo operator两个数值变量之间:a = b % c

关于python数组符号简写:"k%parents.shape[0]"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53041810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com