【问题标题】:Python uniform random number generation to a triangle shapePython统一随机数生成为三角形
【发布时间】:2023-04-05 02:45:01
【问题描述】:

我有三个数据点,我执行了线性拟合并获得了 1 sigma 不确定性线。现在我想生成 100k 数据点,均匀分布在 1 个 sigma 误差条(左侧的大三角形)之间,但我不知道我怎么能做到这一点。这是我的代码

import matplotlib.pyplot as plt
import numpy as np
import math
from scipy.optimize import curve_fit

x = np.array([339.545772, 339.545781, 339.545803])
y = np.array([-0.430843, -0.43084 , -0.430842])

def line(x,m,c):   
    return m*x + c

popt, pcov = curve_fit(line,x,y)
slope = popt[0]
intercept = popt[1]

xx = np.array([326.0,343.0])

fit  = line(xx,slope,intercept)
fit_plus1sigma = line(xx, slope + pcov[0,0]**0.5, intercept - pcov[1,1]**0.5)
fit_minus1sigma = line(xx, slope - pcov[0,0]**0.5, intercept + pcov[1,1]**0.5)


plt.plot(xx,fit,"C4",label="Linear fit")
plt.plot(xx,fit_plus1sigma,'g--',label=r'One sigma uncertainty')
plt.plot(xx,fit_minus1sigma,'g--')
plt.fill_between(xx, fit_plus1sigma, fit_minus1sigma, facecolor="gray", alpha=0.15)

在 NumPy 中有一个 Numpy random triangle function,但是,在我的情况下,我无法实现它,我什至不确定这是否是正确的方法。感谢您的帮助。

【问题讨论】:

  • 请注意,如果您在水平区间内均匀采样 x 并在相应的垂直区间内采样 y,则与您希望在三角形内均匀采样 x 和 y 时,您会得到不同的解决方案。

标签: python python-3.x numpy matplotlib random


【解决方案1】:

您可以使用 answer by Severin Pappadeux 以三角形均匀采样。为此,您需要三角形的角点。

要查找您的线相交的位置,您可以通过Norbu Tsering 关注此answer。然后,您只需要三角形的左上角和左下角坐标。

将所有这些放在一起,您可以像这样解决您的问题。

找到交叉点:

# Source: https://stackoverflow.com/a/42727584/5320601
def get_intersect(a1, a2, b1, b2):
    """
    Returns the point of intersection of the lines passing through a2,a1 and b2,b1.
    a1: [x, y] a point on the first line
    a2: [x, y] another point on the first line
    b1: [x, y] a point on the second line
    b2: [x, y] another point on the second line
    """
    s = np.vstack([a1, a2, b1, b2])  # s for stacked
    h = np.hstack((s, np.ones((4, 1))))  # h for homogeneous
    l1 = np.cross(h[0], h[1])  # get first line
    l2 = np.cross(h[2], h[3])  # get second line
    x, y, z = np.cross(l1, l2)  # point of intersection
    if z == 0:  # lines are parallel
        return (float('inf'), float('inf'))
    return (x / z, y / z)

p1 = ((xx[0], fit_plus1sigma[0]), (xx[1], fit_plus1sigma[1]))
p2 = ((xx[0], fit_minus1sigma[0]), (xx[1], fit_minus1sigma[1]))
cross = get_intersect(p1[0], p1[1], p2[0], p2[1])

这样你就可以得到每条线上的两个点,以及你需要从这个三角形中采样的交点。

然后就可以对需要的点进行采样了:

# Source: https://stackoverflow.com/a/47425047/5320601
def trisample(A, B, C):
    """
    Given three vertices A, B, C,
    sample point uniformly in the triangle
    """
    r1 = random.random()
    r2 = random.random()

    s1 = math.sqrt(r1)

    x = A[0] * (1.0 - s1) + B[0] * (1.0 - r2) * s1 + C[0] * r2 * s1
    y = A[1] * (1.0 - s1) + B[1] * (1.0 - r2) * s1 + C[1] * r2 * s1

    return (x, y)


points = []
for _ in range(100000):
    points.append(trisample(p1[0], p2[0], cross))

1000分的示例图片:

【讨论】:

  • @SeverinPappadeux 是否可以轻松地赋予三角形部分重量?例如,假设我想让 x=335 和 x=337 之间的数据的权重(数据点数)是其余数据的两倍。我考虑过为该范围生成一组新的统一数据并将它们相加,但我想知道是否有更 Pythonic 的方法!
  • @SaraKrauss 当然。一般方法是对区域(任何区域)进行三角剖分,然后为了均匀密度,您首先以概率 A_i/A_total(A 表示区域)拾取三角形i,然后在三角形中采样。这将为您提供均匀的点密度。然后很容易根据您的喜好修改密度 - 您选择一些概率较高的三角形子集(其余三角形的概率较低),然后像以前一样在所选三角形中采样点
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-18
  • 1970-01-01
  • 1970-01-01
  • 2018-05-04
  • 1970-01-01
  • 2011-12-16
  • 2013-12-07
相关资源
最近更新 更多