使用scipy
最简单的插值方法是使用scipy
interpolate.interp1d
函数。scipy与numpy密切相关,您可能已经安装了它。
interp1d
的优点是它可以为您对数据进行排序。这是以有点古怪的语法为代价的。在许多插值函数中,假设您试图从x值中插值y值。这些函数通常需要“x”值单调递增。在您的例子中,我们交换了x和y的正常意义。y值有一个异常值,正如@abhishek mishra指出的那样。对于您的数据,您是幸运的,您可以通过将离群值留在中而逃脱。
导入numpy as np
将matplotlib.pyplot导入为plt
从scipy.interpolate导入interp1d
我的x=[4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,
48、50、52、54、56、58、60、62、64、66条]
my_y_raw=np.数组([0.99470977497817203,0.99434995886145172,
0.98974611323163653,0.961630837657524,0.9932763558441175,
0.9933892769251909,0.99428263292577534,0.9869051421271611,
0.99111667721533181,0.99149418924880861,0.99133773062680464,
0.99143506380003499,0.9915108464011454,0.99268261743308517,
0.9928975725282316,0.99100207861144063,0.99157171773324027,
0.99112571824824358,0.99031608691035722,0.9897810426076905,
0.989782674787969,0.98897835092187614,0.98517540405423909,
0.98308943666187076,0.9608181781994603,0.85563541881892147,
0.61570811548079107,0.3307627640577052,0.1465134838124245,
0.07685314712214226,0.035831324928136087,0.021344669212790181])
#设为“假定已排序”使scipy自动为您排序
F=interp1d(我的“原始”,我的“x”,假设“排序”=false)
X新=F(0.5)
print('插入值为',xnew)
plt.绘图(my_x,my_y_raw,'x-',markersize=10)
plt.plot(xnew,0.5,'x',color='r',markersize=20)
plt.plot((0,xnew),(0.5,0.5),':')
Plt.网格(真)
请显示())
哪个给了
插值值为56.81214249272691
使用numpy
numpy也有一个interp函数,但它不为您排序。如果你不分类,你会很抱歉的:
不检查X坐标序列xp是否正在增加。如果是xp
不增加,结果是无意义的。
我能让np.interp工作的唯一方法是将数据推送到结构化数组中。
导入numpy as np
将matplotlib.pyplot导入为plt
my_x=np.数组([4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,
48,50,52,54,56,58,60,62,64,66],dtype=np.float)
my_y_raw=np.数组([0.99470977497817203,0.99434995886145172,
0.98974611323163653,0.961630837657524,0.9932763558441175,
0.9933892769251909,0.99428263292577534,0.9869051421271611,
0.99111667721533181,0.99149418924880861,0.99133773062680464,
0.99143506380003499,0.9915108464011454,0.99268261743308517,
0.9928975725282316,0.99100207861144063,0.99157171773324027,
0.99112571824824358,0.99031608691035722,0.9897810426076905,
0.989782674787969,0.98897835092187614,0.98517540405423909,
0.98308943666187076,0.9608181781994603,0.85563541881892147,
0.61570811548079107,0.3307627640577052,0.1465134838124245,
0.07685314712214226,0.035831324928136087,0.021344669212790181],
d类型=NP.浮动)
dt=np.dtype([('x',np.float),('y',np.float)]
数据=np.zeros((len(my_x)),dtype=dt)
数据['x']=我的x
数据['Y']=我的原始数据
data.sort(order='y')按y值对数据进行就地排序
print('numpy interp gives',np.interp(0.5,data['y'],data['x']))
哪个给了
numpy interp给出56.81214249272691
功能。scipy与numpy密切相关,您可能已经安装了它。有利于interp1d
它可以为您对数据进行排序。这是以有点古怪的语法为代价的。在许多插值函数中,假设您试图从x值中插值y值。这些函数通常需要“x”值单调递增。在您的例子中,我们交换了x和y的正常意义。y值有一个异常值,正如@abhishek mishra指出的那样。对于您的数据,您是幸运的,您可以摆脱离开离群值在。
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
my_x = [4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,
48,50,52,54,56,58,60,62,64,66]
my_y_raw=np.array([0.99470977497817203, 0.99434995886145172,
0.98974611323163653, 0.961630837657524, 0.99327633558441175,
0.99338952769251909, 0.99428263292577534, 0.98690514212711611,
0.99111667721533181, 0.99149418924880861, 0.99133773062680464,
0.99143506380003499, 0.99151080464011454, 0.99268261743308517,
0.99289757252812316, 0.99100207861144063, 0.99157171773324027,
0.99112571824824358, 0.99031608691035722, 0.98978104266076905,
0.989782674787969, 0.98897835092187614, 0.98517540405423909,
0.98308943666187076, 0.96081810781994603, 0.85563541881892147,
0.61570811548079107, 0.33076276040577052, 0.14655134838124245,
0.076853147122142126, 0.035831324928136087, 0.021344669212790181])
# set assume_sorted to have scipy automatically sort for you
f = interp1d(my_y_raw, my_x, assume_sorted = False)
xnew = f(0.5)
print('interpolated value is ', xnew)
plt.plot(my_x, my_y_raw,'x-', markersize=10)
plt.plot(xnew, 0.5, 'x', color = 'r', markersize=20)
plt.plot((0, xnew), (0.5,0.5), ':')
plt.grid(True)
plt.show()
哪个给了
interpolated value is 56.81214249272691
使用numpy
numpy也有一个interp功能,但它不适合您。如果你不分类,你会很抱歉:
不检查X坐标序列xp是否正在增加。如果是xp
没有增加,结果是胡说。
我能让np.interp工作的唯一方法是将数据推送到结构化数组中。
import numpy as np
import matplotlib.pyplot as plt
my_x = np.array([4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,
48,50,52,54,56,58,60,62,64,66], dtype = np.float)
my_y_raw=np.array([0.99470977497817203, 0.99434995886145172,
0.98974611323163653, 0.961630837657524, 0.99327633558441175,
0.99338952769251909, 0.99428263292577534, 0.98690514212711611,
0.99111667721533181, 0.99149418924880861, 0.99133773062680464,
0.99143506380003499, 0.99151080464011454, 0.99268261743308517,
0.99289757252812316, 0.99100207861144063, 0.99157171773324027,
0.99112571824824358, 0.99031608691035722, 0.98978104266076905,
0.989782674787969, 0.98897835092187614, 0.98517540405423909,
0.98308943666187076, 0.96081810781994603, 0.85563541881892147,
0.61570811548079107, 0.33076276040577052, 0.14655134838124245,
0.076853147122142126, 0.035831324928136087, 0.021344669212790181],
dtype = np.float)
dt = np.dtype([('x', np.float), ('y', np.float)])
data = np.zeros( (len(my_x)), dtype = dt)
data['x'] = my_x
data['y'] = my_y_raw
data.sort(order = 'y') # sort data in place by y values
print('numpy interp gives ', np.interp(0.5, data['y'], data['x']))
哪个给了
numpy interp gives 56.81214249272691