{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%pip install fastapi\n", "%pip install uvicorn\n", "%pip install librosa\n", "%pip install xgboost\n", "%pip install pandas" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "import datetime\n", "import string\n", "import random\n", "from pycaret.classification import *" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# data = pd.read_csv('data/data_feature_eng.csv')\n", "data = pd.read_csv('data/feature_ori.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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
 DescriptionValue
0session_id6597
1Targetlabel
2Target TypeBinary
3Label Encoded0.0: 0, 1.0: 1
4Original Data(64800, 11)
5Missing Values0
6Numeric Features8
7Categorical Features2
8Ordinal Features0
9High Cardinality Features0
10High Cardinality MethodNone
11Transformed Train Set(45359, 19)
12Transformed Test Set(19441, 19)
13Shuffle Train-TestTrue
14Stratify Train-TestFalse
15Fold GeneratorStratifiedKFold
16Fold Number10
17CPU Jobs-1
18Use GPU1
19Log Experiment0
20Experiment Nameclf-default-name
21USI717f
22Imputation Typesimple
23Iterative Imputation IterationNone
24Numeric Imputermean
25Iterative Imputation Numeric ModelNone
26Categorical Imputerconstant
27Iterative Imputation Categorical ModelNone
28Unknown Categoricals Handlingleast_frequent
29Normalize0
30Normalize MethodNone
31Transformation0
32Transformation MethodNone
33PCA0
34PCA MethodNone
35PCA ComponentsNone
36Ignore Low Variance0
37Combine Rare Levels0
38Rare Level ThresholdNone
39Numeric Binning0
40Remove Outliers0
41Outliers ThresholdNone
42Remove Multicollinearity0
43Multicollinearity ThresholdNone
44Remove Perfect Collinearity1
45Clustering0
46Clustering IterationNone
47Polynomial Features0
48Polynomial DegreeNone
49Trignometry Features0
50Polynomial ThresholdNone
51Group Features0
52Feature Selection0
53Feature Selection Methodclassic
54Features Selection ThresholdNone
55Feature Interaction0
56Feature Ratio0
57Interaction ThresholdNone
58Fix Imbalance0
59Fix Imbalance MethodSMOTE
\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "m_setup = setup(data=data, target='label', normalize=False, \n", " feature_interaction=False, \n", " feature_ratio=False,\n", " trigonometry_features=False,\n", " use_gpu=True)" ] }, { "cell_type": "code", "execution_count": 4, "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.98100.99570.93880.93600.93740.92620.9262
10.98610.99800.95630.95220.95420.94610.9461
20.98520.99670.95630.94670.95150.94280.9428
30.98060.99540.94320.92970.93640.92500.9250
40.98500.99770.96070.94150.95100.94220.9422
50.98430.99610.95780.94000.94880.93960.9396
60.98460.99650.95340.94520.94930.94020.9402
70.98850.99750.96650.95820.96230.95560.9556
80.98240.99590.94180.94180.94180.93140.9314
90.98300.99670.95190.93690.94430.93430.9343
Mean0.98410.99660.95270.94280.94770.93830.9383
SD0.00230.00080.00840.00780.00750.00890.0089
\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "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.97840.99490.92710.92980.92850.91570.9157
10.98370.99740.93740.95410.94570.93610.9361
20.97970.99590.92870.93690.93270.92080.9208
30.97400.99300.91120.91650.91390.89850.8986
40.98210.99660.94470.93790.94130.93070.9307
50.97930.99500.93740.92660.93200.91980.9198
60.98460.99630.93890.95840.94850.93950.9395
70.98720.99760.95200.96320.95750.95000.9500
80.97750.99460.91270.93720.92480.91160.9117
90.97910.99520.93440.92760.93100.91870.9187
Mean0.98060.99560.93240.93880.93560.92410.9242
SD0.00370.00130.01230.01440.01210.01430.0143
\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": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1,\n", " colsample_bynode=1, colsample_bytree=0.4,\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=4096, 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=6597, 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": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tuned_xgboost" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# plot_model(tuned_dt, plot='auc')\n", "evaluate_model(tuned_xgboost)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Transformation Pipeline and Model Successfully Saved\n" ] }, { "data": { "text/plain": [ "(Pipeline(memory=None,\n", " steps=[('dtypes',\n", " DataTypes_Auto_infer(categorical_features=[],\n", " display_types=True, features_todrop=[],\n", " id_columns=[],\n", " ml_usecase='classification',\n", " numerical_features=[], target='label',\n", " time_features=[])),\n", " ('imputer',\n", " Simple_Imputer(categorical_strategy='not_available',\n", " fill_value_categorical=None,\n", " fill_value_numerical=None,\n", " numeric_strate...\n", " max_delta_step=0, max_depth=64, max_leaves=4096,\n", " min_child_weight=1, missing=nan,\n", " monotone_constraints='()', n_estimators=100,\n", " n_jobs=-1, num_parallel_tree=1,\n", " objective='binary:logistic', predictor='auto',\n", " random_state=6597, reg_alpha=0, reg_lambda=1,\n", " scale_pos_weight=1, subsample=1,\n", " tree_method='gpu_hist', use_label_encoder=True,\n", " validate_parameters=1, ...)]],\n", " verbose=False),\n", " 'tuned_xgboost_orifeature_0323.pkl')" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mdl = finalize_model(tuned_xgboost)\n", "save_model(mdl, 'tuned_xgboost_orifeature_0323')\n", "# save_model(tuned_xgboost, 'tuned_xgboost_0207')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "load_mdl = load_model('tuned_xgboost_orifeature_0323')\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 }