daily pastebin goal
60%
SHARE
TWEET

Untitled

a guest Feb 22nd, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import xgboost as xgb
  2.  
  3. import seaborn as sns
  4.  
  5. def xgb_mape(preds, dtrain):
  6.    labels = dtrain.get_label()
  7.    return('mape', np.mean(np.abs((labels - preds) / (labels+1))))
  8.  
  9. mpg = sns.load_dataset('mpg')
  10.  
  11. mpg = mpg.sample(frac = 1)
  12.  
  13. n = int(mpg.shape[0] * 0.7)
  14.  
  15. mpg_train = mpg.iloc[:n, :7]
  16.  
  17. mpg_test = mpg.iloc[n:, :7]
  18.  
  19. mpg_train_y = mpg_train.iloc[:, 0].values
  20.  
  21. mpg_test_y = mpg_test.iloc[:, 0].values
  22.  
  23. mpg_train_X = mpg_train.iloc[:, 1:].values
  24.  
  25. mpg_test_X = mpg_test.iloc[:, 1:].values
  26.  
  27. xgb_model_mpg = xgb.XGBRegressor(max_depth= 10, learning_rate=0.1, n_estimators=1000, silent=True,
  28.                                  objective='reg:linear',
  29.                  booster='gbtree', subsample= 0.6, colsample_bytree= 0.9, colsample_bylevel= 1, reg_lambda= 20 ,
  30.                  random_state=1 , seed= 1, importance_type='gain')
  31.  
  32. xgb_model_mpg.fit(mpg_train_X ,mpg_train_y , eval_set= [(mpg_test_X , mpg_test_y )], eval_metric= xgb_mape,
  33.               early_stopping_rounds= 20)
  34. [...]
  35. 82] validation_0-rmse:3.41167   validation_0-mape:0.085761
  36. [83]    validation_0-rmse:3.40828   validation_0-mape:0.085618
  37. [84]    validation_0-rmse:3.40087   validation_0-mape:0.085519
  38. [85]    validation_0-rmse:3.403 validation_0-mape:0.085631
  39. [86]    validation_0-rmse:3.39977   validation_0-mape:0.085711
  40. [87]    validation_0-rmse:3.39626   validation_0-mape:0.085739
  41. [88]    validation_0-rmse:3.40048   validation_0-mape:0.085727
  42. [89]    validation_0-rmse:3.40356   validation_0-mape:0.085883
  43. [90]    validation_0-rmse:3.40341   validation_0-mape:0.085664
  44. Stopping. Best iteration:
  45. [70]    validation_0-rmse:3.42186   validation_0-mape:0.085076
  46.  
  47. XGBRegressor(base_score=0.5, booster='gbtree', colsample_bylevel=1,
  48.        colsample_bytree=0.9, gamma=0, importance_type='gain',
  49.        learning_rate=0.1, max_delta_step=0, max_depth=10,
  50.        min_child_weight=1, missing=None, n_estimators=1000, n_jobs=1,
  51.        nthread=None, objective='reg:linear', random_state=1, reg_alpha=0,
  52.        reg_lambda=20, scale_pos_weight=1, seed=1, silent=True,
  53.        subsample=0.6)
  54.  
  55. y_pred = xgb_model_mpg.predict(mpg_test_X)
  56.  
  57. results = pd.DataFrame({'actual':mpg_test_y, 'predictions' : y_pred})
  58.  
  59. results['Absolute_Percent_Error'] = 100 * np.abs(results['actual'] - results['predictions'])/results['actual']
  60.  
  61. MAPE = results['Absolute_Percent_Error'].mean()
  62.  
  63. MAPE
  64. 8.982732737486339
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top