teceai

RF_hyperparameters search

Jan 25th, 2021
645
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. best_model = None
  2. best_acc = 0
  3. best_f1 = 0
  4. best_depth = 0
  5. best_est = 0
  6.  
  7. for est in range(1, 50):
  8.     for depth in range (1, 16):
  9.         model_forest = RandomForestClassifier(random_state=42, n_estimators=est, max_depth=depth, class_weight='balanced')
  10.         model_forest.fit(X_train_counts, y_train)
  11.         predictions_rf = model_forest.predict(X_test_counts)
  12.         acc_score = accuracy_score(predictions_rf, y_test)
  13.         f1score = f1_score(predictions_rf, y_test, pos_label=None, average='weighted')
  14.         if f1score > best_f1:
  15.             best_model = model_forest
  16.             best_f1 = f1score
  17.             best_est = est
  18.             best_depth = depth
  19. print("F1:", best_f1, "Количество деревьев:", best_est, "Максимальная глубина:", best_depth)
RAW Paste Data