model_xgboost.ipynb 139 KB

%pip install awswrangler pycaret[full]
%pip install xgboost
# import awswrangler as wr
import pandas as pd
import numpy as np
# import boto3
# from sagemaker import get_execution_role
import datetime
import string
import random
from pycaret.classification import *
data = pd.read_csv('data/data_feature_eng.csv')
# print(data)
m_setup = setup(data=data, target='label', normalize=True, 
                   feature_interaction=False, 
                   feature_ratio=False,
                   trigonometry_features=False,
                   use_gpu=True)
  Description Value
0 session_id 1504
1 Target label
2 Target Type Binary
3 Label Encoded 0.0: 0, 1.0: 1
4 Original Data (64800, 13)
5 Missing Values False
6 Numeric Features 10
7 Categorical Features 2
8 Ordinal Features False
9 High Cardinality Features False
10 High Cardinality Method None
11 Transformed Train Set (45359, 21)
12 Transformed Test Set (19441, 21)
13 Shuffle Train-Test True
14 Stratify Train-Test False
15 Fold Generator StratifiedKFold
16 Fold Number 10
17 CPU Jobs -1
18 Use GPU True
19 Log Experiment False
20 Experiment Name clf-default-name
21 USI ece6
22 Imputation Type simple
23 Iterative Imputation Iteration None
24 Numeric Imputer mean
25 Iterative Imputation Numeric Model None
26 Categorical Imputer constant
27 Iterative Imputation Categorical Model None
28 Unknown Categoricals Handling least_frequent
29 Normalize True
30 Normalize Method zscore
31 Transformation False
32 Transformation Method None
33 PCA False
34 PCA Method None
35 PCA Components None
36 Ignore Low Variance False
37 Combine Rare Levels False
38 Rare Level Threshold None
39 Numeric Binning False
40 Remove Outliers False
41 Outliers Threshold None
42 Remove Multicollinearity False
43 Multicollinearity Threshold None
44 Remove Perfect Collinearity True
45 Clustering False
46 Clustering Iteration None
47 Polynomial Features False
48 Polynomial Degree None
49 Trignometry Features False
50 Polynomial Threshold None
51 Group Features False
52 Feature Selection False
53 Feature Selection Method classic
54 Features Selection Threshold None
55 Feature Interaction False
56 Feature Ratio False
57 Interaction Threshold None
58 Fix Imbalance False
59 Fix Imbalance Method SMOTE
# max_depth = 트리 최대 깊이
# max_leaves = 트리 최대 리프
# subsample = row sampling
# colsample_bytree = column sampling 각 이터레이션에 사용되는 칼럼의 비율
# 일반적으로 row sampling 보다는 column sampling이 모형성능과 학습시간에 더 큰 영향을 준다
# xgboost = create_model('xgboost', max_depth=16, max_leaves=255)
# xgboost = create_model('xgboost')
xgboost = create_model('xgboost', max_depth=8, max_leaves=255)
# best_model = compare_models(n_select=6)
  Accuracy AUC Recall Prec. F1 Kappa MCC
0 0.9775 0.9946 0.9299 0.9219 0.9259 0.9126 0.9126
1 0.9793 0.9949 0.9372 0.9264 0.9318 0.9196 0.9196
2 0.9826 0.9950 0.9344 0.9496 0.9420 0.9317 0.9318
3 0.9795 0.9957 0.9300 0.9341 0.9321 0.9200 0.9200
4 0.9804 0.9957 0.9431 0.9283 0.9356 0.9241 0.9241
5 0.9773 0.9951 0.9300 0.9206 0.9253 0.9119 0.9119
6 0.9817 0.9975 0.9300 0.9480 0.9389 0.9282 0.9282
7 0.9810 0.9946 0.9359 0.9386 0.9372 0.9261 0.9261
8 0.9802 0.9958 0.9300 0.9382 0.9341 0.9224 0.9224
9 0.9802 0.9962 0.9255 0.9421 0.9337 0.9221 0.9221
Mean 0.9800 0.9955 0.9326 0.9348 0.9337 0.9219 0.9219
SD 0.0016 0.0008 0.0048 0.0098 0.0050 0.0059 0.0059
# max leaves -> 2**(n-1), n = max_depth
params    = {'max_depth': [128, 64, 32],
             'max_leaves': [256, 1024, 4096],              
             'colsample_bytree':[0.2, 0.4, 0.6, 0.8, 1.0],
             'learning_rate':[0.05, 0.005]
             }#range(14,17)}
# tuned_xgboost = tune_model(xgboost, optimize='F1', custom_grid=params)
tuned_xgboost = tune_model(xgboost, optimize='Accuracy', custom_grid=params, tuner_verbose=3)
# tuned_xgboost = tune_model(xgboost, optimize='Kappa')
  Accuracy AUC Recall Prec. F1 Kappa MCC
0 0.9757 0.9949 0.9197 0.9197 0.9197 0.9054 0.9054
1 0.9786 0.9957 0.9358 0.9236 0.9297 0.9171 0.9171
2 0.9826 0.9960 0.9402 0.9444 0.9423 0.9320 0.9320
3 0.9813 0.9964 0.9388 0.9374 0.9381 0.9271 0.9271
4 0.9837 0.9966 0.9490 0.9435 0.9462 0.9366 0.9366
5 0.9782 0.9955 0.9271 0.9285 0.9278 0.9149 0.9149
6 0.9832 0.9980 0.9373 0.9512 0.9442 0.9343 0.9344
7 0.9804 0.9953 0.9402 0.9307 0.9355 0.9239 0.9239
8 0.9813 0.9956 0.9373 0.9387 0.9380 0.9270 0.9270
9 0.9806 0.9967 0.9241 0.9462 0.9350 0.9236 0.9237
Mean 0.9806 0.9961 0.9350 0.9364 0.9356 0.9242 0.9242
SD 0.0023 0.0008 0.0083 0.0099 0.0077 0.0091 0.0091
tuned_xgboost
XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1,
              colsample_bynode=1, colsample_bytree=0.8,
              enable_categorical=False, gamma=0, gpu_id=0, importance_type=None,
              interaction_constraints='', learning_rate=0.05, max_delta_step=0,
              max_depth=32, max_leaves=1024, min_child_weight=1, missing=nan,
              monotone_constraints='()', n_estimators=100, n_jobs=-1,
              num_parallel_tree=1, objective='binary:logistic',
              predictor='auto', random_state=6868, reg_alpha=0, reg_lambda=1,
              scale_pos_weight=1, subsample=1, tree_method='gpu_hist',
              use_label_encoder=True, validate_parameters=1, ...)
interpret_model(tuned_xgboost)
eda(tuned_xgboost)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
c:\Users\User\Desktop\ssd-work\homeiot\preproc\model_xgboost.ipynb Cell 9' in <module>
----> <a href='vscode-notebook-cell:/c%3A/Users/User/Desktop/ssd-work/homeiot/preproc/model_xgboost.ipynb#ch0000008?line=0'>1</a> eda(tuned_xgboost)

NameError: name 'eda' is not defined
# plot_model(tuned_dt, plot='auc')
evaluate_model(tuned_xgboost)
interactive(children=(ToggleButtons(description='Plot Type:', icons=('',), options=(('Hyperparameters', 'param…