In [1]:
%matplotlib inline
In [2]:
from __future__ import division
from matplotlib import pyplot as plt
import numpy as np
In [18]:
coeffs = [3.3, -4.2, 0.1, 7]
x = np.sort(5 * np.random.rand(100) - 2)
y = np.poly1d(coeffs)(x)
y += 10 * np.random.rand(100) - 5
coeff_fit = np.polyfit(x, y, 3)
In [19]:
plt.figure(figsize=(10, 5))
plt.scatter(x, y)
plt.plot(x, np.poly1d(coeff_fit)(x), color='orange', lw=3)
plt.grid()
In [7]:
from IPython.html.widgets import interact
In [24]:
COEFFS = 10 * np.random.rand(7) - 5
@interact(noise=(0, 30), degree=(1, len(COEFFS)))
def fit_example(noise, degree):
coeffs = COEFFS[:degree]
x = np.sort(5 * np.random.rand(100) - 2)
y = np.poly1d(coeffs)(x)
y += 2*noise * np.random.rand(100) - noise
coeff_fit = np.polyfit(x, y, len(coeffs)-1)
plt.figure(figsize=(10, 5))
plt.scatter(x, y)
plt.plot(x, np.poly1d(coeff_fit)(x), color='orange', lw=3)
print coeffs
print coeff_fit
In [ ]: