{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"","metadata":{"_uuid":"7fea4ce8-552f-417c-8ecd-7d51e9905ac8","_cell_guid":"7c404888-0f2b-4489-b7c0-e5e87feef930","trusted":true}},{"cell_type":"markdown","source":"","metadata":{"_uuid":"bd52ac55-8dc3-48eb-bd25-ad65078243cd","_cell_guid":"a34198ee-803c-453b-a5c0-67842e03bf21","trusted":true}},{"cell_type":"markdown","source":"","metadata":{"_uuid":"b5517a23-089f-447f-a755-de4be34cfe9a","_cell_guid":"816ac41a-dfe1-447c-815f-614c41c48f7b","trusted":true}},{"cell_type":"markdown","source":"<p style = \"font-size:18px\"><b> .bytes file</b></p>\n<pre>\n00401000 00 00 80 40 40 28 00 1C 02 42 00 C4 00 20 04 20\n00401010 00 00 20 09 2A 02 00 00 00 00 8E 10 41 0A 21 01\n00401020 40 00 02 01 00 90 21 00 32 40 00 1C 01 40 C8 18\n00401030 40 82 02 63 20 00 00 09 10 01 02 21 00 82 00 04\n00401040 82 20 08 83 00 08 00 00 00 00 02 00 60 80 10 80\n00401050 18 00 00 20 A9 00 00 00 00 04 04 78 01 02 70 90\n00401060 00 02 00 08 20 12 00 00 00 40 10 00 80 00 40 19\n00401070 00 00 00 00 11 20 80 04 80 10 00 20 00 00 25 00\n00401080 00 00 01 00 00 04 00 10 02 C1 80 80 00 20 20 00\n00401090 08 A0 01 01 44 28 00 00 08 10 20 00 02 08 00 00\n004010A0 00 40 00 00 00 34 40 40 00 04 00 08 80 08 00 08\n004010B0 10 00 40 00 68 02 40 04 E1 00 28 14 00 08 20 0A\n004010C0 06 01 02 00 40 00 00 00 00 00 00 20 00 02 00 04\n004010D0 80 18 90 00 00 10 A0 00 45 09 00 10 04 40 44 82\n004010E0 90 00 26 10 00 00 04 00 82 00 00 00 20 40 00 00\n004010F0 B4 00 00 40 00 02 20 25 08 00 00 00 00 00 00 00\n00401100 08 00 00 50 00 08 40 50 00 02 06 22 08 85 30 00\n00401110 00 80 00 80 60 00 09 00 04 20 00 00 00 00 00 00\n00401120 00 82 40 02 00 11 46 01 4A 01 8C 01 E6 00 86 10\n00401130 4C 01 22 00 64 00 AE 01 EA 01 2A 11 E8 10 26 11\n00401140 4E 11 8E 11 C2 00 6C 00 0C 11 60 01 CA 00 62 10\n00401150 6C 01 A0 11 CE 10 2C 11 4E 10 8C 00 CE 01 AE 01\n00401160 6C 10 6C 11 A2 01 AE 00 46 11 EE 10 22 00 A8 00\n00401170 EC 01 08 11 A2 01 AE 10 6C 00 6E 00 AC 11 8C 00\n00401180 EC 01 2A 10 2A 01 AE 00 40 00 C8 10 48 01 4E 11\n00401190 0E 00 EC 11 24 10 4A 10 04 01 C8 11 E6 01 C2 00\n\n</pre>","metadata":{"_uuid":"86b4d6c3-381b-4316-b506-b71b7f725287","_cell_guid":"840b34d4-f708-406f-9fa5-14b621675260","trusted":true}},{"cell_type":"code","source":"import pandas as pd\nimport seaborn as sns\nimport numpy as np\nimport matplotlib.pyplot as plt\n\n\nY = pd.read_csv(\"../input/malware-classification/trainLabels.csv\")\n\ntotal = len(Y) * 1.0\n\nax = sns.countplot(x=\"Class\", data=Y)\n\n\nfor p in ax.patches:\n    ax.annotate('{:.1f}%'.format(100 * p.get_height() / total), (p.get_x() + 0.1, p.get_height() + 5))\n\n\nax.yaxis.set_ticks(np.linspace(0, total, 11))\n\n\nax.set_yticklabels(map('{:.1f}%'.format, 100 * ax.yaxis.get_majorticklocs() / total))\n\n\nplt.show()","metadata":{"_uuid":"3befb00d-fcb3-417e-b3fd-5cdf73d0e47f","_cell_guid":"e783c0c1-f6a7-4d8d-95bc-888911a7c910","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-10-26T09:52:19.277432Z","iopub.execute_input":"2023-10-26T09:52:19.277719Z","iopub.status.idle":"2023-10-26T09:52:21.064564Z","shell.execute_reply.started":"2023-10-26T09:52:19.277693Z","shell.execute_reply":"2023-10-26T09:52:21.063862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Import**","metadata":{"_uuid":"4b57e803-e5a6-4cda-8ffe-6002ab5d0388","_cell_guid":"4e242e22-081b-4c62-8629-d668819e693c","trusted":true}},{"cell_type":"markdown","source":"","metadata":{"_uuid":"6d2d8d8c-dc79-4143-aa8d-bcf0349d7627","_cell_guid":"93b90d62-aa22-4896-ba5c-fcad2e8e4d65","trusted":true}},{"cell_type":"markdown","source":"**use pandas, numpy**\n\n**We use tqdm to know the progress.**\n\n**os for reading files and warnings for ignoring warnings,**\n\n**Stratified kfold for producing stacking data using cv,**\n\n**In addition, we imported boosting models that are showing good performance in kaggle these days.**","metadata":{"_uuid":"6f467691-ba6d-4110-8ada-ce08de0809af","_cell_guid":"e5b26360-3570-4bd4-b01b-2e37348677ba","trusted":true}},{"cell_type":"code","source":"\nimport pandas as pd\nimport numpy as np\nfrom tqdm import tqdm\nimport os\nimport warnings\n\n\nwarnings.filterwarnings(\"ignore\")\n\n\nfrom sklearn.model_selection import StratifiedKFold\n\n\nfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=62)\n\nfrom lightgbm import LGBMClassifier\nfrom xgboost import XGBClassifier\nfrom catboost import CatBoostClassifier","metadata":{"_uuid":"1f4a26eb-6356-47f0-8632-f4c445c99260","_cell_guid":"22b44834-e2f9-4618-b263-69db297bfea5","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-10-26T09:52:21.066255Z","iopub.execute_input":"2023-10-26T09:52:21.06674Z","iopub.status.idle":"2023-10-26T09:52:24.809881Z","shell.execute_reply.started":"2023-10-26T09:52:21.066713Z","shell.execute_reply":"2023-10-26T09:52:24.808962Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **(data load)**","metadata":{"_uuid":"3eb69131-7e0d-4bba-8f56-f227d4f0e2b1","_cell_guid":"da5109f2-7f4b-4e91-b828-cbe53ffa55d1","trusted":true}},{"cell_type":"markdown","source":"\n\n**Read the sample submission in the form of train labels and submission where the class of the file is written.**","metadata":{"_uuid":"0b479492-6350-462e-89a3-364c46d93f7e","_cell_guid":"fa32e8b5-7ca4-489c-8120-0a549703835d","trusted":true}},{"cell_type":"code","source":"train_labels=pd.read_csv(\"../input/malware-classification/trainLabels.csv\")\nsample = pd.read_csv(\"../input/malware-classification/sampleSubmission.csv\",index_col=\"Id\")\na = '\"0\",\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\",\"0a\",\"0b\",\"0c\",\"0d\",\"0e\",\"0f\",\"10\",\"11\",\"12\",\"13\",\"14\",\"15\",\"16\",\"17\",\"18\",\"19\",\"1a\",\"1b\",\"1c\",\"1d\",\"1e\",\"1f\",\"20\",\"21\",\"22\",\"23\",\"24\",\"25\",\"26\",\"27\",\"28\",\"29\",\"2a\",\"2b\",\"2c\",\"2d\",\"2e\",\"2f\",\"30\",\"31\",\"32\",\"33\",\"34\",\"35\",\"36\",\"37\",\"38\",\"39\",\"3a\",\"3b\",\"3c\",\"3d\",\"3e\",\"3f\",\"40\",\"41\",\"42\",\"43\",\"44\",\"45\",\"46\",\"47\",\"48\",\"49\",\"4a\",\"4b\",\"4c\",\"4d\",\"4e\",\"4f\",\"50\",\"51\",\"52\",\"53\",\"54\",\"55\",\"56\",\"57\",\"58\",\"59\",\"5a\",\"5b\",\"5c\",\"5d\",\"5e\",\"5f\",\"60\",\"61\",\"62\",\"63\",\"64\",\"65\",\"66\",\"67\",\"68\",\"69\",\"6a\",\"6b\",\"6c\",\"6d\",\"6e\",\"6f\",\"70\",\"71\",\"72\",\"73\",\"74\",\"75\",\"76\",\"77\",\"78\",\"79\",\"7a\",\"7b\",\"7c\",\"7d\",\"7e\",\"7f\",\"80\",\"81\",\"82\",\"83\",\"84\",\"85\",\"86\",\"87\",\"88\",\"89\",\"8a\",\"8b\",\"8c\",\"8d\",\"8e\",\"8f\",\"90\",\"91\",\"92\",\"93\",\"94\",\"95\",\"96\",\"97\",\"98\",\"99\",\"9a\",\"9b\",\"9c\",\"9d\",\"9e\",\"9f\",\"a0\",\"a1\",\"a2\",\"a3\",\"a4\",\"a5\",\"a6\",\"a7\",\"a8\",\"a9\",\"aa\",\"ab\",\"ac\",\"ad\",\"ae\",\"af\",\"b0\",\"b1\",\"b2\",\"b3\",\"b4\",\"b5\",\"b6\",\"b7\",\"b8\",\"b9\",\"ba\",\"bb\",\"bc\",\"bd\",\"be\",\"bf\",\"c0\",\"c1\",\"c2\",\"c3\",\"c4\",\"c5\",\"c6\",\"c7\",\"c8\",\"c9\",\"ca\",\"cb\",\"cc\",\"cd\",\"ce\",\"cf\",\"d0\",\"d1\",\"d2\",\"d3\",\"d4\",\"d5\",\"d6\",\"d7\",\"d8\",\"d9\",\"da\",\"db\",\"dc\",\"dd\",\"de\",\"df\",\"e0\",\"e1\",\"e2\",\"e3\",\"e4\",\"e5\",\"e6\",\"e7\",\"e8\",\"e9\",\"ea\",\"eb\",\"ec\",\"ed\",\"ee\",\"ef\",\"f0\",\"f1\",\"f2\",\"f3\",\"f4\",\"f5\",\"f6\",\"f7\",\"f8\",\"f9\",\"fa\",\"fb\",\"fc\",\"fd\",\"fe\",\"ff\",\"??\",\"size\",\"Class\"'","metadata":{"_uuid":"73d478eb-2cd1-4b17-8354-d03c419766eb","_cell_guid":"7b70178b-7b69-447f-99a9-1d972668823c","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-10-26T09:52:24.811192Z","iopub.execute_input":"2023-10-26T09:52:24.811803Z","iopub.status.idle":"2023-10-26T09:52:24.909864Z","shell.execute_reply.started":"2023-10-26T09:52:24.811767Z","shell.execute_reply":"2023-10-26T09:52:24.909138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **(preprocessing)**","metadata":{"_uuid":"7d7f04e1-5324-4c92-8641-fccb8cb67b79","_cell_guid":"84a5a6df-48ef-4d2a-8eec-ad32368c5cf6","trusted":true}},{"cell_type":"markdown","source":"```python\nimport numpy as np\nimport os\nfrom tqdm import tqdm\nimport pandas as pd\n\ntrain_np = np.zeros((len(files), len(a.split(\",\"))))\n\n\nfiles = os.listdir(\"../input/malware-only-byte/only_byte\")\nfiles.sort()\n\nk = 0\n\nfor file in tqdm(files):\n    \n    statinfo = os.stat(\"../input/malware-only-byte/only_byte/\" + file)\n\n    \n    with open(\"../input/malware-only-byte/only_byte/\" + file, \"r\") as fp:\n        for lines in fp.readlines():\n            line = lines.rstrip().split(\" \")[1:]\n            for hex_code in line:\n                if hex_code == '??':\n                    train_np[k][256] += 1\n                else:\n                    train_np[k][int(hex_code, 16)] += 1\n\n\n        train_np[k][257] = statinfo.st_size / (1024 * 1024)\n        train_np[k][258] = train_labels[train_labels[\"Id\"] == file.split('.')[0]][\"Class\"].tolist()[0]\n\n    fp.close()\n    k += 1\n\n\ntrain = pd.DataFrame(train_np, columns=a[1:-1].split('\",\"'))\ntrain.to_csv(\"train_data.csv\", index=False)\n\n\ntrain.head()\n\n\n```","metadata":{"_uuid":"3ca2fa37-4273-4410-8530-3cb7b3895e50","_cell_guid":"a10d43ec-28a9-44b0-843a-96d4590f1071","trusted":true}},{"cell_type":"markdown","source":"","metadata":{"_uuid":"d2dcb93e-1ccf-4f9f-b237-3e89e5f1abb9","_cell_guid":"6d62e197-be54-44cb-b7e3-88a7d867d3e5","trusted":true}},{"cell_type":"markdown","source":"","metadata":{"_uuid":"674c58af-7b9f-4a71-bf5a-38ebcfa7b9ec","_cell_guid":"8ce3ea14-c6c3-4048-be58-4cc2483d187d","trusted":true}},{"cell_type":"code","source":"train=pd.read_csv(\"../input/malware-only-byte/train_data.csv\")\n\n","metadata":{"_uuid":"c26571dd-5367-4ca1-840f-5ce24df41a8b","_cell_guid":"be8ab9b0-1311-47bf-a7f9-93eb82d7b905","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-10-26T09:52:24.911763Z","iopub.execute_input":"2023-10-26T09:52:24.912062Z","iopub.status.idle":"2023-10-26T09:52:25.554026Z","shell.execute_reply.started":"2023-10-26T09:52:24.912037Z","shell.execute_reply":"2023-10-26T09:52:25.553212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"_uuid":"05c47e47-8c30-4953-9b86-41b26b8fc526","_cell_guid":"c767c152-20fc-460a-90cc-24cf7ef1b684","collapsed":false,"jupyter":{"outputs_hidden":false},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"```python\nimport numpy as np\nimport os\nfrom tqdm import tqdm\nimport pandas as pd\n\n\nfile2 = []\n\n\nfor file in files:\n    file2.append(file.split(\".\")[0])\n\n\ntest_np = np.zeros((len(files), len(a.split(\",\"))))\n\n\nfiles = os.listdir(\"../input/malware-only-byte/test\")\nfiles.sort()\n\nk = 0\n\nfor file in tqdm(files):\n\n    statinfo = os.stat(\"../input/malware-only-byte/test/\" + file)\n\n\n    with open(\"../input/malware-only-byte/test/\" + file, \"r\") as fp:\n        for lines in fp.readlines():\n            line = lines.rstrip().split(\" \")[1:]\n            for hex_code in line:\n                if hex_code == '??':\n                \n                    test_np[k][256] += 1\n                else:\n                   \n                    test_np[k][int(hex_code, 16)] += 1\n\n\n        test_np[k][257] = statinfo.st_size / (1024 * 1024)\n\n    fp.close()\n    k += 1\n\ntest = pd.DataFrame(test_np, columns=a[1:-1].split('\",\"'))\n\ntest[\"Id\"] = file2\n\n\ntest.to_csv(\"test_data.csv\", index=False)\n\n\ntest.head()\n\n","metadata":{"_uuid":"fa986cea-6f43-49ed-b495-d7a83de10ad1","_cell_guid":"1c496e38-030b-4f48-907e-3fbcf8fdbd2a","trusted":true}},{"cell_type":"markdown","source":"","metadata":{"_uuid":"dec5bcda-7035-4017-9638-67007e4029dc","_cell_guid":"2959710a-aa59-49bb-a02e-f913edc81407","trusted":true}},{"cell_type":"code","source":"test=pd.read_csv(\"../input/malware-only-byte/test_data.csv\")","metadata":{"_uuid":"3b277e2e-a205-4bd0-9050-2695e0796a16","_cell_guid":"758b253f-73f8-4c17-8704-6501635c9ee1","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-10-26T09:52:25.555045Z","iopub.execute_input":"2023-10-26T09:52:25.555307Z","iopub.status.idle":"2023-10-26T09:52:26.169464Z","shell.execute_reply.started":"2023-10-26T09:52:25.555285Z","shell.execute_reply":"2023-10-26T09:52:26.168691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"_uuid":"e5ec5858-70f7-488c-aa36-ee7e73bdd1f5","_cell_guid":"4890d1a7-8f96-40f9-8c8f-15c1915a63a6","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-10-26T09:52:26.170705Z","iopub.execute_input":"2023-10-26T09:52:26.17114Z","iopub.status.idle":"2023-10-26T09:52:26.220565Z","shell.execute_reply.started":"2023-10-26T09:52:26.171107Z","shell.execute_reply":"2023-10-26T09:52:26.21985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **(modeling and stacking)**","metadata":{"_uuid":"5ec8f09c-6c78-481c-a2ea-6f59f01b80be","_cell_guid":"9409321a-b1c1-44fa-bdd3-4aa70df4ddf8","trusted":true}},{"cell_type":"code","source":"\nX = train.drop(['Class'], axis=1)\n\n\ny = train['Class']","metadata":{"_uuid":"f9f86ed3-c178-4a76-9458-e467cdf327db","_cell_guid":"2f3b006e-92e9-4714-b72c-f21cb8e1a445","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-10-26T09:52:26.221517Z","iopub.execute_input":"2023-10-26T09:52:26.221748Z","iopub.status.idle":"2023-10-26T09:52:26.234833Z","shell.execute_reply.started":"2023-10-26T09:52:26.221727Z","shell.execute_reply":"2023-10-26T09:52:26.233965Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n\n**Divide the values excluding the predicted values by X and y.**","metadata":{"_uuid":"fbb7064a-f2c1-47f6-a493-7a39d8e9f646","_cell_guid":"df0f6c08-3300-4704-9076-a9bf2f023a2c","trusted":true}},{"cell_type":"markdown","source":"","metadata":{"_uuid":"630aaf82-8cea-4056-9cda-ea072bb8cbbd","_cell_guid":"9e270d36-8a8f-420f-a836-ad6dfb84f8c5","trusted":true}},{"cell_type":"markdown","source":"","metadata":{"_uuid":"64a81707-479e-47c3-a502-bcfb74240fd7","_cell_guid":"1fa13f25-a7c1-446e-b5e0-7bb30daaf18f","trusted":true}},{"cell_type":"code","source":"\n\nstack_df = pd.read_csv(\"../input/malware-only-byte/train_data.csv\")\n\nfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)\n\n\nfor i, j in enumerate(fold.split(X, y)):\n\n    stack_train_X = X.iloc[j[0]]\n    stack_train_y = y.iloc[j[0]]\n    stack_test_X = X.iloc[j[1]]\n    stack_test_y = y.iloc[j[1]]\n    \n    \n    model = LGBMClassifier(learning_rate=0.025, n_estimators=850, min_child_weight=1, boosting_type=\"gbdt\", min_child_samples=68, random_state=62, objective=\"multi-class\", metric=\"multi_logloss\")\n    model.fit(stack_train_X, stack_train_y)\n    \n    \n    preds = model.predict_proba(stack_test_X)\n    preds = pd.DataFrame(preds)\n    \n\n    stack_df[\"0\"].iloc[j[1]] = preds[0]\n    stack_df[\"1\"].iloc[j[1]] = preds[1]\n    stack_df[\"2\"].iloc[j[1]] = preds[2]\n    stack_df[\"3\"].iloc[j[1]] = preds[3]\n    stack_df[\"4\"].iloc[j[1]] = preds[4]\n    stack_df[\"5\"].iloc[j[1]] = preds[5]\n    stack_df[\"6\"].iloc[j[1]] = preds[6]\n    stack_df[\"7\"].iloc[j[1]] = preds[7]\n    stack_df[\"8\"].iloc[j[1]] = preds[8]\n\n\nlgbm_stack = stack_df[[\"0\", \"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\"]]\n\n\nlgbm_stack.columns = [\"lgbm_Prediction1\", \"lgbm_Prediction2\", \"lgbm_Prediction3\", \"lgbm_Prediction4\", \"lgbm_Prediction5\", \"lgbm_Prediction6\", \"lgbm_Prediction7\", \"lgbm_Prediction8\", \"lgbm_Prediction9\"]","metadata":{"_uuid":"827a4bed-31fd-4b76-902e-9feb4607ccc7","_cell_guid":"ef8f2ca4-63a2-4ded-b56c-8583844c07fb","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-10-26T09:52:26.23607Z","iopub.execute_input":"2023-10-26T09:52:26.236385Z","iopub.status.idle":"2023-10-26T09:56:18.744557Z","shell.execute_reply.started":"2023-10-26T09:52:26.23636Z","shell.execute_reply":"2023-10-26T09:56:18.743766Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\n\nstack_df = pd.read_csv(\"../input/malware-only-byte/train_data.csv\")\n\n\nfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)\n\n\nfor i, j in enumerate(fold.split(X, y)):\n\n    y = y.astype(int)\n    stack_train_X = X.iloc[j[0]]\n    stack_train_y = y.iloc[j[0]]\n    stack_test_X = X.iloc[j[1]]\n    stack_test_y = y.iloc[j[1]]\n    \n\n    model = XGBClassifier(booster=\"gbtree\", eta=0.0975, min_child_weight=2, random_state=62, objective=\"multi:softmax\", eval_metric=\"logloss\")\n    model.fit(stack_train_X, stack_train_y)\n    \n\n    preds = model.predict_proba(stack_test_X)\n    preds = pd.DataFrame(preds)\n    \n\n    stack_df[\"0\"].iloc[j[1]] = preds[0]\n    stack_df[\"1\"].iloc[j[1]] = preds[1]\n    stack_df[\"2\"].iloc[j[1]] = preds[2]\n    stack_df[\"3\"].iloc[j[1]] = preds[3]\n    stack_df[\"4\"].iloc[j[1]] = preds[4]\n    stack_df[\"5\"].iloc[j[1]] = preds[5]\n    stack_df[\"6\"].iloc[j[1]] = preds[6]\n    stack_df[\"7\"].iloc[j[1]] = preds[7]\n    stack_df[\"8\"].iloc[j[1]] = preds[8]\n\n\nxgb_stack = stack_df[[\"0\", \"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\"]]\n\n\nxgb_stack.columns = [\"xgb_Prediction1\", \"xgb_Prediction2\", \"xgb_Prediction3\", \"xgb_Prediction4\", \"xgb_Prediction5\", \"xgb_Prediction6\", \"xgb_Prediction7\", \"xgb_Prediction8\", \"xgb_Prediction9\"]","metadata":{"_uuid":"6f15ae6e-f557-4a91-b2f5-e6fafdf63ab6","_cell_guid":"0a0774b0-8adc-4bb5-ab2e-d75bb58934c7","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-10-26T09:56:18.745707Z","iopub.execute_input":"2023-10-26T09:56:18.746486Z","iopub.status.idle":"2023-10-26T09:56:20.118066Z","shell.execute_reply.started":"2023-10-26T09:56:18.746455Z","shell.execute_reply":"2023-10-26T09:56:20.115834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nstack_df = pd.read_csv(\"../input/malware-only-byte/train_data.csv\")\n\n\nfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)\n\n\nfor i, j in enumerate(fold.split(X, y)):\n\n    stack_train_X = X.iloc[j[0]]\n    stack_train_y = y.iloc[j[0]]\n    stack_test_X = X.iloc[j[1]]\n    stack_test_y = y.iloc[j[1]]\n    \n    \n    model = CatBoostClassifier(verbose=0)\n    model.fit(stack_train_X, stack_train_y)\n    \n    \n    preds = model.predict_proba(stack_test_X)\n    preds = pd.DataFrame(preds)\n    \n    \n    stack_df[\"0\"].iloc[j[1]] = preds[0]\n    stack_df[\"1\"].iloc[j[1]] = preds[1]\n    stack_df[\"2\"].iloc[j[1]] = preds[2]\n    stack_df[\"3\"].iloc[j[1]] = preds[3]\n    stack_df[\"4\"].iloc[j[1]] = preds[4]\n    stack_df[\"5\"].iloc[j[1]] = preds[5]\n    stack_df[\"6\"].iloc[j[1]] = preds[6]\n    stack_df[\"7\"].iloc[j[1]] = preds[7]\n    stack_df[\"8\"].iloc[j[1]] = preds[8]\n\n\ncat_stack = stack_df[[\"0\", \"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\"]]\n\n\ncat_stack.columns = [\"cat_Prediction1\", \"cat_Prediction2\", \"cat_Prediction3\", \"cat_Prediction4\", \"cat_Prediction5\", \"cat_Prediction6\", \"cat_Prediction7\", \"cat_Prediction8\", \"cat_Prediction9\"]","metadata":{"_uuid":"7c43f9ce-bf1f-4348-ae88-a97dc4ec5cef","_cell_guid":"80ae20ae-e904-4c09-a989-5900c7c18013","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-10-26T09:56:20.118809Z","iopub.status.idle":"2023-10-26T09:56:20.119145Z","shell.execute_reply.started":"2023-10-26T09:56:20.118976Z","shell.execute_reply":"2023-10-26T09:56:20.11899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nstacking_X = pd.concat([lgbm_stack,cat_stack], axis=1)","metadata":{"_uuid":"94898cb8-ff84-4b69-89b6-ac6baa5ccaa5","_cell_guid":"2228ba18-d626-454d-80c9-9f4a3645d8f7","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-10-26T09:56:20.120854Z","iopub.status.idle":"2023-10-26T09:56:20.121331Z","shell.execute_reply.started":"2023-10-26T09:56:20.121103Z","shell.execute_reply":"2023-10-26T09:56:20.121124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"\n\n**Stacking was performed on the train set using each model, and the predicted value was made into new learning data.**","metadata":{"_uuid":"4b93ecf6-c217-4266-a92d-dec79ba62fc4","_cell_guid":"453e0c72-fb83-4620-b1ca-57e65afa0185","trusted":true}},{"cell_type":"code","source":"\n\n\nmodel = LGBMClassifier(learning_rate=0.025, n_estimators=850, min_child_weight=1, boosting_type=\"gbdt\", min_child_samples=68, random_state=62, objective=\"multi-class\", metric=\"multi_logloss\")\n\n\nmodel.fit(X, y)\n\n\nlgbm_pred = model.predict_proba(test.drop(\"Id\", axis=1))\n\nlgbm_pred = pd.DataFrame(lgbm_pred)\n\n\nlgbm_pred.columns = [\"lgbm_Prediction1\", \"lgbm_Prediction2\", \"lgbm_Prediction3\", \"lgbm_Prediction4\", \"lgbm_Prediction5\", \"lgbm_Prediction6\", \"lgbm_Prediction7\", \"lgbm_Prediction8\", \"lgbm_Prediction9\"]","metadata":{"_uuid":"b1ad722d-3504-4b99-b65b-d4598e4e2cdd","_cell_guid":"778c2750-8ca7-4484-ab6c-0ffd95db7fca","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-10-26T09:56:20.122673Z","iopub.status.idle":"2023-10-26T09:56:20.123322Z","shell.execute_reply.started":"2023-10-26T09:56:20.123081Z","shell.execute_reply":"2023-10-26T09:56:20.123103Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\n\nmodel = XGBClassifier(booster=\"gbtree\", eta=0.0975, min_child_weight=2, random_state=62, objective=\"multi:softmax\", eval_metric=\"logloss\")\n\nmodel.fit(X, y)\n\nxgb_pred = model.predict_proba(test.drop(\"Id\", axis=1))\n\nxgb_pred = pd.DataFrame(xgb_pred)\n\nxgb_pred.columns = [\"xgb_Prediction1\", \"xgb_Prediction2\", \"xgb_Prediction3\", \"xgb_Prediction4\", \"xgb_Prediction5\", \"xgb_Prediction6\", \"xgb_Prediction7\", \"xgb_Prediction8\", \"xgb_Prediction9\"]","metadata":{"_uuid":"21ce4dc5-9b56-473f-ae62-9f202dfa148d","_cell_guid":"552e4ec9-0a96-4bf1-93fa-7976b65ea52a","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-10-26T09:56:20.124247Z","iopub.status.idle":"2023-10-26T09:56:20.12469Z","shell.execute_reply.started":"2023-10-26T09:56:20.124454Z","shell.execute_reply":"2023-10-26T09:56:20.124475Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n\nplt.figure(figsize=(10, 6))\nsns.barplot(data=xgb_pred, ci=None)\nplt.title(\"XGBoost Classifier Prediction Probabilities\")\nplt.ylabel(\"Prediction Probability\")\nplt.xlabel(\"Category\")\nplt.xticks(rotation=45)\nplt.show()","metadata":{"_uuid":"8178d836-5b8f-4d3a-a8ce-534b902d4904","_cell_guid":"2e50222d-9dd4-41bf-8fa1-16283b7a4de7","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-10-26T09:56:20.125716Z","iopub.status.idle":"2023-10-26T09:56:20.126165Z","shell.execute_reply.started":"2023-10-26T09:56:20.12594Z","shell.execute_reply":"2023-10-26T09:56:20.125961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nmodel = CatBoostClassifier(verbose=0)\n\nmodel.fit(X, y)\n\n\ncat_pred = model.predict_proba(test.drop(\"Id\", axis=1))\n\ncat_pred = pd.DataFrame(cat_pred)\n\ncat_pred.columns = [\"cat_Prediction1\", \"cat_Prediction2\", \"cat_Prediction3\", \"cat_Prediction4\", \"cat_Prediction5\", \"cat_Prediction6\", \"cat_Prediction7\", \"cat_Prediction8\", \"cat_Prediction9\"]","metadata":{"_uuid":"c5d785ef-68d9-4ac3-839a-0db854c6cfc7","_cell_guid":"d22f9b58-8845-4094-9546-057c4cac22f7","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-10-26T09:56:20.127953Z","iopub.status.idle":"2023-10-26T09:56:20.1284Z","shell.execute_reply.started":"2023-10-26T09:56:20.12818Z","shell.execute_reply":"2023-10-26T09:56:20.1282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cat_pred","metadata":{"_uuid":"acb80112-612f-4cbd-8368-369e71f41fd8","_cell_guid":"4f233e62-d029-4637-8127-328dfe2d939b","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-10-26T09:56:20.131142Z","iopub.status.idle":"2023-10-26T09:56:20.13157Z","shell.execute_reply.started":"2023-10-26T09:56:20.13135Z","shell.execute_reply":"2023-10-26T09:56:20.131371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntest_X = pd.concat([lgbm_pred, cat_pred], axis=1)","metadata":{"_uuid":"9550f5a3-47a9-48bd-93cf-b1c46ebd0c3c","_cell_guid":"10019e7f-68d5-4b89-bfaf-5a7c008ebc32","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-10-26T09:56:20.132725Z","iopub.status.idle":"2023-10-26T09:56:20.133172Z","shell.execute_reply.started":"2023-10-26T09:56:20.132948Z","shell.execute_reply":"2023-10-26T09:56:20.132969Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_X","metadata":{"_uuid":"7b67b929-fe6d-4589-b614-49f3095e5aee","_cell_guid":"a57e0902-45cf-41aa-a685-393087ee17c7","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-10-26T09:56:20.134996Z","iopub.status.idle":"2023-10-26T09:56:20.13543Z","shell.execute_reply.started":"2023-10-26T09:56:20.135206Z","shell.execute_reply":"2023-10-26T09:56:20.135226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{"_uuid":"1d28a0b5-fb0d-4bb3-aabc-62507df60929","_cell_guid":"4c5b54af-7299-49e4-bbf0-4fd7e0c1d51d","trusted":true}},{"cell_type":"code","source":"\n\nmodel = CatBoostClassifier(verbose=0)\n\nmodel.fit(stacking_X, y)\n\npred = model.predict_proba(test_X)\n\nimsi_df = pd.DataFrame(pred)\nimsi_df = imsi_df.set_index(test[\"Id\"])","metadata":{"_uuid":"c215809d-ce2a-4a4e-b94e-14dbb3d70a2a","_cell_guid":"156897e7-2583-4614-a943-844b0df47696","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-10-26T09:56:20.136553Z","iopub.status.idle":"2023-10-26T09:56:20.137015Z","shell.execute_reply.started":"2023-10-26T09:56:20.136772Z","shell.execute_reply":"2023-10-26T09:56:20.136792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test","metadata":{"_uuid":"ee09b758-062d-4227-818e-b2e7ebeb63b3","_cell_guid":"44fc54c5-bacf-4e14-a1b0-be751a9a2acb","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-10-26T09:56:20.138679Z","iopub.status.idle":"2023-10-26T09:56:20.13913Z","shell.execute_reply.started":"2023-10-26T09:56:20.138886Z","shell.execute_reply":"2023-10-26T09:56:20.138906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imsi_df","metadata":{"_uuid":"33afe132-a882-4016-b10d-efded9663320","_cell_guid":"47838292-26a3-41a1-9527-412d9b936716","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-10-26T09:56:20.14037Z","iopub.status.idle":"2023-10-26T09:56:20.140811Z","shell.execute_reply.started":"2023-10-26T09:56:20.140576Z","shell.execute_reply":"2023-10-26T09:56:20.140596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nsample = pd.read_csv(\"../input/malware-classification/sampleSubmission.csv\",index_col=\"Id\")\nsubmission = pd.concat([sample, imsi_df], axis=1)\n\nsubmission = submission.drop([\"Prediction1\", \"Prediction2\", \"Prediction3\", \"Prediction4\", \"Prediction5\", \"Prediction6\", \"Prediction7\", \"Prediction8\", \"Prediction9\"], axis=1)\nsubmission.columns = [\"Prediction1\", \"Prediction2\", \"Prediction3\", \"Prediction4\", \"Prediction5\", \"Prediction6\", \"Prediction7\", \"Prediction8\", \"Prediction9\"]\n\nsubmission.to_csv(\"submission.csv\", index=False)","metadata":{"_uuid":"b02d6ceb-3db4-4bef-a844-57b5a3b4ca3b","_cell_guid":"1370b9c3-02e1-4589-bc8f-8381a8f96b4d","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-10-26T09:56:20.142132Z","iopub.status.idle":"2023-10-26T09:56:20.142456Z","shell.execute_reply.started":"2023-10-26T09:56:20.142298Z","shell.execute_reply":"2023-10-26T09:56:20.142314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"_uuid":"3ea42c50-be35-4485-9191-16be310b1d14","_cell_guid":"f52b8d16-ee99-441f-b7f5-d6e9706ee808","collapsed":false,"jupyter":{"outputs_hidden":false},"execution":{"iopub.status.busy":"2023-10-26T09:56:20.143671Z","iopub.status.idle":"2023-10-26T09:56:20.144002Z","shell.execute_reply.started":"2023-10-26T09:56:20.143821Z","shell.execute_reply":"2023-10-26T09:56:20.143834Z"},"trusted":true},"execution_count":null,"outputs":[]}]}