{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","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":"nvidiaTeslaT4","dataSources":[{"sourceId":103436,"databundleVersionId":12476472,"sourceType":"competition"}],"dockerImageVersionId":31041,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:05:21.138923Z","iopub.execute_input":"2025-06-17T17:05:21.139144Z","iopub.status.idle":"2025-06-17T17:05:21.694914Z","shell.execute_reply.started":"2025-06-17T17:05:21.139123Z","shell.execute_reply":"2025-06-17T17:05:21.694251Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:05:21.695598Z","iopub.execute_input":"2025-06-17T17:05:21.695955Z","iopub.status.idle":"2025-06-17T17:05:21.699924Z","shell.execute_reply.started":"2025-06-17T17:05:21.695922Z","shell.execute_reply":"2025-06-17T17:05:21.699013Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_delay_4_6 = pd.read_csv(\"/kaggle/input/ds-108-p-21-assigment-06/delay_4_6_CONDITION_PRODUCT_SUPPLIER.csv\")\ndf_delay_7_9 = pd.read_csv(\"/kaggle/input/ds-108-p-21-assigment-06/delay_7_9_CONDITION_PRODUCT_SUPPLIER.csv\")\ndf_not_delay_4_6 = pd.read_csv(\"/kaggle/input/ds-108-p-21-assigment-06/not_delay_4_6_CONDITION_PRODUCT_SUPPLIER.csv\")\ndf_not_delay_7_9 = pd.read_csv(\"/kaggle/input/ds-108-p-21-assigment-06/not_delay_7_9_CONDITION_PRODUCT_SUPPLIER.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:05:21.701594Z","iopub.execute_input":"2025-06-17T17:05:21.701854Z","iopub.status.idle":"2025-06-17T17:05:31.610204Z","shell.execute_reply.started":"2025-06-17T17:05:21.701833Z","shell.execute_reply":"2025-06-17T17:05:31.609669Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.concat([df_delay_4_6, df_delay_7_9, df_not_delay_4_6, df_not_delay_7_9], ignore_index=True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:05:31.610815Z","iopub.execute_input":"2025-06-17T17:05:31.611008Z","iopub.status.idle":"2025-06-17T17:05:32.099205Z","shell.execute_reply.started":"2025-06-17T17:05:31.610993Z","shell.execute_reply":"2025-06-17T17:05:32.098665Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:05:32.099834Z","iopub.execute_input":"2025-06-17T17:05:32.100014Z","iopub.status.idle":"2025-06-17T17:05:32.105221Z","shell.execute_reply.started":"2025-06-17T17:05:32.100000Z","shell.execute_reply":"2025-06-17T17:05:32.104674Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:05:32.105806Z","iopub.execute_input":"2025-06-17T17:05:32.105967Z","iopub.status.idle":"2025-06-17T17:05:33.207165Z","shell.execute_reply.started":"2025-06-17T17:05:32.105955Z","shell.execute_reply":"2025-06-17T17:05:33.206406Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_example = pd.read_csv(\"/kaggle/input/ds-108-p-21-assigment-06/PILOT_10.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:05:33.208065Z","iopub.execute_input":"2025-06-17T17:05:33.208321Z","iopub.status.idle":"2025-06-17T17:05:33.799419Z","shell.execute_reply.started":"2025-06-17T17:05:33.208296Z","shell.execute_reply":"2025-06-17T17:05:33.798835Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"col = []\nfor i in df.columns:\n    if i not in df_example.columns:\n        print(i)\n        col.append(i)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:05:33.800205Z","iopub.execute_input":"2025-06-17T17:05:33.800846Z","iopub.status.idle":"2025-06-17T17:05:33.805079Z","shell.execute_reply.started":"2025-06-17T17:05:33.800820Z","shell.execute_reply":"2025-06-17T17:05:33.804363Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"col.remove('label')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:05:33.807262Z","iopub.execute_input":"2025-06-17T17:05:33.807451Z","iopub.status.idle":"2025-06-17T17:05:33.819720Z","shell.execute_reply.started":"2025-06-17T17:05:33.807436Z","shell.execute_reply":"2025-06-17T17:05:33.819116Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = df.drop(labels=col, axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:05:33.820436Z","iopub.execute_input":"2025-06-17T17:05:33.820694Z","iopub.status.idle":"2025-06-17T17:05:34.230204Z","shell.execute_reply.started":"2025-06-17T17:05:33.820674Z","shell.execute_reply":"2025-06-17T17:05:34.229665Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:05:34.230946Z","iopub.execute_input":"2025-06-17T17:05:34.231176Z","iopub.status.idle":"2025-06-17T17:05:34.236235Z","shell.execute_reply.started":"2025-06-17T17:05:34.231159Z","shell.execute_reply":"2025-06-17T17:05:34.235680Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def parse_date(s):\n    try:\n        return pd.to_datetime(s, format='%Y-%m-%d %H:%M:%S')\n    except:\n        try:\n            return pd.to_datetime(s, format='%Y-%m-%d')\n        except:\n            return pd.NaT\n\ndf['Order date'] = df['Order date'].apply(parse_date)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:05:34.236910Z","iopub.execute_input":"2025-06-17T17:05:34.237118Z","iopub.status.idle":"2025-06-17T17:08:06.171878Z","shell.execute_reply.started":"2025-06-17T17:05:34.237095Z","shell.execute_reply":"2025-06-17T17:08:06.171098Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import datetime as dt\ndf['year'] = df['Order date'].dt.year\ndf['month'] = df['Order date'].dt.month\ndf['day'] = df['Order date'].dt.day\ndf['weekday'] = df['Order date'].dt.weekday\ndf['hour'] = df['Order date'].dt.hour\ndf['minute'] = df['Order date'].dt.minute","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:08:06.172741Z","iopub.execute_input":"2025-06-17T17:08:06.172987Z","iopub.status.idle":"2025-06-17T17:08:06.479775Z","shell.execute_reply.started":"2025-06-17T17:08:06.172964Z","shell.execute_reply":"2025-06-17T17:08:06.479186Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['VSD'] = df['VSD'].apply(parse_date)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:08:06.480792Z","iopub.execute_input":"2025-06-17T17:08:06.481081Z","iopub.status.idle":"2025-06-17T17:10:39.920796Z","shell.execute_reply.started":"2025-06-17T17:08:06.481058Z","shell.execute_reply":"2025-06-17T17:10:39.920020Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import datetime as dt\ndf['year_vsd'] = df['VSD'].dt.year\ndf['month_vsd'] = df['VSD'].dt.month\ndf['day_vsd'] = df['VSD'].dt.day\ndf['weekday_vsd'] = df['VSD'].dt.weekday\ndf['hour_vsd'] = df['VSD'].dt.hour\ndf['minute_vsd'] = df['VSD'].dt.minute","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:10:39.921664Z","iopub.execute_input":"2025-06-17T17:10:39.921936Z","iopub.status.idle":"2025-06-17T17:10:40.245029Z","shell.execute_reply.started":"2025-06-17T17:10:39.921912Z","shell.execute_reply":"2025-06-17T17:10:40.244483Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = df.drop(['Order date', 'VSD'], axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:10:40.245928Z","iopub.execute_input":"2025-06-17T17:10:40.246148Z","iopub.status.idle":"2025-06-17T17:10:40.566592Z","shell.execute_reply.started":"2025-06-17T17:10:40.246131Z","shell.execute_reply":"2025-06-17T17:10:40.566061Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:10:40.567224Z","iopub.execute_input":"2025-06-17T17:10:40.567428Z","iopub.status.idle":"2025-06-17T17:10:40.572184Z","shell.execute_reply.started":"2025-06-17T17:10:40.567405Z","shell.execute_reply":"2025-06-17T17:10:40.571554Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"'year', 'hour', 'minute', 'hour_vsd', 'minute_vsd'","metadata":{}},{"cell_type":"markdown","source":"SUBSIDIARY_CD', 'GLOBAL_NO', 'CLASSIFY_CD', 'CUST_CD', 'BRAND_CD',\n       'INNER_CD', 'SUPPLIER_CD', 'Stock class', 'OTHER AREA SHIP DIV', 'PACKING RANK', PRODUCT_CD,'LOGICAL PLANT', 'DELI_DIV', 'Ship Mode','SHIP DECISION NO','SUPPLIER_DIV', 'REASON_CD', 'SOUF_RCV_NO','QTUF_RCV_NO'","metadata":{}},{"cell_type":"code","source":"# Giả sử df là DataFrame của bạn\nnan_columns = df.columns[df.isna().any()].tolist()\n\nprint(\"Các cột chứa giá trị NaN:\")\nprint(nan_columns)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:10:40.572910Z","iopub.execute_input":"2025-06-17T17:10:40.573103Z","iopub.status.idle":"2025-06-17T17:10:41.331333Z","shell.execute_reply.started":"2025-06-17T17:10:40.573090Z","shell.execute_reply":"2025-06-17T17:10:41.330668Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['QTUF_RCV_NO'] = df['QTUF_RCV_NO'].fillna(0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:10:41.332010Z","iopub.execute_input":"2025-06-17T17:10:41.332202Z","iopub.status.idle":"2025-06-17T17:10:41.344991Z","shell.execute_reply.started":"2025-06-17T17:10:41.332187Z","shell.execute_reply":"2025-06-17T17:10:41.344459Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['SOUF_RCV_NO'].unique()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:10:41.345711Z","iopub.execute_input":"2025-06-17T17:10:41.345936Z","iopub.status.idle":"2025-06-17T17:10:41.381402Z","shell.execute_reply.started":"2025-06-17T17:10:41.345921Z","shell.execute_reply":"2025-06-17T17:10:41.380811Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['SOUF_RCV_NO'] = df['SOUF_RCV_NO'].fillna(0)\ndf['SOUF_RCV_NO'] = df['SOUF_RCV_NO'].apply(lambda x: pd.to_numeric(x, errors='ignore'))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:10:41.382099Z","iopub.execute_input":"2025-06-17T17:10:41.382324Z","iopub.status.idle":"2025-06-17T17:10:51.267747Z","shell.execute_reply.started":"2025-06-17T17:10:41.382308Z","shell.execute_reply":"2025-06-17T17:10:51.266999Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['REASON_CD'] = df['REASON_CD'].replace(['   ', np.nan], 0)\ndf['REASON_CD'] = df['REASON_CD'].astype(int)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:10:51.268654Z","iopub.execute_input":"2025-06-17T17:10:51.268896Z","iopub.status.idle":"2025-06-17T17:10:51.462014Z","shell.execute_reply.started":"2025-06-17T17:10:51.268875Z","shell.execute_reply":"2025-06-17T17:10:51.461214Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['SUPPLIER_DIV'] = df['SUPPLIER_DIV'].fillna(0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:10:51.462884Z","iopub.execute_input":"2025-06-17T17:10:51.463186Z","iopub.status.idle":"2025-06-17T17:10:51.477571Z","shell.execute_reply.started":"2025-06-17T17:10:51.463145Z","shell.execute_reply":"2025-06-17T17:10:51.476961Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['SHIP DECISION NO'] = df['SHIP DECISION NO'].fillna(0)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:10:51.478352Z","iopub.execute_input":"2025-06-17T17:10:51.478575Z","iopub.status.idle":"2025-06-17T17:10:51.493715Z","shell.execute_reply.started":"2025-06-17T17:10:51.478556Z","shell.execute_reply":"2025-06-17T17:10:51.493144Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['Ship Mode'] = df['Ship Mode'].fillna('0')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:10:51.494439Z","iopub.execute_input":"2025-06-17T17:10:51.494680Z","iopub.status.idle":"2025-06-17T17:10:51.613974Z","shell.execute_reply.started":"2025-06-17T17:10:51.494660Z","shell.execute_reply":"2025-06-17T17:10:51.613197Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['Consider count hodiday Saturday'] = df['Consider count hodiday Saturday'].replace(' ', 0)\ndf['Consider count hodiday Saturday'] = df['Consider count hodiday Saturday'].astype(int)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:10:51.614826Z","iopub.execute_input":"2025-06-17T17:10:51.615055Z","iopub.status.idle":"2025-06-17T17:10:51.804799Z","shell.execute_reply.started":"2025-06-17T17:10:51.615031Z","shell.execute_reply":"2025-06-17T17:10:51.804140Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['OTHER AREA SHIP DIV'] = df['OTHER AREA SHIP DIV'].replace([' ', '1', np.nan], [0, 1, 0])\ndf['OTHER AREA SHIP DIV'] = df['OTHER AREA SHIP DIV'].astype(int)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:10:51.808874Z","iopub.execute_input":"2025-06-17T17:10:51.809090Z","iopub.status.idle":"2025-06-17T17:10:52.098562Z","shell.execute_reply.started":"2025-06-17T17:10:51.809075Z","shell.execute_reply":"2025-06-17T17:10:52.097644Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = df.drop(['year', 'hour', 'minute', 'hour_vsd', 'minute_vsd'], axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:10:52.100152Z","iopub.execute_input":"2025-06-17T17:10:52.100367Z","iopub.status.idle":"2025-06-17T17:10:52.350346Z","shell.execute_reply.started":"2025-06-17T17:10:52.100350Z","shell.execute_reply":"2025-06-17T17:10:52.349666Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"label_col = ['SUBSIDIARY_CD', 'GLOBAL_NO', 'CLASSIFY_CD', 'CUST_CD', 'BRAND_CD',\n       'INNER_CD', 'SUPPLIER_CD', 'Stock class', 'OTHER AREA SHIP DIV', 'PACKING RANK', 'PRODUCT_CD','LOGICAL PLANT', 'DELI_DIV', 'Ship Mode','SHIP DECISION NO','SUPPLIER_DIV', 'REASON_CD', 'SOUF_RCV_NO','QTUF_RCV_NO']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:10:52.351123Z","iopub.execute_input":"2025-06-17T17:10:52.351358Z","iopub.status.idle":"2025-06-17T17:10:52.355680Z","shell.execute_reply.started":"2025-06-17T17:10:52.351337Z","shell.execute_reply":"2025-06-17T17:10:52.354956Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\n\ndf_v2 = df.copy()\n# Giả sử df là DataFrame và 'label' là biến mục tiêu\nle = LabelEncoder()\n\n# Lặp qua các cột dạng object và label encode\nfor col in label_col:\n    df_v2[col] = le.fit_transform(df_v2[col].astype(str))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:10:52.356494Z","iopub.execute_input":"2025-06-17T17:10:52.356776Z","iopub.status.idle":"2025-06-17T17:11:13.100100Z","shell.execute_reply.started":"2025-06-17T17:10:52.356757Z","shell.execute_reply":"2025-06-17T17:11:13.099307Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"correlations = df_v2.corr(numeric_only=True)['label'].drop('label')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:11:13.101001Z","iopub.execute_input":"2025-06-17T17:11:13.101322Z","iopub.status.idle":"2025-06-17T17:11:19.481586Z","shell.execute_reply.started":"2025-06-17T17:11:13.101305Z","shell.execute_reply":"2025-06-17T17:11:19.481038Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import seaborn as sns\nimport matplotlib.pyplot as plt\n\n# Giả sử df đã Label Encode, và 'label' là biến mục tiêu\n\n# Tính hệ số tương quan\n# Sắp xếp theo giá trị tuyệt đối giảm dần\ncorrs_sorted = correlations.reindex(correlations.abs().sort_values(ascending=False).index)\n\n# Vẽ biểu đồ cột ngang\nplt.figure(figsize=(10, 6))\nsns.barplot(x=corrs_sorted.values, y=corrs_sorted.index, palette='viridis')\n\n# Ghi nhãn\nplt.title('Độ tương quan giữa các cột và biến label')\nplt.xlabel('Hệ số tương quan')\nplt.ylabel('Tên cột')\nplt.tight_layout()\nplt.show()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:11:19.482400Z","iopub.execute_input":"2025-06-17T17:11:19.482614Z","iopub.status.idle":"2025-06-17T17:11:20.296598Z","shell.execute_reply.started":"2025-06-17T17:11:19.482597Z","shell.execute_reply":"2025-06-17T17:11:20.295978Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"top_20_cols = correlations.abs().sort_values(ascending=False).head(20).index.tolist()\n\nprint(\"20 cột có tương quan cao nhất với label:\")\nprint(top_20_cols)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:11:20.297396Z","iopub.execute_input":"2025-06-17T17:11:20.298111Z","iopub.status.idle":"2025-06-17T17:11:20.303058Z","shell.execute_reply.started":"2025-06-17T17:11:20.298085Z","shell.execute_reply":"2025-06-17T17:11:20.302230Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# from sklearn.preprocessing import LabelEncoder","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:11:20.303788Z","iopub.execute_input":"2025-06-17T17:11:20.304020Z","iopub.status.idle":"2025-06-17T17:11:20.317047Z","shell.execute_reply.started":"2025-06-17T17:11:20.304000Z","shell.execute_reply":"2025-06-17T17:11:20.316364Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# le = LabelEncoder()\n\n# # Lặp qua các cột dạng object và label encode\n# for col in label_col:\n#     df[col] = le.fit_transform(df[col].astype(str))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:11:20.317691Z","iopub.execute_input":"2025-06-17T17:11:20.317906Z","iopub.status.idle":"2025-06-17T17:11:20.328183Z","shell.execute_reply.started":"2025-06-17T17:11:20.317892Z","shell.execute_reply":"2025-06-17T17:11:20.327466Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.preprocessing import OrdinalEncoder\n\n# Khởi tạo encoder với cấu hình cho unseen labels\nencoder = OrdinalEncoder(handle_unknown='use_encoded_value', unknown_value=-1)\n\n# Áp dụng cho các cột cần label encode\ndf[label_col] = encoder.fit_transform(df[label_col].astype(str))\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:11:20.329043Z","iopub.execute_input":"2025-06-17T17:11:20.329306Z","iopub.status.idle":"2025-06-17T17:11:46.841355Z","shell.execute_reply.started":"2025-06-17T17:11:20.329290Z","shell.execute_reply":"2025-06-17T17:11:46.840545Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X = df[top_20_cols]\ny = df['label']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:11:46.842204Z","iopub.execute_input":"2025-06-17T17:11:46.842827Z","iopub.status.idle":"2025-06-17T17:11:46.924639Z","shell.execute_reply.started":"2025-06-17T17:11:46.842801Z","shell.execute_reply":"2025-06-17T17:11:46.924144Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# from sklearn.model_selection import train_test_split\n# from sklearn.metrics import classification_report, accuracy_score\n# from sklearn.ensemble import RandomForestClassifier\n# from sklearn.linear_model import LogisticRegression\n# from sklearn.tree import DecisionTreeClassifier\n# from xgboost import XGBClassifier\n# from lightgbm import LGBMClassifier\n# from catboost import CatBoostClassifier\n\n# # Giả sử bạn đã có X và y (dữ liệu đã xử lý sẵn)\n# X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n\n# # Danh sách các mô hình phân loại\n# models = {\n#     \"Logistic Regression\": LogisticRegression(max_iter=1000),\n#     \"Random Forest\": RandomForestClassifier(),\n#     \"Decision Tree\": DecisionTreeClassifier(),\n#     \"XGBoost\": XGBClassifier(use_label_encoder=False, eval_metric='logloss'),\n# }\n\n# results = []\n\n# # Huấn luyện và đánh giá các mô hình\n# for name, model in models.items():\n#     print(f\"\\n🔍 Đang chạy mô hình: {name}\")\n    \n#     model.fit(X_train, y_train)\n#     y_pred = model.predict(X_test)\n    \n#     # Đánh giá mô hình\n#     accuracy = accuracy_score(y_test, y_pred)\n#     print(f\"✅ Accuracy: {accuracy:.4f}\")\n#     print(\"📊 Classification Report:\")\n#     print(classification_report(y_test, y_pred))\n    \n#     results.append({\n#         \"model\": name,\n#         \"accuracy\": accuracy\n#     })\n\n# # Tổng hợp kết quả\n# print(\"\\n📈 Tổng hợp kết quả:\")\n# final_df = pd.DataFrame(results).sort_values(by='accuracy', ascending=False)\n# print(final_df)\n# from sklearn.model_selection import train_test_split\n# from sklearn.metrics import classification_report, accuracy_score\n# from sklearn.ensemble import RandomForestClassifier\n# from sklearn.linear_model import LogisticRegression\n# from sklearn.tree import DecisionTreeClassifier\n# from xgboost import XGBClassifier\n# from lightgbm import LGBMClassifier\n# from catboost import CatBoostClassifier\n\n# # Giả sử bạn đã có X và y (dữ liệu đã xử lý sẵn)\n# X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n\n# # Danh sách các mô hình phân loại\n# models = {\n#     \"Logistic Regression\": LogisticRegression(max_iter=1000),\n#     \"Random Forest\": RandomForestClassifier(),\n#     \"Decision Tree\": DecisionTreeClassifier(),\n#     \"XGBoost\": XGBClassifier(use_label_encoder=False, eval_metric='logloss'),\n#     \"LightGBM\": LGBMClassifier(),\n#     \"CatBoost\": CatBoostClassifier(verbose=0)\n# }\n\n# results = []\n\n# # Huấn luyện và đánh giá các mô hình\n# for name, model in models.items():\n#     print(f\"\\n🔍 Đang chạy mô hình: {name}\")\n    \n#     model.fit(X_train, y_train)\n#     y_pred = model.predict(X_test)\n    \n#     # Đánh giá mô hình\n#     accuracy = accuracy_score(y_test, y_pred)\n#     print(f\"✅ Accuracy: {accuracy:.4f}\")\n#     print(\"📊 Classification Report:\")\n#     print(classification_report(y_test, y_pred))\n    \n#     results.append({\n#         \"model\": name,\n#         \"accuracy\": accuracy\n#     })\n\n# # Tổng hợp kết quả\n# print(\"\\n📈 Tổng hợp kết quả:\")\n# final_df = pd.DataFrame(results).sort_values(by='accuracy', ascending=False)\n# print(final_df)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:11:46.925314Z","iopub.execute_input":"2025-06-17T17:11:46.925576Z","iopub.status.idle":"2025-06-17T17:11:46.930358Z","shell.execute_reply.started":"2025-06-17T17:11:46.925547Z","shell.execute_reply":"2025-06-17T17:11:46.929664Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# from sklearn.ensemble import RandomForestClassifier\n\n# # Tạo mô hình với các tham số đã chỉ định\n# rf = RandomForestClassifier(\n#     n_estimators=200,    # Số lượng cây\n#     max_depth=10,        # Độ sâu tối đa của mỗi cây\n#     min_samples_split=2, # Số mẫu tối thiểu để chia nút\n#     min_samples_leaf=1,  # Số mẫu tối thiểu ở lá\n#     max_features='sqrt', # Số lượng tính năng xem xét khi chia nút\n#     random_state=42,     # Đảm bảo kết quả có thể tái lập\n#     bootstrap=True       # Sử dụng bootstrap sampling\n# )\n\n# # Huấn luyện mô hình\n# rf.fit(X, y)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:11:46.931234Z","iopub.execute_input":"2025-06-17T17:11:46.931452Z","iopub.status.idle":"2025-06-17T17:11:46.946840Z","shell.execute_reply.started":"2025-06-17T17:11:46.931429Z","shell.execute_reply":"2025-06-17T17:11:46.946234Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import xgboost as xgb\nparams = {\n    'booster': 'gbtree',\n    'learning_rate': 0.1,\n    'n_estimators': 100,\n    'max_depth': 6,\n    'subsample': 0.8,\n    'colsample_bytree': 0.8,\n    'gamma': 0.1,\n    'lambda': 1,\n    'alpha': 0.5,\n    'scale_pos_weight': 1\n}\n\n# Khởi tạo mô hình XGBoost\nmodel_xgb = xgb.XGBClassifier(**params)\nmodel_xgb.fit(X, y)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:13:56.348865Z","iopub.execute_input":"2025-06-17T17:13:56.349115Z","iopub.status.idle":"2025-06-17T17:14:05.237916Z","shell.execute_reply.started":"2025-06-17T17:13:56.349099Z","shell.execute_reply":"2025-06-17T17:14:05.237320Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_example.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:14:10.156864Z","iopub.execute_input":"2025-06-17T17:14:10.157249Z","iopub.status.idle":"2025-06-17T17:14:10.228365Z","shell.execute_reply.started":"2025-06-17T17:14:10.157225Z","shell.execute_reply":"2025-06-17T17:14:10.227776Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def preprocess(df):\n    df['Order date'] = df['Order date'].apply(parse_date)\n    df['year'] = df['Order date'].dt.year\n    df['month'] = df['Order date'].dt.month\n    df['day'] = df['Order date'].dt.day\n    df['weekday'] = df['Order date'].dt.weekday\n    df['hour'] = df['Order date'].dt.hour\n    df['minute'] = df['Order date'].dt.minute\n    df['VSD'] = df['VSD'].apply(parse_date)\n    df['year_vsd'] = df['VSD'].dt.year\n    df['month_vsd'] = df['VSD'].dt.month\n    df['day_vsd'] = df['VSD'].dt.day\n    df['weekday_vsd'] = df['VSD'].dt.weekday\n    df['hour_vsd'] = df['VSD'].dt.hour\n    df['minute_vsd'] = df['VSD'].dt.minute\n    df = df.drop(['Order date', 'VSD'], axis=1)\n    df['QTUF_RCV_NO'] = df['QTUF_RCV_NO'].fillna(0)\n    df['SOUF_RCV_NO'] = df['SOUF_RCV_NO'].fillna(0)\n    df['SOUF_RCV_NO'] = df['SOUF_RCV_NO'].apply(lambda x: pd.to_numeric(x, errors='ignore'))\n    df['REASON_CD'] = df['REASON_CD'].replace(['   ', np.nan], 0)\n    df['REASON_CD'] = df['REASON_CD'].astype(int)\n    df['SUPPLIER_DIV'] = df['SUPPLIER_DIV'].fillna(0)\n    df['SHIP DECISION NO'] = df['SHIP DECISION NO'].fillna(0)\n    df['Ship Mode'] = df['Ship Mode'].fillna('0')\n    df['Consider count hodiday Saturday'] = df['Consider count hodiday Saturday'].replace(' ', 0)\n    df['Consider count hodiday Saturday'] = df['Consider count hodiday Saturday'].astype(int)\n    df['OTHER AREA SHIP DIV'] = df['OTHER AREA SHIP DIV'].replace([' ', '1', np.nan], [0, 1, 0])\n    df['OTHER AREA SHIP DIV'] = df['OTHER AREA SHIP DIV'].astype(int)\n    df = df.drop(['year', 'hour', 'minute', 'hour_vsd', 'minute_vsd'], axis=1)\n    return df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:14:13.873682Z","iopub.execute_input":"2025-06-17T17:14:13.874165Z","iopub.status.idle":"2025-06-17T17:14:13.881494Z","shell.execute_reply.started":"2025-06-17T17:14:13.874146Z","shell.execute_reply":"2025-06-17T17:14:13.880682Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_process = preprocess(df_example)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:14:17.769770Z","iopub.execute_input":"2025-06-17T17:14:17.770384Z","iopub.status.idle":"2025-06-17T17:14:38.372445Z","shell.execute_reply.started":"2025-06-17T17:14:17.770362Z","shell.execute_reply":"2025-06-17T17:14:38.371864Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def label_encoder(df):\n    df[label_col] = encoder.transform(df[label_col].astype(str))\n    return df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:14:38.373658Z","iopub.execute_input":"2025-06-17T17:14:38.373860Z","iopub.status.idle":"2025-06-17T17:14:38.377672Z","shell.execute_reply.started":"2025-06-17T17:14:38.373846Z","shell.execute_reply":"2025-06-17T17:14:38.376930Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_label = label_encoder(df_process)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:14:38.378401Z","iopub.execute_input":"2025-06-17T17:14:38.378676Z","iopub.status.idle":"2025-06-17T17:14:45.261165Z","shell.execute_reply.started":"2025-06-17T17:14:38.378655Z","shell.execute_reply":"2025-06-17T17:14:45.260311Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# def model(df):\n#     X = df[top_20_cols]\n#     X = X.fillna(0)\n#     df['label'] = rf.predict(X)\n#     return df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:14:45.263124Z","iopub.execute_input":"2025-06-17T17:14:45.263889Z","iopub.status.idle":"2025-06-17T17:14:45.266923Z","shell.execute_reply.started":"2025-06-17T17:14:45.263867Z","shell.execute_reply":"2025-06-17T17:14:45.266254Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def model(df):\n    X = df[top_20_cols]\n    X = X.fillna(0)\n    df['label'] = model_xgb.predict(X)\n    return df","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:14:45.267588Z","iopub.execute_input":"2025-06-17T17:14:45.267948Z","iopub.status.idle":"2025-06-17T17:14:45.278948Z","shell.execute_reply.started":"2025-06-17T17:14:45.267925Z","shell.execute_reply":"2025-06-17T17:14:45.278331Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_final = model(df_label)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:14:45.279612Z","iopub.execute_input":"2025-06-17T17:14:45.279839Z","iopub.status.idle":"2025-06-17T17:14:45.421168Z","shell.execute_reply.started":"2025-06-17T17:14:45.279825Z","shell.execute_reply":"2025-06-17T17:14:45.420656Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_final = df_final[['ID', 'label']]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:14:45.421611Z","iopub.execute_input":"2025-06-17T17:14:45.421798Z","iopub.status.idle":"2025-06-17T17:14:45.427012Z","shell.execute_reply.started":"2025-06-17T17:14:45.421782Z","shell.execute_reply":"2025-06-17T17:14:45.426287Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df_final.to_csv(\"submission.csv\", index= False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-17T17:14:45.428697Z","iopub.execute_input":"2025-06-17T17:14:45.429000Z","iopub.status.idle":"2025-06-17T17:14:45.523130Z","shell.execute_reply.started":"2025-06-17T17:14:45.428968Z","shell.execute_reply":"2025-06-17T17:14:45.522665Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}