Python 参数的数据优化传播
Python Data Optimal Spreading of Parameters
假设我有一定数量的描述系统的参数:
即
position, velocity, mass, length, width
现在每个参数都有关联的上限和下限:
position = [0,100]
velocity = [10,300]
mass = [50,200]
length = [2,10]
width = [2,10]
数据点由这些参数的特定组合定义:
即
data_point = [10,250,50,4,2]
现在,问题是:是否有 python package/algorithm 可以初始化一定数量的数据点(即 5 个),以便这些数据点最佳分布在参数 space.
上
旁注:
是的,我知道“最佳传播”没有明确定义,但我真的不确定如何去这里。一种可能的定义是:
最大化数据点之间的距离(向量之间的欧几里德距离)
编辑:
使用 linspace 是个好主意。但是,我很快注意到我的数据存在问题。居然忘了说约束:
有些数据点是不可能的。即
constraints = [lenght*2-width, position-velocity]
...如果这些值大于或等于零,则可以认为数据点是可行的。
所以我的问题是:如何以巧妙的方式包含约束?
使用 linspace,你会发现速度总是大于位置,因此我们不会得到可行的数据点。
position = [0,100]
velocity = [10,300]
mass = [50,200]
length = [2,10]
width = [2,10]
# Find Samples
start = [s[0] for s in [position, velocity, mass, length, width]]
end = [s[1] for s in [position, velocity, mass, length, width]]
num_samples = 5
samples = np.linspace(start, end, num_samples)
print(samples)
这是输出:
[[ 0. 10. 50. 2. 2. ]
[ 25. 82.5 87.5 4. 4. ]
[ 50. 155. 125. 6. 6. ]
[ 75. 227.5 162.5 8. 8. ]
[100. 300. 200. 10. 10. ]]
现在,让我们检查约束条件:
def check_constraint(samples, constraints):
checked_samples = []
for dimensions in samples:
position, velocity, mass, length, width = dimensions
# Here I am checking the constraints:
if any([i<0 for i in [length*2-width, position-velocity]]):
pass
else:
checked_samples.append(dimensions)
return checked_samples
samples_checked = check_onstraint(samples, constraints)
print(samples_checked)
这些将是检查约束后留下的样本:
[]
您可以这样做以获得均匀的点网格:
import numpy as np
...
start = [s[0] for s in [position, velocity, ...]]
end = [s[1] for s in [position, velocity, ...]]
num_samples = 5
samples = np.linspace(start, end, num_samples)
这将 return 点在整个参数 spaced 中均匀分布 space。
编辑
要包含更多约束,最好执行以下操作:
start = ...
end = ...
num_results = 5
results = []
while len(results) < num_results:
sample = np.random.uniform(start, end)
if is_valid(sample):
results.append(sample)
这样您就可以定义 is_valid
函数并检查您想要的任何条件!结果点应均匀分布在参数 space.
周围
假设我有一定数量的描述系统的参数:
即
position, velocity, mass, length, width
现在每个参数都有关联的上限和下限:
position = [0,100]
velocity = [10,300]
mass = [50,200]
length = [2,10]
width = [2,10]
数据点由这些参数的特定组合定义: 即
data_point = [10,250,50,4,2]
现在,问题是:是否有 python package/algorithm 可以初始化一定数量的数据点(即 5 个),以便这些数据点最佳分布在参数 space.
上旁注:
是的,我知道“最佳传播”没有明确定义,但我真的不确定如何去这里。一种可能的定义是:
最大化数据点之间的距离(向量之间的欧几里德距离)
编辑:
使用 linspace 是个好主意。但是,我很快注意到我的数据存在问题。居然忘了说约束:
有些数据点是不可能的。即
constraints = [lenght*2-width, position-velocity]
...如果这些值大于或等于零,则可以认为数据点是可行的。
所以我的问题是:如何以巧妙的方式包含约束?
使用 linspace,你会发现速度总是大于位置,因此我们不会得到可行的数据点。
position = [0,100]
velocity = [10,300]
mass = [50,200]
length = [2,10]
width = [2,10]
# Find Samples
start = [s[0] for s in [position, velocity, mass, length, width]]
end = [s[1] for s in [position, velocity, mass, length, width]]
num_samples = 5
samples = np.linspace(start, end, num_samples)
print(samples)
这是输出:
[[ 0. 10. 50. 2. 2. ]
[ 25. 82.5 87.5 4. 4. ]
[ 50. 155. 125. 6. 6. ]
[ 75. 227.5 162.5 8. 8. ]
[100. 300. 200. 10. 10. ]]
现在,让我们检查约束条件:
def check_constraint(samples, constraints):
checked_samples = []
for dimensions in samples:
position, velocity, mass, length, width = dimensions
# Here I am checking the constraints:
if any([i<0 for i in [length*2-width, position-velocity]]):
pass
else:
checked_samples.append(dimensions)
return checked_samples
samples_checked = check_onstraint(samples, constraints)
print(samples_checked)
这些将是检查约束后留下的样本:
[]
您可以这样做以获得均匀的点网格:
import numpy as np
...
start = [s[0] for s in [position, velocity, ...]]
end = [s[1] for s in [position, velocity, ...]]
num_samples = 5
samples = np.linspace(start, end, num_samples)
这将 return 点在整个参数 spaced 中均匀分布 space。
编辑 要包含更多约束,最好执行以下操作:
start = ...
end = ...
num_results = 5
results = []
while len(results) < num_results:
sample = np.random.uniform(start, end)
if is_valid(sample):
results.append(sample)
这样您就可以定义 is_valid
函数并检查您想要的任何条件!结果点应均匀分布在参数 space.