【问题标题】:histogram matching in PythonPython中的直方图匹配
【发布时间】:2023-04-05 04:46:02
【问题描述】:

我正在尝试将模拟数据与观测到的降水数据进行直方图匹配。下面显示了一个简单的模拟案例。我得到了模拟数据和观察数据的 CDF 并被困在那里。我希望一个线索能帮助我理解..提前谢谢你

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
import scipy.stats as st


sim = st.gamma(1,loc=0,scale=0.8) # Simulated
obs = st.gamma(2,loc=0,scale=0.7) # Observed
x = np.linspace(0,4,1000)
simpdf = sim.pdf(x)
obspdf = obs.pdf(x)
plt.plot(x,simpdf,label='Simulated')
plt.plot(x,obspdf,'r--',label='Observed')
plt.title('PDF of Observed and Simulated Precipitation')
plt.legend(loc='best')
plt.show()

plt.figure(1)
simcdf = sim.cdf(x)
obscdf = obs.cdf(x)
plt.plot(x,simcdf,label='Simulated')
plt.plot(x,obscdf,'r--',label='Observed')
plt.title('CDF of Observed and Simulated Precipitation')
plt.legend(loc='best')
plt.show()

# Inverse CDF
invcdf = interp1d(obscdf,x)
transfer_func = invcdf(simcdf)

plt.figure(2)
plt.plot(transfer_func,x,'g-')
plt.show()

【问题讨论】:

  • 您的问题是什么?你想做什么却做不到?
  • @Ben,我正在尝试 CDF 匹配/直方图匹配,如图所示。s8.postimage.org/4txybzz8l/test.jpg。我无法反转观察到的数据的 CDF,这是下一步。
  • 看起来您需要为所需的 y 值插入 CDF,然后用旧的 x 值绘制它。
  • @tiago,如果您能提出解决方案,我将不胜感激。
  • @subash, what have you tried? 我不会为你写代码。您的示例代码主要是情节,您似乎没有尝试过任何解决此问题的方法。索取代码并不是 SO 的目的。

标签: python numpy histogram cdf


【解决方案1】:

我试图重现您的代码,并得到以下错误:

ValueError: A value in x_new is above the interpolation range.

如果您查看您的两个 CDF 的情节,很容易弄清楚发生了什么:

现在定义invcdf = interp1d(obscdf, x) 时,请注意obscdf 的范围为

>>> obscdf[0]
0.0
>>> obscdf[-1]
0.977852889924409

所以invcdf 只能在这些限制之间插入值:超出它们我们将不得不进行外推,这并不是很好定义的。 SciPy 的默认行为是在被要求推断时引发错误。当您请求invcdf(simcdf) 时,这正是发生的情况,因为

>>> simcdf[-1]
0.99326205300091452

超出插值范围。

如果您阅读the interp1d docs,您会发现可以修改此行为

invcdf = interp1d(obscdf, x, bounds_error=False)

现在一切正常,尽管您需要将绘图参数的顺序颠倒到plt.plot(x, transfer_func,'g-') 以获得与您发布的图中相同的顺序:

【讨论】:

  • @Jaime .. 谢谢.. 我按照建议进行了更改,并且可以生成所有 4 个图,如我发布的链接中所示。我接受了你的回答。