- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我一直在研究一个使用线性 scipy.interpolate.interp1d 为 scipy 中的 ode 求解器创建历史函数的包,描述为 here.
相关的代码是这样的
def update(self, ti, Y):
""" Add one new (ti, yi) to the interpolator """
self.itpr.x = np.hstack([self.itpr.x, [ti]])
yi = np.array([Y]).T
self.itpr.y = np.hstack([self.itpr.y, yi])
#self.itpr._y = np.hstack([self.itpr.y, yi])
self.itpr.fill_value = Y
在 __init__ 中初始化“self.itpr”的地方:
def __init__(self, g, tc=0):
""" g(t) = expression of Y(t) for t<tc """
self.g = g
self.tc = tc
# We must fill the interpolator with 2 points minimum
self.itpr = scipy.interpolate.interp1d(
np.array([tc-1, tc]), # X
np.array([self.g(tc), self.g(tc)]).T, # Y
kind='linear', bounds_error=False,
fill_value = self.g(tc))
其中 g
是一些函数,它返回一组值的数组,这些值是一组微分方程的解,tc
是当前时间。
这对我来说似乎很好,因为每次我想更新值的范围时都不必重新创建新的插值器对象(这发生在模拟期间的每个显式时间步)。这种更新插值器的方法在 scipy v 0.11.0 下运行良好。但是,在更新到 v 0.12.0 后,我遇到了问题。我看到新的插值器现在包含一个数组 _y
,它似乎只是原始数组的另一个副本。如上所述,仅更新 _y
是否安全和/或理智? 是否有更简单、更 pythonic 的方法来解决这个问题,希望对 scipy 的 future 更新更健壮?同样,在 v 0.11 中,一切都运行良好,并产生了预期的结果,在 v 0.12 中,我得到一个 IndexError
when _y
is referenced因为它没有在我的函数中更新,而 y 本身是。
如有任何帮助/指点,我们将不胜感激!
最佳答案
看起来 _y
只是 y
的一个副本,已被 interp1d._reshape_yi()
reshape 。因此,使用以下方法更新它应该是安全的:
self.itpr._y = self.itpr._reshape_yi(self.itpr.y)
事实上,据我所知,只有 _y
被插值器内部使用,所以我认为你可以在根本不更新 y
的情况下逃脱.
一个更优雅的解决方案是使 _y
成为插值器的一个属性,它返回一个适当 reshape 的 y
副本。可以通过在 interp1d
的特定实例创建后对其进行猴子修补来实现此目的(有关更多解释,请参阅 Alex Martelli 的回答 here):
x = np.arange(100)
y = np.random.randn(100)
itpr = interp1d(x,y)
# method to get self._y from self.y
def get_y(self):
return self._reshape_yi(self.y)
meth = property(get_y,doc='reshaped version of self.y')
# make this a method of this interp1d instance only
basecls = type(itpr)
cls = type(basecls.__name__, (basecls,), {})
setattr(cls, '_y', meth)
itpr.__class__ = cls
# itpr._y is just a reshaped version of itpr.y
print itpr.y.shape,itpr._y.shape
>>> (100,) (100, 1)
现在 itpr._y
会在您更新 itpr.y
时更新
itpr.x = np.arange(110)
itpr.y = np.random.randn(110)
print itpr._y.shape
>>> (110,) (110, 1)
这一切都非常繁琐而且不是很 Pythonic - 修复 scipy 源代码(scipy/interpolate/interpolate.py)要容易得多。您需要做的就是从 interp1d.__init__()
设置的最后一行中删除它:
self._y = y
并添加这些行:
@property
def _y(self):
return self._reshape_yi(self.y)
关于python - scipy 0.11.0 到 0.12.0 改变了线性 scipy.interpolate.interp1d,打破了我不断更新的插值器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17496749/
我正在尝试使用 AngularJS 将 YouTube 视频插入到我的网站中,但我一直收到相同的错误: Error: $interpolate:interr Interpolation Error 为
我正在尝试将动态链接从 json 加载到我的 iframe 模板中。当我加载 iframe 页面时,会弹出此错误。我真的不知道为什么。这是我第一次看到这个错误。下面是代码。 Controller ap
我想从 x 进行插值到 z .但有一个警告: 取决于状态y , 我有一个不同的 xGrid - 我需要对其进行插值。 我有一个 y 的网格, yGrid .说 yGrid=[0,1] .和 xGrid
我是 javascript 的新手,但几周前刚刚钻研 d3.js 尝试创建时空可视化。 我想要实现的是基于以下代码的类似 ( https://jsfiddle.net/dmatekenya/mz5fx
scipy.interpolate.splrep(x, y, w=None, xb=None, xe=None, k=3, task=0, s=None, t=None, full_output=0,
Scipy 函数 griddata和 Rbf两者都可以用于对随机分散的 n 维数据进行插值。它们之间有什么区别?其中之一在准确性或性能方面更胜一筹吗? IMO,这不是 this question 的重
我需要(以数字方式)计算函数的一阶和二阶导数,为此我尝试同时使用 splrep 和 UnivariateSpline 来创建样条曲线插值函数的导数。 但是,对于幅度为 10^-1 或更低的函数,样条表
好的,所以我最近一直在研究插值。遗憾的是,我读过的几乎每篇文章都只讨论精确到 0.0 到 1.0 的小数级别的插值。我想插入整数整数,不管它们有多大,或者是否有负数或其他什么。我用线性插值完成了这个:
如何在 FORTRAN 中实现二维插值,其中数据如下所示。 x 和 y 是两个坐标,z 是依赖于它们的值 x 间隔均匀但 y 不均匀间隔且 y 的最大值 对应于 x 的统一值不断增加。 在不损失太多准
在彼得阿尔弗雷德的 article关于多元散点数据插值,他提到,从各种方案中,只有少数方案真正受到从业者的欢迎。例如,他命名为 Shepard 方法和 Hardy Multiquadrics。但那篇文
我不清楚图像处理中重采样和插值之间的区别。如果我有一个 geotiff 并且我想提高它的分辨率,我应该使用重采样方法,例如最近邻,对吗?例如,我发现 gdalwarp 函数可以做到这一点。 插值方法,
对于这个有点令人困惑的标题,我感到很抱歉,但我不确定如何更清楚地总结这一点。 我有两组 X,Y 数据,每组对应一个总体值。它们是从原始数据中相当密集地采样的。我正在寻找一种方法,为任何给定的 Y 找到
我很抱歉标题有点困惑,但我不确定如何更清楚地总结这一点。 我有两组X,Y数据,每组对应一个大概的整体值。它们是从原始数据中相当密集地采样的。我正在寻找的是一种方法,可以为我已有的集合之间的值找到任何给
我是 D3 新手,正在尝试一些图表。在使用 D3 V4 构建折线图时,我遇到了以下错误。 d3.line(...).x(...).y(...).interpolate is not a functio
问候, 在我正在开发的网络应用程序中,我想做如下事情: 我有一个 bean class Gene{ String geneid; String sequence; .. } // EL express
有什么方法可以将 Angular 的 $interpolate 与数组而不是对象一起使用? 示例代码: var exp = $interpolate('Hello {{name}}!'); var r
我是编程新手,我会尝试编写一个线性插值函数: from bisect import bisect_left def interpolate((x_list, y_list), x_test):
我启动了一个带有共享元素的场景转换的 Activity,它工作正常。 ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTr
背景分析 在传统的html页面中我们可以定义变量吗?当然不可以,那我们假如希望通过变量的方式实现页面内容的数据操作也是不可以的。当然我们可以在服务端通过定义html标签库方式,然后以html作为模
我在 wikipedia 上阅读了关于双三次插值的信息.我遇到了变量 t这是没有定义的。 等式是: 谁能告诉我这个变量是什么意思以及它的常用值是什么? 最佳答案 t 是 0 到 1 之间的任何数字。
我是一名优秀的程序员,十分优秀!