scipy interp1d中的错误 - python

我不了解interp1d报告的结果。我收到NAN,我应该收到号码。

In [131]: bb
Out[131]: 
array([ 0.        ,  1.80286595,  1.87443683,  2.70410611,  3.02764722,
        3.11305985,  3.11534355,  3.18695351,  3.20693444])

In [132]: alphas1
Out[134]: 
array([  3.80918778e+00,   2.06547222e+00,   1.99234191e+00,
         7.55942418e-01,   2.56971574e-01,   1.05144676e-01,
         9.30852046e-02,   1.52574183e-02,   1.23664407e-07])

In [135]: bb.shape
Out[135]: (9,)

In [136]: alphas1.shape
Out[140]: (9,)

In [141]: pol = interp1d(alphas1, bb, bounds_error=False)

In [149]: pol(pol.x)
Out[149]: array([ nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan]) # I was expecting to receive nan only at the borders.

python参考方案

我认为,如果您检查interp1d类的source code,即_check_bounds方法,则可以看到问题所在:

def _check_bounds(self, x_new):

    ...

    below_bounds = x_new < self.x[0]
    above_bounds = x_new > self.x[-1]

    # !! Could provide more information about which values are out of bounds
    if self.bounds_error and below_bounds.any():
        raise ValueError("A value in x_new is below the interpolation "
            "range.")
    if self.bounds_error and above_bounds.any():
        raise ValueError("A value in x_new is above the interpolation "
            "range.")

该方法检查您尝试放入的x的值是否小于self.x[0]x的第一个元素)(在您的情况下为alphas1)。由于alphas1[0]x列表中最大的元素,因此此后的每个元素都将“超出范围”,即小于第一个元素。

解决此问题的一种方法是反转您的xy列表:

bb = bb[::-1]
alphas1 = alphas[::-1]
pol = interp1d(alphas1, bb, bounds_error=False)

现在,如scipy所期望的那样,alphas1将会增加,并且pol(pol.x)将如预期的那样返回bb(现在反向)。

Python sqlite3数据库已锁定 - python

我在Windows上使用Python 3和sqlite3。我正在开发一个使用数据库存储联系人的小型应用程序。我注意到,如果应用程序被强制关闭(通过错误或通过任务管理器结束),则会收到sqlite3错误(sqlite3.OperationalError:数据库已锁定)。我想这是因为在应用程序关闭之前,我没有正确关闭数据库连接。我已经试过了: connectio…

从Matlab到Python的interp2(X,Y,Z,XI,YI) - python

我需要此Matlab function的确切Python等效功能,以便对矩阵进行插值。在Matlab中,我有:interp2(X, Y, Z, XI, YI) 在Scipy中,我有:interp2d(X, Y, Z). 在Scipy XI和YI中失踪了。我该如何解决?我在Matlab中使用所有参数。 python大神给出的解决方案 正确的语法是ip = in…

Python pytz时区函数返回的时区为9分钟 - python

由于某些原因,我无法从以下代码中找出原因:>>> from pytz import timezone >>> timezone('America/Chicago') 我得到:<DstTzInfo 'America/Chicago' LMT-1 day, 18:09:00 STD…

用大写字母拆分字符串,但忽略AAA Python Regex - python

我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…

如何打印浮点数的全精度[Python] - python

我编写了以下函数,其中传递了x,y的值:def check(x, y): print(type(x)) print(type(y)) print(x) print(y) if x == y: print "Yes" 现在当我打电话check(1.00000000000000001, 1.0000000000000002)它正在打印:<…