{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%pip install awswrangler pycaret[full]\n", "# conda install -c nvidia -c rapidsai py-xgboost\n", "%pip install xgboost\n", "%pip install mlflow" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# import awswrangler as wr\n", "import pandas as pd\n", "import numpy as np\n", "# import boto3\n", "# from sagemaker import get_execution_role\n", "import datetime\n", "import string\n", "import random\n", "from pycaret.classification import *" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "data = pd.read_csv('data/data_feature_eng.csv')\n", "# print(data)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Description Value
0session_id2998
1Targetlabel
2Target TypeBinary
3Label Encoded0.0: 0, 1.0: 1
4Original Data(64800, 13)
5Missing ValuesFalse
6Numeric Features10
7Categorical Features2
8Ordinal FeaturesFalse
9High Cardinality FeaturesFalse
10High Cardinality MethodNone
11Transformed Train Set(45359, 21)
12Transformed Test Set(19441, 21)
13Shuffle Train-TestTrue
14Stratify Train-TestFalse
15Fold GeneratorStratifiedKFold
16Fold Number10
17CPU Jobs-1
18Use GPUTrue
19Log ExperimentFalse
20Experiment Nameclf-default-name
21USI452f
22Imputation Typesimple
23Iterative Imputation IterationNone
24Numeric Imputermean
25Iterative Imputation Numeric ModelNone
26Categorical Imputerconstant
27Iterative Imputation Categorical ModelNone
28Unknown Categoricals Handlingleast_frequent
29NormalizeTrue
30Normalize Methodzscore
31TransformationFalse
32Transformation MethodNone
33PCAFalse
34PCA MethodNone
35PCA ComponentsNone
36Ignore Low VarianceFalse
37Combine Rare LevelsFalse
38Rare Level ThresholdNone
39Numeric BinningFalse
40Remove OutliersFalse
41Outliers ThresholdNone
42Remove MulticollinearityFalse
43Multicollinearity ThresholdNone
44Remove Perfect CollinearityTrue
45ClusteringFalse
46Clustering IterationNone
47Polynomial FeaturesFalse
48Polynomial DegreeNone
49Trignometry FeaturesFalse
50Polynomial ThresholdNone
51Group FeaturesFalse
52Feature SelectionFalse
53Feature Selection Methodclassic
54Features Selection ThresholdNone
55Feature InteractionFalse
56Feature RatioFalse
57Interaction ThresholdNone
58Fix ImbalanceFalse
59Fix Imbalance MethodSMOTE
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "m_setup = setup(data=data, target='label', normalize=True, \n", " feature_interaction=False, \n", " feature_ratio=False,\n", " trigonometry_features=False,\n", " use_gpu=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# max_depth = 트리 최대 깊이\n", "# max_leaves = 트리 최대 리프\n", "# subsample = row sampling\n", "# colsample_bytree = column sampling 각 이터레이션에 사용되는 칼럼의 비율\n", "# learning_rate = 러닝레이트\n", "# 일반적으로 row sampling 보다는 column sampling이 모형성능과 학습시간에 더 큰 영향을 준다\n", "# xgboost = create_model('xgboost', max_depth=16, max_leaves=255)\n", "# xgboost = create_model('xgboost')\n", "xgboost = create_model('xgboost', max_depth=8, max_leaves=256)\n", "# best_model = compare_models(n_select=6)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
 AccuracyAUCRecallPrec.F1KappaMCC
00.98210.99700.92970.95060.94000.92960.9296
10.98280.99620.93570.94960.94260.93250.9325
20.98590.99720.95030.95590.95310.94480.9448
30.98370.99560.94440.94720.94580.93620.9362
40.97800.99560.91810.93450.92630.91330.9133
50.98390.99710.94150.95130.94640.93690.9369
60.98300.99690.93860.94830.94340.93340.9335
70.98280.99640.94300.94300.94300.93290.9329
80.97860.99440.92840.92970.92900.91650.9165
90.98260.99700.93410.94940.94170.93150.9315
Mean0.98230.99630.93640.94590.94110.93070.9308
SD0.00230.00090.00880.00760.00760.00890.0089
\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# max leaves -> 2**(n-1), n = max_depth\n", "params = {'max_depth': [128, 64, 32],\n", " 'max_leaves': [256, 1024, 4096], \n", " 'colsample_bytree':[0.2, 0.4, 0.6, 0.8, 1.0],\n", " 'learning_rate':[0.05, 0.005]\n", " }#range(14,17)}\n", "# tuned_xgboost = tune_model(xgboost, optimize='F1', custom_grid=params)\n", "tuned_xgboost = tune_model(xgboost, optimize='Accuracy', custom_grid=params, tuner_verbose=3)\n", "# tuned_xgboost = tune_model(xgboost, optimize='Kappa')" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Transformation Pipeline and Model Successfully Saved\n" ] }, { "data": { "text/plain": [ "XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1,\n", " colsample_bynode=1, colsample_bytree=0.6,\n", " enable_categorical=False, gamma=0, gpu_id=0, importance_type=None,\n", " interaction_constraints='', learning_rate=0.05, max_delta_step=0,\n", " max_depth=64, max_leaves=1024, min_child_weight=1, missing=nan,\n", " monotone_constraints='()', n_estimators=100, n_jobs=-1,\n", " num_parallel_tree=1, objective='binary:logistic',\n", " predictor='auto', random_state=1504, reg_alpha=0, reg_lambda=1,\n", " scale_pos_weight=1, subsample=1, tree_method='gpu_hist',\n", " use_label_encoder=True, validate_parameters=1, ...)" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tuned_xgboost" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "95949f8bf87d41b9a14b71c866fc7444", "version_major": 2, "version_minor": 0 }, "text/plain": [ "interactive(children=(ToggleButtons(description='Plot Type:', icons=('',), options=(('Hyperparameters', 'param…" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# plot_model(tuned_dt, plot='auc')\n", "evaluate_model(tuned_xgboost)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Transformation Pipeline and Model Successfully Saved\n" ] }, { "data": { "text/html": [ "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
 ModelAccuracyAUCRecallPrec.F1KappaMCC
0Extreme Gradient Boosting0.99680.99980.98790.99090.98940.98750.9875
\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
humd_humdd_humtempd_tempdd_tempdoormotionillumhour...dayofweek_4dayofweek_5dayofweek_6month_10month_11month_8month_9labelLabelScore
0-1.945604-2.2906720.9095582.2327160.689506-0.019712-0.123705-0.074166-0.6243940.072370...1.00.00.00.01.00.00.00.00.00.9851
11.3114850.0057790.0006290.9234330.0030510.717519-0.123705-0.074166-1.0054541.230731...0.00.00.00.00.00.01.00.00.00.9941
21.028260-0.4316400.4843731.7090030.003051-0.001014-0.123705-0.074166-0.2433340.941141...0.00.00.00.00.00.01.01.01.00.9728
3-1.3083480.0057790.000629-1.1714190.003051-0.001014-0.123705-0.074166-0.624394-0.217220...0.01.00.00.01.00.00.00.00.00.9958
40.4618100.005779-0.5696800.848617-0.389210-0.212032-0.123705-0.074166-1.005454-1.665171...0.00.01.00.00.00.01.00.00.00.9958
..................................................................
194361.240679-1.6345431.4225820.9608420.003051-1.304522-0.123705-0.074166-1.005454-0.941195...0.00.00.00.00.00.01.00.00.00.9947
194370.2493910.0057790.000629-0.7973380.689506-0.019712-0.123705-0.074166-0.624394-0.362015...0.00.00.00.01.00.00.00.00.00.9621
194380.3910030.0057790.000629-0.8721550.003051-0.001014-0.123705-0.074166-0.6243941.665117...0.00.00.00.01.00.00.00.00.00.9953
194391.0990660.005779-0.6842510.698985-0.9776001.233045-0.123705-0.074166-1.005454-0.941195...0.00.00.00.00.00.01.00.00.00.9940
194401.3114850.0057790.0006291.2975140.003051-0.001014-0.123705-0.0741661.2809060.361961...0.00.00.00.00.00.01.01.01.00.7373
\n", "

19441 rows × 24 columns

\n", "
" ], "text/plain": [ " hum d_hum dd_hum temp d_temp dd_temp door \\\n", "0 -1.945604 -2.290672 0.909558 2.232716 0.689506 -0.019712 -0.123705 \n", "1 1.311485 0.005779 0.000629 0.923433 0.003051 0.717519 -0.123705 \n", "2 1.028260 -0.431640 0.484373 1.709003 0.003051 -0.001014 -0.123705 \n", "3 -1.308348 0.005779 0.000629 -1.171419 0.003051 -0.001014 -0.123705 \n", "4 0.461810 0.005779 -0.569680 0.848617 -0.389210 -0.212032 -0.123705 \n", "... ... ... ... ... ... ... ... \n", "19436 1.240679 -1.634543 1.422582 0.960842 0.003051 -1.304522 -0.123705 \n", "19437 0.249391 0.005779 0.000629 -0.797338 0.689506 -0.019712 -0.123705 \n", "19438 0.391003 0.005779 0.000629 -0.872155 0.003051 -0.001014 -0.123705 \n", "19439 1.099066 0.005779 -0.684251 0.698985 -0.977600 1.233045 -0.123705 \n", "19440 1.311485 0.005779 0.000629 1.297514 0.003051 -0.001014 -0.123705 \n", "\n", " motion illum hour ... dayofweek_4 dayofweek_5 \\\n", "0 -0.074166 -0.624394 0.072370 ... 1.0 0.0 \n", "1 -0.074166 -1.005454 1.230731 ... 0.0 0.0 \n", "2 -0.074166 -0.243334 0.941141 ... 0.0 0.0 \n", "3 -0.074166 -0.624394 -0.217220 ... 0.0 1.0 \n", "4 -0.074166 -1.005454 -1.665171 ... 0.0 0.0 \n", "... ... ... ... ... ... ... \n", "19436 -0.074166 -1.005454 -0.941195 ... 0.0 0.0 \n", "19437 -0.074166 -0.624394 -0.362015 ... 0.0 0.0 \n", "19438 -0.074166 -0.624394 1.665117 ... 0.0 0.0 \n", "19439 -0.074166 -1.005454 -0.941195 ... 0.0 0.0 \n", "19440 -0.074166 1.280906 0.361961 ... 0.0 0.0 \n", "\n", " dayofweek_6 month_10 month_11 month_8 month_9 label Label Score \n", "0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.9851 \n", "1 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.9941 \n", "2 0.0 0.0 0.0 0.0 1.0 1.0 1.0 0.9728 \n", "3 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.9958 \n", "4 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.9958 \n", "... ... ... ... ... ... ... ... ... \n", "19436 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.9947 \n", "19437 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.9621 \n", "19438 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.9953 \n", "19439 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.9940 \n", "19440 0.0 0.0 0.0 0.0 1.0 1.0 1.0 0.7373 \n", "\n", "[19441 rows x 24 columns]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mdl = finalize_model(tuned_xgboost)\n", "save_model(mdl, 'tuned_xgboost_0207')\n", "# save_model(tuned_xgboost, 'tuned_xgboost_0207')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "load_mdl = load_model('tuned_xgboost_0207')\n", "prediction = predict_model(load_mdl, data=data.iloc[-10000:])\n", "prediction = prediction.astype({'Label':'float64'})\n", "prediction.info()\n", "from pycaret.utils import check_metric\n", "check_metric(prediction['Label'], prediction['label'], metric = 'F1')" ] } ], "metadata": { "interpreter": { "hash": "916dbcbb3f70747c44a77c7bcd40155683ae19c65e1c03b4aa3499c5328201f1" }, "kernelspec": { "display_name": "Python 3.8.10 64-bit", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.12" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }