{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "2519c254", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import cProfile\n", "import pstats\n", "import joblib\n", "import time\n" ] }, { "cell_type": "code", "execution_count": null, "id": "7553870c", "metadata": {}, "outputs": [], "source": [ "df_logs = pd.read_parquet(\"../logs/predictions_log.parquet\")" ] }, { "cell_type": "code", "execution_count": null, "id": "f40c42f6", "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", "
num__SK_ID_CURRnum__BUREAU_SK_ID_BUREAU_maxnum__POS_SK_DPD_minnum__CC_SK_DPD_DEF_maxnum__PREV_NAME_TYPE_SUITE_Familynum__PREV_DAYS_FIRST_DRAWING_sumnum__PREV_NAME_GOODS_CATEGORY_Othernum__DAYS_LAST_PHONE_CHANGEnum__CC_CNT_DRAWINGS_OTHER_CURRENT_meannum__PREV_DAYS_FIRST_DRAWING_max...cpu_percentram_percentsystem_loadnum_threadsmethodstatuserror_messagelatency_msrequest_size_bytesresponse_size_bytes
0-0.3024290.3260390.249338NaN-0.535936-0.3001650.23674-0.987792NaN0.246419...14.578.30.021POSTsuccessNaN383.74137912798.00.0
1-0.1471320.5539470.249338NaN-0.5359360.7428040.236740.895050NaN0.246419...15.878.30.021POSTsuccessNaN102.71501512775.00.0
2-0.4296380.4486350.249338NaN0.494736-0.8216490.236740.962033NaN0.246419...12.278.40.021POSTsuccessNaN67.81530412875.00.0
30.2462230.4919430.2493380.6915352.5560814.3924880.23674-2.1922741.9975730.246419...12.478.40.021POSTsuccessNaN89.75052812927.00.0
4-1.1696000.6991970.249338NaN-0.535936-0.8216490.236740.744033NaN0.246419...16.878.30.021POSTsuccessNaN68.77660812836.00.0
\n", "

5 rows × 403 columns

\n", "
" ], "text/plain": [ " num__SK_ID_CURR num__BUREAU_SK_ID_BUREAU_max num__POS_SK_DPD_min \\\n", "0 -0.302429 0.326039 0.249338 \n", "1 -0.147132 0.553947 0.249338 \n", "2 -0.429638 0.448635 0.249338 \n", "3 0.246223 0.491943 0.249338 \n", "4 -1.169600 0.699197 0.249338 \n", "\n", " num__CC_SK_DPD_DEF_max num__PREV_NAME_TYPE_SUITE_Family \\\n", "0 NaN -0.535936 \n", "1 NaN -0.535936 \n", "2 NaN 0.494736 \n", "3 0.691535 2.556081 \n", "4 NaN -0.535936 \n", "\n", " num__PREV_DAYS_FIRST_DRAWING_sum num__PREV_NAME_GOODS_CATEGORY_Other \\\n", "0 -0.300165 0.23674 \n", "1 0.742804 0.23674 \n", "2 -0.821649 0.23674 \n", "3 4.392488 0.23674 \n", "4 -0.821649 0.23674 \n", "\n", " num__DAYS_LAST_PHONE_CHANGE num__CC_CNT_DRAWINGS_OTHER_CURRENT_mean \\\n", "0 -0.987792 NaN \n", "1 0.895050 NaN \n", "2 0.962033 NaN \n", "3 -2.192274 1.997573 \n", "4 0.744033 NaN \n", "\n", " num__PREV_DAYS_FIRST_DRAWING_max ... cpu_percent ram_percent \\\n", "0 0.246419 ... 14.5 78.3 \n", "1 0.246419 ... 15.8 78.3 \n", "2 0.246419 ... 12.2 78.4 \n", "3 0.246419 ... 12.4 78.4 \n", "4 0.246419 ... 16.8 78.3 \n", "\n", " system_load num_threads method status error_message latency_ms \\\n", "0 0.0 21 POST success NaN 383.741379 \n", "1 0.0 21 POST success NaN 102.715015 \n", "2 0.0 21 POST success NaN 67.815304 \n", "3 0.0 21 POST success NaN 89.750528 \n", "4 0.0 21 POST success NaN 68.776608 \n", "\n", " request_size_bytes response_size_bytes \n", "0 12798.0 0.0 \n", "1 12775.0 0.0 \n", "2 12875.0 0.0 \n", "3 12927.0 0.0 \n", "4 12836.0 0.0 \n", "\n", "[5 rows x 403 columns]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_logs.head()" ] }, { "cell_type": "code", "execution_count": 19, "id": "29eeb703", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 NaN\n", "1 NaN\n", "2 NaN\n", "3 NaN\n", "4 NaN\n", " ..\n", "195 NaN\n", "196 NaN\n", "197 NaN\n", "198 NaN\n", "199 NaN\n", "Name: inference_ms, Length: 200, dtype: float64" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_logs['inference_ms']" ] }, { "cell_type": "code", "execution_count": 9, "id": "a16d44c9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "count 200.000000\n", "mean 86.127709\n", "std 29.946769\n", "min 58.841944\n", "25% 70.760190\n", "50% 76.784968\n", "75% 94.737291\n", "max 383.741379\n", "Name: latency_ms, dtype: float64" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_logs[\"latency_ms\"].describe()" ] }, { "cell_type": "code", "execution_count": 13, "id": "cc4e442a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "count 200.000000\n", "mean 0.213953\n", "std 0.161526\n", "min 0.021356\n", "25% 0.084569\n", "50% 0.191494\n", "75% 0.286569\n", "max 0.793830\n", "Name: score_x, dtype: float64" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_logs[\"score_x\"].describe()" ] }, { "cell_type": "code", "execution_count": 17, "id": "6aea8f28", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "latency_mean_p95_max: (86.12770915031433, 118.6904191970825, 383.7413787841797)\n", "cpu_ram: (23.789499999999997, 73.7385)\n", "infer: (nan, nan)\n" ] } ], "source": [ "lat_mean = df_logs[\"latency_ms\"].mean()\n", "lat_p95 = df_logs[\"latency_ms\"].quantile(0.95)\n", "lat_max = df_logs[\"latency_ms\"].max()\n", "\n", "cpu_mean = df_logs[\"cpu_percent\"].mean()\n", "ram_mean = df_logs[\"ram_percent\"].mean()\n", "\n", "infer_mean = df_logs[\"inference_ms\"].mean()\n", "infer_p95 = df_logs[\"inference_ms\"].quantile(0.95)\n", "\n", "print(f\"latency_mean_p95_max: {lat_mean, lat_p95, lat_max}\")\n", "print(f\"cpu_ram: {cpu_mean, ram_mean}\")\n", "print(f\"infer: {infer_p95, infer_mean}\")" ] }, { "cell_type": "code", "execution_count": 11, "id": "23e91355", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "decision_x\n", "ACCORDÉ 183\n", "REFUSÉ 17\n", "Name: count, dtype: int64" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_logs[\"decision_x\"].value_counts()" ] }, { "cell_type": "code", "execution_count": null, "id": "26f53c38", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\base.py:463: InconsistentVersionWarning: Trying to unpickle estimator StandardScaler from version 1.4.2 when using version 1.8.0. This might lead to breaking code or invalid results. Use at your own risk. For more info please refer to:\n", "https://scikit-learn.org/stable/model_persistence.html#security-maintainability-limitations\n", " warnings.warn(\n", "c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\base.py:463: InconsistentVersionWarning: Trying to unpickle estimator OneHotEncoder from version 1.4.2 when using version 1.8.0. This might lead to breaking code or invalid results. Use at your own risk. For more info please refer to:\n", "https://scikit-learn.org/stable/model_persistence.html#security-maintainability-limitations\n", " warnings.warn(\n", "c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\base.py:463: InconsistentVersionWarning: Trying to unpickle estimator TargetEncoder from version 1.4.2 when using version 1.8.0. This might lead to breaking code or invalid results. Use at your own risk. For more info please refer to:\n", "https://scikit-learn.org/stable/model_persistence.html#security-maintainability-limitations\n", " warnings.warn(\n", "c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\base.py:463: InconsistentVersionWarning: Trying to unpickle estimator FunctionTransformer from version 1.4.2 when using version 1.8.0. This might lead to breaking code or invalid results. Use at your own risk. For more info please refer to:\n", "https://scikit-learn.org/stable/model_persistence.html#security-maintainability-limitations\n", " warnings.warn(\n", "c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\base.py:463: InconsistentVersionWarning: Trying to unpickle estimator ColumnTransformer from version 1.4.2 when using version 1.8.0. This might lead to breaking code or invalid results. Use at your own risk. For more info please refer to:\n", "https://scikit-learn.org/stable/model_persistence.html#security-maintainability-limitations\n", " warnings.warn(\n", "c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\base.py:463: InconsistentVersionWarning: Trying to unpickle estimator Pipeline from version 1.4.2 when using version 1.8.0. This might lead to breaking code or invalid results. Use at your own risk. For more info please refer to:\n", "https://scikit-learn.org/stable/model_persistence.html#security-maintainability-limitations\n", " warnings.warn(\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Sun Jun 7 20:34:13 2026 profiling.prof\n", "\n", " 3120174 function calls (3086873 primitive calls) in 6.680 seconds\n", "\n", " Ordered by: cumulative time\n", " List reduced from 703 to 18 due to restriction <18>\n", "\n", " ncalls tottime percall cumtime percall filename:lineno(function)\n", " 1 0.000 0.000 6.693 6.693 {built-in method builtins.exec}\n", " 1 0.000 0.000 6.693 6.693 :1()\n", " 1 0.001 0.001 6.693 6.693 C:\\Users\\Lenovo\\AppData\\Local\\Temp\\ipykernel_12576\\161287911.py:9(benchmark)\n", " 100 0.002 0.000 6.690 0.067 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\pipeline.py:815(predict_proba)\n", " 500/100 0.010 0.000 6.530 0.065 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\utils\\_set_output.py:314(wrapped)\n", " 100 0.037 0.000 6.521 0.065 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\compose\\_column_transformer.py:1031(transform)\n", " 100 0.010 0.000 6.191 0.062 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\compose\\_column_transformer.py:829(_call_func_on_transformers)\n", " 100 0.001 0.000 4.446 0.044 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\utils\\parallel.py:54(__call__)\n", " 100 0.002 0.000 4.443 0.044 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\joblib\\parallel.py:1969(__call__)\n", " 600 0.007 0.000 4.432 0.007 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\joblib\\parallel.py:1888(_get_sequential_output)\n", " 400 0.048 0.000 4.419 0.011 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\utils\\parallel.py:140(__call__)\n", " 400 0.005 0.000 4.255 0.011 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\pipeline.py:1442(_transform_one)\n", " 1700 0.150 0.000 2.598 0.002 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\utils\\validation.py:725(check_array)\n", " 2000 0.025 0.000 2.368 0.001 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\utils\\_indexing.py:265(_safe_indexing)\n", " 200 0.038 0.000 2.306 0.012 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\preprocessing\\_encoders.py:193(_transform)\n", " 2000 0.019 0.000 2.275 0.001 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\utils\\_indexing.py:66(_pandas_indexing)\n", "2500/2100 0.028 0.000 2.226 0.001 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\pandas\\core\\indexing.py:1192(__getitem__)\n", "2400/2000 0.069 0.000 1.943 0.001 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\pandas\\core\\indexing.py:1058(_getitem_lowerdim)\n", "\n", "\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "\n", "pipe = joblib.load(\"../BestModel/pipeline_complet.joblib\")\n", "df = joblib.load(\"../data/app_test_clean_v2.joblib\").head(1)\n", "\n", "def benchmark():\n", " for _ in range(100):\n", " pipe.predict_proba(df)\n", "\n", "cProfile.run(\n", " \"benchmark()\",\n", " \"profiling.prof\")\n", "\n", "stats = pstats.Stats(\"profiling.prof\")\n", "stats.sort_stats(\"cumtime\")\n", "stats.print_stats(18)" ] }, { "cell_type": "code", "execution_count": null, "id": "b313e60b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "preprocessing : 37.56080009043217\n", "model : 0.9570997208356857\n", "total : 38.51789981126785\n" ] } ], "source": [ "\n", "\n", "try:\n", " df_example = joblib.load(\"../data/app_test_clean_v2.joblib\")\n", "except:\n", " df_example = None\n", "\n", "X = df_example.iloc[[0]]\n", "\n", "t0 = time.perf_counter()\n", "\n", "X_transformed = pipe[:-1].transform(X)\n", "\n", "t1 = time.perf_counter()\n", "\n", "score = pipe[-1].predict_proba(X_transformed)\n", "\n", "t2 = time.perf_counter()\n", "\n", "print(\"preprocessing :\", (t1 - t0)*1000)\n", "print(\"model :\", (t2 - t1)*1000)\n", "print(\"total :\", (t2 - t0)*1000)" ] }, { "cell_type": "code", "execution_count": null, "id": "4041e8d6", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100 appels unitaires : 3832.566499710083 ms\n" ] } ], "source": [ "start = time.perf_counter()\n", "\n", "for i in range(100):\n", " pipe.predict_proba(df_example.iloc[[i]])\n", "\n", "end = time.perf_counter()\n", "\n", "print(\n", " \"100 appels unitaires :\",\n", " (end - start) * 1000,\n", " \"ms\"\n", ")" ] }, { "cell_type": "code", "execution_count": 22, "id": "ac7fb688", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 batch de 100 lignes : 63.72349988669157 ms\n" ] } ], "source": [ "X_batch = df_example.iloc[:100]\n", "\n", "start = time.perf_counter()\n", "\n", "pipe.predict_proba(X_batch)\n", "\n", "end = time.perf_counter()\n", "\n", "print(\n", " \"1 batch de 100 lignes :\",\n", " (end - start) * 1000,\n", " \"ms\"\n", ")" ] }, { "cell_type": "code", "execution_count": 25, "id": "cf6d322d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Index(['SK_ID_CURR', 'NAME_CONTRACT_TYPE', 'CODE_GENDER', 'FLAG_OWN_CAR',\n", " 'FLAG_OWN_REALTY', 'CNT_CHILDREN', 'AMT_INCOME_TOTAL',\n", " 'AMT_GOODS_PRICE', 'NAME_TYPE_SUITE', 'NAME_INCOME_TYPE',\n", " ...\n", " 'POS_SK_DPD_DEF_max', 'POS_SK_DPD_DEF_min',\n", " 'POS_NAME_CONTRACT_STATUS_NUNIQUE',\n", " 'POS_NAME_CONTRACT_STATUS_Amortized debt',\n", " 'POS_NAME_CONTRACT_STATUS_Approved',\n", " 'POS_NAME_CONTRACT_STATUS_Canceled',\n", " 'POS_NAME_CONTRACT_STATUS_Completed',\n", " 'POS_NAME_CONTRACT_STATUS_Returned to the store',\n", " 'POS_NAME_CONTRACT_STATUS_Signed', 'POS_NAME_CONTRACT_STATUS_XNA'],\n", " dtype='object', length=327)" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_example = joblib.load(\"../data/app_test_clean_v2.joblib\")\n", "df_example.columns" ] }, { "cell_type": "code", "execution_count": null, "id": "6367ef06", "metadata": {}, "outputs": [], "source": [ "df_example['PREV_NAME_GOODS_CATEGORY_Direct Sales']" ] }, { "cell_type": "code", "execution_count": 26, "id": "0abb3f2e", "metadata": {}, "outputs": [], "source": [ "df_200 = df_example.sample(200, random_state=42)" ] }, { "cell_type": "code", "execution_count": null, "id": "24b9f428", "metadata": {}, "outputs": [], "source": [ "df_200['PREV_NAME_GOODS_CATEGORY_Direct Sales']" ] } ], "metadata": { "kernelspec": { "display_name": "mlops", "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.11.9" } }, "nbformat": 4, "nbformat_minor": 5 }