Code created to understand the scope of interpolation and extrapolation.
It is necessary to be careful about overfitting, but in the range of extrapolation, It is necessary to thoroughly examine whether the obtained approximation formula is applicable.
Also, if the purpose of approximation is "analysis" rather than "prediction" If you increase the number of variables or the order more than necessary, it will be difficult to interpret. The challenge is how easy it is, or how simple it can be tailored (where to show your arms?).
Red dashed line </ font>: Extrapolation range, Blue solid line </ font>: Interpolation range.
interpolation_and_extrapolation
import numpy as np
import matplotlib.pyplot as plt
x = np.array([2.0, 3.5, 4.0, 4.5, 5.0, 5.5])
y = np.array([3.0, 3.2, 3.9, 5.2, 8.4, 10.5])
xp = np.linspace(2, 5.5, 100)
xp1 = np.linspace(0, 2, 100)
xp2 = np.linspace(5.5, 8, 100)
for val in range(1, 6):
fx = np.poly1d(np.polyfit(x, y, val))
plt.rcParams["font.size"] = 20
fig, ax = plt.subplots(figsize=(15, 10))
ax.plot(xp, fx(xp), '-', color='blue')
ax.plot(xp1, fx(xp1), '-', color='red', linestyle='dashed')
ax.plot(xp2, fx(xp2), '-', color='red', linestyle='dashed')
ax.scatter(x, y, color='deepskyblue', s=32)
s = '$y =$'
for idx, deg in enumerate(reversed(range(0, val+1))):
if (fx.coef[idx] > 0) & (idx != 0):
s += '$ +$'
if deg > 1:
s += f' ${fx.coef[idx]:.2f} x^{deg}$'
if deg == 1:
s += f' ${fx.coef[idx]:.2f} x$'
if deg == 0:
s += f' ${fx.coef[idx]:.2f}$'
# ax.text(0.05, 0.8, s=s, size='x-large', transform=ax.transAxes)
ax.axhline([0], color='black')
ax.axvline([2], color='gray', linestyle='dotted')
ax.axvline([5.5], color='gray', linestyle='dotted')
ax.set_xlim(0, 8)
ax.set_ylim(-3, 14)
ax.set_title(s)
ax.set_ylabel('response variable')
ax.set_xlabel('explanatory variables')
fig.savefig(f'./data/img/inter_and_extrapolation{val}.png')
First-order approximation
Second-order approximation
3rd order approximation
4th order approximation
5th order approximation
Recommended Posts