Created
February 10, 2020 00:22
-
-
Save M3nin0/153bdee45db1543f6e3f1f299694f12b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
# Example to understand the difference between interpolation and regression | |
import numpy as np | |
from scipy import interpolate | |
import matplotlib.pyplot as plt | |
from sklearn.linear_model import LinearRegression | |
# ===== Regression | |
x = np.array([5, 15, 25, 35, 45, 55]).reshape((-1, 1)) | |
y = np.array([5, 20, 14, 32, 22, 38]) | |
model = LinearRegression() | |
model.fit(x, y) | |
y_pred = model.predict(x) | |
plt.figure() | |
plt.plot(x, y, 'o') | |
plt.plot(x, y_pred) | |
plt.legend(['Original', 'Regression']) | |
plt.show() | |
# ===== Interpolation | |
x = np.array([5, 15, 25, 35, 45, 55]) | |
y = np.array([5, 20, 14, 32, 22, 38]) | |
f = interpolate.interp1d(x, y) | |
y_new = f(x) | |
plt.plot(x, y, 'o', x, y_new, '-') | |
plt.legend(['Original', 'Interpolation']) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment