Python 中缩小(下采样)或放大(上采样)数组

问题描述 投票:0回答:1

我对编码还比较陌生。有不懂的地方请追问!

我正在生成一个 x 数组用作 beta-pdf 的输入。 x 值取决于梯度。梯度越高,“像元值”越小,这意味着我的 x 值彼此越接近。 所有 x 值的总和必须为 1,因为我想使用它们来生成质量更好的 beta-pdf。 我的问题是新的 x 数组根据梯度的长度不同。 我想要 x 数组中的值有一个目标值。例如 300 x 值。 例如,如果我的原始值为 1249,同时它需要保持其相对分布,否则使其依赖于梯度的整个过程是无用的。 谢谢你们!

输入 = 100 x 值

输出应该是 300 个 x 值,但根据梯度进行分布

简单解释一下我之前做了什么,

import numpy as np
import scipy.stats as stats

b = 1
a = 0
werte = 100
alpha = 2  #for example
beta1 = 1.5
prozent = 10

x_array = np.linspace(0, 1, 100)
pdf_values = stats.beta.pdf(x_values, alpha, beta1)
differences = np.diff(pdf_values)
gradients = differences / ((b - a) / (werte - 1))
pdf_gradients_abs = np.abs(gradients)
pv_array = np.linspace(a, b, num=werte)
gradient_array = [pv_array[i + 1] - pv_array[i] for i in range(len(pv_array) - 1)]

for i in range(len(gradient_array)):
    gradient_array[i] = gradient_array[i] / 2
    gradient_array[i] = 1 / ((werte - 1) * 2) + i * (1 / (werte - 1))
integral_pdf_gradients = np.trapz(gradient_array, y=pdf_gradients_abs)

c_delta = (prozent / 100) * integral_pdf_gradients
f_delta = 1 / (pdf_gradients_abs + c_delta)
integral_f_delta = np.trapz(f_delta, gradient_array)
d_x_rel = f_delta / integral_f_delta  #  <- my 'cell width'
zellgroesse = 1 / (werte - 1)
for value in d_x_rel:
    if value >= zellgroesse:  # zellgroesse is the max width one cell can have due to that           being the gradient affected area
     x = zellgroesse
     neues_delta_x = np.append(neues_delta_x, x)
elif value < zellgroesse:
     n = zellgroesse / value
     n_round = int(np.rint(n))
     if n_round >= 1000:
        n_round = 1000
     x_cor = zellgroesse / n_round
     for i in range(n_round):
          neues_delta_x = np.append(neues_delta_x, x_cor)

输出 x 数组,以我需要的方式分布,但它的值太多或不够

我需要 300 个 x 数组值,这可能有 1539 个值!

python pdf interpolation downsampling
1个回答
0
投票
  • 正如您已经所做的:首先对您的函数进行采样
  • 然后计算绝对陡度
  • 用 cumsum 总结那些陡度
  • 结果序列告诉您
  • 您应该有多少分达到该 x 值
  • 将函数缩放到您想要的点数
  • 使用 np.interp 来插值该函数
  • 用它来创建索引数组
import numpy as np
from scipy import stats

# originally sample the beta pdf with the parameters you want
# here 2.5 and 0.6
# it seems the pdf gets an infinity value at 1.0

xv = np.linspace(0, 1, 100)
pdf_values = stats.beta.pdf(xv, 2.5, 0.6)

# we want to know where the absolut value of the gradient is high
# in order to place more points there
#
dpf=np.abs(np.diff(pdf_values))

# but we cut the steepnes to a max (here 10) 
dpf=np.where(dpf > 10,10,dpf)

# adding up the steepnes values and
# padding it with a zero at the beginning
xp=np.cumsum(dpf)
xp=np.pad(xp,(1,0))

# now we interpolate this function with the pv
# function. we also scale it to the number of points
# we want at the maximum. here 300.
# note that this is the "inverse" function.
# the scaled xp functio would tell us how many
# points we would want up to that x vale.
# with the inverse (x and y swaped)
# we can plug in 0 to index-1
xmax=xp[-1]
pv=lambda vec:np.interp(vec,xp*300/xmax,xv)

# now we calculate our new x values
# the 300 here should match the 300 above
idx=np.arange(300)
newx=pv(idx)

#


© www.soinside.com 2019 - 2024. All rights reserved.