Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- df_cars['mpg'] = df_cars['mpg'].fillna((df_cars.mpg).mean())
- best_r2 = 0
- best_n = 0;
- for n_value in range (1, 16):
- ls = np.polyfit(df_cars.mpg, df_cars.wt, n_value)
- p = np.poly1d(ls) # Allows for operations on polynomials for calculation in graph
- xp = np.linspace(df_cars.mpg.min(), df_cars.mpg.max(), 100)
- results = []
- for x_coord in df_cars.mpg:
- results.append(p(x_coord))
- rsquared = metrics.r2_score(df_cars.wt.values, results)
- if rsquared > best_r2:
- best_r2 = rsquared
- best_n = n_value
- print(f'n of {best_n}, gives bests r2 =', round(rsquared, 5))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement