如何在python中运行Pchip或interp1d以获得三次函数?

时间:2018-06-18 20:33:50

标签: python-2.7 interpolation

我不能让这个代码工作,我疯了,我有一些数据(时间与当前)及其对数图,我想使用pchip插值或至少在点之间的立方,因为数据是真实的测量并且只是假设日志提供了太多错误。

import os
import numpy as np
import pandas as pd
import sys
import xlrd
from scipy.interpolate import interp1d

df = pd.read_excel("re.xlsx")
array = df.values

ans = []
maxy = array.shape[0]
x = []
y = []

count = 0
while count < maxy:
    x.append(float(array[count,0]))
    y.append(float(array[count,1]))
    count = count +1
print(x)
time = 0.001
while(time < 5):
    f = interp1d(x, y, kind='cubic', copy=True, bounds_error=True, fill_value=np.nan)
    print(time,f(time))
    ans.append(f(time))
    time = (time *1.1)

这是输出,我没有得到错误?数组是否正确不是吗?这是立方插值尝试,但当我尝试pchip时,我有一个类似的错误,说x必须加入

[0.0837, 0.0841, 0.0845, 0.0853, 0.0856, 0.0866, 0.0881, 0.0882, 0.09, 
0.0921, 0.0921, 0.0947, 0.0973, 0.0977, 0.1042, 0.1122, 0.1202, 0.1233, 
0.1304, 0.1365, 0.1415, 0.1432, 0.147, 0.1531, 0.1595, 0.1598, 0.1689, 
0.1772, 0.1792, 0.191, 0.1999, 0.206, 0.2239, 0.2274, 0.2533, 0.2539, 0.2934, 
0.294, 0.346, 0.3462, 0.4201, 0.4428, 0.5215, 0.5947, 0.6346, 0.7889, 0.8605, 
0.9382, 0.9846, 1.128, 1.261, 1.4086, 1.5932, 1.6089, 1.8511, 2.0602, 2.167, 
2.56, 2.6284, 3.228, 3.2321, 4.0363, 4.0959, 5.1183]

Traceback (most recent call last):
  File "C:/Python27/projects/interpolationFromExcel.py", line 25, in <module>
    f = interp1d(x, y, kind='cubic', copy=True, bounds_error=True, 
fill_value=np.nan)
  File "C:\Python27\lib\site-packages\scipy\interpolate\interpolate.py", line 
535, in __init__
    check_finite=False)
  File "C:\Python27\lib\site-packages\scipy\interpolate\_bsplines.py", line 
777, in make_interp_spline
    raise ValueError("Expect x to be a 1-D sorted array_like.")
ValueError: Expect x to be a 1-D sorted array_like.

0 个答案:

没有答案