Interpolation and Extrapolation
概念
Interpolation(插值):在已知数据点之间估算未知数据点的值。假设已知数据点之间的变化是平滑且可预测的。
Extrapolation(外推):在已知数据点范围之外预测未知数据点的值。假设已知数据点的模式和趋势在未知范围内继续有效。
原理
Interpolation:插值方法通过已知数据点之间的关系,推测或计算出数据点之间的值。常用的方法包括线性插值、多项式插值和样条插值。
Extrapolation:外推方法利用已知数据的趋势和模式,预测数据范围之外的值。常用的方法包括线性回归、多项式回归和指数回归。
常用方法
Interpolation
-
线性插值(Linear Interpolation):
-
多项式插值(Polynomial Interpolation):
-
样条插值(Spline Interpolation):
- 三次样条插值(Cubic Spline Interpolation)
Extrapolation
-
线性回归(Linear Regression):
-
多项式回归(Polynomial Regression):
-
指数回归(Exponential Regression):
Python 实现
Interpolation
线性插值(Linear Interpolation):
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
# 已知数据点
x = np.array([0, 1, 2, 3, 4, 5])
y = np.array([0, 2, 4, 6, 8, 10])
# 线性插值函数
linear_interpolator = interp1d(x, y, kind='linear')
# 新数据点
x_new = np.linspace(0, 5, 50)
y_new = linear_interpolator(x_new)
# 可视化
plt.figure(figsize=(10, 6))
plt.plot(x, y, 'o', label='Original Data')
plt.plot(x_new, y_new, '-', label='Linear Interpolation')
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Linear Interpolation Example')
plt.legend()
plt.show()
多项式插值(Polynomial Interpolation):
# 多项式插值
poly_interpolator = np.poly1d(np.polyfit(x, y, deg=2))
y_poly_new = poly_interpolator(x_new)
plt.figure(figsize=(10, 6))
plt.plot(x, y, 'o', label='Original Data')
plt.plot(x_new, y_poly_new, '-', label='Polynomial Interpolation')
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Polynomial Interpolation Example')
plt.legend()
plt.show()
样条插值(Spline Interpolation):
from scipy.interpolate import CubicSpline
# 三次样条插值
spline_interpolator = CubicSpline(x, y)
y_spline_new = spline_interpolator(x_new)
plt.figure(figsize=(10, 6))
plt.plot(x, y, 'o', label='Original Data')
plt.plot(x_new, y_spline_new, '-', label='Cubic Spline Interpolation')
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Cubic Spline Interpolation Example')
plt.legend()
plt.show()
Extrapolation
线性回归(Linear Regression):
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
# 已知数据点
x_train = np.array([1, 2, 3, 4, 5]).reshape(-1, 1)
y_train = np.array([2, 4, 5, 4, 5])
# 创建线性回归模型并拟合数据
model = LinearRegression()
model.fit(x_train, y_train)
# 预测范围外的数据点
x_new = np.array([6, 7, 8]).reshape(-1, 1)
y_new = model.predict(x_new)
# 可视化
plt.figure(figsize=(10, 6))
plt.scatter(x_train, y_train, color='blue', label='Training Data')
plt.plot(x_train, model.predict(x_train), color='green', label='Regression Line')
plt.scatter(x_new, y_new, color='red', label='Extrapolated Data')
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Linear Regression and Extrapolation')
plt.legend()
plt.show()
多项式回归(Polynomial Regression):
# 已知数据点
x_train = np.array([1, 2, 3, 4, 5])
y_train = np.array([2, 4, 5, 4, 5])
# 创建多项式回归模型并拟合数据
coefficients = np.polyfit(x_train, y_train, deg=2)
polynomial = np.poly1d(coefficients)
# 预测范围外的数据点
x_new = np.array([6, 7, 8])
y_new = polynomial(x_new)
# 可视化
plt.figure(figsize=(10, 6))
plt.scatter(x_train, y_train, color='blue', label='Training Data')
plt.plot(x_train, polynomial(x_train), color='green', label='Polynomial Regression')
plt.scatter(x_new, y_new, color='red', label='Extrapolated Data')
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Polynomial Regression and Extrapolation')
plt.legend()
plt.show()
指数回归(Exponential Regression):
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
# 定义指数函数
def exp_func(x, a, b):
return a * np.exp(b * x)
# 已知数据点
x_train = np.array([1, 2, 3, 4, 5])
y_train = np.array([2, 4, 8, 16, 32])
# 拟合指数函数
params, covariance = curve_fit(exp_func, x_train, y_train)
# 预测范围外的数据点
x_new = np.array([6, 7, 8])
y_new = exp_func(x_new, *params)
# 可视化
plt.figure(figsize=(10, 6))
plt.scatter(x_train, y_train, color='blue', label='Training Data')
plt.plot(x_train, exp_func(x_train, *params), color='green', label='Exponential Regression')
plt.scatter(x_new, y_new, color='red', label='Extrapolated Data')
plt.xlabel('X')
plt.ylabel('Y')
plt.title('Exponential Regression and Extrapolation')
plt.legend()
plt.show()