model.py
def plot_polyreg(y_test, lm_sq_pred_t, y_pred_test, bl):
    '''
    Plots the baseline, polynomial line of best fit,
    and the actual vs predicted values for popularity
    '''
    from scipy.optimize import curve_fit
    # define the true objective function
    def objective(x, a, b, c):
        return a * x + b * x**2 + c

    x, y = y_test, y_pred_test
    popt, _ = curve_fit(objective, x, y)
    a, b, c = popt
    equation = f'y = %.3f $x^2$ + %.3f x + %.2f' % (a, b, c)

    # first, a gray line representing the baseline prediction,
    # a horizontal line because it only predicts the average
    plt.figure(figsize=(16,8))
    plt.axhline(bl, alpha=.95, color="black", linestyle='--', label='Baseline Prediction: Average')

    # define a sequence of inputs between the smallest and largest known inputs
    x_line = np.arange(min(x), max(x), 1)
    # calculate the output for the range
    y_line = objective(x_line, a, b, c)
    # create a line plot for the mapping function
    plt.plot(x_line, y_line, color='dodgerblue', linewidth=3, label='The Line of Best Fit')

    # next, a scatter plot representing each observation (song popularity) as the actual value vs. the predicted value
    plt.scatter(y_test, lm_sq_pred_t, 
            alpha=.4, color='red', s=50, label='Model: Polynomial Regression - 2nd Degree')

    # adding plot labels
    plt.annotate(equation, xy=(35, 38.5), xytext=(40,17), size=14,
                arrowprops=dict(facecolor='dodgerblue', shrink=0.05, alpha=.8, linewidth=.5))
    plt.legend(fontsize=14)
    plt.xticks([0, 20, 40, 60, 80, 100])
    plt.yticks([0, 20, 40, 60])
    #plt.xlabel("Actual Song Popularity", size=14)
    #plt.ylabel("Predicted Song Popularity", size=14)
    #plt.title("How Does the Final Model Compare to Baseline? And to Actual Values?", size=15)
    plt.show()
    print(equation)

    Plots the baseline, polynomial line of best fit,
    and the actual vs predicted values for popularity
    
Plots the baseline, polynomial line of best fit,
and the actual vs predicted values for popularity
Plots the baseline polynomial line of best fit and the actual vs predicted values for popularity
def plot_polyreg(y_test, lm_sq_pred_t, y_pred_test, bl):
    from scipy.optimize import curve_fit
    def objective(x, a, b, c):
        return a * x + b * x**2 + c
    x, y = y_test, y_pred_test
    popt, _ = curve_fit(objective, x, y)
    a, b, c = popt
    equation = f'y = %.3f $x^2$ + %.3f x + %.2f' % (a, b, c)
    plt.figure(figsize=(16,8))
    plt.axhline(bl, alpha=.95, color="black", linestyle='--', label='Baseline Prediction: Average')
    x_line = np.arange(min(x), max(x), 1)
    y_line = objective(x_line, a, b, c)
    plt.plot(x_line, y_line, color='dodgerblue', linewidth=3, label='The Line of Best Fit')
    plt.scatter(y_test, lm_sq_pred_t, 
            alpha=.4, color='red', s=50, label='Model: Polynomial Regression - 2nd Degree')
    plt.annotate(equation, xy=(35, 38.5), xytext=(40,17), size=14,
                arrowprops=dict(facecolor='dodgerblue', shrink=0.05, alpha=.8, linewidth=.5))
    plt.legend(fontsize=14)
    plt.xticks([0, 20, 40, 60, 80, 100])
    plt.yticks([0, 20, 40, 60])
    plt.show()
    print(equation)
def plot_polyreg ( y_test , lm_sq_pred_t , y_pred_test , bl ) : from scipy . optimize import curve_fit def objective ( x , a , b , c ) : return a * x + b * x ** 2 + c x , y = y_test , y_pred_test popt , _ = curve_fit ( objective , x , y ) a , b , c = popt equation = f'y = %.3f $x^2$ + %.3f x + %.2f' % ( a , b , c ) plt . figure ( figsize = ( 16 , 8 ) ) plt . axhline ( bl , alpha = .95 , color = "black" , linestyle = '--' , label = 'Baseline Prediction: Average' ) x_line = np . arange ( min ( x ) , max ( x ) , 1 ) y_line = objective ( x_line , a , b , c ) plt . plot ( x_line , y_line , color = 'dodgerblue' , linewidth = 3 , label = 'The Line of Best Fit' ) plt . scatter ( y_test , lm_sq_pred_t , alpha = .4 , color = 'red' , s = 50 , label = 'Model: Polynomial Regression - 2nd Degree' ) plt . annotate ( equation , xy = ( 35 , 38.5 ) , xytext = ( 40 , 17 ) , size = 14 , arrowprops = dict ( facecolor = 'dodgerblue' , shrink = 0.05 , alpha = .8 , linewidth = .5 ) ) plt . legend ( fontsize = 14 ) plt . xticks ( [ 0 , 20 , 40 , 60 , 80 , 100 ] ) plt . yticks ( [ 0 , 20 , 40 , 60 ] ) plt . show ( ) print ( equation )
Plots the baseline, polynomial line of best fit,
and the actual vs predicted values for popularity
Plots the baseline polynomial line of best fit and the actual vs predicted values for popularity
'''
    Plots the baseline, polynomial line of best fit,
    and the actual vs predicted values for popularity
    ''' # define the true objective function # first, a gray line representing the baseline prediction, # a horizontal line because it only predicts the average # define a sequence of inputs between the smallest and largest known inputs # calculate the output for the range # create a line plot for the mapping function # next, a scatter plot representing each observation (song popularity) as the actual value vs. the predicted value # adding plot labels #plt.xlabel("Actual Song Popularity", size=14) #plt.ylabel("Predicted Song Popularity", size=14) #plt.title("How Does the Final Model Compare to Baseline? And to Actual Values?", size=15)
y_test
lm_sq_pred_t
y_pred_test
bl