关于python出现ValueError: A value in x_new is below the interpolation range.的解决方法

本文介绍了解决scipy.interpolate.interp1d()函数在计算EER时出现的ValueError错误的方法。该错误源于插值点超出了现有数据范围。通过在数据两端添加合适的人工参考点可以有效避免此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

在使用scipy.interpolate.interp1d()函数计算EER的时候出现了上述报错。从如何优雅地平均多条曲线 & scipy.interpolate.interp1d below the interpolation range error 这篇CSDN文章中发现,原因是我在

EER = optimize.brentq(lambda x : 1. - x - interpolate.interp1d(list_FAR, list_TPR)(x), 0., 1.)

插值的点只能在现有点之内,而传给interp1d()函数的list_FAR(x轴)不一定会在0和1有值,即最小值不一定是0,最大值不一定是1,导致函数没法在最小值到0之间插值,最大值到1之间插值。
例如提供的参考点x轴为0.2~0.8,那么就无法在0.2和0.8之外的点插值。

解决方法:人为添加x=0和x=1的参考点,注意添加的时候要看(x,y)连成的曲线趋势是什么样的,如果为单调递增,那么要添加(0,0)和(1,1)的点;如果为单调递减,添加(0,1)和(1,0)的点。

ValueError Traceback (most recent call last) Cell In[52], line 69 67 f = interp1d(B2[0, :], B2[1, :], kind='quadratic') 68 a8 = f(i2) ---> 69 a9 = f(a20) 70 derivative = (a9 - a8) / a7 71 if derivative - a9 > 10e-6: File ~\AppData\Roaming\Python\Python39\site-packages\scipy\interpolate\_polyint.py:80, in _Interpolator1D.__call__(self, x) 59 """ 60 Evaluate the interpolant 61 (...) 77 78 """ 79 x, x_shape = self._prepare_x(x) ---> 80 y = self._evaluate(x) 81 return self._finish_y(y, x_shape) File ~\AppData\Roaming\Python\Python39\site-packages\scipy\interpolate\_interpolate.py:752, in interp1d._evaluate(self, x_new) 750 y_new = self._call(self, x_new) 751 if not self._extrapolate: --> 752 below_bounds, above_bounds = self._check_bounds(x_new) 753 if len(y_new) > 0: 754 # Note fill_value must be broadcast up to the proper size 755 # and flattened to work here 756 y_new[below_bounds] = self._fill_value_below File ~\AppData\Roaming\Python\Python39\site-packages\scipy\interpolate\_interpolate.py:786, in interp1d._check_bounds(self, x_new) 784 if self.bounds_error and above_bounds.any(): 785 above_bounds_value = x_new[np.argmax(above_bounds)] --> 786 raise ValueError("A value ({}) in x_new is above " 787 "the interpolation range's maximum value ({})." 788 .format(above_bounds_value, self.x[-1])) 790 # !! Should we emit a warning if some values are out of bounds? 791 # !! matlab does not. 792 return below_bounds, above_bounds ValueError: A value (0.21347609900000009) in x_new is above the interpolation range's maximum value (0.213476099).该怎么修改,代码怎么写
05-24
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值