{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":103436,"databundleVersionId":12476472,"sourceType":"competition"}],"dockerImageVersionId":31040,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"id":"f23bdf3b","cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:05.878607Z","iopub.execute_input":"2025-06-17T13:45:05.879043Z","iopub.status.idle":"2025-06-17T13:45:08.444183Z","shell.execute_reply.started":"2025-06-17T13:45:05.878983Z","shell.execute_reply":"2025-06-17T13:45:08.443231Z"}},"outputs":[],"execution_count":null},{"id":"93c0757f","cell_type":"markdown","source":"## Import dataset","metadata":{}},{"id":"4c20e3aa","cell_type":"code","source":"A_delay = pd.read_csv('/kaggle/input/ds-108-p-21-assigment-06/delay_4_6_CONDITION_PRODUCT_SUPPLIER.csv', low_memory=False)\nA_not_delay = pd.read_csv('/kaggle/input/ds-108-p-21-assigment-06/not_delay_4_6_CONDITION_PRODUCT_SUPPLIER.csv', low_memory=False)\n\nA_delay['label'] = 1\nA_not_delay['label'] = 0\n\nA = pd.concat([A_delay, A_not_delay], axis=0)\nA.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:08.445040Z","iopub.execute_input":"2025-06-17T13:45:08.445434Z","iopub.status.idle":"2025-06-17T13:45:13.625461Z","shell.execute_reply.started":"2025-06-17T13:45:08.445411Z","shell.execute_reply":"2025-06-17T13:45:13.624420Z"}},"outputs":[],"execution_count":null},{"id":"45fa6589","cell_type":"code","source":"B_delay = pd.read_csv('/kaggle/input/ds-108-p-21-assigment-06/delay_7_9_CONDITION_PRODUCT_SUPPLIER.csv', low_memory=False)\nB_not_delay = pd.read_csv('/kaggle/input/ds-108-p-21-assigment-06/not_delay_7_9_CONDITION_PRODUCT_SUPPLIER.csv', low_memory=False)\n\nB_delay['label'] = 1\nB_not_delay['label'] = 0\n\nB = pd.concat([B_delay, B_not_delay], axis=0)\nB.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:13.627708Z","iopub.execute_input":"2025-06-17T13:45:13.627938Z","iopub.status.idle":"2025-06-17T13:45:24.690265Z","shell.execute_reply.started":"2025-06-17T13:45:13.627921Z","shell.execute_reply":"2025-06-17T13:45:24.689297Z"}},"outputs":[],"execution_count":null},{"id":"78a517c3","cell_type":"code","source":"submission = pd.read_csv('/kaggle/input/ds-108-p-21-assigment-06/PILOT_10.csv', low_memory=False)\nsubmission['label'] = np.nan\nsubmission.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:24.694084Z","iopub.execute_input":"2025-06-17T13:45:24.694350Z","iopub.status.idle":"2025-06-17T13:45:25.902821Z","shell.execute_reply.started":"2025-06-17T13:45:24.694330Z","shell.execute_reply":"2025-06-17T13:45:25.901978Z"}},"outputs":[],"execution_count":null},{"id":"23c59742","cell_type":"markdown","source":"### Preprocess differences between datasets","metadata":{}},{"id":"8ea39da1","cell_type":"markdown","source":"Unmatched columns","metadata":{}},{"id":"e65efee4","cell_type":"code","source":"AB_diff = set(A.columns) - set(B.columns)\nprint(AB_diff)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:25.903675Z","iopub.execute_input":"2025-06-17T13:45:25.903970Z","iopub.status.idle":"2025-06-17T13:45:25.910698Z","shell.execute_reply.started":"2025-06-17T13:45:25.903945Z","shell.execute_reply":"2025-06-17T13:45:25.909712Z"}},"outputs":[],"execution_count":null},{"id":"94c49982","cell_type":"code","source":"for col in AB_diff:\n    A.drop(col, axis=1, inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:25.911941Z","iopub.execute_input":"2025-06-17T13:45:25.912302Z","iopub.status.idle":"2025-06-17T13:45:26.827284Z","shell.execute_reply.started":"2025-06-17T13:45:25.912271Z","shell.execute_reply":"2025-06-17T13:45:26.825938Z"}},"outputs":[],"execution_count":null},{"id":"60fcf8df","cell_type":"code","source":"submission_diff = set(submission.columns) - set(B.columns)\nprint(submission_diff)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:26.828619Z","iopub.execute_input":"2025-06-17T13:45:26.828878Z","iopub.status.idle":"2025-06-17T13:45:26.835197Z","shell.execute_reply.started":"2025-06-17T13:45:26.828858Z","shell.execute_reply":"2025-06-17T13:45:26.833603Z"}},"outputs":[],"execution_count":null},{"id":"d7af246c","cell_type":"code","source":"A['ID'] = np.nan\nB['ID'] = np.nan","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:26.838758Z","iopub.execute_input":"2025-06-17T13:45:26.839131Z","iopub.status.idle":"2025-06-17T13:45:26.861361Z","shell.execute_reply.started":"2025-06-17T13:45:26.839083Z","shell.execute_reply":"2025-06-17T13:45:26.860283Z"}},"outputs":[],"execution_count":null},{"id":"26382538","cell_type":"markdown","source":"Unmatched date","metadata":{}},{"id":"18e92aae","cell_type":"code","source":"A['Order date'].head(), B['Order date'].head(), submission['Order date'].head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:26.862393Z","iopub.execute_input":"2025-06-17T13:45:26.862846Z","iopub.status.idle":"2025-06-17T13:45:26.887984Z","shell.execute_reply.started":"2025-06-17T13:45:26.862813Z","shell.execute_reply":"2025-06-17T13:45:26.887172Z"}},"outputs":[],"execution_count":null},{"id":"ed0eeb35","cell_type":"code","source":"A['Order date'] = pd.to_datetime(A['Order date'], format='%Y-%m-%d %H:%M:%S')\nB['Order date'] = pd.to_datetime(B['Order date'], format='%Y-%m-%d')\nsubmission['Order date'] = pd.to_datetime(submission['Order date'], format='%m/%d/%Y')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:26.889121Z","iopub.execute_input":"2025-06-17T13:45:26.889468Z","iopub.status.idle":"2025-06-17T13:45:27.127005Z","shell.execute_reply.started":"2025-06-17T13:45:26.889442Z","shell.execute_reply":"2025-06-17T13:45:27.126161Z"}},"outputs":[],"execution_count":null},{"id":"02694150","cell_type":"markdown","source":"##### Unmatched type","metadata":{}},{"id":"db0b3d62","cell_type":"code","source":"for col in A.columns:\n    if col not in ['ID', 'label']:\n        if (A[col].dtype != B[col].dtype) or (A[col].dtype != submission[col].dtype):\n            print(f'{col}: {A[col].dtype} {B[col].dtype} {submission[col].dtype}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:27.127853Z","iopub.execute_input":"2025-06-17T13:45:27.128110Z","iopub.status.idle":"2025-06-17T13:45:27.137975Z","shell.execute_reply.started":"2025-06-17T13:45:27.128090Z","shell.execute_reply":"2025-06-17T13:45:27.136905Z"}},"outputs":[],"execution_count":null},{"id":"141db9fd","cell_type":"markdown","source":"GLOBAL_NO","metadata":{}},{"id":"a801fb22","cell_type":"code","source":"A['GLOBAL_NO'].unique(), B['GLOBAL_NO'].unique(), submission['GLOBAL_NO'].unique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:27.138871Z","iopub.execute_input":"2025-06-17T13:45:27.139248Z","iopub.status.idle":"2025-06-17T13:45:27.392560Z","shell.execute_reply.started":"2025-06-17T13:45:27.139225Z","shell.execute_reply":"2025-06-17T13:45:27.391730Z"}},"outputs":[],"execution_count":null},{"id":"3798760f","cell_type":"code","source":"A['GLOBAL_NO'] = A['GLOBAL_NO'].astype(str)\nB['GLOBAL_NO'] = B['GLOBAL_NO'].astype(str)\nsubmission['GLOBAL_NO'] = submission['GLOBAL_NO'].astype(str)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:27.393511Z","iopub.execute_input":"2025-06-17T13:45:27.393827Z","iopub.status.idle":"2025-06-17T13:45:27.577915Z","shell.execute_reply.started":"2025-06-17T13:45:27.393798Z","shell.execute_reply":"2025-06-17T13:45:27.577102Z"}},"outputs":[],"execution_count":null},{"id":"f630a997","cell_type":"markdown","source":"Consider count holiday Saturday","metadata":{}},{"id":"c2f72c23","cell_type":"code","source":"A['Consider count hodiday Saturday'].unique(), B['Consider count hodiday Saturday'].unique(), submission['Consider count hodiday Saturday'].unique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:27.578987Z","iopub.execute_input":"2025-06-17T13:45:27.579359Z","iopub.status.idle":"2025-06-17T13:45:27.605059Z","shell.execute_reply.started":"2025-06-17T13:45:27.579336Z","shell.execute_reply":"2025-06-17T13:45:27.604228Z"}},"outputs":[],"execution_count":null},{"id":"4ff6f933","cell_type":"code","source":"A['Consider count hodiday Saturday'] = A['Consider count hodiday Saturday'].astype(str).str.strip()\nA['Consider count hodiday Saturday'] = A['Consider count hodiday Saturday'].replace('', '0')\nA['Consider count hodiday Saturday'] = A['Consider count hodiday Saturday'].astype(np.int64)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:27.605886Z","iopub.execute_input":"2025-06-17T13:45:27.606276Z","iopub.status.idle":"2025-06-17T13:45:27.756818Z","shell.execute_reply.started":"2025-06-17T13:45:27.606122Z","shell.execute_reply":"2025-06-17T13:45:27.755968Z"}},"outputs":[],"execution_count":null},{"id":"89445fbd","cell_type":"markdown","source":"OTHER AREA SHIP DIV","metadata":{}},{"id":"5676e9a6","cell_type":"code","source":"A['OTHER AREA SHIP DIV'].unique(), B['OTHER AREA SHIP DIV'].unique(), submission['OTHER AREA SHIP DIV'].unique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:27.757848Z","iopub.execute_input":"2025-06-17T13:45:27.758130Z","iopub.status.idle":"2025-06-17T13:45:27.793415Z","shell.execute_reply.started":"2025-06-17T13:45:27.758103Z","shell.execute_reply":"2025-06-17T13:45:27.792726Z"}},"outputs":[],"execution_count":null},{"id":"8affc327","cell_type":"code","source":"A['OTHER AREA SHIP DIV'] = A['OTHER AREA SHIP DIV'].astype(str).str.strip()\nA['OTHER AREA SHIP DIV'] = A['OTHER AREA SHIP DIV'].replace('', np.nan)\nA['OTHER AREA SHIP DIV'] = A['OTHER AREA SHIP DIV'].astype(np.float64)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:27.794224Z","iopub.execute_input":"2025-06-17T13:45:27.794487Z","iopub.status.idle":"2025-06-17T13:45:27.932964Z","shell.execute_reply.started":"2025-06-17T13:45:27.794462Z","shell.execute_reply":"2025-06-17T13:45:27.931850Z"}},"outputs":[],"execution_count":null},{"id":"ee9def5a","cell_type":"markdown","source":"SO_DAY_OF_WEEK","metadata":{}},{"id":"d8b11318","cell_type":"code","source":"A['SO_DAY_OF_WEEK'].unique(), B['SO_DAY_OF_WEEK'].unique(), submission['SO_DAY_OF_WEEK'].unique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:27.933967Z","iopub.execute_input":"2025-06-17T13:45:27.934240Z","iopub.status.idle":"2025-06-17T13:45:27.956081Z","shell.execute_reply.started":"2025-06-17T13:45:27.934219Z","shell.execute_reply":"2025-06-17T13:45:27.954926Z"}},"outputs":[],"execution_count":null},{"id":"5c498d41","cell_type":"code","source":"submission['SO_DAY_OF_WEEK'] = submission['SO_DAY_OF_WEEK'].astype(str).str.strip()\nsubmission['SO_DAY_OF_WEEK'] = submission['SO_DAY_OF_WEEK'].astype(float).astype(np.int64)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:27.957247Z","iopub.execute_input":"2025-06-17T13:45:27.957548Z","iopub.status.idle":"2025-06-17T13:45:28.073920Z","shell.execute_reply.started":"2025-06-17T13:45:27.957521Z","shell.execute_reply":"2025-06-17T13:45:28.072986Z"}},"outputs":[],"execution_count":null},{"id":"c8353888","cell_type":"markdown","source":"REASON_CD","metadata":{}},{"id":"27cc7c20","cell_type":"code","source":"A['REASON_CD'].unique(), B['REASON_CD'].unique(), submission['REASON_CD'].unique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:28.074962Z","iopub.execute_input":"2025-06-17T13:45:28.075308Z","iopub.status.idle":"2025-06-17T13:45:28.108072Z","shell.execute_reply.started":"2025-06-17T13:45:28.075280Z","shell.execute_reply":"2025-06-17T13:45:28.107231Z"}},"outputs":[],"execution_count":null},{"id":"c27389a2","cell_type":"code","source":"A['REASON_CD'] = A['REASON_CD'].astype(str).str.strip()\nA['REASON_CD'] = A['REASON_CD'].replace('', np.nan)\nA['REASON_CD'] = A['REASON_CD'].astype(np.float64)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:28.109407Z","iopub.execute_input":"2025-06-17T13:45:28.109684Z","iopub.status.idle":"2025-06-17T13:45:28.240912Z","shell.execute_reply.started":"2025-06-17T13:45:28.109662Z","shell.execute_reply":"2025-06-17T13:45:28.239772Z"}},"outputs":[],"execution_count":null},{"id":"ff92fd97","cell_type":"markdown","source":"SO_TIME","metadata":{}},{"id":"b2e6c196","cell_type":"code","source":"A['SO_TIME'].unique(), B['SO_TIME'].unique(), submission['SO_TIME'].unique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:28.242139Z","iopub.execute_input":"2025-06-17T13:45:28.242429Z","iopub.status.idle":"2025-06-17T13:45:28.272846Z","shell.execute_reply.started":"2025-06-17T13:45:28.242399Z","shell.execute_reply":"2025-06-17T13:45:28.272090Z"}},"outputs":[],"execution_count":null},{"id":"d2b922da","cell_type":"code","source":"submission['SO_TIME'] = submission['SO_TIME'].astype(np.int64)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:28.273676Z","iopub.execute_input":"2025-06-17T13:45:28.273939Z","iopub.status.idle":"2025-06-17T13:45:28.280043Z","shell.execute_reply.started":"2025-06-17T13:45:28.273920Z","shell.execute_reply":"2025-06-17T13:45:28.279104Z"}},"outputs":[],"execution_count":null},{"id":"1c835eab","cell_type":"markdown","source":"### Merging for convenient preprocessing","metadata":{}},{"id":"8c2f772b","cell_type":"code","source":"A['source'] = np.nan\nB['source'] = np.nan\nsubmission['source'] = 'submission'\ndf = pd.concat([A, B, submission], axis=0)\ndf.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:28.281197Z","iopub.execute_input":"2025-06-17T13:45:28.281522Z","iopub.status.idle":"2025-06-17T13:45:29.921216Z","shell.execute_reply.started":"2025-06-17T13:45:28.281485Z","shell.execute_reply":"2025-06-17T13:45:29.919777Z"}},"outputs":[],"execution_count":null},{"id":"83a73551","cell_type":"markdown","source":"## Preprocessing:","metadata":{}},{"id":"c1e9ac6f","cell_type":"code","source":"identify_cols = ['ID', 'label', 'source']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:29.922284Z","iopub.execute_input":"2025-06-17T13:45:29.922526Z","iopub.status.idle":"2025-06-17T13:45:29.926927Z","shell.execute_reply.started":"2025-06-17T13:45:29.922508Z","shell.execute_reply":"2025-06-17T13:45:29.925913Z"}},"outputs":[],"execution_count":null},{"id":"28e4d2bb","cell_type":"markdown","source":"#### Duplicate cols","metadata":{}},{"id":"801bab73","cell_type":"code","source":"duplicate_cols = []\nfor col1 in df.columns:\n    for col2 in df.columns:\n        if col1 != col2 and col1 not in duplicate_cols and col2 not in duplicate_cols:\n            if df[col1].equals(df[col2]):\n                print(f'{col1} = {col2}')\n                duplicate_cols.append(col2)\n                break","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:29.935252Z","iopub.execute_input":"2025-06-17T13:45:29.935549Z","iopub.status.idle":"2025-06-17T13:45:31.366386Z","shell.execute_reply.started":"2025-06-17T13:45:29.935529Z","shell.execute_reply":"2025-06-17T13:45:31.365271Z"}},"outputs":[],"execution_count":null},{"id":"b32befb4","cell_type":"code","source":"for col in duplicate_cols:\n    df.drop(col, axis=1, inplace=True)\ndf.reset_index(drop=True, inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:31.367257Z","iopub.execute_input":"2025-06-17T13:45:31.367521Z","iopub.status.idle":"2025-06-17T13:45:32.636348Z","shell.execute_reply.started":"2025-06-17T13:45:31.367501Z","shell.execute_reply":"2025-06-17T13:45:32.635479Z"}},"outputs":[],"execution_count":null},{"id":"d066b2e1","cell_type":"markdown","source":"#### cols that are known after delay","metadata":{}},{"id":"274fdb42","cell_type":"code","source":"df.drop(['REASON_CD', 'SOUF_RCV_NO', 'QTUF_RCV_NO', 'VSD'], inplace=True, axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:32.637494Z","iopub.execute_input":"2025-06-17T13:45:32.637787Z","iopub.status.idle":"2025-06-17T13:45:32.949656Z","shell.execute_reply.started":"2025-06-17T13:45:32.637760Z","shell.execute_reply":"2025-06-17T13:45:32.948540Z"}},"outputs":[],"execution_count":null},{"id":"62498c3c","cell_type":"code","source":"df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:32.951076Z","iopub.execute_input":"2025-06-17T13:45:32.951652Z","iopub.status.idle":"2025-06-17T13:45:32.957521Z","shell.execute_reply.started":"2025-06-17T13:45:32.951621Z","shell.execute_reply":"2025-06-17T13:45:32.956562Z"}},"outputs":[],"execution_count":null},{"id":"d3807a72","cell_type":"markdown","source":"#### ID columns","metadata":{}},{"id":"cf11b97a","cell_type":"code","source":"df.columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:32.958623Z","iopub.execute_input":"2025-06-17T13:45:32.958888Z","iopub.status.idle":"2025-06-17T13:45:32.980687Z","shell.execute_reply.started":"2025-06-17T13:45:32.958862Z","shell.execute_reply":"2025-06-17T13:45:32.979622Z"}},"outputs":[],"execution_count":null},{"id":"021af6bb","cell_type":"code","source":"id_cols = ['SUBSIDIARY_CD', 'GLOBAL_NO', 'CLASSIFY_CD', 'CUST_CD',\n       'BRAND_CD', 'INNER_CD', 'SUPPLIER_CD','PRODUCT_CD', 'SHIP DECISION NO', 'Ship Mode']\nfor col in id_cols:\n    nunique = df[col].nunique()\n    percent = round(nunique * 100 / df.shape[0], 4)\n    print(f'{col}: {nunique} - {percent}%')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:32.981769Z","iopub.execute_input":"2025-06-17T13:45:32.982091Z","iopub.status.idle":"2025-06-17T13:45:34.605220Z","shell.execute_reply.started":"2025-06-17T13:45:32.982062Z","shell.execute_reply":"2025-06-17T13:45:34.604059Z"}},"outputs":[],"execution_count":null},{"id":"087ec636","cell_type":"code","source":"# column with 1 unique value\ndf.drop(['SUBSIDIARY_CD'], inplace=True, axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:34.606011Z","iopub.execute_input":"2025-06-17T13:45:34.606252Z","iopub.status.idle":"2025-06-17T13:45:34.904970Z","shell.execute_reply.started":"2025-06-17T13:45:34.606235Z","shell.execute_reply":"2025-06-17T13:45:34.903946Z"}},"outputs":[],"execution_count":null},{"id":"e9b33c27","cell_type":"code","source":"#labels column with large unique values and small frequency\ndf.drop(['GLOBAL_NO', 'PRODUCT_CD', 'SHIP DECISION NO'], axis=1, inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:34.906000Z","iopub.execute_input":"2025-06-17T13:45:34.906262Z","iopub.status.idle":"2025-06-17T13:45:35.151206Z","shell.execute_reply.started":"2025-06-17T13:45:34.906241Z","shell.execute_reply":"2025-06-17T13:45:35.150382Z"}},"outputs":[],"execution_count":null},{"id":"95b54005","cell_type":"markdown","source":"BRAND_CD","metadata":{}},{"id":"0ddb0d47","cell_type":"code","source":"df['BRAND_CD'].value_counts()* 100 / df.shape[0]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:35.152134Z","iopub.execute_input":"2025-06-17T13:45:35.152566Z","iopub.status.idle":"2025-06-17T13:45:35.288004Z","shell.execute_reply.started":"2025-06-17T13:45:35.152537Z","shell.execute_reply":"2025-06-17T13:45:35.287090Z"}},"outputs":[],"execution_count":null},{"id":"c47a1e28","cell_type":"code","source":"df['BRAND_CD_MSM1'] = df['BRAND_CD'].apply(lambda x: 1 if x == 'MSM1' else 0)\ndf.drop(['BRAND_CD'], axis=1, inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:35.288921Z","iopub.execute_input":"2025-06-17T13:45:35.289253Z","iopub.status.idle":"2025-06-17T13:45:36.165580Z","shell.execute_reply.started":"2025-06-17T13:45:35.289230Z","shell.execute_reply":"2025-06-17T13:45:36.164326Z"}},"outputs":[],"execution_count":null},{"id":"f365ac7c","cell_type":"code","source":"id_cols = list(set(id_cols) - {'BRAND_CD', 'GLOBAL_NO', 'PRODUCT_CD', 'SHIP DECISION NO', 'SUBSIDIARY_CD'})\nid_cols","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:36.166596Z","iopub.execute_input":"2025-06-17T13:45:36.166920Z","iopub.status.idle":"2025-06-17T13:45:36.173131Z","shell.execute_reply.started":"2025-06-17T13:45:36.166893Z","shell.execute_reply":"2025-06-17T13:45:36.172320Z"}},"outputs":[],"execution_count":null},{"id":"a1613615","cell_type":"code","source":"#group rare classes into 'other'\nfor col in id_cols:\n    df[col] = df[col].fillna('other')\n    df[col] = df[col].astype('object')\n    vc = df[col].value_counts()\n\n    # Tìm các nhãn xuất hiện ít hơn 1%\n    rare = set(vc[vc * 100 / len(df) <= 1].index)\n    # Gán các nhãn hiếm thành 'other'\n    mask = df[col].isin(rare)\n    df.loc[mask, col] = 'other'\n    # Chuyển đổi lại về category\n    df[col] = df[col].astype('category')\n    print(f'{col} --> {df[col].unique()}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:36.174199Z","iopub.execute_input":"2025-06-17T13:45:36.174533Z","iopub.status.idle":"2025-06-17T13:45:40.098697Z","shell.execute_reply.started":"2025-06-17T13:45:36.174495Z","shell.execute_reply":"2025-06-17T13:45:40.097554Z"}},"outputs":[],"execution_count":null},{"id":"0a57ae38","cell_type":"markdown","source":"#### DATE","metadata":{}},{"id":"fe18cb61","cell_type":"code","source":"df['Order date'].dtypes, df['SO_DAY_OF_MONTH'].dtypes, df['SO_DAY_OF_WEEK'].dtypes","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:40.099565Z","iopub.execute_input":"2025-06-17T13:45:40.099881Z","iopub.status.idle":"2025-06-17T13:45:40.107828Z","shell.execute_reply.started":"2025-06-17T13:45:40.099854Z","shell.execute_reply":"2025-06-17T13:45:40.106344Z"}},"outputs":[],"execution_count":null},{"id":"0eb04609","cell_type":"code","source":"df['SO_DAY_OF_MONTH'] = df['SO_DAY_OF_MONTH'].astype('category')\ndf['SO_DAY_OF_WEEK'] = df['SO_DAY_OF_WEEK'].astype('category')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:40.108799Z","iopub.execute_input":"2025-06-17T13:45:40.109134Z","iopub.status.idle":"2025-06-17T13:45:40.176407Z","shell.execute_reply.started":"2025-06-17T13:45:40.109107Z","shell.execute_reply":"2025-06-17T13:45:40.175256Z"}},"outputs":[],"execution_count":null},{"id":"1afe047c","cell_type":"code","source":"df['Order_month'] = df['Order date'].dt.month.astype('category')\ndf['Order_week'] = pd.cut(\n    df['SO_DAY_OF_MONTH'],\n    bins=[0, 7, 14, 21, 31],\n    labels=['1-7', '8-14', '15-21', '22-31'],\n    include_lowest=True\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:40.177503Z","iopub.execute_input":"2025-06-17T13:45:40.177842Z","iopub.status.idle":"2025-06-17T13:45:40.321089Z","shell.execute_reply.started":"2025-06-17T13:45:40.177813Z","shell.execute_reply":"2025-06-17T13:45:40.319881Z"}},"outputs":[],"execution_count":null},{"id":"8fb8d6a0","cell_type":"code","source":"df['SO_TIME'] = df['SO_TIME'].astype(str).str.zfill(6)\ndf['SO_TIME'] = pd.to_datetime(df['SO_TIME'], format='%H%M%S').dt.time\ndf['SO_TIME'].head(), df['SO_TIME'].dtype","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:40.322489Z","iopub.execute_input":"2025-06-17T13:45:40.322827Z","iopub.status.idle":"2025-06-17T13:45:42.564487Z","shell.execute_reply.started":"2025-06-17T13:45:40.322794Z","shell.execute_reply":"2025-06-17T13:45:42.563478Z"}},"outputs":[],"execution_count":null},{"id":"ed228af2","cell_type":"code","source":"df['Order_hour'] = df['SO_TIME'].apply(lambda x: int(str(x).split(':')[0]))\ndf['Order_hour'] = df['Order_hour'].astype('category')\ndf['Order_hour'] = df['SO_TIME'].apply(lambda x: int(str(x).split(':')[0]))\ndf['Order_time_slot'] = pd.cut(\n    df['Order_hour'],\n    bins=[0, 6, 12, 18, 24],\n    labels=['0-6', '6-12', '12-18', '18-24'],\n    include_lowest=True\n)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:42.565403Z","iopub.execute_input":"2025-06-17T13:45:42.565754Z","iopub.status.idle":"2025-06-17T13:45:46.404118Z","shell.execute_reply.started":"2025-06-17T13:45:42.565727Z","shell.execute_reply":"2025-06-17T13:45:46.403115Z"}},"outputs":[],"execution_count":null},{"id":"c29cfc7c","cell_type":"code","source":"df[['Order date', 'Order_month', 'SO_DAY_OF_MONTH', 'Order_week', 'SO_DAY_OF_WEEK', 'SO_TIME', 'Order_hour', 'Order_time_slot']].head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:46.405033Z","iopub.execute_input":"2025-06-17T13:45:46.405339Z","iopub.status.idle":"2025-06-17T13:45:46.485589Z","shell.execute_reply.started":"2025-06-17T13:45:46.405319Z","shell.execute_reply":"2025-06-17T13:45:46.484579Z"}},"outputs":[],"execution_count":null},{"id":"12054811","cell_type":"code","source":"df.drop(['Order date', 'Order_hour', 'SO_TIME', 'SO_DAY_OF_MONTH', 'Order_hour'], axis=1, inplace=True)\nprint(df[['Order_month', 'Order_week', 'SO_DAY_OF_WEEK', 'Order_time_slot']].dtypes)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:46.486955Z","iopub.execute_input":"2025-06-17T13:45:46.487460Z","iopub.status.idle":"2025-06-17T13:45:46.613497Z","shell.execute_reply.started":"2025-06-17T13:45:46.487419Z","shell.execute_reply":"2025-06-17T13:45:46.612623Z"}},"outputs":[],"execution_count":null},{"id":"f34f2d25","cell_type":"markdown","source":"#### NA","metadata":{}},{"id":"5727d573","cell_type":"code","source":"na_percent = df.drop(columns=identify_cols).isna().mean()\nprint(na_percent[na_percent > 0].sort_values(ascending=False))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:46.614457Z","iopub.execute_input":"2025-06-17T13:45:46.614751Z","iopub.status.idle":"2025-06-17T13:45:46.947828Z","shell.execute_reply.started":"2025-06-17T13:45:46.614729Z","shell.execute_reply":"2025-06-17T13:45:46.946599Z"}},"outputs":[],"execution_count":null},{"id":"0ad103c7","cell_type":"markdown","source":"**OTHER AREA SHIP DIV**","metadata":{}},{"id":"381821f9","cell_type":"code","source":"df['OTHER AREA SHIP DIV'] = df['OTHER AREA SHIP DIV'].fillna(0)\ndf['OTHER AREA SHIP DIV'] = df['OTHER AREA SHIP DIV'].replace('1.0', 1)\ndf['OTHER AREA SHIP DIV'] = df['OTHER AREA SHIP DIV'].astype(np.int64)\ndf['OTHER AREA SHIP DIV'].isna().sum(), df['OTHER AREA SHIP DIV'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:46.948832Z","iopub.execute_input":"2025-06-17T13:45:46.949137Z","iopub.status.idle":"2025-06-17T13:45:47.004426Z","shell.execute_reply.started":"2025-06-17T13:45:46.949109Z","shell.execute_reply":"2025-06-17T13:45:47.003507Z"}},"outputs":[],"execution_count":null},{"id":"6832e80a","cell_type":"markdown","source":"**SUPPLIER_DIV**","metadata":{}},{"id":"cba69004","cell_type":"code","source":"df['SUPPLIER_DIV'].isna().sum(), df['SUPPLIER_DIV'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:47.005324Z","iopub.execute_input":"2025-06-17T13:45:47.005566Z","iopub.status.idle":"2025-06-17T13:45:47.039353Z","shell.execute_reply.started":"2025-06-17T13:45:47.005548Z","shell.execute_reply":"2025-06-17T13:45:47.038523Z"}},"outputs":[],"execution_count":null},{"id":"c8f72bfb","cell_type":"code","source":"df['SUPPLIER_DIV'] = df['SUPPLIER_DIV'].fillna(0)\ndf['SUPPLIER_DIV'] = df['SUPPLIER_DIV'].replace('1.0', 1)\ndf['SUPPLIER_DIV'] = df['SUPPLIER_DIV'].replace('2.0', 2)\ndf['SUPPLIER_DIV'] = df['SUPPLIER_DIV'].replace('3.0', 3)\ndf['SUPPLIER_DIV'] = df['SUPPLIER_DIV'].replace('4.0', 4)\ndf['SUPPLIER_DIV'] = df['SUPPLIER_DIV'].astype(int)\ndf['SUPPLIER_DIV'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:47.040242Z","iopub.execute_input":"2025-06-17T13:45:47.040486Z","iopub.status.idle":"2025-06-17T13:45:47.106004Z","shell.execute_reply.started":"2025-06-17T13:45:47.040467Z","shell.execute_reply":"2025-06-17T13:45:47.105204Z"}},"outputs":[],"execution_count":null},{"id":"70343480","cell_type":"markdown","source":"#### DIFFERENT COLUMNS TYPE","metadata":{}},{"id":"a1132c82","cell_type":"markdown","source":"##### binary columns:","metadata":{}},{"id":"7426eb0a","cell_type":"code","source":"bin_cols = []\nfor col in df.drop(columns=identify_cols).columns:\n    if df[col].nunique() == 2:\n        print(f'{col} - {df[col].dtypes}')\n        bin_cols.append(col)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:47.107113Z","iopub.execute_input":"2025-06-17T13:45:47.107653Z","iopub.status.idle":"2025-06-17T13:45:47.591288Z","shell.execute_reply.started":"2025-06-17T13:45:47.107626Z","shell.execute_reply":"2025-06-17T13:45:47.590398Z"}},"outputs":[],"execution_count":null},{"id":"02ad9519","cell_type":"markdown","source":"##### categorical columns:","metadata":{}},{"id":"609344e8","cell_type":"code","source":"cat_cols = []\nfor col in df.columns:\n    if df[col].nunique() > 2 and df[col].nunique() <= 20:\n        print(col, '-', df[col].nunique(), '-', df[col].dtypes)\n        cat_cols.append(col)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:47.592155Z","iopub.execute_input":"2025-06-17T13:45:47.592392Z","iopub.status.idle":"2025-06-17T13:45:48.665885Z","shell.execute_reply.started":"2025-06-17T13:45:47.592374Z","shell.execute_reply":"2025-06-17T13:45:48.664943Z"}},"outputs":[],"execution_count":null},{"id":"8da46bb5","cell_type":"code","source":"cat_cols = list(set(cat_cols) - {'Consider count hodiday Saturday'})\ndf[cat_cols] = df[cat_cols].astype('category')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:48.666928Z","iopub.execute_input":"2025-06-17T13:45:48.667263Z","iopub.status.idle":"2025-06-17T13:45:48.919770Z","shell.execute_reply.started":"2025-06-17T13:45:48.667234Z","shell.execute_reply":"2025-06-17T13:45:48.918890Z"}},"outputs":[],"execution_count":null},{"id":"63140630","cell_type":"markdown","source":"##### continous columns","metadata":{}},{"id":"6af67a7d","cell_type":"code","source":"continous_cols = []\nfor col in set(df.columns) - set(cat_cols) - set(bin_cols) - set(id_cols) - set(identify_cols):\n    if df[col].dtype != 'object':\n        print(col, '-', df[col].dtype, '(', df[col].nunique(), ')')\n        continous_cols.append(col)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:48.920557Z","iopub.execute_input":"2025-06-17T13:45:48.920791Z","iopub.status.idle":"2025-06-17T13:45:49.045593Z","shell.execute_reply.started":"2025-06-17T13:45:48.920772Z","shell.execute_reply":"2025-06-17T13:45:49.044593Z"}},"outputs":[],"execution_count":null},{"id":"34e5e4da","cell_type":"code","source":"countinous_cols = set(continous_cols) - {'Consider count hodiday Saturday'}\npd.set_option('display.float_format', '{:.2f}'.format)\ndf[list(countinous_cols)].describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:49.046543Z","iopub.execute_input":"2025-06-17T13:45:49.046778Z","iopub.status.idle":"2025-06-17T13:45:49.479697Z","shell.execute_reply.started":"2025-06-17T13:45:49.046762Z","shell.execute_reply":"2025-06-17T13:45:49.478710Z"}},"outputs":[],"execution_count":null},{"id":"017b0c8a","cell_type":"code","source":"print('% PACKQTY == 0:', (df['PACK QTY'] == 0).sum()*100 / df.shape[0])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:49.480745Z","iopub.execute_input":"2025-06-17T13:45:49.481083Z","iopub.status.idle":"2025-06-17T13:45:49.490968Z","shell.execute_reply.started":"2025-06-17T13:45:49.481051Z","shell.execute_reply":"2025-06-17T13:45:49.490089Z"}},"outputs":[],"execution_count":null},{"id":"7f99acb2","cell_type":"code","source":"# Change PACK QTY to binary\ndf['PACK_QTY_0'] = df['PACK QTY'].apply(lambda x: 1 if x == 0 else 0)\ndf['PACK_QTY_0'].dtype\nbin_cols.append('PACK_QTY_0')\ndf.drop('PACK QTY', axis=1, inplace=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:49.491906Z","iopub.execute_input":"2025-06-17T13:45:49.492155Z","iopub.status.idle":"2025-06-17T13:45:50.151535Z","shell.execute_reply.started":"2025-06-17T13:45:49.492135Z","shell.execute_reply":"2025-06-17T13:45:50.150270Z"}},"outputs":[],"execution_count":null},{"id":"65c387f4","cell_type":"code","source":"cols_to_normalize = list(set(countinous_cols) - {'PACK QTY'})\n\nplt.figure(figsize=(15, 2.5))\nfor i, col in enumerate(cols_to_normalize):\n    plt.subplot(1, 5, i + 1)\n    df[col].hist(bins=50)\n    plt.title(col)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:50.152599Z","iopub.execute_input":"2025-06-17T13:45:50.152825Z","iopub.status.idle":"2025-06-17T13:45:51.444183Z","shell.execute_reply.started":"2025-06-17T13:45:50.152807Z","shell.execute_reply":"2025-06-17T13:45:51.443265Z"}},"outputs":[],"execution_count":null},{"id":"74433b20","cell_type":"markdown","source":"**--> apply log1p inside the modeling so no leakage**","metadata":{}},{"id":"586102d0","cell_type":"markdown","source":"### final","metadata":{}},{"id":"e5027da2","cell_type":"code","source":"df.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:51.445071Z","iopub.execute_input":"2025-06-17T13:45:51.445360Z","iopub.status.idle":"2025-06-17T13:45:51.593449Z","shell.execute_reply.started":"2025-06-17T13:45:51.445328Z","shell.execute_reply":"2025-06-17T13:45:51.592579Z"}},"outputs":[],"execution_count":null},{"id":"d276b52b","cell_type":"code","source":"# one-hot encoding for Random Forest\n# df = pd.get_dummies(df, columns=cat_cols, drop_first=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:51.594348Z","iopub.execute_input":"2025-06-17T13:45:51.594653Z","iopub.status.idle":"2025-06-17T13:45:51.599037Z","shell.execute_reply.started":"2025-06-17T13:45:51.594632Z","shell.execute_reply":"2025-06-17T13:45:51.598317Z"}},"outputs":[],"execution_count":null},{"id":"7799804a","cell_type":"code","source":"submission_df = df[df['source'] == 'submission'].drop(columns={'source', 'label'})\nsubmission_df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:51.599881Z","iopub.execute_input":"2025-06-17T13:45:51.600131Z","iopub.status.idle":"2025-06-17T13:45:51.689062Z","shell.execute_reply.started":"2025-06-17T13:45:51.600108Z","shell.execute_reply":"2025-06-17T13:45:51.688318Z"}},"outputs":[],"execution_count":null},{"id":"6ff9a30c","cell_type":"code","source":"df = df[df['source'] != 'submission']\ndf.drop(columns={'source', 'ID'}, inplace=True)\ndf.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:51.689906Z","iopub.execute_input":"2025-06-17T13:45:51.690225Z","iopub.status.idle":"2025-06-17T13:45:52.059900Z","shell.execute_reply.started":"2025-06-17T13:45:51.690203Z","shell.execute_reply":"2025-06-17T13:45:52.058833Z"}},"outputs":[],"execution_count":null},{"id":"a5c8c6f3","cell_type":"markdown","source":"## MODELING:","metadata":{}},{"id":"442995c3","cell_type":"code","source":"import lightgbm as lgb\nfrom sklearn.metrics import classification_report, confusion_matrix\nfrom sklearn.model_selection import StratifiedKFold\nfrom sklearn.ensemble import RandomForestClassifier","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:52.060717Z","iopub.execute_input":"2025-06-17T13:45:52.060982Z","iopub.status.idle":"2025-06-17T13:45:59.795840Z","shell.execute_reply.started":"2025-06-17T13:45:52.060954Z","shell.execute_reply":"2025-06-17T13:45:59.794794Z"}},"outputs":[],"execution_count":null},{"id":"3261140a","cell_type":"code","source":"X = df.drop(columns='label')\ny = df['label']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:59.796861Z","iopub.execute_input":"2025-06-17T13:45:59.797764Z","iopub.status.idle":"2025-06-17T13:45:59.862794Z","shell.execute_reply.started":"2025-06-17T13:45:59.797730Z","shell.execute_reply":"2025-06-17T13:45:59.861477Z"}},"outputs":[],"execution_count":null},{"id":"cd35698e","cell_type":"code","source":"submission_ids = submission_df['ID'].copy()\nX_test = submission_df.drop(columns=['ID'])\nX_test[cols_to_normalize] = np.log1p(X_test[cols_to_normalize])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:59.864105Z","iopub.execute_input":"2025-06-17T13:45:59.864518Z","iopub.status.idle":"2025-06-17T13:45:59.908357Z","shell.execute_reply.started":"2025-06-17T13:45:59.864486Z","shell.execute_reply":"2025-06-17T13:45:59.907447Z"}},"outputs":[],"execution_count":null},{"id":"c9e9007b","cell_type":"code","source":"oof_preds = np.zeros(len(X))\ntest_preds = np.zeros(len(X_test))\nskf = StratifiedKFold(n_splits=3, shuffle=True, random_state=42)\nthreshold = 0.2","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:59.909345Z","iopub.execute_input":"2025-06-17T13:45:59.909643Z","iopub.status.idle":"2025-06-17T13:45:59.916060Z","shell.execute_reply.started":"2025-06-17T13:45:59.909621Z","shell.execute_reply":"2025-06-17T13:45:59.914980Z"}},"outputs":[],"execution_count":null},{"id":"f4e09bcb","cell_type":"code","source":"def evaluate_model(y_test, y_pred):\n    print(classification_report(y_test, y_pred))\n    cm = confusion_matrix(y_test, y_pred)\n    print(\"Confusion Matrix:\")\n    print(f\"        Pred 0   Pred 1\")\n    print(f\"True 0{cm[0,0]:8} {cm[0,1]:8}\")\n    print(f\"True 1{cm[1,0]:8} {cm[1,1]:8}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:59.917288Z","iopub.execute_input":"2025-06-17T13:45:59.917660Z","iopub.status.idle":"2025-06-17T13:45:59.943383Z","shell.execute_reply.started":"2025-06-17T13:45:59.917629Z","shell.execute_reply":"2025-06-17T13:45:59.942395Z"}},"outputs":[],"execution_count":null},{"id":"25007b57-ccce-43ba-911a-a3cfdf5f9409","cell_type":"markdown","source":"### LightGBM","metadata":{}},{"id":"3f56f2fa","cell_type":"code","source":"print(\"===== K-Fold Validation =====\")\n\nfor fold, (train_idx, val_idx) in enumerate(skf.split(X, y), 1):\n    print(f\"\\nFold {fold} training...\")\n\n    X_train, y_train = X.iloc[train_idx].copy(), y.iloc[train_idx]\n    X_val, y_val = X.iloc[val_idx].copy(), y.iloc[val_idx]\n\n    X_train[cols_to_normalize] = np.log1p(X_train[cols_to_normalize])\n    X_val[cols_to_normalize] = np.log1p(X_val[cols_to_normalize])\n\n    train_data = lgb.Dataset(X_train, label=y_train)\n    val_data = lgb.Dataset(X_val, label=y_val)\n\n    params = {\n        'objective': 'binary',\n        'metric': ['binary_logloss', 'auc'],\n        'boosting_type': 'gbdt',\n        'learning_rate': 0.05,\n        'verbosity': -1,\n        'num_threads': 4,\n    }\n\n    model = lgb.train(\n        params,\n        train_data,\n        valid_sets=[val_data],\n        num_boost_round=1000,\n        callbacks=[lgb.early_stopping(50)],\n    )\n\n    val_preds = model.predict(X_val, num_iteration=model.best_iteration)\n    oof_preds[val_idx] = val_preds\n    test_preds += model.predict(X_test, num_iteration=model.best_iteration)\n    evaluate_model(y_val, (val_preds > threshold).astype(int))\n\n# Evaluate OOF\nprint(\"\\n==> Final OOF Evaluation:\")\nevaluate_model(y, (oof_preds > threshold).astype(int))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:45:59.945126Z","iopub.execute_input":"2025-06-17T13:45:59.945414Z","iopub.status.idle":"2025-06-17T13:50:46.796186Z","shell.execute_reply.started":"2025-06-17T13:45:59.945393Z","shell.execute_reply":"2025-06-17T13:50:46.795318Z"}},"outputs":[],"execution_count":null},{"id":"8fe8fd52","cell_type":"code","source":"submission = pd.DataFrame({\n    'ID': submission_ids,\n    'label': test_preds\n})\nsubmission['label'] = (submission['label'] > threshold).astype(int)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:50:46.797337Z","iopub.execute_input":"2025-06-17T13:50:46.797596Z","iopub.status.idle":"2025-06-17T13:50:46.805000Z","shell.execute_reply.started":"2025-06-17T13:50:46.797574Z","shell.execute_reply":"2025-06-17T13:50:46.804166Z"}},"outputs":[],"execution_count":null},{"id":"a401ebae","cell_type":"code","source":"submission.to_csv('submission_LGBM.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T13:50:46.806146Z","iopub.execute_input":"2025-06-17T13:50:46.806545Z","iopub.status.idle":"2025-06-17T13:50:46.982665Z","shell.execute_reply.started":"2025-06-17T13:50:46.806516Z","shell.execute_reply":"2025-06-17T13:50:46.981691Z"}},"outputs":[],"execution_count":null},{"id":"a71e6041","cell_type":"markdown","source":"## Random Forest","metadata":{}},{"id":"1593599e","cell_type":"code","source":"print(\"===== K-Fold Random Forest Validation =====\")\n\nfor fold, (train_idx, val_idx) in enumerate(skf.split(X, y), 1):\n    print(f\"\\nFold {fold} training...\")\n\n    X_train, y_train = X.iloc[train_idx].copy(), y.iloc[train_idx]\n    X_val, y_val = X.iloc[val_idx].copy(), y.iloc[val_idx]\n\n    # Normalize selected columns\n    X_train[cols_to_normalize] = np.log1p(X_train[cols_to_normalize])\n    X_val[cols_to_normalize] = np.log1p(X_val[cols_to_normalize])\n\n    model = RandomForestClassifier(n_estimators=100, random_state=42)\n    model.fit(X_train, y_train)\n    val_pred = model.predict_proba(X_val)[:, 1]\n    test_pred = model.predict_proba(X_test)[:, 1]\n    evaluate_model(y_val, (val_pred > threshold).astype(int))\n\n    oof_preds[val_idx] = val_pred\n    test_preds += test_pred\n\n# Average test predictions\ntest_preds /= skf.get_n_splits()\n\n# Final OOF evaluation\nprint(\"\\n==> Final OOF Evaluation:\")\nevaluate_model(y, (oof_preds > threshold).astype(int))","metadata":{},"outputs":[],"execution_count":null},{"id":"90c50cdf","cell_type":"code","source":"submission = pd.DataFrame({\n    'ID': submission_ids,\n    'label': test_preds\n})\nsubmission['label'] = (submission['label'] > threshold).astype(int)\nprint(submission['label'].value_counts())","metadata":{},"outputs":[],"execution_count":null},{"id":"3ae35945","cell_type":"code","source":"submission.to_csv('submission_RF.csv', index=False)","metadata":{},"outputs":[],"execution_count":null},{"id":"ba33ca7c-66c4-4409-9d03-0f05fedd5711","cell_type":"markdown","source":"## submission's score: \n* Random Forest: 0.6346\n* LightGBM: 0.6263 ","metadata":{}}]}