Advertisement
mbazs

Polynomial Regression

Oct 13th, 2017
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.86 KB | None | 0 0
  1. import numpy as np
  2. from scipy import stats
  3. import matplotlib.pyplot as plt
  4. import sklearn.metrics
  5.  
  6. def main():
  7.     xs = np.random.normal(4.0, 1.2, 1000)
  8.     ys = np.random.normal(40.0, 1.2, 1000) / xs
  9.  
  10.     f2 = np.poly1d(np.polyfit(np.array(xs), np.array(ys), 2))
  11.     f3 = np.poly1d(np.polyfit(np.array(xs), np.array(ys), 3))
  12.     f4 = np.poly1d(np.polyfit(np.array(xs), np.array(ys), 4))
  13.     f5 = np.poly1d(np.polyfit(np.array(xs), np.array(ys), 5))
  14.     xp = np.linspace(0, 7, 100)
  15.  
  16.     plt.title("Polynomial Regression")
  17.     plt.scatter(xs, ys)
  18.     p2, = plt.plot(xp, f2(xp), c = 'r')
  19.     p3, = plt.plot(xp, f3(xp), c = 'g')
  20.     p4, = plt.plot(xp, f4(xp), c = 'y')
  21.     p5, = plt.plot(xp, f5(xp), c = 'b')
  22.     plt.legend([p2, p3, p4, p5], [label + " degree" for label in ["2nd", "3rd", "4th", "5th"]])
  23.     plt.show()
  24.  
  25. if __name__ == '__main__':
  26.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement