{"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":"# 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","execution":{"iopub.status.busy":"2023-01-07T20:15:59.346702Z","iopub.execute_input":"2023-01-07T20:15:59.347095Z","iopub.status.idle":"2023-01-07T20:15:59.354475Z","shell.execute_reply.started":"2023-01-07T20:15:59.347065Z","shell.execute_reply":"2023-01-07T20:15:59.353364Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\npd.set_option('display.max_colwidth', None)\n\n\ntrain = pd.read_csv(\"/kaggle/input/emsi-5iir-ds/train.csv\") \ntest = pd.read_csv(\"/kaggle/input/emsi-5iir-ds/test.csv\") \nfIG_SIZE = (8, 5)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:15:59.359607Z","iopub.execute_input":"2023-01-07T20:15:59.360158Z","iopub.status.idle":"2023-01-07T20:16:06.579554Z","shell.execute_reply.started":"2023-01-07T20:15:59.360128Z","shell.execute_reply":"2023-01-07T20:16:06.578413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:16:06.581761Z","iopub.execute_input":"2023-01-07T20:16:06.582850Z","iopub.status.idle":"2023-01-07T20:16:06.608599Z","shell.execute_reply.started":"2023-01-07T20:16:06.582803Z","shell.execute_reply":"2023-01-07T20:16:06.607712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\n\n\ndef display_data_info(dict_df):\n    '''\n        Show how many rows and cols, \n        number of missing rows,\n        number of columns withs missing values,\n        number of duplicates\n    '''\n    \n    # Table column names\n    index = [\n        'No. rows'          , \n        'No. cols'          , \n        'No. null cols'     , \n        'No. missing values', \n        'No. duplicate rows', \n        'Float columns'     ,\n        'Int columns'       ,\n        'Object columns'    ,\n    ]\n    \n    # empty data\n    data = {}\n    \n    # looping over each dataframe\n    for name, df in dict_df.items():\n        \n        # getting dataframe info\n        num_rows, num_cols = df.shape                  # no. of rows , no. cols\n        num_null_cols      = np.sum(df.isna().sum()>0) # No. null cols\n        num_null_rows      = df.isna().sum().sum()     # no. missing values\n        num_duplic_rows    = np.sum(df.duplicated()>0) # no. duplicated rows\n        data_types         = df.dtypes.to_dict()       # data types\n        \n        # getting column of each type\n        float_cols         = []\n        int_cols           = []\n        obj_cols           = []\n        for col, t in data_types.items():\n            if t == 'float64' : float_cols.append(col)\n            elif t == 'int64' : int_cols.append(col)\n            else              : obj_cols.append(col)\n            \n        data[name] = [\n            num_rows        , \n            num_cols        , \n            num_null_cols   , \n            num_null_rows   , \n            num_duplic_rows , \n            float_cols      ,\n            int_cols        ,\n            obj_cols        ,\n        ]\n        \n    new_df = pd.DataFrame(data=data, index=index)\n    \n    display(new_df)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:16:06.610230Z","iopub.execute_input":"2023-01-07T20:16:06.610578Z","iopub.status.idle":"2023-01-07T20:16:06.620243Z","shell.execute_reply.started":"2023-01-07T20:16:06.610548Z","shell.execute_reply":"2023-01-07T20:16:06.619164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_data_info({'train': train, 'test': test})","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:16:06.622533Z","iopub.execute_input":"2023-01-07T20:16:06.622881Z","iopub.status.idle":"2023-01-07T20:16:10.868197Z","shell.execute_reply.started":"2023-01-07T20:16:06.622852Z","shell.execute_reply":"2023-01-07T20:16:10.867186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def barchart(df, column):\n    '''draws a barchart based on the column name'''\n    ax = df[column].value_counts().head(10).plot.bar(figsize=fIG_SIZE)\n    ax.bar_label(ax.containers[0])\n    display(ax)\n    \ndef pie(df, column):\n    '''draws a pie based on the column name'''\n    display(df[column].value_counts().head(10).plot(kind='pie', autopct='%1.1f%%', figsize=fIG_SIZE))","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:16:10.869561Z","iopub.execute_input":"2023-01-07T20:16:10.870082Z","iopub.status.idle":"2023-01-07T20:16:10.877013Z","shell.execute_reply.started":"2023-01-07T20:16:10.870051Z","shell.execute_reply":"2023-01-07T20:16:10.875958Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"barchart(train, 'target')","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:16:10.878299Z","iopub.execute_input":"2023-01-07T20:16:10.878622Z","iopub.status.idle":"2023-01-07T20:16:11.020526Z","shell.execute_reply.started":"2023-01-07T20:16:10.878574Z","shell.execute_reply":"2023-01-07T20:16:11.019483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# int columns\ntrain.hist(column=[\n    'f_07', 'f_08', \n    'f_09', 'f_10', \n    'f_11', 'f_12', \n    'f_13', 'f_14', \n    'f_15', 'f_16', \n    'f_17', 'f_18', \n    'f_29', 'f_30'\n], figsize=(20,15), bins=25);","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:16:11.021895Z","iopub.execute_input":"2023-01-07T20:16:11.022961Z","iopub.status.idle":"2023-01-07T20:16:13.873310Z","shell.execute_reply.started":"2023-01-07T20:16:11.022899Z","shell.execute_reply":"2023-01-07T20:16:13.872204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# float columns\ntrain.hist(column=[\n    'f_00', 'f_01', \n    'f_02', 'f_03', \n    'f_04', 'f_05', \n    'f_06', 'f_19', \n    'f_20', 'f_21', \n    'f_22', 'f_23', \n    'f_24', 'f_25', \n    'f_26', 'f_28'\n], figsize=(20,15), bins=25);","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:16:13.874941Z","iopub.execute_input":"2023-01-07T20:16:13.875974Z","iopub.status.idle":"2023-01-07T20:16:17.111972Z","shell.execute_reply.started":"2023-01-07T20:16:13.875925Z","shell.execute_reply":"2023-01-07T20:16:17.110978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# object columns \nbarchart(train, 'f_27')","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:16:17.113705Z","iopub.execute_input":"2023-01-07T20:16:17.114297Z","iopub.status.idle":"2023-01-07T20:16:17.940096Z","shell.execute_reply.started":"2023-01-07T20:16:17.114265Z","shell.execute_reply":"2023-01-07T20:16:17.938995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from collections import Counter\n\n\ndef add_feature(df):\n    # Feature interactions: create three ternary features\n    # Every ternary feature can have the values -1, 0 and +1\n    df['i_02_21'] = (df.f_21 + df.f_02 > 5.2).astype(int) - (df.f_21 + df.f_02 < -5.3).astype(int)\n    df['i_05_22'] = (df.f_22 + df.f_05 > 5.1).astype(int) - (df.f_22 + df.f_05 < -5.4).astype(int)\n    i_00_01_26 = df.f_00 + df.f_01 + df.f_26\n    df['i_00_01_26'] = (i_00_01_26 > 5.0).astype(int) - (i_00_01_26 < -5.0).astype(int)\n    \n    \ndef encode(df):\n    index = [\n        'A','B','C','D','E','F',\n        'G','H','I','J','K','L',\n        'M','N','O','P','Q','R',\n        'S','T'\n    ]\n    \n    df['uniq_chars'] = df['f_27'].apply(lambda w: len(set(w)))\n    df['prefix']     = 0\n    df['suffix']     = 0\n\n    for col in index:\n        df[col] = 0\n        df[f'ch{col}'] = 0\n    \n    for k, v in df['f_27'].iteritems():\n        alpha_count = Counter(v)\n        \n        prefix = 0\n        for p in v[:4]:\n            prefix += ord(p)\n            \n        suffix = 0\n        for s in v[-4:]:\n            suffix += ord(p)\n            \n        df.at[k, 'prefix'] = prefix\n        df.at[k, 'suffix'] = suffix\n        \n        for i, (l, c) in enumerate(alpha_count.items()):\n            df.at[k, l]        = c\n            df.at[k, f'ch{l}'] = ord(l) - ord('A')\n            \n        \n    add_feature(df)\n    display(df.head())","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:16:17.943817Z","iopub.execute_input":"2023-01-07T20:16:17.944483Z","iopub.status.idle":"2023-01-07T20:16:17.958223Z","shell.execute_reply.started":"2023-01-07T20:16:17.944449Z","shell.execute_reply":"2023-01-07T20:16:17.957163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"relevent_train = train\nencode(relevent_train)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:16:17.960099Z","iopub.execute_input":"2023-01-07T20:16:17.960595Z","iopub.status.idle":"2023-01-07T20:18:14.854323Z","shell.execute_reply.started":"2023-01-07T20:16:17.960548Z","shell.execute_reply":"2023-01-07T20:18:14.853325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"relevent_train = relevent_train.drop('f_27', axis=1)\nrelevent_train = relevent_train.drop('id', axis=1)\n# from sklearn.preprocessing import LabelEncoder\n\n# le = LabelEncoder()\n\n# relevent_train['f_27'] = le.fit_transform(train['f_27'])","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:18:22.474353Z","iopub.execute_input":"2023-01-07T20:18:22.474732Z","iopub.status.idle":"2023-01-07T20:18:22.498805Z","shell.execute_reply.started":"2023-01-07T20:18:22.474702Z","shell.execute_reply":"2023-01-07T20:18:22.497352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"relevent_train[['uniq_chars', 'prefix', 'suffix', 'target']].head(20)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:18:54.803537Z","iopub.execute_input":"2023-01-07T20:18:54.803971Z","iopub.status.idle":"2023-01-07T20:18:54.822080Z","shell.execute_reply.started":"2023-01-07T20:18:54.803937Z","shell.execute_reply":"2023-01-07T20:18:54.820932Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n\ny = relevent_train['target']\nX = relevent_train.drop('target', axis=1)\n\nX_train, X_valid, y_train, y_valid = train_test_split(\n    X, y, \n    test_size=0.2,\n    train_size=0.8, \n    shuffle=True,\n    random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:19:21.227371Z","iopub.execute_input":"2023-01-07T20:19:21.227937Z","iopub.status.idle":"2023-01-07T20:19:22.126691Z","shell.execute_reply.started":"2023-01-07T20:19:21.227874Z","shell.execute_reply":"2023-01-07T20:19:22.125466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.options.mode.chained_assignment = None\n\nf = [x for x in X_train.columns.values if x[0]==\"f\"]\n\nX_train['abs_sum'] = X_train.loc[:,f].abs().sum(axis=1)\nX_train['median']  = X_train.loc[:,f].median(axis=1)\nX_train['std']     = X_train.loc[:,f].std(axis=1)\nX_train['mean']    = X_train.loc[:,f].mean(axis=1)\n\nX_train.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:19:29.576250Z","iopub.execute_input":"2023-01-07T20:19:29.577448Z","iopub.status.idle":"2023-01-07T20:19:32.126112Z","shell.execute_reply.started":"2023-01-07T20:19:29.577374Z","shell.execute_reply":"2023-01-07T20:19:32.125077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"relevent_features = X_train.columns","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:21:21.656072Z","iopub.execute_input":"2023-01-07T20:21:21.656444Z","iopub.status.idle":"2023-01-07T20:21:21.661298Z","shell.execute_reply.started":"2023-01-07T20:21:21.656415Z","shell.execute_reply":"2023-01-07T20:21:21.659975Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_valid['abs_sum'] = X_valid.loc[:,f].abs().sum(axis=1)\nX_valid['median']  = X_valid.loc[:,f].median(axis=1)\nX_valid['std']     = X_valid.loc[:,f].std(axis=1)\nX_valid['mean']    = X_valid.loc[:,f].mean(axis=1)\n\n\npd.options.mode.chained_assignment = 'warn'\nX_valid.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:21:34.522845Z","iopub.execute_input":"2023-01-07T20:21:34.523267Z","iopub.status.idle":"2023-01-07T20:21:35.098208Z","shell.execute_reply.started":"2023-01-07T20:21:34.523232Z","shell.execute_reply":"2023-01-07T20:21:35.097156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\nscaler = StandardScaler()\n\ndef robust_scale(X_t, X_v):\n    return pd.DataFrame(scaler.fit_transform(X_t)), pd.DataFrame(scaler.transform(X_v))\n\nX_train_st, X_val_st = robust_scale(X_train, X_valid)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:21:45.584034Z","iopub.execute_input":"2023-01-07T20:21:45.584448Z","iopub.status.idle":"2023-01-07T20:21:46.819285Z","shell.execute_reply.started":"2023-01-07T20:21:45.584414Z","shell.execute_reply":"2023-01-07T20:21:46.818115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_data_info({'train': X_train_st, 'test': X_val_st})","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:22:24.788214Z","iopub.execute_input":"2023-01-07T20:22:24.788611Z","iopub.status.idle":"2023-01-07T20:22:28.381710Z","shell.execute_reply.started":"2023-01-07T20:22:24.788580Z","shell.execute_reply":"2023-01-07T20:22:28.380642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\n\n\nclass Network(nn.Module):\n    def __init__(self, in_feature):\n        super().__init__()\n        \n        self.main = nn.Sequential(\n            nn.Linear(in_feature, 512),\n            nn.SiLU(),\n#             nn.LeakyReLU(0.2, inplace=True),\n            nn.Dropout(0.3),\n            \n            nn.Linear(512, 256),\n            nn.SiLU(),\n#             nn.LeakyReLU(0.2, inplace=True),\n            nn.Linear(256, 128),\n            nn.SiLU(),\n#             nn.LeakyReLU(0.2, inplace=True),\n            nn.Linear(128, 128),\n            nn.SiLU(),\n#             nn.LeakyReLU(0.2, inplace=True),\n            nn.Dropout(0.3),\n            \n            nn.Linear(128, 64),\n            nn.SiLU(),\n#             nn.LeakyReLU(0.2, inplace=True),\n            nn.Dropout(0.3),\n\n            nn.Linear(64, 1),\n            nn.Sigmoid()\n        )\n        \n    \n    def forward(self, x):\n        x = self.main(x)\n        return x.view(-1) ","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:22:58.845144Z","iopub.execute_input":"2023-01-07T20:22:58.846218Z","iopub.status.idle":"2023-01-07T20:23:00.286185Z","shell.execute_reply.started":"2023-01-07T20:22:58.846177Z","shell.execute_reply":"2023-01-07T20:23:00.285296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch import optim\n\ndevice         = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\nin_feature     = X_train.shape[1]\n\nmodel          = Network(in_feature)\nmodel.to(device)\n\nepochs         = 100\nbatch_size     = 1024\n\nloss_function  = nn.BCELoss()\n\nmax_learning_rate = 0.01\noptimizer      = optim.Adam(model.parameters(), lr=max_learning_rate, weight_decay=1e-6)\nscheduler      = optim.lr_scheduler.OneCycleLR(optimizer,\n                                              max_lr = max_learning_rate,\n                                              epochs = epochs,\n                                              steps_per_epoch = 720000,\n                                              pct_start = 0.01,\n                                              anneal_strategy = \"cos\")\n# optim.lr_scheduler.StepLR(optimizer, step_size=20, gamma=0.01)\n","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:23:26.097848Z","iopub.execute_input":"2023-01-07T20:23:26.098487Z","iopub.status.idle":"2023-01-07T20:23:26.114801Z","shell.execute_reply.started":"2023-01-07T20:23:26.098448Z","shell.execute_reply":"2023-01-07T20:23:26.113824Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\n\nclass TPS2022(torch.utils.data.Dataset):\n    def __init__(self, X, Y):\n        self.X = torch.tensor(X.values,dtype=torch.float)\n        self.Y = torch.tensor(Y.values,dtype=torch.float)\n        \n    def __len__(self):\n        return len(self.Y)\n    \n    def __getitem__(self, idx):\n        return self.X[idx], self.Y[idx]","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:23:51.495754Z","iopub.execute_input":"2023-01-07T20:23:51.496153Z","iopub.status.idle":"2023-01-07T20:23:51.504192Z","shell.execute_reply.started":"2023-01-07T20:23:51.496121Z","shell.execute_reply":"2023-01-07T20:23:51.502986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import DataLoader\n\ntrain_dataset = TPS2022(X_train_st, y_train)\nvalid_dataset = TPS2022(X_val_st, y_valid)\n\ntrain_loader  = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)\nvalid_loader  = DataLoader(valid_dataset, batch_size=batch_size, shuffle=False)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:24:05.271524Z","iopub.execute_input":"2023-01-07T20:24:05.271952Z","iopub.status.idle":"2023-01-07T20:24:05.455233Z","shell.execute_reply.started":"2023-01-07T20:24:05.271894Z","shell.execute_reply":"2023-01-07T20:24:05.454195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_losses, valid_losses = [], []\nvalid_loss_min = np.Inf\n\nfor i in range(epochs):\n    tot_train_loss = 0\n    \n    model.train()\n    \n    for x, y in train_loader:\n        pred = model(x.to(device))\n        loss = loss_function(pred, y.to(device))\n        \n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()\n        \n        tot_train_loss += loss.item()\n        \n    else:\n        tot_valid_loss = 0\n        valid_correct = 0\n        \n        model.eval()\n        with torch.no_grad():\n        \n            for x, y in valid_loader:      \n                pred = model(x.to(device))\n                loss = loss_function(pred, y.to(device))\n                tot_valid_loss += loss.item()\n        \n        train_loss = tot_valid_loss / len(train_loader.dataset)\n        valid_loss = tot_valid_loss / len(valid_loader.dataset)\n        \n        train_losses.append(train_loss)\n        valid_losses.append(valid_loss)\n        \n        print(\"Epoch: {}/{}.. \".format(i+1, epochs),\n              \"Training Loss: {:.6f}.. \".format(train_loss),\n              \"Test Loss: {:.6f}.. \".format(valid_loss),\n              )\n        \n        if valid_loss <= valid_loss_min:\n            print('Validation loss decreased ({:.6f} --> {:.6f}).  Saving model ...'.format(\n            valid_loss_min,\n            valid_loss))\n            torch.save(model.state_dict(), 'model.pt')\n            valid_loss_min = valid_loss\n            \n        scheduler.step()","metadata":{"execution":{"iopub.status.busy":"2023-01-07T20:34:59.045376Z","iopub.execute_input":"2023-01-07T20:34:59.045785Z","iopub.status.idle":"2023-01-07T21:07:35.735785Z","shell.execute_reply.started":"2023-01-07T20:34:59.045749Z","shell.execute_reply":"2023-01-07T21:07:35.734593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n\nplt.plot(train_losses, label='Train Loss')\nplt.plot(valid_losses, label='Valid Loss')","metadata":{"execution":{"iopub.status.busy":"2023-01-07T21:12:32.770584Z","iopub.execute_input":"2023-01-07T21:12:32.770978Z","iopub.status.idle":"2023-01-07T21:12:32.918725Z","shell.execute_reply.started":"2023-01-07T21:12:32.770949Z","shell.execute_reply":"2023-01-07T21:12:32.917515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Network(in_feature)\nmodel.load_state_dict(torch.load('model.pt'))\n\nmodel.to(device);","metadata":{"execution":{"iopub.status.busy":"2023-01-07T21:13:21.513269Z","iopub.execute_input":"2023-01-07T21:13:21.513645Z","iopub.status.idle":"2023-01-07T21:13:21.524151Z","shell.execute_reply.started":"2023-01-07T21:13:21.513615Z","shell.execute_reply":"2023-01-07T21:13:21.523162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"relevent_test = test.iloc[:, 1:]","metadata":{"execution":{"iopub.status.busy":"2023-01-07T21:13:49.710893Z","iopub.execute_input":"2023-01-07T21:13:49.711972Z","iopub.status.idle":"2023-01-07T21:13:49.812410Z","shell.execute_reply.started":"2023-01-07T21:13:49.711904Z","shell.execute_reply":"2023-01-07T21:13:49.811447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"encode(relevent_test)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T21:16:17.622291Z","iopub.execute_input":"2023-01-07T21:16:17.622687Z","iopub.status.idle":"2023-01-07T21:16:17.682004Z","shell.execute_reply.started":"2023-01-07T21:16:17.622653Z","shell.execute_reply":"2023-01-07T21:16:17.680602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"relevent_test = relevent_test.drop('f_27', axis=1)\n# relevent_test['f_27'] = le.fit_transform(relevent_test['f_27'])\n\nrelevent_test['abs_sum'] = relevent_test.loc[:,f].abs().sum(axis=1)\nrelevent_test['median']  = relevent_test.loc[:,f].median(axis=1)\nrelevent_test['std']     = relevent_test.loc[:,f].std(axis=1)\nrelevent_test['mean']    = relevent_test.loc[:,f].mean(axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T21:16:11.235411Z","iopub.execute_input":"2023-01-07T21:16:11.235812Z","iopub.status.idle":"2023-01-07T21:16:11.269629Z","shell.execute_reply.started":"2023-01-07T21:16:11.235776Z","shell.execute_reply":"2023-01-07T21:16:11.267163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"relevent_features","metadata":{"execution":{"iopub.status.busy":"2023-01-07T21:16:35.043330Z","iopub.execute_input":"2023-01-07T21:16:35.043723Z","iopub.status.idle":"2023-01-07T21:16:35.049906Z","shell.execute_reply.started":"2023-01-07T21:16:35.043691Z","shell.execute_reply":"2023-01-07T21:16:35.049178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"relevent_test.columns","metadata":{"execution":{"iopub.status.busy":"2023-01-07T21:18:34.200800Z","iopub.execute_input":"2023-01-07T21:18:34.201303Z","iopub.status.idle":"2023-01-07T21:18:34.209934Z","shell.execute_reply.started":"2023-01-07T21:18:34.201258Z","shell.execute_reply":"2023-01-07T21:18:34.208794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# scaler = RobustScaler()\n# scaler = StandardScaler()   \nX_test_st = pd.DataFrame(scaler.transform(relevent_test))","metadata":{"execution":{"iopub.status.busy":"2023-01-07T21:20:52.071258Z","iopub.execute_input":"2023-01-07T21:20:52.072002Z","iopub.status.idle":"2023-01-07T21:20:52.564272Z","shell.execute_reply.started":"2023-01-07T21:20:52.071966Z","shell.execute_reply":"2023-01-07T21:20:52.563300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_data_info({'test': X_test_st})","metadata":{"execution":{"iopub.status.busy":"2023-01-07T21:21:04.480182Z","iopub.execute_input":"2023-01-07T21:21:04.481006Z","iopub.status.idle":"2023-01-07T21:21:07.112782Z","shell.execute_reply.started":"2023-01-07T21:21:04.480964Z","shell.execute_reply":"2023-01-07T21:21:07.111689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class TestDataset(torch.utils.data.Dataset):\n    def __init__(self, X):\n        self.X = torch.tensor(X.values,dtype=torch.float)\n        \n    def __len__(self):\n        return len(self.X)\n    \n    def __getitem__(self, idx):\n        return self.X[idx]\n    \n    \ntestset     = TestDataset(X_test_st)\ntest_loader = DataLoader(testset, batch_size=1024, shuffle=False, num_workers=0)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T21:21:24.998558Z","iopub.execute_input":"2023-01-07T21:21:24.999377Z","iopub.status.idle":"2023-01-07T21:21:25.112631Z","shell.execute_reply.started":"2023-01-07T21:21:24.999331Z","shell.execute_reply":"2023-01-07T21:21:25.111799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds_class = np.array([], dtype=int)\n\nwith torch.no_grad():\n    for x in test_loader:\n        preds = model(x.to(device)).cpu()\n        preds_class = np.append(preds_class, preds)","metadata":{"execution":{"iopub.status.busy":"2023-01-07T21:21:45.697788Z","iopub.execute_input":"2023-01-07T21:21:45.698454Z","iopub.status.idle":"2023-01-07T21:21:51.328976Z","shell.execute_reply.started":"2023-01-07T21:21:45.698419Z","shell.execute_reply":"2023-01-07T21:21:51.327812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame({\n    'id': test.id,\n    'target': preds_class\n})\n\ndf = df.set_index('id')\ndf.to_csv('submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-01-07T21:22:01.772639Z","iopub.execute_input":"2023-01-07T21:22:01.773182Z","iopub.status.idle":"2023-01-07T21:22:03.564926Z","shell.execute_reply.started":"2023-01-07T21:22:01.773141Z","shell.execute_reply":"2023-01-07T21:22:03.563997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-07T21:22:12.315148Z","iopub.execute_input":"2023-01-07T21:22:12.316253Z","iopub.status.idle":"2023-01-07T21:22:12.326692Z","shell.execute_reply.started":"2023-01-07T21:22:12.316209Z","shell.execute_reply":"2023-01-07T21:22:12.325690Z"},"trusted":true},"execution_count":null,"outputs":[]}]}