Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Функция для подбора наилучших гиперпараметров модели
- # Обучает модель на обучающем наборе данных, возвращает лучшие: модель, параметры и метрику качества (ROC-AUC)
- # Дополнительно формирует список для отчёта по работе различных моделей
- # Стратегии для заполнения пропусков 'mean', 'median', 'most_frequent', 'const'
- def best_model(model, params, features, target, title, list_num, list_cat):
- num_features = list_num
- cat_features = list_cat
- preprocessor_model = ColumnTransformer(
- transformers=[
- ('num_imputer', SimpleImputer(strategy='median'), num_features),
- ('cat_imputer', SimpleImputer(strategy='most_frequent'), cat_features)
- ])
- pipeline_model = Pipeline(
- steps=[
- ('preprocessor', preprocessor_model),
- ('model', model)
- ])
- gs_model = GridSearchCV(estimator=pipeline_model,
- param_grid=params,
- scoring='roc_auc',
- cv=CV,
- n_jobs=-1)
- gs_model.fit(features, target)
- best_model = gs_model.best_estimator_
- best_params = gs_model.best_params_
- best_score = gs_model.best_score_
- params_out.append({'model_name': title,
- 'model': gs_model.best_estimator_,
- 'model_params': gs_model.best_params_,
- 'ROC-AUC': gs_model.best_score_})
- return best_model, best_params, best_score
- # Создаем модель и список параметров
- lgbm_model = LGBMClassifier(random_state=STATE)
- lgbm_params = {'model__n_estimators': np.arange(50, 251, 50),
- 'model__max_depth': np.arange(5, 11),
- 'model__learning_rate': [.5, .8]}
- # Вызов функции
- lgbm, lgbm_best_params, lgbm_score = best_model(lgbm_model, lgbm_params, X, y, 'LGBM', numerical_features, categorical_features)
Advertisement
Comments
-
- i got all types of premium tradingview indicators codes available on telegram - https://t.me/tradingview_premium_indicator
Add Comment
Please, Sign In to add comment
Advertisement