Advertisement
vishneva_olga

Untitled

Oct 7th, 2023 (edited)
95
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.41 KB | None | 0 0
  1. # Функция для подбора наилучших гиперпараметров модели
  2. # Обучает модель на обучающем наборе данных, возвращает лучшие: модель, параметры и метрику качества (ROC-AUC)
  3. # Дополнительно формирует список для отчёта по работе различных моделей
  4.  
  5. # Стратегии для заполнения пропусков 'mean', 'median', 'most_frequent', 'const'
  6.  
  7. def best_model(model, params, features, target, title, list_num, list_cat):
  8.     num_features = list_num
  9.     cat_features = list_cat
  10.        
  11.     preprocessor_model = ColumnTransformer(
  12.                                     transformers=[
  13.                                         ('num_imputer', SimpleImputer(strategy='median'), num_features),
  14.                                         ('cat_imputer', SimpleImputer(strategy='most_frequent'), cat_features)
  15.                                                     ])
  16.        
  17.     pipeline_model = Pipeline(
  18.                             steps=[
  19.                                 ('preprocessor', preprocessor_model),
  20.                                 ('model', model)
  21.                                     ])
  22.    
  23.     gs_model = GridSearchCV(estimator=pipeline_model,
  24.                             param_grid=params,
  25.                             scoring='roc_auc',
  26.                             cv=CV,
  27.                             n_jobs=-1)
  28.        
  29.     gs_model.fit(features, target)
  30.    
  31.     best_model = gs_model.best_estimator_
  32.     best_params = gs_model.best_params_
  33.     best_score = gs_model.best_score_
  34.    
  35.     params_out.append({'model_name': title,
  36.                        'model': gs_model.best_estimator_,
  37.                        'model_params': gs_model.best_params_,
  38.                        'ROC-AUC': gs_model.best_score_})
  39.    
  40.     return best_model, best_params, best_score
  41.    
  42. # Создаем модель и список параметров    
  43. lgbm_model = LGBMClassifier(random_state=STATE)
  44. lgbm_params = {'model__n_estimators': np.arange(50, 251, 50),
  45.                'model__max_depth': np.arange(5, 11),
  46.                'model__learning_rate': [.5, .8]}
  47.  
  48. # Вызов функции            
  49. lgbm, lgbm_best_params, lgbm_score = best_model(lgbm_model, lgbm_params, X, y, 'LGBM', numerical_features, categorical_features)
Advertisement
Comments
  • hemal9022
    1 year
    # text 0.12 KB | 0 0
    1. 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