Advertisement
korenizla

grid_search

Oct 31st, 2022 (edited)
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.52 KB | None | 0 0
  1. %%time
  2. from sklearn.model_selection import GridSearchCV
  3.  
  4. model_gs = RandomForestClassifier(random_state=12345, criterion="gini")
  5. parametrs_gs  = {
  6.     'n_estimators': range(1,126, 25),
  7.     'min_samples_leaf': range (1,8),
  8.     'max_depth': range (1,13, 2)
  9. }
  10. gs = GridSearchCV(estimator = model_gs,
  11.                   param_grid = parametrs_gs,
  12.                   cv=5,
  13.                   scoring=['f1', 'roc_auc'],
  14.                   refit='f1')
  15. gs.fit(pd.concat([features_train, features_valid]), pd.concat([target_train, target_valid]))
  16.  
  17. print(gs.best_estimator_)
  18. print(gs.best_score_)
  19.    
  20.     #Output
  21.     #RandomForestClassifier(max_depth=11, min_samples_leaf=2, n_estimators=76,
  22.                        #random_state=12345)
  23.     #0.866125
  24.     #CPU times: user 3min 23s, sys: 932 ms, total: 3min 24s
  25.     #Wall time: 3min 25s
  26.  
  27. # ПРИМЕНЯЮ ПАРАМЕТРЫ НА МОДЕЛИ
  28. %%time
  29. model = RandomForestClassifier(random_state=12345, criterion='gini', max_depth=11, min_samples_leaf=2, n_estimators=76)
  30. model.fit(features_train, target_train)
  31. predicted_train = model.predict(features_train)
  32. predicted_valid = model.predict(features_valid)
  33. # print(f1_score(target_train, predicted_train))
  34. print("F1: ", f1_score(target_valid, predicted_valid))
  35. print("ROC_AUC: ", roc_auc_score(target_valid, predicted_valid))
  36. print("RECALL: ", recall_score(target_valid, predicted_valid))
  37. print("PRECISION: ", precision_score(target_valid, predicted_valid))
  38. print("Accuracy_score ", accuracy_score(target_valid, predicted_valid))
  39.  
  40. #Output
  41. #F1:  0.5466448445171849
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement