将水平偏移应用于Python中的插值函数

huangapple go评论60阅读模式
英文:

Applying Horizontal Shifts to interpolated function in Python

问题

提前通知:
嗨,我并不是说我有一个问题,我有点能解决我正在处理的问题,但我想知道是否有一种简单的方法或其他解决方案,也许需要其他方法、包或模块,比如"曲线拟合"。

我正在寻找其他选项和解决方案,因为我正在处理的原始代码很大,我正在寻找每一种方法来简化它。

描述:

我有一组具有x和y坐标的点,我使用scipy.interpolate.interp1d方法进行插值。之后的结果将是我想要沿x轴移动的函数。

编辑:我现在知道在指定点之后可以将偏移应用于结果数组,但我想保持interp1d类型的结果,因为我正在做更多与函数相关的工作(例如:变换、积分)。有时我需要特定的点,而不关心整个曲线,在其他情况下我只需要整个曲线。

所以从一开始就有数组会妨碍代码的预期流程。

示例:

import numpy as np
from scipy import interpolate
import matplotlib.pyplot as plt

# 一些x和y点集
x = np.array([0, 1, 2, 3, 4])
y = np.array([0, 1, 4, 7, 8])

# 插值函数
func = interpolate.interp1d(x, y, kind='quadratic', bounds_error=False, fill_value=(y[0], y[-1]))

x_samp = np.linspace(x[0] - x.mean(), x[-1] + x.mean())
y_samp = func(x_samp)

plt.plot(x, y, 'o', label='points')
plt.plot(x_samp, y_samp, '-', label='function')
plt.legend()
plt.show()

点和插值函数

现在在此之后,我有了我的函数,我想应用一个水平偏移。我到处搜索如何应用它,但我找到的唯一方法是在x数组上应用偏移,然后重新应用interp1d(x_new, y)。

**但在我这样做之前,是否有一些简单的技巧,可能在Scipy或其他包中,我没有看到或找到?**出于好奇,我需要找到这样的东西。

在x数组上应用偏移,然后重新应用interp1d(x_new, y):

def shifting_func(x, y, shift):
    x_shift = x + shift
    function = interpolate.interp1d(x_shift, y, kind='quadratic', bounds_error=False, fill_value=(y[0], y[-1]))
    return function

func_shift = shifting_func(x, y, 2)

y_new = func_shift(x_samp)

plt.plot(x, y, 'o', label='points')
plt.plot(x_samp, y_samp, '-', label='function')
plt.plot(x_samp, y_new, '--', label='applying shift')
plt.legend()
plt.show()

点和插值函数以及平移后的函数

如您所见,偏移已应用,但我仍然感到有点麻烦,是否有其他方法?

我的原始点看起来像累积密度函数,也许它们遵循GEV_CDF,那么我该如何受益于这一点呢?

英文:

advance notice:
Hi, I don't say that I have A problem, and I kinda can solve the thing I'm dealing with 'and I will show my silly way of doing that', but I want to know if there is an easy way or other solutions "maybe curve fitting" that requires another methods, packages or modules.

I'm looking for other options and solutions because my original code that I'm working on is big and I'm looking for every trick to simplify it.

Description:

I have a set of points with x & y coordinates that I preformed interpolation using scipy.interpolate.interp1d method on them. after that the result would be of a Function that I want to shift along the x axis.

Edit: I know now that I can apply the shift to the resulting array after I specify the points, but I want to keep the result of type interp1d because I'm doing more function related stuff (ex: transformations, integrating).
and sometimes I need specific points and I don't care about the whole curve, and in others I just need the whole curve.

so having array from the start gets in the way of the intended flow of the code.

example:

import numpy as np
from scipy import interpolate
import matplotlib.pyplot as plt

# some set of x and y points
x = np.array([0, 1, 2, 3, 4])
y = np.array([0, 1, 4, 7, 8])

# interpolation function
func = interpolate.interp1d(x, y, kind='quadratic', bounds_error=False, fill_value=(y[0], y[-1]))

x_samp = np.linspace(x[0] - x.mean(), x[-1] + x.mean())
y_samp = func(x_samp)

plt.plot(x, y, 'o', label='points')
plt.plot(x_samp, y_samp, '-', label='function')
plt.legend()
plt.show()

Points & interpolated function

now after that I had my function, I want to apply a horizontal shift.
I searched everywhere for how to apply it, but the only way that I found referred to applying the shift on the x array, and then reapply interp1d(x_new, y)

but before that I do that, is there some easy trick that could be in Scipy or other packages that I could not see or find? I need to find such thing for curiosity.

applying the shift on the x array, and then reapply interp1d(x_new, y):

def shifting_func(x, y, shift):
    x_shift = x + shift
    function = interpolate.interp1d(x_shift, y, kind='quadratic', bounds_error=False, fill_value=(y[0], y[-1]))
    return function

func_shift = shifting_func(x, y, 2)

y_new = func_shift(x_samp)

plt.plot(x, y, 'o', label='points')
plt.plot(x_samp, y_samp, '-', label='function')
plt.plot(x_samp, y_new, '--', label='applying shift')
plt.legend()
plt.show()

Points & interpolated function & shifted function

as you can see the shift is applied, but I still feel it kinda hassle, is there any other way?

my original points do have the look of cumulative density function and mayby they follow GEV_CDF, so How can I benefit of that?

答案1

得分: 1

你不需要再移动数据并重新插值函数。如果你只想评估函数,那么你可以将函数包装在一个 lambda 函数(或一个常规函数)中,在评估之前移动 x 值。

func_shift = lambda x: func(x-shift)

其中,负数的 shift 将向左移动,正数的 shift 将向右移动。

英文:

You don't need to shift the data and interpolate the function again. If all you want to do is evaluate the function, then you can wrap the function in a lambda function (or a regular function) that will shift the x value before evaluation.

func_shift = lambda x: func(x-shift)

Where a negative shift will be to the left and a positive shift will be to the right.

huangapple
  • 本文由 发表于 2023年6月11日 20:39:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76450516.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定