Sunday, September 15, 2019

Linear regression is a private case of polynomial regression


  • The formula of linear regression: y=mx+b .This formula represents a straight line but not all relationships are linear.
  • Linear regression is just one example of all class of regressions.It's actually a first-degree polynomial regression.
  • Polynomial regressions:
    first degree polynomial regression - linear regression

    second degree polynomial regression
    third degree polynomial regression

  • python code example
    import numpy as np
    import matplotlib.pyplot as plt
    
    np.random.seed(2)
    numberOfVisitors = np.random.normal(3, 1, 1000)
    numberOfClicks = np.random.normal(50, 10, 1000) / numberOfVisitors
    
    x = np.array(numberOfVisitors)
    y = np.array(numberOfClicks)
    
    # create a line from x=0 to x=7 width 100 evenly spaced values
    xp = np.linspace(0, 7, 100)
    
    # 4 degree polynomial fit
    p4 = np.poly1d(np.polyfit(x, y, 4))
    plt.scatter(x, y)
    plt.plot(xp, p4(xp), c='r')
    plt.show()
4 degree polynomial fit






No comments:

Post a Comment