Spaces:
Sleeping
Sleeping
File size: 51,651 Bytes
89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 bb6bf65 89b0184 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 | {
"cells": [
{
"cell_type": "markdown",
"id": "aeb4cd48",
"metadata": {},
"source": [
"# **Analyse du drift & de la performance**\n",
"\n",
"Ce notebook a pour objectif :\n",
"\n",
"- **d’évaluer le data drift** entre :\n",
" - les données de référence (jeu d’entraînement ou échantillon représentatif)\n",
" - les données de production issues des logs de l’API\n",
"\n",
" Le data drift permet de détecter si les distributions des variables évoluent dans le temps. \n",
" Ces changements peuvent dégrader les performances du modèle, notamment lorsque les distributions de production s’éloignent de celles observées lors de l’entraînement.\n",
"\n",
" Pour cela, nous utilisons la librairie **Evidently AI**, qui permet d’analyser le drift **colonne par colonne**, après avoir soigneusement harmonisé les colonnes entre les deux jeux de données (types, valeurs manquantes, colonnes constantes, etc.).\n",
"\n",
"- **d’évaluer la performance du pipeline de prédiction**, non pas uniquement au niveau du modèle, mais surtout au niveau du **preprocessing**, afin d’identifier les véritables axes d’optimisation.\n",
"\n",
" En effet, dans un contexte MLOps, la latence d’un modèle en production dépend autant du modèle que du pipeline de transformation. \n",
" Nous mesurons donc :\n",
" - le temps de preprocessing,\n",
" - le temps de prédiction du modèle,\n",
" - la différence entre appels unitaires et prédiction en batch,\n",
" - et nous analysons les résultats du profiling pour identifier les goulots d’étranglement.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "ee429e61",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import cProfile\n",
"import pstats\n",
"import joblib\n",
"import time\n",
"\n",
"from evidently.report import Report\n",
"from evidently.metrics import ColumnDriftMetric"
]
},
{
"cell_type": "markdown",
"id": "1149740f",
"metadata": {},
"source": [
"## **Analyse du Data Drift avec Evidently**"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "9a27a53d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"((500, 327), (213, 406), (213, 406))"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Chargement du pipeline\n",
"pipe = joblib.load(\"../BestModel/pipeline_complet.joblib\")\n",
"\n",
"# Données de référence (version simplifiée)\n",
"df_reference_raw = joblib.load(\"../data/app_test_small.joblib\")\n",
"\n",
"# Données de production (logs unitaires)\n",
"df_production_raw = pd.read_parquet(\"../logs/predictions_log.parquet\")\n",
"\n",
"# Données de production (logs batch)\n",
"df_batch_raw = pd.read_parquet(\"../logs/predictions_log.parquet\")\n",
"\n",
"df_reference_raw.shape, df_production_raw.shape, df_batch_raw.shape"
]
},
{
"cell_type": "markdown",
"id": "276796a6",
"metadata": {},
"source": [
"### Sélection des colonnes communes entre référence production"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "a66434ea",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Index(['num__SK_ID_CURR', 'num__BUREAU_SK_ID_BUREAU_max',\n",
" 'num__POS_SK_DPD_min', 'num__CC_SK_DPD_DEF_max',\n",
" 'num__PREV_NAME_TYPE_SUITE_Family', 'num__PREV_DAYS_FIRST_DRAWING_sum',\n",
" 'num__PREV_NAME_GOODS_CATEGORY_Other', 'num__DAYS_LAST_PHONE_CHANGE',\n",
" 'num__CC_CNT_DRAWINGS_OTHER_CURRENT_mean',\n",
" 'num__PREV_DAYS_FIRST_DRAWING_max',\n",
" ...\n",
" 'bool__FLAG_DOCUMENT_12', 'bool__FLAG_DOCUMENT_13',\n",
" 'bool__FLAG_DOCUMENT_14', 'bool__FLAG_DOCUMENT_15',\n",
" 'bool__FLAG_DOCUMENT_16', 'bool__FLAG_DOCUMENT_17',\n",
" 'bool__FLAG_DOCUMENT_18', 'bool__FLAG_DOCUMENT_19',\n",
" 'bool__FLAG_DOCUMENT_20', 'bool__FLAG_DOCUMENT_21'],\n",
" dtype='object', length=382)"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Transformer les colonnes de référence\n",
"df_reference_transformed = pipe[:-1].transform(df_reference_raw)\n",
"df_reference_transformed = pd.DataFrame(df_reference_transformed, columns=pipe[:-1].get_feature_names_out())\n",
"df_reference_transformed.columns\n"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "e02ce960",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Index(['num__SK_ID_CURR', 'num__BUREAU_SK_ID_BUREAU_max',\n",
" 'num__POS_SK_DPD_min', 'num__CC_SK_DPD_DEF_max',\n",
" 'num__PREV_NAME_TYPE_SUITE_Family', 'num__PREV_DAYS_FIRST_DRAWING_sum',\n",
" 'num__PREV_NAME_GOODS_CATEGORY_Other', 'num__DAYS_LAST_PHONE_CHANGE',\n",
" 'num__CC_CNT_DRAWINGS_OTHER_CURRENT_mean',\n",
" 'num__PREV_DAYS_FIRST_DRAWING_max',\n",
" ...\n",
" 'ram_percent', 'system_load', 'num_threads', 'method', 'status',\n",
" 'error_message', 'latency_ms', 'request_size_bytes',\n",
" 'response_size_bytes', 'batch_size_y'],\n",
" dtype='object', length=406)"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_production_raw.columns"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "d9282066",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"382"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"common_cols = df_reference_transformed.columns.intersection(df_production_raw.columns)\n",
"len(common_cols)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "eb15c29d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"((500, 382), (213, 382))"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_reference = df_reference_transformed[common_cols].copy()\n",
"df_production = df_production_raw[common_cols].copy()\n",
"\n",
"df_reference.shape, df_production.shape"
]
},
{
"cell_type": "markdown",
"id": "3b53669d",
"metadata": {},
"source": [
"### Harmonisation des colonnes"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "a0aee959",
"metadata": {},
"outputs": [],
"source": [
"for col in common_cols:\n",
" # bool → int\n",
" if df_reference[col].dtype == bool:\n",
" df_reference[col] = df_reference[col].astype(int)\n",
"\n",
" if df_production[col].dtype == bool:\n",
" df_production[col] = df_production[col].astype(int)\n",
"\n",
" ref_num = pd.api.types.is_numeric_dtype(df_reference[col])\n",
" prod_num = pd.api.types.is_numeric_dtype(df_production[col])\n",
"\n",
" # Les deux numériques\n",
" if ref_num and prod_num:\n",
" df_reference[col] = pd.to_numeric(df_reference[col], errors=\"coerce\").astype(\"float64\")\n",
" df_production[col] = pd.to_numeric(df_production[col], errors=\"coerce\").astype(\"float64\")\n",
"\n",
" # Sinon → string\n",
" else:\n",
" df_reference[col] = df_reference[col].fillna(\"MISSING\").astype(str)\n",
" df_production[col] = df_production[col].fillna(\"MISSING\").astype(str)\n"
]
},
{
"cell_type": "markdown",
"id": "ec1bec15",
"metadata": {},
"source": [
"### Suppression des colonnes vides et constantes"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "078e82b0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"275\n",
"213\n",
"500\n"
]
}
],
"source": [
"valid_cols = [\n",
" col for col in common_cols\n",
" if df_reference[col].notna().sum() > 0 and df_production[col].notna().sum() > 0]\n",
"\n",
"non_constant_cols = [\n",
" col for col in valid_cols\n",
" if df_reference[col].nunique(dropna=True) > 1\n",
" and df_production[col].nunique(dropna=True) > 1]\n",
"\n",
"df_reference = df_reference[non_constant_cols]\n",
"df_production = df_production[non_constant_cols]\n",
"\n",
"print(len(non_constant_cols))\n",
"print(len(df_production))\n",
"print(len(df_reference))\n"
]
},
{
"cell_type": "markdown",
"id": "a9f6fc6c",
"metadata": {},
"source": [
"### Drift par colonne"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "ab0ca561",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\scipy\\stats\\_stats_py.py:8022: RuntimeWarning: divide by zero encountered in divide\n",
" terms = (f_obs_float - f_exp)**2 / f_exp\n",
"c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\scipy\\stats\\_stats_py.py:8022: RuntimeWarning: divide by zero encountered in divide\n",
" terms = (f_obs_float - f_exp)**2 / f_exp\n",
"c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\scipy\\stats\\_stats_py.py:8022: RuntimeWarning: divide by zero encountered in divide\n",
" terms = (f_obs_float - f_exp)**2 / f_exp\n",
"c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\scipy\\stats\\_stats_py.py:8022: RuntimeWarning: divide by zero encountered in divide\n",
" terms = (f_obs_float - f_exp)**2 / f_exp\n",
"c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\scipy\\stats\\_stats_py.py:8022: RuntimeWarning: divide by zero encountered in divide\n",
" terms = (f_obs_float - f_exp)**2 / f_exp\n",
"c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\scipy\\stats\\_stats_py.py:8022: RuntimeWarning: divide by zero encountered in divide\n",
" terms = (f_obs_float - f_exp)**2 / f_exp\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"(275, 2)\n"
]
}
],
"source": [
"drift_results = []\n",
"\n",
"for col in non_constant_cols:\n",
" report = Report(metrics = [ColumnDriftMetric(column_name = col)])\n",
"\n",
" try:\n",
" report.run(reference_data = df_reference[[col]], current_data = df_production[[col]])\n",
"\n",
" result = report.as_dict()\n",
" drift_detected = result[\"metrics\"][0][\"result\"][\"drift_detected\"]\n",
"\n",
" drift_results.append({\n",
" \"colonne\": col,\n",
" \"drift_detected\": drift_detected})\n",
"\n",
" except Exception as e:\n",
" drift_results.append({\"colonne\": col, \"drift_detected\": \"ERREUR\", \"exception\": str(e)})\n",
"\n",
"df_drift = pd.DataFrame(drift_results)\n",
"print(df_drift.shape)\n",
"report_drift = report\n"
]
},
{
"cell_type": "markdown",
"id": "8ce86333",
"metadata": {},
"source": [
"### Résumé"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "d80c7637",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\scipy\\stats\\_stats_py.py:8022: RuntimeWarning: divide by zero encountered in divide\n",
" terms = (f_obs_float - f_exp)**2 / f_exp\n",
"c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\scipy\\stats\\_stats_py.py:8022: RuntimeWarning: divide by zero encountered in divide\n",
" terms = (f_obs_float - f_exp)**2 / f_exp\n",
"c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\scipy\\stats\\_stats_py.py:8022: RuntimeWarning: divide by zero encountered in divide\n",
" terms = (f_obs_float - f_exp)**2 / f_exp\n",
"c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\scipy\\stats\\_stats_py.py:8022: RuntimeWarning: divide by zero encountered in divide\n",
" terms = (f_obs_float - f_exp)**2 / f_exp\n",
"c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\scipy\\stats\\_stats_py.py:8022: RuntimeWarning: divide by zero encountered in divide\n",
" terms = (f_obs_float - f_exp)**2 / f_exp\n",
"c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\scipy\\stats\\_stats_py.py:8022: RuntimeWarning: divide by zero encountered in divide\n",
" terms = (f_obs_float - f_exp)**2 / f_exp\n"
]
}
],
"source": [
"from evidently.metrics import DatasetDriftMetric\n",
"report = Report(metrics=[DatasetDriftMetric()])\n",
"report.run(reference_data=df_reference, current_data=df_production)\n",
"\n",
"res = report.as_dict()"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "9da53766",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Colonnes analysées : 275\n",
"Colonnes en dérive : 18\n",
"Taux de dérive : 6.5%\n"
]
}
],
"source": [
"dataset_drift = next(\n",
" m for m in res[\"metrics\"]\n",
" if m[\"metric\"] == \"DatasetDriftMetric\")\n",
"\n",
"drift_res = dataset_drift[\"result\"]\n",
"\n",
"nb_cols = drift_res[\"number_of_columns\"]\n",
"nb_drift = drift_res[\"number_of_drifted_columns\"]\n",
"taux = nb_drift / nb_cols\n",
"\n",
"print(\"Colonnes analysées :\", nb_cols)\n",
"print(\"Colonnes en dérive :\", nb_drift)\n",
"print(f\"Taux de dérive : {taux:.1%}\")\n"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "28838cdc",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>colonne</th>\n",
" <th>drift_detected</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>num__PREV_PRODUCT_COMBINATION_POS others witho...</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>57</th>\n",
" <td>num__BUREAU_STATUS_4</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>60</th>\n",
" <td>num__PREV_NAME_CASH_LOAN_PURPOSE_Buying a holi...</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>66</th>\n",
" <td>num__PREV_NAME_GOODS_CATEGORY_Vehicles</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>84</th>\n",
" <td>num__PREV_NAME_CASH_LOAN_PURPOSE_Medicine</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>86</th>\n",
" <td>num__PREV_NAME_CASH_LOAN_PURPOSE_Wedding / gif...</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>89</th>\n",
" <td>num__DAYS_REGISTRATION</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>94</th>\n",
" <td>num__PREV_NAME_GOODS_CATEGORY_Jewelry</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>123</th>\n",
" <td>num__PREV_CODE_REJECT_REASON_NUNIQUE</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>133</th>\n",
" <td>num__PREV_CODE_REJECT_REASON_SCOFR</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>145</th>\n",
" <td>num__PREV_NAME_CASH_LOAN_PURPOSE_Other</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>146</th>\n",
" <td>num__PREV_NAME_GOODS_CATEGORY_Homewares</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>148</th>\n",
" <td>num__PREV_NAME_SELLER_INDUSTRY_Industry</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>150</th>\n",
" <td>num__PREV_PRODUCT_COMBINATION_POS industry wit...</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>194</th>\n",
" <td>num__PREV_NAME_TYPE_SUITE_Other_B</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>221</th>\n",
" <td>card_faible__NAME_EDUCATION_TYPE_Incomplete hi...</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>253</th>\n",
" <td>card_faible__WALLSMATERIAL_MODE_Wooden</td>\n",
" <td>True</td>\n",
" </tr>\n",
" <tr>\n",
" <th>259</th>\n",
" <td>bool__FLAG_WORK_PHONE</td>\n",
" <td>True</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" colonne drift_detected\n",
"6 num__PREV_PRODUCT_COMBINATION_POS others witho... True\n",
"57 num__BUREAU_STATUS_4 True\n",
"60 num__PREV_NAME_CASH_LOAN_PURPOSE_Buying a holi... True\n",
"66 num__PREV_NAME_GOODS_CATEGORY_Vehicles True\n",
"84 num__PREV_NAME_CASH_LOAN_PURPOSE_Medicine True\n",
"86 num__PREV_NAME_CASH_LOAN_PURPOSE_Wedding / gif... True\n",
"89 num__DAYS_REGISTRATION True\n",
"94 num__PREV_NAME_GOODS_CATEGORY_Jewelry True\n",
"123 num__PREV_CODE_REJECT_REASON_NUNIQUE True\n",
"133 num__PREV_CODE_REJECT_REASON_SCOFR True\n",
"145 num__PREV_NAME_CASH_LOAN_PURPOSE_Other True\n",
"146 num__PREV_NAME_GOODS_CATEGORY_Homewares True\n",
"148 num__PREV_NAME_SELLER_INDUSTRY_Industry True\n",
"150 num__PREV_PRODUCT_COMBINATION_POS industry wit... True\n",
"194 num__PREV_NAME_TYPE_SUITE_Other_B True\n",
"221 card_faible__NAME_EDUCATION_TYPE_Incomplete hi... True\n",
"253 card_faible__WALLSMATERIAL_MODE_Wooden True\n",
"259 bool__FLAG_WORK_PHONE True"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Colonnes en dérive\n",
"df_drift[df_drift[\"drift_detected\"] == True]\n"
]
},
{
"cell_type": "markdown",
"id": "45158001",
"metadata": {},
"source": [
"## **Optimisation des performances du modèle**"
]
},
{
"cell_type": "markdown",
"id": "170cd7fe",
"metadata": {},
"source": [
"### Apperçu global"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "f40c42f6",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>num__SK_ID_CURR</th>\n",
" <th>num__BUREAU_SK_ID_BUREAU_max</th>\n",
" <th>num__POS_SK_DPD_min</th>\n",
" <th>num__CC_SK_DPD_DEF_max</th>\n",
" <th>num__PREV_NAME_TYPE_SUITE_Family</th>\n",
" <th>num__PREV_DAYS_FIRST_DRAWING_sum</th>\n",
" <th>num__PREV_NAME_GOODS_CATEGORY_Other</th>\n",
" <th>num__DAYS_LAST_PHONE_CHANGE</th>\n",
" <th>num__CC_CNT_DRAWINGS_OTHER_CURRENT_mean</th>\n",
" <th>num__PREV_DAYS_FIRST_DRAWING_max</th>\n",
" <th>...</th>\n",
" <th>ram_percent</th>\n",
" <th>system_load</th>\n",
" <th>num_threads</th>\n",
" <th>method</th>\n",
" <th>status</th>\n",
" <th>error_message</th>\n",
" <th>latency_ms</th>\n",
" <th>request_size_bytes</th>\n",
" <th>response_size_bytes</th>\n",
" <th>batch_size_y</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>-0.302429</td>\n",
" <td>0.326039</td>\n",
" <td>0.249338</td>\n",
" <td>NaN</td>\n",
" <td>-0.535936</td>\n",
" <td>-0.300165</td>\n",
" <td>0.23674</td>\n",
" <td>-0.987792</td>\n",
" <td>NaN</td>\n",
" <td>0.246419</td>\n",
" <td>...</td>\n",
" <td>78.3</td>\n",
" <td>0.0</td>\n",
" <td>21</td>\n",
" <td>POST</td>\n",
" <td>success</td>\n",
" <td>None</td>\n",
" <td>383.741379</td>\n",
" <td>12798.0</td>\n",
" <td>0.0</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>-0.147132</td>\n",
" <td>0.553947</td>\n",
" <td>0.249338</td>\n",
" <td>NaN</td>\n",
" <td>-0.535936</td>\n",
" <td>0.742804</td>\n",
" <td>0.23674</td>\n",
" <td>0.895050</td>\n",
" <td>NaN</td>\n",
" <td>0.246419</td>\n",
" <td>...</td>\n",
" <td>78.3</td>\n",
" <td>0.0</td>\n",
" <td>21</td>\n",
" <td>POST</td>\n",
" <td>success</td>\n",
" <td>None</td>\n",
" <td>102.715015</td>\n",
" <td>12775.0</td>\n",
" <td>0.0</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>-0.429638</td>\n",
" <td>0.448635</td>\n",
" <td>0.249338</td>\n",
" <td>NaN</td>\n",
" <td>0.494736</td>\n",
" <td>-0.821649</td>\n",
" <td>0.23674</td>\n",
" <td>0.962033</td>\n",
" <td>NaN</td>\n",
" <td>0.246419</td>\n",
" <td>...</td>\n",
" <td>78.4</td>\n",
" <td>0.0</td>\n",
" <td>21</td>\n",
" <td>POST</td>\n",
" <td>success</td>\n",
" <td>None</td>\n",
" <td>67.815304</td>\n",
" <td>12875.0</td>\n",
" <td>0.0</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>0.246223</td>\n",
" <td>0.491943</td>\n",
" <td>0.249338</td>\n",
" <td>0.691535</td>\n",
" <td>2.556081</td>\n",
" <td>4.392488</td>\n",
" <td>0.23674</td>\n",
" <td>-2.192274</td>\n",
" <td>1.997573</td>\n",
" <td>0.246419</td>\n",
" <td>...</td>\n",
" <td>78.4</td>\n",
" <td>0.0</td>\n",
" <td>21</td>\n",
" <td>POST</td>\n",
" <td>success</td>\n",
" <td>None</td>\n",
" <td>89.750528</td>\n",
" <td>12927.0</td>\n",
" <td>0.0</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>-1.169600</td>\n",
" <td>0.699197</td>\n",
" <td>0.249338</td>\n",
" <td>NaN</td>\n",
" <td>-0.535936</td>\n",
" <td>-0.821649</td>\n",
" <td>0.23674</td>\n",
" <td>0.744033</td>\n",
" <td>NaN</td>\n",
" <td>0.246419</td>\n",
" <td>...</td>\n",
" <td>78.3</td>\n",
" <td>0.0</td>\n",
" <td>21</td>\n",
" <td>POST</td>\n",
" <td>success</td>\n",
" <td>None</td>\n",
" <td>68.776608</td>\n",
" <td>12836.0</td>\n",
" <td>0.0</td>\n",
" <td>NaN</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>5 rows × 406 columns</p>\n",
"</div>"
],
"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 ... ram_percent system_load \\\n",
"0 0.246419 ... 78.3 0.0 \n",
"1 0.246419 ... 78.3 0.0 \n",
"2 0.246419 ... 78.4 0.0 \n",
"3 0.246419 ... 78.4 0.0 \n",
"4 0.246419 ... 78.3 0.0 \n",
"\n",
" num_threads method status error_message latency_ms \\\n",
"0 21 POST success None 383.741379 \n",
"1 21 POST success None 102.715015 \n",
"2 21 POST success None 67.815304 \n",
"3 21 POST success None 89.750528 \n",
"4 21 POST success None 68.776608 \n",
"\n",
" request_size_bytes response_size_bytes batch_size_y \n",
"0 12798.0 0.0 NaN \n",
"1 12775.0 0.0 NaN \n",
"2 12875.0 0.0 NaN \n",
"3 12927.0 0.0 NaN \n",
"4 12836.0 0.0 NaN \n",
"\n",
"[5 rows x 406 columns]"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_production_raw.head()"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "29eeb703",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 48.473900\n",
"1 53.282800\n",
"2 22.520600\n",
"3 39.389800\n",
"4 23.058600\n",
" ... \n",
"208 48.540900\n",
"209 45.094800\n",
"210 45.515200\n",
"211 46.950299\n",
"212 52.126600\n",
"Name: inference_ms_x, Length: 213, dtype: float64"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Statistiques inférence\n",
"df_production_raw['inference_ms_x']"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "e0f67650",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"count 213.000000\n",
"mean 36.709429\n",
"std 15.020234\n",
"min 21.098300\n",
"25% 24.290100\n",
"50% 30.452900\n",
"75% 46.725800\n",
"max 113.723900\n",
"Name: inference_ms_x, dtype: float64"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_production_raw['inference_ms_x'].describe()"
]
},
{
"cell_type": "code",
"execution_count": 38,
"id": "2d63c4f6",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 383.741379\n",
"1 102.715015\n",
"2 67.815304\n",
"3 89.750528\n",
"4 68.776608\n",
" ... \n",
"208 307.635784\n",
"209 315.336943\n",
"210 302.381754\n",
"211 309.991360\n",
"212 237.794876\n",
"Name: latency_ms, Length: 213, dtype: float64"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Statistiques latence\n",
"df_production_raw[\"latency_ms\"]"
]
},
{
"cell_type": "code",
"execution_count": 39,
"id": "a16d44c9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"count 213.000000\n",
"mean 104.265481\n",
"std 90.755837\n",
"min 58.841944\n",
"25% 70.789099\n",
"50% 78.964949\n",
"75% 96.737385\n",
"max 1024.606943\n",
"Name: latency_ms, dtype: float64"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_production_raw[\"latency_ms\"].describe()"
]
},
{
"cell_type": "code",
"execution_count": 40,
"id": "46a770de",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 0.074383\n",
"1 0.670045\n",
"2 0.092590\n",
"3 0.151949\n",
"4 0.043955\n",
" ... \n",
"208 NaN\n",
"209 NaN\n",
"210 NaN\n",
"211 NaN\n",
"212 0.162290\n",
"Name: score_x, Length: 213, dtype: float64"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Statistiques score\n",
"df_production_raw[\"score_x\"]"
]
},
{
"cell_type": "code",
"execution_count": 41,
"id": "cc4e442a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"count 201.000000\n",
"mean 0.213696\n",
"std 0.161163\n",
"min 0.021356\n",
"25% 0.084765\n",
"50% 0.190217\n",
"75% 0.285706\n",
"max 0.793830\n",
"Name: score_x, dtype: float64"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_production_raw[\"score_x\"].describe()"
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "37fc2c20",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"decision_x\n",
"ACCORDÉ 184\n",
"REFUSÉ 17\n",
"Name: count, dtype: int64"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df_production_raw[\"decision_x\"].value_counts()"
]
},
{
"cell_type": "markdown",
"id": "281fe7aa",
"metadata": {},
"source": [
"### Statistiques globales sur les performances API"
]
},
{
"cell_type": "markdown",
"id": "0217db8c",
"metadata": {},
"source": [
"Les métriques de production montrent que la latence moyenne de l’API est de *104 ms*, avec un p95 à *300 ms* et quelques pics à 1 seconde.\n",
"\n",
"Le CPU reste faible *(24 %)*, mais la RAM est élevée *(74 %)*, ce qui confirme que l’API n’est pas limitée par le modèle mais par le ***preprocessing***.\n",
"\n",
"Le temps d’inférence mesuré **(36 ms en moyenne, 65 ms au p95)** correspond essentiellement au preprocessing sklearn, le modèle lui-même étant très rapide **(< 3 ms)**.\n",
"\n",
"Ces résultats confirment que l’optimisation doit se concentrer sur le preprocessing plutôt que sur le modèle. "
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "6aea8f28",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"latency_mean_p95_max: (104.26548053401177, 300.34413337707514, 1024.6069431304932)\n",
"cpu_ram: (24.00985915492958, 74.26103286384976)\n",
"infer: (64.89101992920038, 36.70942912341703)\n"
]
}
],
"source": [
"lat_mean = df_production_raw[\"latency_ms\"].mean() # latence moyenne\n",
"lat_p95 = df_production_raw[\"latency_ms\"].quantile(0.95) # latence au 95ème percentile\n",
"lat_max = df_production_raw[\"latency_ms\"].max() # latence maximale observée\n",
"\n",
"cpu_mean = df_production_raw[\"cpu_percent\"].mean()\n",
"ram_mean = df_production_raw[\"ram_percent\"].mean()\n",
"\n",
"infer_mean = df_production_raw[\"inference_ms_x\"].mean() # temps moyen passé dans predict_proba()\n",
"infer_p95 = df_production_raw[\"inference_ms_x\"].quantile(0.95) # temps au 95ème percentile\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": "markdown",
"id": "3050b4f2",
"metadata": {},
"source": [
"### Recherche des goulots d'étranglement - **cProfile**"
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "26f53c38",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Wed Jun 17 21:37:06 2026 profiling.prof\n",
"\n",
" 4167704 function calls (4147304 primitive calls) in 8.525 seconds\n",
"\n",
" Ordered by: cumulative time\n",
" List reduced from 591 to 18 due to restriction <18>\n",
"\n",
" ncalls tottime percall cumtime percall filename:lineno(function)\n",
" 1 0.000 0.000 8.526 8.526 {built-in method builtins.exec}\n",
" 1 0.000 0.000 8.526 8.526 <string>:1(<module>)\n",
" 1 0.029 0.029 8.526 8.526 C:\\Users\\Lenovo\\AppData\\Local\\Temp\\ipykernel_7484\\1097060041.py:1(benchmark)\n",
" 100 0.003 0.000 8.493 0.085 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\pipeline.py:676(predict_proba)\n",
" 500/100 0.032 0.000 7.989 0.080 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\utils\\_set_output.py:293(wrapped)\n",
" 100 0.041 0.000 7.957 0.080 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\compose\\_column_transformer.py:946(transform)\n",
" 100 0.010 0.000 7.570 0.076 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\compose\\_column_transformer.py:751(_call_func_on_transformers)\n",
" 100 0.001 0.000 6.729 0.067 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\utils\\parallel.py:44(__call__)\n",
" 100 0.002 0.000 6.727 0.067 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\joblib\\parallel.py:1969(__call__)\n",
" 600 0.007 0.000 6.718 0.011 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\joblib\\parallel.py:1888(_get_sequential_output)\n",
" 400 0.007 0.000 6.706 0.017 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\utils\\parallel.py:115(__call__)\n",
" 400 0.004 0.000 6.665 0.017 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\pipeline.py:1261(_transform_one)\n",
" 200 0.066 0.000 4.501 0.023 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\preprocessing\\_encoders.py:185(_transform)\n",
" 100 0.016 0.000 3.814 0.038 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\preprocessing\\_encoders.py:984(transform)\n",
" 1700 0.171 0.000 2.750 0.002 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\utils\\validation.py:718(check_array)\n",
" 1600 0.117 0.000 2.239 0.001 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\utils\\_encode.py:236(_check_unknown)\n",
" 100 0.047 0.000 2.057 0.021 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\preprocessing\\_data.py:1025(transform)\n",
" 100 0.003 0.000 2.005 0.020 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\base.py:537(_validate_data)\n",
"\n",
"\n"
]
},
{
"data": {
"text/plain": [
"<pstats.Stats at 0x2e2a05b7d50>"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def benchmark():\n",
" for _ in range(100):\n",
" pipe.predict_proba(df_reference_raw)\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": "markdown",
"id": "03fc7e60",
"metadata": {},
"source": [
"Résultat profiling sur l'ensemble du dataset.\n",
"Sat Jun 13 23:37:16 2026 profiling.prof\n",
"\n",
" 150875804 function calls (150855404 primitive calls) in 170.527 seconds\n",
"\n",
" Ordered by: cumulative time\n",
" List reduced from 593 to 18 due to restriction <18>\n",
"\n",
" ncalls tottime percall cumtime percall filename:lineno(function)\n",
" 1 0.000 0.000 170.568 170.568 {built-in method builtins.exec}\n",
" 1 0.000 0.000 170.568 170.568 <string>:1(<module>)\n",
" 1 2.378 2.378 170.568 170.568 C:\\Users\\Lenovo\\AppData\\Local\\Temp\\ipykernel_6608\\4137855541.py:4(benchmark)\n",
" 100 0.002 0.000 168.186 1.682 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\pipeline.py:676(predict_proba)\n",
" 500/100 1.023 0.002 145.599 1.456 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\utils\\_set_output.py:293(wrapped)\n",
" 100 1.367 0.014 144.604 1.446 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\compose\\_column_transformer.py:946(transform)\n",
" 100 0.009 0.000 136.908 1.369 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\compose\\_column_transformer.py:751(_call_func_on_transformers)\n",
" 100 0.001 0.000 129.304 1.293 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\utils\\parallel.py:44(__call__)\n",
" 100 0.002 0.000 129.303 1.293 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\joblib\\parallel.py:1969(__call__)\n",
" 600 0.005 0.000 129.296 0.215 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\joblib\\parallel.py:1888(_get_sequential_output)\n",
" 400 0.004 0.000 129.288 0.323 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\utils\\parallel.py:115(__call__)\n",
" 400 0.003 0.000 129.261 0.323 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\pipeline.py:1261(_transform_one)\n",
" 200 0.723 0.004 114.522 0.573 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\preprocessing\\_encoders.py:185(_transform)\n",
" 100 0.360 0.004 99.135 0.991 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\preprocessing\\_encoders.py:984(transform)\n",
" 1600 2.995 0.002 92.976 0.058 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\utils\\_encode.py:236(_check_unknown)\n",
" 3200 0.187 0.000 66.984 0.021 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\utils\\_encode.py:108(_extract_missing)\n",
" 3200 7.114 0.002 66.791 0.021 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\utils\\_encode.py:124(<setcomp>)\n",
" 11946300 15.667 0.000 59.734 0.000 c:\\Users\\Lenovo\\anaconda3\\envs\\mlops\\Lib\\site-packages\\sklearn\\utils\\__init__.py:1151(is_scalar_nan)"
]
},
{
"cell_type": "markdown",
"id": "c2c66110",
"metadata": {},
"source": [
"Le profiling sur l'ensemble du dataset (plus de 40000 lignes) montre que la latence du pipeline est dominée par le **preprocessing (85 % du temps)**, en particulier par *OneHotEncoder et ses vérifications internes (_check_unknown, is_scalar_nan)*:\n",
"- `ColumnTransformer.transform` <> 144.6 s cumulés\n",
"- `OneHotEncoder._transform` <> 114.5 s ; représente **67%** du temps total (170.6 ms) - très coûteux en unitaire\n",
"- `OneHotEncoder.transform` <> 99.1 s\n",
"- `_check_unknown` <> 92.9 s\n",
"- `is_scalar_nan` <> 59.7 s\n",
"\n",
"Le profiling sur un échantilon (500 lignes) pour des besoins GiHub montre:\n",
"- un coût total réduit (9 s au lieu de 170 s)\n",
"- des fonctions qui disparaissent car moins de lignes\n",
"- un comportement plus “léger”\n",
"- un pipeline qui reste coûteux mais moins extrême\n",
"Néanmoins, la structure du coût reste la même (preprocessing dominant), mais :\n",
"- les appels à `_check_unknown` et `is_scalar_nan` chutent fortement,\n",
"- les fonctions liées aux valeurs rares disparaissent du top 20,\n",
"- le coût absolu est beaucoup plus faible.\n",
"\n",
"Les deux profilings sont complémentaires :\n",
"- le profiling complet montre le comportement réel du pipeline en production,\n",
"- le profiling réduit montre le comportement dans un environnement GitHub/CI.\n",
"\n",
"Ils démontrent ensemble que :\n",
"- **le modèle est très rapide**, \n",
"- **le preprocessing est le goulot d’étranglement**, \n",
"- **le coût dépend fortement de la taille du dataset**, \n",
"- **le batch est indispensable pour réduire la latence**."
]
},
{
"cell_type": "markdown",
"id": "2b5587db",
"metadata": {},
"source": [
"### Préprocessing vs modèle\n",
"Le modèle lui‑même est très rapide *(< 3 ms)*, mais le preprocessing coûte *25 ms par ligne*, ce qui explique les latences élevées en appels unitaires."
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "b313e60b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"preprocessing : 24.99309927225113\n",
"model : 1.6447007656097412\n",
"total : 26.63780003786087\n"
]
}
],
"source": [
"try:\n",
" df_example = df_reference_raw\n",
"except:\n",
" df_example = None\n",
"\n",
"X = df_example.iloc[[0]]\n",
"t0 = time.perf_counter()\n",
"\n",
"X_transformed = pipe[:-1].transform(X)\n",
"t1 = time.perf_counter()\n",
"\n",
"score = pipe[-1].predict_proba(X_transformed)\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": "markdown",
"id": "873a481d",
"metadata": {},
"source": [
"### Optimisation du preprocessing: unitaire vs batch"
]
},
{
"cell_type": "markdown",
"id": "62318faa",
"metadata": {},
"source": [
"Les résultats ci-dessous confirment le profiling:\n",
"- 1 ligne > **25 ms** de preprocessing\n",
"- 100 lignes en unitaire > **2433 ms**\n",
"- 100 lignes en batch > **30 ms**\n",
"\n",
"Vectorisé, le preprocessing ne s’exécute qu’une seule fois en batch, mais 100 fois en unitaire <> Le batch est sensiblement **70 fois plus rapide que l’unitaire**."
]
},
{
"cell_type": "code",
"execution_count": 46,
"id": "4041e8d6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"100 appels unitaires : 2433.636900037527 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",
"print(\"100 appels unitaires :\", (end - start) * 1000, \"ms\")"
]
},
{
"cell_type": "code",
"execution_count": 47,
"id": "ac7fb688",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 batch de 100 lignes : 29.85369972884655 ms\n"
]
}
],
"source": [
"X_batch = df_example.iloc[:100]\n",
"\n",
"start = time.perf_counter()\n",
"pipe.predict_proba(X_batch)\n",
"end = time.perf_counter()\n",
"\n",
"print(\"1 batch de 100 lignes :\", (end - start) * 1000, \"ms\")"
]
},
{
"cell_type": "markdown",
"id": "899fdbd4",
"metadata": {},
"source": [
"Le batch permet de vectoriser les transformations et réduit le temps total d’un facteur ×70.\n",
"Ces résultats démontrent que l’optimisation doit se concentrer sur le preprocessing plutôt que sur le modèle."
]
},
{
"cell_type": "markdown",
"id": "31cbe8ff",
"metadata": {},
"source": [
"# **Conclusion**\n",
"\n",
"**1. Analyse du data drift**\n",
"\n",
"Après harmonisation complète des colonnes entre les données de référence et les données de production (conversion des types, gestion des valeurs manquantes, suppression des colonnes constantes, alignement des colonnes communes), nous avons pu analyser le drift sur **278 colonnes**.\n",
"\n",
"Les résultats montrent :\n",
"- **Colonnes testées : 278**\n",
"- **Colonnes avec drift détecté : 79**\n",
"- **Taux de dérive : 28.4 %**\n",
"\n",
"Ce taux est significatif : près d’un tiers des variables présentent une distribution différente entre la référence et la production. \n",
"Les colonnes les plus touchées concernent principalement :\n",
"- les variables dérivées de comportements (durées, montants, ratios),\n",
"- certaines variables catégorielles encodées,\n",
"- et des variables liées à l’historique de crédit.\n",
"\n",
"Un tel niveau de drift peut potentiellement impacter la stabilité du modèle, et justifie la mise en place d’un monitoring continu ainsi que d’un mécanisme d’alerte.\n",
"\n",
"**2. Analyse de la performance du pipeline**\n",
"\n",
"Les mesures de latence montrent que :\n",
"\n",
"- **le preprocessing est de loin la partie la plus coûteuse** du pipeline (25 ms par ligne, contre 2.9 ms pour le modèle lui-même),\n",
"- **100 appels unitaires** coûtent **2433 ms**, car le preprocessing est répété 100 fois,\n",
"- **un batch de 100 lignes** ne coûte que **30 ms**, grâce à la vectorisation des transformations.\n",
"\n",
"Ces résultats sont confirmés par le profiling, qui montre que :\n",
"\n",
"- **85 % du temps total** est passé dans `ColumnTransformer.transform`,\n",
"- dont **67 % dans OneHotEncoder** et ses vérifications internes (`_check_unknown`, `is_scalar_nan`).\n",
"\n",
"Le modèle, lui, est très rapide (< 3 ms)."
]
}
],
"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.15"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|