import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
设置 Numpy
浮点数显示格式:
np.set_printoptions(precision=2, suppress=True)
从文本中读入数据,数据来自 http://kinetics.nist.gov/janaf/html/C-067.txt ,保存为结构体数组:
data = np.genfromtxt("JANAF_CH4.txt",
delimiter="\t", # TAB 分隔
skiprows=1, # 忽略首行
names=True, # 读入属性
missing_values="INFINITE", # 缺失值
filling_values=np.inf) # 填充缺失值
显示部分数据:
for row in data[:7]:
print "{}\t{}".format(row['TK'], row['Cp'])
print "...\t..."
0.0 0.0
100.0 33.258
200.0 33.473
250.0 34.216
298.15 35.639
300.0 35.708
350.0 37.874
... ...
绘图:
p = plt.plot(data['TK'], data['Cp'], 'kx')
t = plt.title("JANAF data for Methane $CH_4$")
a = plt.axis([0, 6000, 30, 120])
x = plt.xlabel("Temperature (K)")
y = plt.ylabel(r"$C_p$ ($\frac{kJ}{kg K}$)")
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-D34hjquN-1576402113341)(04.02-interpolation-with-scipy_files/04.02-interpolation-with-scipy_9_0.png)]
假设我们要对这组数据进行插值。
先导入一维插值函数 interp1d
:
interp1d(x, y)
from scipy.interpolate import interp1d
ch4_cp = interp1d(data['TK'], data['Cp'])
interp1d
的返回值可以像函数一样接受输入,并返回插值的结果。
单个输入值,注意返回的是数组:
ch4_cp(382.2)
array(39.565144000000004)
输入数组,返回的是对应的数组:
ch4_cp([32.2,323.2])
array([ 10.71, 36.71])
默认情况下,输入值要在插值允许的范围内,否则插值会报错:
ch4_cp(8752)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
in ()
----> 1 ch4_cp(8752)
d:\Miniconda\lib\site-packages\scipy\interpolate\polyint.pyc in __call__(self, x)
77 """
78 x, x_shape = self._prepare_x(x)
---> 79 y = self._evaluate(x)
80 return self._finish_y(y, x_shape)
81
d:\Miniconda\lib\site-packages\scipy\interpolate\interpolate.pyc in _evaluate(self, x_new)
496 # The behavior is set by the bounds_error variable.
497 x_new = asarray(x_new)
--> 498 out_of_bounds = self._check_bounds(x_new)
499 y_new = self._call(self, x_new)
500 if len(y_new) > 0:
d:\Miniconda\lib\site-packages\scipy\interpolate\interpolate.pyc in _check_bounds(self, x_new)
526 "range.")
527 if self.bounds_error and above_bounds.any():
--> 528 raise ValueError("A value in x_new is above the interpolation "
529 "range.")
530
ValueError: A value in x_new is above the interpolation range.
但我们可以通过参数设置允许超出范围的值存在:
ch4_cp = interp1d(data['TK'], data['Cp'],
bounds_error=False)
不过由于超出范围,所以插值的输出是非法值:
ch4_cp(8752)
array(nan)
可以使用指定值替代这些非法值:
ch4_cp = interp1d(data['TK'], data['Cp'],
bounds_error=False, fill_value=-999.25)
ch4_cp(8752)
array(-999.25)
interp1d
默认的插值方法是线性,关于线性插值的定义,请参见:
其基本思想是,已知相邻两点 x 1 , x 2 x_1,x_2 x1,x2 对应的值 y 1 , y 2 y_1,y_2 y1,y2 ,那么对于 ( x 1 , x 2 ) (x_1,x_2) (x1,x2) 之间的某一点 x x x ,线性插值对应的值 y y y 满足:点 ( x , y ) (x,y) (x,y) 在 ( x 1 , y 1 ) , ( x 2 , y 2 ) (x_1,y_1),(x_2,y_2) (x1,y1),(x2,y2) 所形成的线段上。
应用线性插值:
T = np.arange(100,355,5)
plt.plot(T, ch4_cp(T), "+k")
p = plt.plot(data['TK'][1:7], data['Cp'][1:7], 'ro', markersize=8)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-iINEyXW5-1576402113342)(04.02-interpolation-with-scipy_files/04.02-interpolation-with-scipy_29_0.png)]
其中红色的圆点为原来的数据点,黑色的十字点为对应的插值点,可以明显看到,相邻的数据点的插值在一条直线上。
我们可以通过 kind
参数来调节使用的插值方法,来得到不同的结果:
nearest
最近邻插值zero
0阶插值linear
线性插值quadratic
二次插值cubic
三次插值4,5,6,7
更高阶插值最近邻插值:
cp_ch4 = interp1d(data['TK'], data['Cp'], kind="nearest")
p = plt.plot(T, cp_ch4(T), "k+")
p = plt.plot(data['TK'][1:7], data['Cp'][1:7], 'ro', markersize=8)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rI4gJwYc-1576402113343)(04.02-interpolation-with-scipy_files/04.02-interpolation-with-scipy_33_0.png)]
0阶插值:
cp_ch4 = interp1d(data['TK'], data['Cp'], kind="zero")
p = plt.plot(T, cp_ch4(T), "k+")
p = plt.plot(data['TK'][1:7], data['Cp'][1:7], 'ro', markersize=8)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AJb886ej-1576402113343)(04.02-interpolation-with-scipy_files/04.02-interpolation-with-scipy_35_0.png)]
二次插值:
cp_ch4 = interp1d(data['TK'], data['Cp'], kind="quadratic")
p = plt.plot(T, cp_ch4(T), "k+")
p = plt.plot(data['TK'][1:7], data['Cp'][1:7], 'ro', markersize=8)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rhxLfjoQ-1576402113344)(04.02-interpolation-with-scipy_files/04.02-interpolation-with-scipy_37_0.png)]
三次插值:
cp_ch4 = interp1d(data['TK'], data['Cp'], kind="cubic")
p = plt.plot(T, cp_ch4(T), "k+")
p = plt.plot(data['TK'][1:7], data['Cp'][1:7], 'ro', markersize=8)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-brDW7ElR-1576402113344)(04.02-interpolation-with-scipy_files/04.02-interpolation-with-scipy_39_0.png)]
事实上,我们可以使用更高阶的多项式插值,只要将 kind
设为对应的数字即可:
四次多项式插值:
cp_ch4 = interp1d(data['TK'], data['Cp'], kind=4)
p = plt.plot(T, cp_ch4(T), "k+")
p = plt.plot(data['TK'][1:7], data['Cp'][1:7], 'ro', markersize=8)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LYXNDx6F-1576402113344)(04.02-interpolation-with-scipy_files/04.02-interpolation-with-scipy_42_0.png)]
可以参见:
对于二维乃至更高维度的多项式插值:
from scipy.interpolate import interp2d, interpnd
其使用方法与一维类似。
关于径向基函数,可以参阅:
径向基函数,简单来说就是点 x x x 处的函数值只依赖于 x x x 与某点 c c c 的距离:
Φ ( x , c ) = Φ ( ∥ x − c ∥ ) \Phi(x,c) = \Phi(\|x-c\|) Φ(x,c)=Φ(∥x−c∥)
x = np.linspace(-3,3,100)
常用的径向基(RBF
)函数有:
高斯函数:
plt.plot(x, np.exp(-1 * x **2))
t = plt.title("Gaussian")
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5VUWsG6s-1576402113345)(04.02-interpolation-with-scipy_files/04.02-interpolation-with-scipy_50_0.png)]
Multiquadric
函数:
plt.plot(x, np.sqrt(1 + x **2))
t = plt.title("Multiquadric")
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sDMhklrP-1576402113345)(04.02-interpolation-with-scipy_files/04.02-interpolation-with-scipy_52_0.png)]
Inverse Multiquadric
函数:
plt.plot(x, 1. / np.sqrt(1 + x **2))
t = plt.title("Inverse Multiquadric")
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BbIVB1mZ-1576402113345)(04.02-interpolation-with-scipy_files/04.02-interpolation-with-scipy_54_0.png)]
对于径向基函数,其插值的公式为:
f ( x ) = ∑ j n j Φ ( ∥ x − x j ∥ ) f(x) = \sum_j n_j \Phi(\|x-x_j\|) f(x)=j∑njΦ(∥x−xj∥)
我们通过数据点 x j x_j xj 来计算出 n j n_j nj 的值,来计算 x x x 处的插值结果。
from scipy.interpolate.rbf import Rbf
使用 multiquadric
核的:
cp_rbf = Rbf(data['TK'], data['Cp'], function = "multiquadric")
plt.plot(data['TK'], data['Cp'], 'k+')
p = plt.plot(data['TK'], cp_rbf(data['TK']), 'r-')
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GHEvkmSi-1576402113346)(04.02-interpolation-with-scipy_files/04.02-interpolation-with-scipy_59_0.png)]
使用 gaussian
核:
cp_rbf = Rbf(data['TK'], data['Cp'], function = "gaussian")
plt.plot(data['TK'], data['Cp'], 'k+')
p = plt.plot(data['TK'], cp_rbf(data['TK']), 'r-')
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-U3HwOd5C-1576402113346)(04.02-interpolation-with-scipy_files/04.02-interpolation-with-scipy_61_0.png)]
使用 nverse_multiquadric
核:
cp_rbf = Rbf(data['TK'], data['Cp'], function = "inverse_multiquadric")
plt.plot(data['TK'], data['Cp'], 'k+')
p = plt.plot(data['TK'], cp_rbf(data['TK']), 'r-')
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dOyJKHaK-1576402113346)(04.02-interpolation-with-scipy_files/04.02-interpolation-with-scipy_63_0.png)]
不同的 RBF
核的结果也不同。
RBF
插值from mpl_toolkits.mplot3d import Axes3D
三维数据点:
x, y = np.mgrid[-np.pi/2:np.pi/2:5j, -np.pi/2:np.pi/2:5j]
z = np.cos(np.sqrt(x**2 + y**2))
fig = plt.figure(figsize=(12,6))
ax = fig.gca(projection="3d")
ax.scatter(x,y,z)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-YNxrRKMk-1576402113347)(04.02-interpolation-with-scipy_files/04.02-interpolation-with-scipy_69_1.png)]
3维 RBF
插值:
zz = Rbf(x, y, z)
xx, yy = np.mgrid[-np.pi/2:np.pi/2:50j, -np.pi/2:np.pi/2:50j]
fig = plt.figure(figsize=(12,6))
ax = fig.gca(projection="3d")
ax.plot_surface(xx,yy,zz(xx,yy),rstride=1, cstride=1, cmap=plt.cm.jet)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GtKDPsUT-1576402113347)(04.02-interpolation-with-scipy_files/04.02-interpolation-with-scipy_72_1.png)]