{"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":"code","source":"from pathlib import Path\n\nimport numpy as np\nimport pandas as pd\nfrom scipy import sparse\nfrom tqdm import tqdm\n\npd.options.display.width = 180\npd.options.display.max_colwidth = 120\n\nBERT_PATH = \"../input/huggingface-bert-variants/distilbert-base-uncased/distilbert-base-uncased\"\n\ndata_dir = Path('../input/AI4Code')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-06-04T09:46:09.582385Z","iopub.execute_input":"2022-06-04T09:46:09.582825Z","iopub.status.idle":"2022-06-04T09:46:09.628534Z","shell.execute_reply.started":"2022-06-04T09:46:09.582711Z","shell.execute_reply":"2022-06-04T09:46:09.627133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"NUM_TRAIN = 200\n\n\ndef read_notebook(path):\n    return (\n        pd.read_json(\n            path,\n            dtype={'cell_type': 'category', 'source': 'str'})\n        .assign(id=path.stem)\n        .rename_axis('cell_id')\n    )\n\n\npaths_train = list((data_dir / 'train').glob('*.json'))[:NUM_TRAIN]\nnotebooks_train = [\n    read_notebook(path) for path in tqdm(paths_train, desc='Train NBs')\n]\ndf = (\n    pd.concat(notebooks_train)\n    .set_index('id', append=True)\n    .swaplevel()\n    .sort_index(level='id', sort_remaining=False)\n)\n","metadata":{"execution":{"iopub.status.busy":"2022-06-04T09:46:09.630021Z","iopub.execute_input":"2022-06-04T09:46:09.631126Z","iopub.status.idle":"2022-06-04T09:46:12.04078Z","shell.execute_reply.started":"2022-06-04T09:46:09.631071Z","shell.execute_reply":"2022-06-04T09:46:12.039812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df","metadata":{"execution":{"iopub.status.busy":"2022-06-04T09:46:12.041952Z","iopub.execute_input":"2022-06-04T09:46:12.042281Z","iopub.status.idle":"2022-06-04T09:46:12.066206Z","shell.execute_reply.started":"2022-06-04T09:46:12.042252Z","shell.execute_reply":"2022-06-04T09:46:12.065053Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Get an example notebook\nnb_id = df.index.unique('id')[6]\nprint('Notebook:', nb_id)\n\nprint(\"The disordered notebook:\")\nnb = df.loc[nb_id, :]\ndisplay(nb)\nprint()","metadata":{"execution":{"iopub.status.busy":"2022-06-04T09:46:12.069335Z","iopub.execute_input":"2022-06-04T09:46:12.070384Z","iopub.status.idle":"2022-06-04T09:46:12.089948Z","shell.execute_reply.started":"2022-06-04T09:46:12.07034Z","shell.execute_reply":"2022-06-04T09:46:12.088883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_orders = pd.read_csv(\n    data_dir / 'train_orders.csv',\n    index_col='id',\n    squeeze=True,\n).str.split()  # Split the string representation of cell_ids into a list\n\ndf_orders","metadata":{"execution":{"iopub.status.busy":"2022-06-04T09:46:12.091634Z","iopub.execute_input":"2022-06-04T09:46:12.092269Z","iopub.status.idle":"2022-06-04T09:46:14.055097Z","shell.execute_reply.started":"2022-06-04T09:46:12.092225Z","shell.execute_reply":"2022-06-04T09:46:14.054015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(df_orders.loc[\"0002115f48f982\"])","metadata":{"execution":{"iopub.status.busy":"2022-06-04T09:46:14.05645Z","iopub.execute_input":"2022-06-04T09:46:14.056923Z","iopub.status.idle":"2022-06-04T09:46:14.065266Z","shell.execute_reply.started":"2022-06-04T09:46:14.056885Z","shell.execute_reply":"2022-06-04T09:46:14.064276Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cell_order = df_orders.loc[nb_id]\n\nprint(\"The ordered notebook:\")\nnb.loc[cell_order, :]","metadata":{"execution":{"iopub.status.busy":"2022-06-04T09:46:14.06645Z","iopub.execute_input":"2022-06-04T09:46:14.067176Z","iopub.status.idle":"2022-06-04T09:46:14.092622Z","shell.execute_reply.started":"2022-06-04T09:46:14.067137Z","shell.execute_reply":"2022-06-04T09:46:14.091767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_ranks(base, derived):\n    return [base.index(d) for d in derived]\n\ncell_ranks = get_ranks(cell_order, list(nb.index))\nnb.insert(0, 'rank', cell_ranks)\n\nnb","metadata":{"execution":{"iopub.status.busy":"2022-06-04T09:46:14.094262Z","iopub.execute_input":"2022-06-04T09:46:14.094664Z","iopub.status.idle":"2022-06-04T09:46:14.122874Z","shell.execute_reply.started":"2022-06-04T09:46:14.0946Z","shell.execute_reply":"2022-06-04T09:46:14.121909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_orders_ = df_orders.to_frame().join(\n    df.reset_index('cell_id').groupby('id')['cell_id'].apply(list),\n    how='right',\n)\n\nranks = {}\nfor id_, cell_order, cell_id in df_orders_.itertuples():\n    ranks[id_] = {'cell_id': cell_id, 'rank': get_ranks(cell_order, cell_id)}\n\ndf_ranks = (\n    pd.DataFrame\n    .from_dict(ranks, orient='index')\n    .rename_axis('id')\n    .apply(pd.Series.explode)\n    .set_index('cell_id', append=True)\n)\n\ndf_ranks","metadata":{"execution":{"iopub.status.busy":"2022-06-04T09:46:14.124108Z","iopub.execute_input":"2022-06-04T09:46:14.124591Z","iopub.status.idle":"2022-06-04T09:46:14.236358Z","shell.execute_reply.started":"2022-06-04T09:46:14.124559Z","shell.execute_reply":"2022-06-04T09:46:14.235323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_ancestors = pd.read_csv(data_dir / 'train_ancestors.csv', index_col='id')\ndf_ancestors","metadata":{"execution":{"iopub.status.busy":"2022-06-04T09:46:14.237946Z","iopub.execute_input":"2022-06-04T09:46:14.238303Z","iopub.status.idle":"2022-06-04T09:46:14.481551Z","shell.execute_reply.started":"2022-06-04T09:46:14.238271Z","shell.execute_reply":"2022-06-04T09:46:14.480476Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df.reset_index().merge(df_ranks, on=[\"id\", \"cell_id\"]).merge(df_ancestors, on=[\"id\"])\ndf","metadata":{"execution":{"iopub.status.busy":"2022-06-04T09:46:14.482981Z","iopub.execute_input":"2022-06-04T09:46:14.483914Z","iopub.status.idle":"2022-06-04T09:46:14.577478Z","shell.execute_reply.started":"2022-06-04T09:46:14.483871Z","shell.execute_reply":"2022-06-04T09:46:14.576407Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df[\"pct_rank\"] = df[\"rank\"] / df.groupby(\"id\")[\"cell_id\"].transform(\"count\")\n\ndf[\"pct_rank\"].hist(bins=10)","metadata":{"execution":{"iopub.status.busy":"2022-06-04T09:46:14.578965Z","iopub.execute_input":"2022-06-04T09:46:14.580136Z","iopub.status.idle":"2022-06-04T09:46:15.091606Z","shell.execute_reply.started":"2022-06-04T09:46:14.58007Z","shell.execute_reply":"2022-06-04T09:46:15.09027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import GroupShuffleSplit\n\nNVALID = 0.1  # size of validation set\n\nsplitter = GroupShuffleSplit(n_splits=1, test_size=NVALID, random_state=0)\n\ntrain_ind, val_ind = next(splitter.split(df, groups=df[\"ancestor_id\"]))\n\ndf_train = df.loc[train_ind].reset_index(drop=True)\ndf_val = df.loc[val_ind].reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-06-04T09:46:15.095163Z","iopub.execute_input":"2022-06-04T09:46:15.095579Z","iopub.status.idle":"2022-06-04T09:46:15.477371Z","shell.execute_reply.started":"2022-06-04T09:46:15.095541Z","shell.execute_reply":"2022-06-04T09:46:15.476153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_val.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-04T09:46:15.478848Z","iopub.execute_input":"2022-06-04T09:46:15.479325Z","iopub.status.idle":"2022-06-04T09:46:15.495115Z","shell.execute_reply.started":"2022-06-04T09:46:15.479279Z","shell.execute_reply":"2022-06-04T09:46:15.493993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from bisect import bisect\n\n\ndef count_inversions(a):\n    inversions = 0\n    sorted_so_far = []\n    for i, u in enumerate(a):\n        j = bisect(sorted_so_far, u)\n        inversions += i - j\n        sorted_so_far.insert(j, u)\n    return inversions\n\n\ndef kendall_tau(ground_truth, predictions):\n    total_inversions = 0\n    total_2max = 0  # twice the maximum possible inversions across all instances\n    for gt, pred in zip(ground_truth, predictions):\n        ranks = [gt.index(x) for x in pred]  # rank predicted order in terms of ground truth\n        total_inversions += count_inversions(ranks)\n        n = len(gt)\n        total_2max += n * (n - 1)\n    return 1 - 4 * total_inversions / total_2max","metadata":{"execution":{"iopub.status.busy":"2022-06-04T09:46:15.496718Z","iopub.execute_input":"2022-06-04T09:46:15.497121Z","iopub.status.idle":"2022-06-04T09:46:15.509453Z","shell.execute_reply.started":"2022-06-04T09:46:15.497076Z","shell.execute_reply":"2022-06-04T09:46:15.508312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_dummy = df_val.groupby('id')['cell_id'].apply(list)\nkendall_tau(df_orders.loc[y_dummy.index], y_dummy)","metadata":{"execution":{"iopub.status.busy":"2022-06-04T09:46:15.510832Z","iopub.execute_input":"2022-06-04T09:46:15.511303Z","iopub.status.idle":"2022-06-04T09:46:15.532055Z","shell.execute_reply.started":"2022-06-04T09:46:15.511262Z","shell.execute_reply":"2022-06-04T09:46:15.530888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_val[\"cell_type\"].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-06-04T09:46:15.533561Z","iopub.execute_input":"2022-06-04T09:46:15.53409Z","iopub.status.idle":"2022-06-04T09:46:15.543705Z","shell.execute_reply.started":"2022-06-04T09:46:15.534041Z","shell.execute_reply":"2022-06-04T09:46:15.542947Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train_mark = df_train[df_train[\"cell_type\"] == \"markdown\"].reset_index(drop=True)\n\ndf_val_mark = df_val[df_val[\"cell_type\"] == \"markdown\"].reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-06-04T09:46:15.544936Z","iopub.execute_input":"2022-06-04T09:46:15.54602Z","iopub.status.idle":"2022-06-04T09:46:15.562192Z","shell.execute_reply.started":"2022-06-04T09:46:15.545976Z","shell.execute_reply":"2022-06-04T09:46:15.561161Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import mean_squared_error\n\nmean_squared_error(df_val_mark[\"pct_rank\"], np.ones(df_val_mark.shape[0])*df_train_mark[\"pct_rank\"].mean())","metadata":{"execution":{"iopub.status.busy":"2022-06-04T09:46:15.563735Z","iopub.execute_input":"2022-06-04T09:46:15.564156Z","iopub.status.idle":"2022-06-04T09:46:15.580743Z","shell.execute_reply.started":"2022-06-04T09:46:15.564119Z","shell.execute_reply":"2022-06-04T09:46:15.579955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm\nimport sys, os\nfrom transformers import DistilBertModel, DistilBertTokenizer\nimport torch.nn.functional as F\nimport torch.nn as nn\nimport torch\n\nMAX_LEN = 128\n\nclass MarkdownModel(nn.Module):\n    def __init__(self):\n        super(MarkdownModel, self).__init__()\n        self.distill_bert = DistilBertModel.from_pretrained(BERT_PATH)\n\n        self.top1 = nn.Linear(768, 64)\n        self.top2 = nn.Linear(64, 1)\n\n        self.dropout1 = torch.nn.Dropout(p=0.2)\n        self.dropout2 = torch.nn.Dropout(p=0.2)\n        \n    def forward(self, ids, mask):\n        x = self.distill_bert(ids, mask)[0][:, 0, :]\n        x = self.dropout1(x)\n        x0 = self.top1(x)\n        x = self.dropout2(x0)\n        x = self.top2(x)\n        x = torch.sigmoid(x)\n        return x","metadata":{"execution":{"iopub.status.busy":"2022-06-04T09:46:15.581943Z","iopub.execute_input":"2022-06-04T09:46:15.582904Z","iopub.status.idle":"2022-06-04T09:46:17.985549Z","shell.execute_reply.started":"2022-06-04T09:46:15.582847Z","shell.execute_reply":"2022-06-04T09:46:17.984419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nranks = {}\nfor id_, cell_order, cell_id in df_orders_.itertuples():\n    ranks[id_] = {'cell_id': cell_id, 'rank': get_ranks(cell_order, cell_id)}\n\nds_ranks = (\n    pd.DataFrame\n    .from_dict(ranks, orient='index')\n    .rename_axis('id')\n    .apply(pd.Series.explode)\n    .set_index('cell_id', append=True)\n)\n\nds_ranks\n","metadata":{"execution":{"iopub.status.busy":"2022-06-04T09:46:17.987307Z","iopub.execute_input":"2022-06-04T09:46:17.988713Z","iopub.status.idle":"2022-06-04T09:46:18.045969Z","shell.execute_reply.started":"2022-06-04T09:46:17.988664Z","shell.execute_reply":"2022-06-04T09:46:18.044782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds_ancestors = pd.read_csv(data_dir / 'train_ancestors.csv', index_col='id')\nds_ancestors","metadata":{"execution":{"iopub.status.busy":"2022-06-04T09:46:18.047461Z","iopub.execute_input":"2022-06-04T09:46:18.047835Z","iopub.status.idle":"2022-06-04T09:46:18.285873Z","shell.execute_reply.started":"2022-06-04T09:46:18.047779Z","shell.execute_reply":"2022-06-04T09:46:18.284789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds = df.reset_index().merge(ds_ranks, on=[\"id\", \"cell_id\"]).merge(ds_ancestors, on=[\"id\"])\nds","metadata":{"execution":{"iopub.status.busy":"2022-06-04T09:46:18.287421Z","iopub.execute_input":"2022-06-04T09:46:18.288212Z","iopub.status.idle":"2022-06-04T09:46:18.386305Z","shell.execute_reply.started":"2022-06-04T09:46:18.288161Z","shell.execute_reply":"2022-06-04T09:46:18.385112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import GroupShuffleSplit\n\nNVALID = 0.1  # size of validation set\n\nsplitter = GroupShuffleSplit(n_splits=1, test_size=NVALID, random_state=0)\n\ntrain_ind, val_ind = next(splitter.split(ds, groups=ds[\"ancestor_id_x\"]))\n\nds_train = ds.loc[train_ind].reset_index(drop=True)\nds_val = ds.loc[val_ind].reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-06-04T09:46:18.388786Z","iopub.execute_input":"2022-06-04T09:46:18.389657Z","iopub.status.idle":"2022-06-04T09:46:18.415042Z","shell.execute_reply.started":"2022-06-04T09:46:18.389592Z","shell.execute_reply":"2022-06-04T09:46:18.414141Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds_train= ds_train[df_train[\"cell_type\"] == \"markdown\"].reset_index(drop=True)\nds_val = ds_val[df_val [\"cell_type\"] == \"markdown\"].reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-06-04T09:46:18.416337Z","iopub.execute_input":"2022-06-04T09:46:18.416834Z","iopub.status.idle":"2022-06-04T09:46:18.427755Z","shell.execute_reply.started":"2022-06-04T09:46:18.41679Z","shell.execute_reply":"2022-06-04T09:46:18.426909Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ds_val.iloc[0]\n","metadata":{"execution":{"iopub.status.busy":"2022-06-04T09:46:18.429082Z","iopub.execute_input":"2022-06-04T09:46:18.429657Z","iopub.status.idle":"2022-06-04T09:46:18.442295Z","shell.execute_reply.started":"2022-06-04T09:46:18.429622Z","shell.execute_reply":"2022-06-04T09:46:18.441383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def adjust_lr(optimizer, epoch):\n    if epoch < 1:\n        lr = 5e-5\n    elif epoch < 2:\n        lr = 4e-5\n    elif epoch < 5:\n        lr = 3e-5\n    else:\n        lr = 2e-5\n\n    for p in optimizer.param_groups:\n        p['lr'] = lr\n    return lr\n    \ndef get_optimizer(net):\n    optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad, net.parameters()), lr=3e-4, betas=(0.9, 0.999),\n                                 eps=1e-08)\n    return optimizer","metadata":{"execution":{"iopub.status.busy":"2022-06-04T09:46:18.44354Z","iopub.execute_input":"2022-06-04T09:46:18.44434Z","iopub.status.idle":"2022-06-04T09:46:18.45657Z","shell.execute_reply.started":"2022-06-04T09:46:18.4443Z","shell.execute_reply":"2022-06-04T09:46:18.455547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import DataLoader, Dataset\n","metadata":{"execution":{"iopub.status.busy":"2022-06-04T09:46:18.45786Z","iopub.execute_input":"2022-06-04T09:46:18.458277Z","iopub.status.idle":"2022-06-04T09:46:18.479263Z","shell.execute_reply.started":"2022-06-04T09:46:18.458241Z","shell.execute_reply":"2022-06-04T09:46:18.478094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"BS = 128\nNW = 8\n\ntrain_loader = DataLoader(ds_train, batch_size=BS, shuffle=True, num_workers=NW,\n                          pin_memory=False, drop_last=True)\nval_loader = DataLoader(ds_val, batch_size=BS, shuffle=False, num_workers=NW,\n                          pin_memory=False, drop_last=False)","metadata":{"execution":{"iopub.status.busy":"2022-06-04T09:46:18.48067Z","iopub.execute_input":"2022-06-04T09:46:18.481155Z","iopub.status.idle":"2022-06-04T09:46:18.496739Z","shell.execute_reply.started":"2022-06-04T09:46:18.481116Z","shell.execute_reply":"2022-06-04T09:46:18.495653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nfrom sklearn.model_selection import train_test_split\nx_train, x_test, y_train, y_test = train_test_split(df, ds, test_size = 0.3)\n","metadata":{"execution":{"iopub.status.busy":"2022-06-04T09:46:18.498828Z","iopub.execute_input":"2022-06-04T09:46:18.499702Z","iopub.status.idle":"2022-06-04T09:46:18.515249Z","shell.execute_reply.started":"2022-06-04T09:46:18.49964Z","shell.execute_reply":"2022-06-04T09:46:18.514243Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"paths_test = list((data_dir / 'test').glob('*.json'))\nnotebooks_test = [\n    read_notebook(path) for path in tqdm(paths_test, desc='Test NBs')\n]\ntest_df = (\n    pd.concat(notebooks_test)\n    .set_index('id', append=True)\n    .swaplevel()\n    .sort_index(level='id', sort_remaining=False)\n).reset_index()\n","metadata":{"execution":{"iopub.status.busy":"2022-06-04T09:46:18.516969Z","iopub.execute_input":"2022-06-04T09:46:18.517876Z","iopub.status.idle":"2022-06-04T09:46:18.568761Z","shell.execute_reply.started":"2022-06-04T09:46:18.517785Z","shell.execute_reply":"2022-06-04T09:46:18.567902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ntest_df[\"rank\"] = test_df.groupby([\"id\", \"cell_type\"]).cumcount()\ntest_df[\"pred\"] = test_df.groupby([\"id\", \"cell_type\"])[\"rank\"].rank(pct=True)","metadata":{"execution":{"iopub.status.busy":"2022-06-04T09:46:18.570079Z","iopub.execute_input":"2022-06-04T09:46:18.570606Z","iopub.status.idle":"2022-06-04T09:46:18.581165Z","shell.execute_reply.started":"2022-06-04T09:46:18.570571Z","shell.execute_reply":"2022-06-04T09:46:18.579995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nlen(test_df)","metadata":{"execution":{"iopub.status.busy":"2022-06-04T09:46:18.582869Z","iopub.execute_input":"2022-06-04T09:46:18.583491Z","iopub.status.idle":"2022-06-04T09:46:18.59886Z","shell.execute_reply.started":"2022-06-04T09:46:18.583451Z","shell.execute_reply":"2022-06-04T09:46:18.597723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.loc[test_df[\"cell_type\"] == \"markdown\", \"pred\"] = y_test\n","metadata":{"execution":{"iopub.status.busy":"2022-06-04T09:46:18.600537Z","iopub.execute_input":"2022-06-04T09:46:18.601262Z","iopub.status.idle":"2022-06-04T09:46:18.614406Z","shell.execute_reply.started":"2022-06-04T09:46:18.60122Z","shell.execute_reply":"2022-06-04T09:46:18.613369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df = test_df.sort_values(\"pred\").groupby(\"id\")[\"cell_id\"].apply(lambda x: \" \".join(x)).reset_index()\nsub_df.rename(columns={\"cell_id\": \"cell_order\"}, inplace=True)\nsub_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-06-04T09:46:18.615705Z","iopub.execute_input":"2022-06-04T09:46:18.616074Z","iopub.status.idle":"2022-06-04T09:46:18.640273Z","shell.execute_reply.started":"2022-06-04T09:46:18.616042Z","shell.execute_reply":"2022-06-04T09:46:18.639405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df.to_csv(\"submission.csv\", index=False)\n","metadata":{"execution":{"iopub.status.busy":"2022-06-04T09:46:18.641255Z","iopub.execute_input":"2022-06-04T09:46:18.642111Z","iopub.status.idle":"2022-06-04T09:46:18.64922Z","shell.execute_reply.started":"2022-06-04T09:46:18.64207Z","shell.execute_reply":"2022-06-04T09:46:18.648215Z"},"trusted":true},"execution_count":null,"outputs":[]}]}