Skip to content

Instantly share code, notes, and snippets.

@M3nin0
Created February 10, 2020 00:22
Show Gist options
  • Save M3nin0/153bdee45db1543f6e3f1f299694f12b to your computer and use it in GitHub Desktop.
Save M3nin0/153bdee45db1543f6e3f1f299694f12b to your computer and use it in GitHub Desktop.
#!/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