{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.12.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[],"dockerImageVersionId":28755,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport xgboost as xgb\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import log_loss\nfrom sklearn.preprocessing import LabelEncoder\n\n# 1. Đọc lại file đặc trưng và nhãn\nprint(\"🚀 Đang nạp dữ liệu từ file CSV...\")\ndf_features = pd.read_csv('/kaggle/input/datasets/phuongnambui/features-full/features_full.csv')\nlabels_df = pd.read_csv('/kaggle/input/competitions/malware-classification/trainLabels.csv')\n\n# 2. Merge để lấy nhãn (Class)\nfinal_df = pd.merge(df_features, labels_df, on='Id', how='inner')\n\n# 3. Chuẩn bị dữ liệu Train\nX = final_df.drop(columns=['Id', 'Class'])\nle = LabelEncoder()\ny = le.fit_transform(final_df['Class'])\n\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)\n\n# 4. Huấn luyện lại model (Bước này giờ rất nhanh vì data đã có sẵn)\nprint(\"🧠 Đang huấn luyện lại AI...\")\nmodel = xgb.XGBClassifier(\n    n_estimators=300,\n    max_depth=8,\n    learning_rate=0.05,\n    tree_method='hist', # Dùng GPU nếu bạn đã bật GPU trong Settings\n    objective='multi:softprob',\n    eval_metric='mlogloss',\n    random_state=42\n)\nmodel.fit(X_train, y_train)\n\n# 5. Kiểm tra lại điểm số\ny_pred_proba = model.predict_proba(X_test)\nprint(f\"✅ Đã xác định được model! Log Loss: {log_loss(y_test, y_pred_proba):.4f}\")\n\n#Lưu model\nmodel.save_model('malware_model_0353.json')\nprint(\"Đã lưu mô hình siêu cấp!\")","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2026-07-22T04:03:15.688599Z","iopub.execute_input":"2026-07-22T04:03:15.688962Z","iopub.status.idle":"2026-07-22T04:04:08.951739Z","shell.execute_reply.started":"2026-07-22T04:03:15.688931Z","shell.execute_reply":"2026-07-22T04:04:08.950831Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\n# Bây giờ lệnh này sẽ chạy mượt mà\nimportance = model.get_booster().get_score(importance_type='weight')\nsorted_importance = sorted(importance.items(), key=lambda x: x[1], reverse=True)[:20]\nfeatures, scores = zip(*sorted_importance)\n\nplt.figure(figsize=(12, 6))\nplt.bar(features, scores, color='darkorange')\nplt.xticks(rotation=45)\nplt.title('Những đặc trưng \"tố cáo\" mã độc mạnh nhất')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2026-07-22T04:04:32.974059Z","iopub.execute_input":"2026-07-22T04:04:32.974508Z","iopub.status.idle":"2026-07-22T04:04:33.371184Z","shell.execute_reply.started":"2026-07-22T04:04:32.974478Z","shell.execute_reply":"2026-07-22T04:04:33.370265Z"}},"outputs":[],"execution_count":null}]}