- model = RandomForestClassifier()
- parametrs = ({'max_depth':range(2, 10),
- 'n_estimators':range(1, 81, 5)
- })
- grid_search_cv_model = GridSearchCV(model, parametrs, scoring='recall', n_jobs=-1, cv=4)
- %%time
- grid_search_cv_model.fit(features_tr, target)
- grid_search_cv_model.best_params_
- #выдал {'max_depth': 9, 'n_estimators': 6}
- grid_search_cv_model.best_score_
- #выдал 0.5176759274663117
- потом разбила (features_tr, target) на таргет и валидацию
- %%time
- best_model = None
- best_result = 0
- best_est = 0
- best_depth = 0
- for est in range(1, 81, 5):
- for depth in range (1, 21):
- model = RandomForestClassifier(random_state=12345, n_estimators=est, max_depth=depth)
- model.fit(features_train,target_train)
- pred=model.predict(features_valid)
- result = recall_score(target_valid,pred)
- if result > best_result:
- best_model = model
- best_result = result
- best_est = est
- best_depth = depth
- print("Recall наилучшей модели на валидационной выборке:", best_result)
- print(best_model)
- #получила Recall наилучшей модели на валидационной выборке: 0.7912161038096822
- RandomForestClassifier(max_depth=20, n_estimators=41, random_state=12345)