【发布时间】:2020-10-12 16:44:29
【问题描述】:
这是我的代码:
mut1 = np.zeros((100, 52))
mutate = lambda mut1 : mut1 + (np.random.uniform(0, 1) > 0.5) * np.random.uniform(-0.1, 0.1)
mut1 = mutate(mut1)
print(mut1)
[[-0.02017634 -0.02017634 -0.02017634 ... -0.02017634 -0.02017634
-0.02017634]
[-0.02017634 -0.02017634 -0.02017634 ... -0.02017634 -0.02017634
-0.02017634]
[-0.02017634 -0.02017634 -0.02017634 ... -0.02017634 -0.02017634
-0.02017634]
...
[-0.02017634 -0.02017634 -0.02017634 ... -0.02017634 -0.02017634
-0.02017634]
[-0.02017634 -0.02017634 -0.02017634 ... -0.02017634 -0.02017634
-0.02017634]
[-0.02017634 -0.02017634 -0.02017634 ... -0.02017634 -0.02017634
-0.02017634]]
问题是我的mut1 数组中的每个元素都获得了相同的突变。
【问题讨论】:
-
这里有 lambda 的原因吗?有什么理由你不能这样做:
mut1 + =(np.random.uniform(0,1,mut1.shape) > 0.5) * np.random.uniform(-0.1,0.1,mut1.shape)? -
哎呀,试图让它过于复杂。谢谢vm
-
发生这种情况是因为
(np.random.uniform(0,1) > 0.5) * np.random.uniform(-0.1,0.1)将被广播以添加到 mut1 数组中。由于它只是广播的单个值,因此您将在 mut1 数组中看到相同的突变。