【发布时间】:2021-05-10 23:00:52
【问题描述】:
假设我有一个这样的数组:
from skopt.space import Space
from skopt.sampler import Lhs
import numpy as np
np.random.seed(42)
rows = 5
cols = 3
dummy = np.zeros((rows, cols))
array([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
我现在想用skopt.Lhs.generate 用1 填充这个数组的某些位置,我想排除存储在ignore 中的某些位置:
ignore = np.array([
[3, 1],
[4, 1]
])
我怎样才能做到最好?
我可以的
space = Space([(0, rows - 1), (0, cols - 1)])
lhs = Lhs(criterion="maximin", iterations=1000)
lh = np.array(lhs.generate(space.dimensions, 3))
dummy[lh[:, 0], lh[:, 1]] = 1
给了
array([[0., 0., 1.],
[1., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 1., 0.]])
但可以看到4, 1 的位置已被占用,但它不应该。
一种方法是将lhs.generate 调用放在while 循环中,然后始终检查ignore 中是否有任何元素,但我想知道是否有更直接的解决方案。
【问题讨论】:
-
skopt.sampler.Lhs似乎不支持任何类型的约束。我认为最好的办法是采取source code of lhs并通过添加限制来修改它。 -
@fdermishin:这将是一个选项,但我想我最好选择这样的
while循环,如我的答案所示。只是想知道是否有比这更聪明的东西(我还检查了pyDOE,但它似乎不适用于离散值)。
标签: python statistics skopt