Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.33 KB | None | 0 0
  1. x = df_aux['201701']#.values.reshape(-1, 1)
  2. y = df_aux['n_visits']#.values.reshape(-1, 1)
  3.  
  4. def fit_poly( degree ):
  5.     p = np.polyfit( x, y, deg = degree )
  6.     df_aux['fit'] = np.polyval( p, x )
  7.     sns.regplot(x, y, fit_reg = False )
  8.    
  9.     lists = sorted(zip(*[x,df_aux['fit']]))
  10.     new_x, new_y = list(zip(*lists))
  11.    
  12.     r2 = r2_score(y,df_aux['fit'])
  13.     print("R squared: ", r2)
  14.     plt.xlabel('n_crimes')
  15.     plt.title('N_visits/N_crimes per Borough')
  16.    
  17.     return plt.plot( new_x, new_y, color='r' )
  18.  
  19.  
  20. def get_rmse( y, y_fit ):
  21.     return np.sqrt( metrics.mean_squared_error( y, y_fit ) )
  22.  
  23. train_X, test_X, train_y, test_y = train_test_split( x,
  24.                                                   y,
  25.                                                   test_size = 0.20,
  26.                                                   random_state = 100 )
  27.  
  28. rmse_df = pd.DataFrame( columns = ["degree", "rmse_train", "rmse_test"] )
  29.  
  30. for i in range( 1, 15 ):
  31.     p = np.polyfit( train_X, train_y, deg = i )
  32.     rmse_df.loc[i-1] = [ i,
  33.                       get_rmse( train_y, np.polyval( p, train_X ) ),
  34.                       get_rmse( test_y, np.polyval( p, test_X ) ) ]
  35.    
  36.  
  37.  
  38.  
  39.  
  40. plt.plot( rmse_df.degree, rmse_df.rmse_train,label='train',color = 'r' )
  41.  
  42. plt.plot( rmse_df.degree,rmse_df.rmse_test,label='test',color = 'g' )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement