{"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":"2022-12-31T03:58:09.345601Z","iopub.execute_input":"2022-12-31T03:58:09.347075Z","iopub.status.idle":"2022-12-31T03:58:09.364139Z","shell.execute_reply.started":"2022-12-31T03:58:09.347010Z","shell.execute_reply":"2022-12-31T03:58:09.362580Z"},"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":"2022-12-31T03:58:09.366985Z","iopub.execute_input":"2022-12-31T03:58:09.367383Z","iopub.status.idle":"2022-12-31T03:58:17.481610Z","shell.execute_reply.started":"2022-12-31T03:58:09.367344Z","shell.execute_reply":"2022-12-31T03:58:17.480319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-31T03:58:17.483823Z","iopub.execute_input":"2022-12-31T03:58:17.484282Z","iopub.status.idle":"2022-12-31T03:58:17.514329Z","shell.execute_reply.started":"2022-12-31T03:58:17.484238Z","shell.execute_reply":"2022-12-31T03:58:17.513024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimport 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":"2022-12-31T03:58:17.515774Z","iopub.execute_input":"2022-12-31T03:58:17.516094Z","iopub.status.idle":"2022-12-31T03:58:17.527733Z","shell.execute_reply.started":"2022-12-31T03:58:17.516064Z","shell.execute_reply":"2022-12-31T03:58:17.526473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_data_info({'train': train, 'test': test})","metadata":{"execution":{"iopub.status.busy":"2022-12-31T03:58:17.531078Z","iopub.execute_input":"2022-12-31T03:58:17.531719Z","iopub.status.idle":"2022-12-31T03:58:22.298200Z","shell.execute_reply.started":"2022-12-31T03:58:17.531657Z","shell.execute_reply":"2022-12-31T03:58:22.297322Z"},"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":"2022-12-31T03:58:22.299395Z","iopub.execute_input":"2022-12-31T03:58:22.300381Z","iopub.status.idle":"2022-12-31T03:58:22.306658Z","shell.execute_reply.started":"2022-12-31T03:58:22.300346Z","shell.execute_reply":"2022-12-31T03:58:22.305733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"barchart(train, 'target')","metadata":{"execution":{"iopub.status.busy":"2022-12-31T03:58:22.308332Z","iopub.execute_input":"2022-12-31T03:58:22.309033Z","iopub.status.idle":"2022-12-31T03:58:22.459217Z","shell.execute_reply.started":"2022-12-31T03:58:22.308990Z","shell.execute_reply":"2022-12-31T03:58:22.457902Z"},"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":"2022-12-31T03:58:22.460876Z","iopub.execute_input":"2022-12-31T03:58:22.461880Z","iopub.status.idle":"2022-12-31T03:58:25.279597Z","shell.execute_reply.started":"2022-12-31T03:58:22.461826Z","shell.execute_reply":"2022-12-31T03:58:25.278388Z"},"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":"2022-12-31T03:58:25.281129Z","iopub.execute_input":"2022-12-31T03:58:25.281446Z","iopub.status.idle":"2022-12-31T03:58:28.809593Z","shell.execute_reply.started":"2022-12-31T03:58:25.281418Z","shell.execute_reply":"2022-12-31T03:58:28.808494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# object columns \nbarchart(train, 'f_27')","metadata":{"execution":{"iopub.status.busy":"2022-12-31T03:58:28.811366Z","iopub.execute_input":"2022-12-31T03:58:28.812223Z","iopub.status.idle":"2022-12-31T03:58:29.810269Z","shell.execute_reply.started":"2022-12-31T03:58:28.812176Z","shell.execute_reply":"2022-12-31T03:58:29.809084Z"},"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":"2022-12-31T03:58:29.815129Z","iopub.execute_input":"2022-12-31T03:58:29.815911Z","iopub.status.idle":"2022-12-31T03:58:29.830143Z","shell.execute_reply.started":"2022-12-31T03:58:29.815874Z","shell.execute_reply":"2022-12-31T03:58:29.829003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"relevent_train = train\nencode(relevent_train)","metadata":{"execution":{"iopub.status.busy":"2022-12-31T03:58:29.831273Z","iopub.execute_input":"2022-12-31T03:58:29.832283Z","iopub.status.idle":"2022-12-31T04:00:19.391692Z","shell.execute_reply.started":"2022-12-31T03:58:29.832246Z","shell.execute_reply":"2022-12-31T04:00:19.390669Z"},"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":"2022-12-31T04:00:19.393369Z","iopub.execute_input":"2022-12-31T04:00:19.393837Z","iopub.status.idle":"2022-12-31T04:00:20.025965Z","shell.execute_reply.started":"2022-12-31T04:00:19.393784Z","shell.execute_reply":"2022-12-31T04:00:20.025030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"relevent_train[['uniq_chars', 'prefix', 'suffix', 'target']].head(20)","metadata":{"execution":{"iopub.status.busy":"2022-12-31T04:00:20.027306Z","iopub.execute_input":"2022-12-31T04:00:20.027764Z","iopub.status.idle":"2022-12-31T04:00:20.045865Z","shell.execute_reply.started":"2022-12-31T04:00:20.027716Z","shell.execute_reply":"2022-12-31T04:00:20.045117Z"},"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":"2022-12-31T04:00:20.047143Z","iopub.execute_input":"2022-12-31T04:00:20.048051Z","iopub.status.idle":"2022-12-31T04:00:21.006274Z","shell.execute_reply.started":"2022-12-31T04:00:20.048009Z","shell.execute_reply":"2022-12-31T04:00:21.005176Z"},"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":"2022-12-31T04:00:21.007739Z","iopub.execute_input":"2022-12-31T04:00:21.008184Z","iopub.status.idle":"2022-12-31T04:00:23.799169Z","shell.execute_reply.started":"2022-12-31T04:00:21.008140Z","shell.execute_reply":"2022-12-31T04:00:23.798045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"relevent_features = X_train.columns","metadata":{"execution":{"iopub.status.busy":"2022-12-31T04:00:23.800722Z","iopub.execute_input":"2022-12-31T04:00:23.801895Z","iopub.status.idle":"2022-12-31T04:00:23.806175Z","shell.execute_reply.started":"2022-12-31T04:00:23.801858Z","shell.execute_reply":"2022-12-31T04:00:23.805165Z"},"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":"2022-12-31T04:00:23.807964Z","iopub.execute_input":"2022-12-31T04:00:23.808772Z","iopub.status.idle":"2022-12-31T04:00:24.396850Z","shell.execute_reply.started":"2022-12-31T04:00:23.808727Z","shell.execute_reply":"2022-12-31T04:00:24.395450Z"},"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":"2022-12-31T04:00:24.398161Z","iopub.execute_input":"2022-12-31T04:00:24.398506Z","iopub.status.idle":"2022-12-31T04:00:25.650564Z","shell.execute_reply.started":"2022-12-31T04:00:24.398476Z","shell.execute_reply":"2022-12-31T04:00:25.649434Z"},"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":"2022-12-31T04:00:25.651857Z","iopub.execute_input":"2022-12-31T04:00:25.652193Z","iopub.status.idle":"2022-12-31T04:00:29.510492Z","shell.execute_reply.started":"2022-12-31T04:00:25.652161Z","shell.execute_reply":"2022-12-31T04:00:29.509447Z"},"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":"2022-12-31T04:00:29.512109Z","iopub.execute_input":"2022-12-31T04:00:29.513090Z","iopub.status.idle":"2022-12-31T04:00:29.522607Z","shell.execute_reply.started":"2022-12-31T04:00:29.513045Z","shell.execute_reply":"2022-12-31T04:00:29.521355Z"},"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)","metadata":{"execution":{"iopub.status.busy":"2022-12-31T04:00:29.524334Z","iopub.execute_input":"2022-12-31T04:00:29.525351Z","iopub.status.idle":"2022-12-31T04:00:29.545164Z","shell.execute_reply.started":"2022-12-31T04:00:29.525306Z","shell.execute_reply":"2022-12-31T04:00:29.543868Z"},"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":"2022-12-31T04:00:29.546772Z","iopub.execute_input":"2022-12-31T04:00:29.547122Z","iopub.status.idle":"2022-12-31T04:00:29.554758Z","shell.execute_reply.started":"2022-12-31T04:00:29.547091Z","shell.execute_reply":"2022-12-31T04:00:29.553648Z"},"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":"2022-12-31T04:00:29.555933Z","iopub.execute_input":"2022-12-31T04:00:29.556244Z","iopub.status.idle":"2022-12-31T04:00:29.741342Z","shell.execute_reply.started":"2022-12-31T04:00:29.556215Z","shell.execute_reply":"2022-12-31T04:00:29.740111Z"},"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":"2022-12-31T04:00:29.742654Z","iopub.execute_input":"2022-12-31T04:00:29.743106Z","iopub.status.idle":"2022-12-31T04:37:31.501933Z","shell.execute_reply.started":"2022-12-31T04:00:29.743065Z","shell.execute_reply":"2022-12-31T04:37:31.500948Z"},"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":"2022-12-31T04:37:31.503534Z","iopub.execute_input":"2022-12-31T04:37:31.504319Z","iopub.status.idle":"2022-12-31T04:37:31.718360Z","shell.execute_reply.started":"2022-12-31T04:37:31.504281Z","shell.execute_reply":"2022-12-31T04:37:31.716911Z"},"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":"2022-12-31T04:37:31.719794Z","iopub.execute_input":"2022-12-31T04:37:31.720120Z","iopub.status.idle":"2022-12-31T04:37:31.732120Z","shell.execute_reply.started":"2022-12-31T04:37:31.720091Z","shell.execute_reply":"2022-12-31T04:37:31.730713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"relevent_test = test.iloc[:, 1:]","metadata":{"execution":{"iopub.status.busy":"2022-12-31T04:37:31.733913Z","iopub.execute_input":"2022-12-31T04:37:31.734261Z","iopub.status.idle":"2022-12-31T04:37:31.810422Z","shell.execute_reply.started":"2022-12-31T04:37:31.734229Z","shell.execute_reply":"2022-12-31T04:37:31.809285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"encode(relevent_test)","metadata":{"execution":{"iopub.status.busy":"2022-12-31T04:37:31.815602Z","iopub.execute_input":"2022-12-31T04:37:31.815983Z","iopub.status.idle":"2022-12-31T04:38:57.436928Z","shell.execute_reply.started":"2022-12-31T04:37:31.815954Z","shell.execute_reply":"2022-12-31T04:38:57.435648Z"},"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":"2022-12-31T04:38:57.438430Z","iopub.execute_input":"2022-12-31T04:38:57.438802Z","iopub.status.idle":"2022-12-31T04:39:01.304331Z","shell.execute_reply.started":"2022-12-31T04:38:57.438769Z","shell.execute_reply":"2022-12-31T04:39:01.302926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"relevent_features","metadata":{"execution":{"iopub.status.busy":"2022-12-31T04:39:01.305819Z","iopub.execute_input":"2022-12-31T04:39:01.306415Z","iopub.status.idle":"2022-12-31T04:39:01.314154Z","shell.execute_reply.started":"2022-12-31T04:39:01.306381Z","shell.execute_reply":"2022-12-31T04:39:01.312979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"relevent_test.columns","metadata":{"execution":{"iopub.status.busy":"2022-12-31T04:39:01.315823Z","iopub.execute_input":"2022-12-31T04:39:01.316188Z","iopub.status.idle":"2022-12-31T04:39:01.329067Z","shell.execute_reply.started":"2022-12-31T04:39:01.316156Z","shell.execute_reply":"2022-12-31T04:39:01.327717Z"},"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":"2022-12-31T04:39:01.330544Z","iopub.execute_input":"2022-12-31T04:39:01.330883Z","iopub.status.idle":"2022-12-31T04:39:01.959245Z","shell.execute_reply.started":"2022-12-31T04:39:01.330853Z","shell.execute_reply":"2022-12-31T04:39:01.957764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"display_data_info({'test': X_test_st})","metadata":{"execution":{"iopub.status.busy":"2022-12-31T04:39:01.961063Z","iopub.execute_input":"2022-12-31T04:39:01.961552Z","iopub.status.idle":"2022-12-31T04:39:05.859011Z","shell.execute_reply.started":"2022-12-31T04:39:01.961519Z","shell.execute_reply":"2022-12-31T04:39:05.857819Z"},"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":"2022-12-31T04:39:05.860883Z","iopub.execute_input":"2022-12-31T04:39:05.861635Z","iopub.status.idle":"2022-12-31T04:39:06.022816Z","shell.execute_reply.started":"2022-12-31T04:39:05.861586Z","shell.execute_reply":"2022-12-31T04:39:06.021810Z"},"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":"2022-12-31T04:39:06.024037Z","iopub.execute_input":"2022-12-31T04:39:06.025188Z","iopub.status.idle":"2022-12-31T04:39:12.681309Z","shell.execute_reply.started":"2022-12-31T04:39:06.025148Z","shell.execute_reply":"2022-12-31T04:39:12.680434Z"},"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":"2022-12-31T04:39:12.682477Z","iopub.execute_input":"2022-12-31T04:39:12.683564Z","iopub.status.idle":"2022-12-31T04:39:14.533600Z","shell.execute_reply.started":"2022-12-31T04:39:12.683528Z","shell.execute_reply":"2022-12-31T04:39:14.532250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-31T04:39:14.535487Z","iopub.execute_input":"2022-12-31T04:39:14.537043Z","iopub.status.idle":"2022-12-31T04:39:14.558231Z","shell.execute_reply.started":"2022-12-31T04:39:14.536991Z","shell.execute_reply":"2022-12-31T04:39:14.550177Z"},"trusted":true},"execution_count":null,"outputs":[]}]}