Skip to main content
 首页 » 编程设计

python之Scipy 插值 splrep 数据错误

2025年01月19日14lautakyan007

我正在编写以下代码,非常基础,只是在 scipy.interpolate 中调用 splrep。 但是它会在数据集上抛出错误。

代码如下

x_nodesTrial = np.array([ 1321.,  1327.,  1329.,  1326.,  1329.,  1327.,  1328.,  1336., 
        1335.,  1326.,  1327.,  1323.,  1337.,  1338.,  1337.,  1338., 
        1338.,  1338.,  1340.,  1340.,  1337.,  1334.,  1333.,  1342., 
        1343.,  1340.,  1344.,  1343.,  1346.,  1347.,  1348.,  1346., 
        1374.,  1377.,  1372.,  1378.,  1380.,  1384.,  1390.,  1391., 
        1386.,  1397.,  1395.,  1396.,  1397.,  1398.,  1399.,  1399., 
        1399.]) 
 
y_nodesTrial = np.array([ 0.97897541,  0.60222801,  0.95832816,  1.2578326 ,  1.08685326, 
        1.29256113,  0.78091064,  0.96806139,  0.92957363,  0.46117087, 
        0.72456884,  1.04872371,  0.87137002,  1.03914457,  0.74450367, 
        0.96806139,  0.93808315,  0.79181076,  0.84673744,  1.15069855, 
        0.8782084 ,  1.14509825,  0.97970477,  0.91182705,  1.12178111, 
        1.07171425,  0.58492979,  0.89831191,  1.17473401,  0.97970477, 
        0.87013956,  1.1568893 ,  1.01242284,  0.7309485 ,  1.00418766, 
        1.04394513,  1.08947891,  0.81053333,  1.21184629,  0.94566681, 
        0.73630351,  0.86839343,  1.07495847,  0.87003695,  0.        , 
        1.01559272,  0.97504579,  1.0377689 ,  1.05694979]) 
 
k = 3 
tck = inter.splrep(x_nodesTrial, y_nodesTrial, k=k, s=0) 
 
I get the following error: 
 
k = 3 
tck = inter.splrep(x_nodesTrial, y_nodesTrial, k=k, s=0) 

回溯:

Traceback (most recent call last): 
 
  File "<ipython-input-24-a978d88e9773>", line 2, in <module> 
    tck = inter.splrep(x_nodesTrial, y_nodesTrial, k=k, s=0) 
 
  File "C:\Anaconda\lib\site-packages\scipy\interpolate\fitpack.py", line 278, in splrep 
    res = _impl.splrep(x, y, w, xb, xe, k, task, s, t, full_output, per, quiet) 
 
  File "C:\Anaconda\lib\site-packages\scipy\interpolate\_fitpack_impl.py", line 515, in splrep 
    raise _iermess[ier][1](_iermess[ier][0]) 
 
ValueError: Error on input data 

有人可以帮忙指出上面的代码/数据集有什么问题吗?

感谢您的帮助。

谢谢

请您参考如下方法:

这是因为你的 x 值没有从小到大排序