简体   繁体   English

Python 参数的数据优化传播

[英]Python Data Optimal Spreading of Parameters

Let's assume that I have a certain number of parameters that describe a system:假设我有一定数量的描述系统的参数:

ie IE

position, velocity, mass, length, width

Now every parameter has an associated upper and lower bound:现在每个参数都有一个关联的上限和下限:

position = [0,100]
velocity = [10,300]
mass = [50,200]
length = [2,10]
width = [2,10]

A data-point is defined by a certain combination of these parameters: ie数据点由这些参数的某种组合定义:即

data_point = [10,250,50,4,2]

Now, the question is: Is there a python package/algorithm such that I can initialize a certain number of data-points (ie 5) such that those data points are optimally spread over the parameter space.现在,问题是:是否有 python 包/算法,以便我可以初始化一定数量的数据点(即 5 个),以便这些数据点最佳地分布在参数空间中。

Side Note:边注:

Yes, I know "optimally spread" is not well defined, but I am really not sure about how to go here.是的,我知道“最佳传播”没有明确定义,但我真的不确定如何在这里 go。 One possible definition could be:一种可能的定义是:

maximize the distance between the data-points (Euclidean distance between vectors)最大化数据点之间的距离(向量之间的欧几里得距离)

EDIT:编辑:

Using linspace is a very good idea.使用 linspace 是一个非常好的主意。 However, I quickly noticed an issues with my data.但是,我很快注意到我的数据存在问题。 I actually forgot to talk about constraints:我实际上忘了谈论约束:

Some data-points are not possible.有些数据点是不可能的。 ie IE

constraints = [lenght*2-width, position-velocity]

...if these values are greater or equal to zero, then the data-point can be considered as feasible. ...如果这些值大于或等于零,则可以认为数据点是可行的。

So my question is: How can I include constraints in a smart way?所以我的问题是:我怎样才能以一种聪明的方式包含约束?

Using linspace, you will see that velocity will always be greater than position, and thus we will get no feasible datapoint.使用 linspace,您将看到速度始终大于 position,因此我们将无法获得可行的数据点。

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)

This is the output:这是 output:

[[  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. ]]

Now, let's check the constraints:现在,让我们检查约束:

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)

These would be the samples left after checking the constraints:这些将是检查约束后留下的样本:

[]

You could do something like this to get an even grid of points:你可以做这样的事情来获得一个均匀的网格点:

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)

This will return points evenly spaced throughout the parameter space.这将返回在整个参数空间中均匀分布的点。

Edit To include more constraints it might be good to do something like:编辑要包含更多约束,最好执行以下操作:

start = ...
end = ...
num_results = 5
results = []

while len(results) < num_results:
    sample = np.random.uniform(start, end)
    if is_valid(sample):
        results.append(sample)

that way you can define the is_valid function and check any conditions you'd like.这样您就可以定义is_valid function 并检查您想要的任何条件。 The resulting points should be uniformly distributed around the parameter space.结果点应均匀分布在参数空间周围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM