Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. df_cars['mpg'] = df_cars['mpg'].fillna((df_cars.mpg).mean())
  2.  
  3. best_r2 = 0
  4. best_n = 0;
  5. for n_value in range (1, 16):
  6. ls = np.polyfit(df_cars.mpg, df_cars.wt, n_value)
  7. p = np.poly1d(ls) # Allows for operations on polynomials for calculation in graph
  8. xp = np.linspace(df_cars.mpg.min(), df_cars.mpg.max(), 100)
  9. results = []
  10. for x_coord in df_cars.mpg:
  11. results.append(p(x_coord))
  12.  
  13. rsquared = metrics.r2_score(df_cars.wt.values, results)
  14. if rsquared > best_r2:
  15. best_r2 = rsquared
  16. best_n = n_value
  17.  
  18. print(f'n of {best_n}, gives bests r2 =', round(rsquared, 5))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement