Lesson 9/41_find_best_fit_line_polynomial.py
def fit_poly(data, error_func, degree=3):
    """Fit a polynomial to given data, using a supplied error function.

    Parameters:
        data: 2D array where each row is a point (X0, Y)
        error_func: function that computes the error between a polynomial and observed data

    Returns polynomial that minimizes the error function.
    """
    # Generate initial guess for polynomial model (all coefficients = 1)
    Cguess = np.poly1d(np.ones(degree + 1, dtype=np.float32))

    # Plot initial guess (optional)
    x = np.linspace(-5, 5, 21)
    plt.plot(x, np.polyval(Cguess, x), 'm--', linewidth=2.0, label="Initial guess")

    # Call optimizer to minimize error function
    result = spo.minimize(error_func, Cguess, args=(data,), method='SLSQP', options={'disp': True})
    return np.poly1d(result.x)
Fit a polynomial to given data, using a supplied error function.

    Parameters:
        data: 2D array where each row is a point (X0, Y)
        error_func: function that computes the error between a polynomial and observed data

    Returns polynomial that minimizes the error function.
    
Fit a polynomial to given data, using a supplied error function.
Fit a polynomial to given data using a supplied error function .
def fit_poly(data, error_func, degree=3):
    Cguess = np.poly1d(np.ones(degree + 1, dtype=np.float32))
    x = np.linspace(-5, 5, 21)
    plt.plot(x, np.polyval(Cguess, x), 'm--', linewidth=2.0, label="Initial guess")
    result = spo.minimize(error_func, Cguess, args=(data,), method='SLSQP', options={'disp': True})
    return np.poly1d(result.x)
def fit_poly ( data , error_func , degree = 3 ) : Cguess = np . poly1d ( np . ones ( degree + 1 , dtype = np . float32 ) ) x = np . linspace ( - 5 , 5 , 21 ) plt . plot ( x , np . polyval ( Cguess , x ) , 'm--' , linewidth = 2.0 , label = "Initial guess" ) result = spo . minimize ( error_func , Cguess , args = ( data , ) , method = 'SLSQP' , options = { 'disp' : True } ) return np . poly1d ( result . x )
Fit a polynomial to given data, using a supplied error function.
Fit a polynomial to given data using a supplied error function .
"""Fit a polynomial to given data, using a supplied error function.

    Parameters:
        data: 2D array where each row is a point (X0, Y)
        error_func: function that computes the error between a polynomial and observed data

    Returns polynomial that minimizes the error function.
    """ # Generate initial guess for polynomial model (all coefficients = 1) # Plot initial guess (optional) # Call optimizer to minimize error function
data
2D array where each row is a point (X0, Y)
2D array where each row is a point ( X0 Y )
error_func
function that computes the error between a polynomial and observed data
function that computes the error between a polynomial and observed data
degree