Python 拉丁超立方采样

#coding=utf-8
from __future__ import division
__author__ = 'wanghai'
import numpy as np
from matplotlib.ticker import MultipleLocator, FuncFormatter
import matplotlib.pyplot as pl

def LHSample( D,bounds,N):
    '''
    :param D:参数个数
    :param bounds:参数对应范围(list)
    :param N:拉丁超立方层数
    :return:样本数据
    '''

    result = np.empty([N, D])
    temp = np.empty([N])
    d = 1.0 / N

    for i in range(D):

        for j in range(N):
            temp[j] = np.random.uniform(
                low=j * d, high=(j + 1) * d, size = 1)[0]

        np.random.shuffle(temp)

        for j in range(N):
            result[j, i] = temp[j]

    #对样本数据进行拉伸
    b = np.array(bounds)
    lower_bounds = b[:,0]
    upper_bounds = b[:,1]
    if np.any(lower_bounds > upper_bounds):
        print '范围出错'
        return None

    #   sample * (upper_bound - lower_bound) + lower_bound
    np.add(np.multiply(result,
                       (upper_bounds - lower_bounds),
                       out=result),
           lower_bounds,
           out=result)
    return result

if __name__ =='__main__':
    D = 2
    N = 30
    bounds = [[0,90],[0,90]]
    xs = (bounds[0][1] - bounds[0][0])/N
    ys = (bounds[1][1] - bounds[1][0])/N
    ax = pl.gca()
    pl.ylim(bounds[1][0] - ys,bounds[1][1]+ys)
    pl.xlim(bounds[0][0] - xs, bounds[0][1] + xs)
    pl.grid()
    ax.xaxis.set_major_locator( MultipleLocator(xs) )
    ax.yaxis.set_major_locator(MultipleLocator(ys))
    samples = LHSample(D,bounds,N)
    XY = np.array(samples)
    X = XY[:,0]
    Y = XY[:,1]
    pl.scatter(X,Y)
    pl.show()

二维拉丁超立方采样图例

  • 8
    点赞
  • 75
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
拉丁立方抽样是一种分层的蒙特卡洛采样方法,适用于多维空间均匀采样,并适用于样本数较少的情况下使用。其采样思想是将系统的每个因素的设计空间分为n个子空间,然后在每个子空间内随机选取一个值,以此生成一个对应的样本数为n的采样矩阵。采样过程需要遵守两个原则:一是每个子设计空间内的样本点必须被随机选取,二是每个子设计空间内有且仅有一个值被选取。 在Python中,可以使用numpy库来实现拉丁立方抽样。可以通过定义一个函数来计算分区后各变量区域的上下限,并将结果存储为一个n行2列的向量。同时,还可以通过修改单变量程序来实现多变量分区计算程序,将各变量的范围组成2列m行的矩阵,并得到各变量的区间划分。 以下是一个示例代码片段,用于实现拉丁立方抽样的计算过程: ```python import numpy as np def partition(lower_limit, upper_limit, number_of_sample): section_variable = np.array([lower_limit, upper_limit]).reshape(-1, 1) coefficient_lower = np.zeros((number_of_sample, 2)) coefficient_upper = np.zeros((number_of_sample, 2)) for i in range(number_of_sample): coefficient_lower[i, 0] = 1 - i / number_of_sample coefficient_lower[i, 1] = i / number_of_sample for i in range(number_of_sample): coefficient_upper[i, 0] = 1 - (i + 1) / number_of_sample coefficient_upper[i, 1] = (i + 1) / number_of_sample partition_lower = coefficient_lower @ section_variable partition_upper = coefficient_upper @ section_variable partition_range = np.hstack((partition_lower, partition_upper)) return partition_range arr = partition(0, 10, 10) print(arr.T) ``` 以上代码示例了如何将0到10的范围分成10个等长区间,并获得每个区间的上下限。 希望以上说明对您有所帮助。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [拉丁立方抽样的Python实现](https://blog.csdn.net/YBKarthus/article/details/117698775)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值