{"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-04T23:39:26.379012Z","iopub.execute_input":"2023-01-04T23:39:26.379555Z","iopub.status.idle":"2023-01-04T23:39:26.385863Z","shell.execute_reply.started":"2023-01-04T23:39:26.379526Z","shell.execute_reply":"2023-01-04T23:39:26.385206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import roc_auc_score\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.tree import DecisionTreeClassifier\nfrom xgboost import XGBClassifier","metadata":{"execution":{"iopub.status.busy":"2023-01-04T23:39:26.418624Z","iopub.execute_input":"2023-01-04T23:39:26.419216Z","iopub.status.idle":"2023-01-04T23:39:26.423279Z","shell.execute_reply.started":"2023-01-04T23:39:26.419177Z","shell.execute_reply":"2023-01-04T23:39:26.422667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/emsi-5iir-ds/train.csv')\ntest = pd.read_csv('/kaggle/input/emsi-5iir-ds/test.csv')","metadata":{"execution":{"iopub.status.busy":"2023-01-04T23:39:26.424774Z","iopub.execute_input":"2023-01-04T23:39:26.425285Z","iopub.status.idle":"2023-01-04T23:39:31.733542Z","shell.execute_reply.started":"2023-01-04T23:39:26.425253Z","shell.execute_reply":"2023-01-04T23:39:31.732164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = train.drop(['id', 'target'], axis=1)\ntarget = train.target\n\ntest_df = test.drop(['id'], axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-01-04T23:39:31.734679Z","iopub.execute_input":"2023-01-04T23:39:31.734995Z","iopub.status.idle":"2023-01-04T23:39:31.850950Z","shell.execute_reply.started":"2023-01-04T23:39:31.734969Z","shell.execute_reply":"2023-01-04T23:39:31.849406Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import OrdinalEncoder","metadata":{"execution":{"iopub.status.busy":"2023-01-04T23:39:31.852658Z","iopub.execute_input":"2023-01-04T23:39:31.853098Z","iopub.status.idle":"2023-01-04T23:39:31.867790Z","shell.execute_reply.started":"2023-01-04T23:39:31.853061Z","shell.execute_reply":"2023-01-04T23:39:31.866456Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"enc = OrdinalEncoder()\ndef feature_eng(df):\n    df=df.copy()\n    df['char_unique']=df['f_27'].apply(lambda x: len(set(x)))\n    for i in range(df.f_27.str.len().max()):\n        df['f_27_char{}'.format(i+1)]=enc.fit_transform(df['f_27'].str.get(i).values.reshape(-1,1))\n    return df.drop(['f_27'],axis=1)\n\ntrain_df=feature_eng(df=train_df)\ntest_df=feature_eng(df=test_df)","metadata":{"execution":{"iopub.status.busy":"2023-01-04T23:39:31.871069Z","iopub.execute_input":"2023-01-04T23:39:31.872021Z","iopub.status.idle":"2023-01-04T23:39:44.648109Z","shell.execute_reply.started":"2023-01-04T23:39:31.871921Z","shell.execute_reply":"2023-01-04T23:39:44.646145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.f_27.str.len().max()","metadata":{"execution":{"iopub.status.busy":"2023-01-04T23:39:44.649872Z","iopub.execute_input":"2023-01-04T23:39:44.650256Z","iopub.status.idle":"2023-01-04T23:39:44.922581Z","shell.execute_reply.started":"2023-01-04T23:39:44.650222Z","shell.execute_reply":"2023-01-04T23:39:44.921216Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-04T23:39:44.923877Z","iopub.execute_input":"2023-01-04T23:39:44.924235Z","iopub.status.idle":"2023-01-04T23:39:44.951974Z","shell.execute_reply.started":"2023-01-04T23:39:44.924206Z","shell.execute_reply":"2023-01-04T23:39:44.951046Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\n\nscaler = StandardScaler()\nscaler.fit(train_df)\ntrain_df = scaler.transform(train_df)\nx_test = scaler.transform(test_df)","metadata":{"execution":{"iopub.status.busy":"2023-01-04T23:39:44.953092Z","iopub.execute_input":"2023-01-04T23:39:44.953492Z","iopub.status.idle":"2023-01-04T23:39:45.474641Z","shell.execute_reply.started":"2023-01-04T23:39:44.953467Z","shell.execute_reply":"2023-01-04T23:39:45.473206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nx_train, x_val, y_train, y_val = train_test_split(train_df, target)","metadata":{"execution":{"iopub.status.busy":"2023-01-04T23:39:45.476156Z","iopub.execute_input":"2023-01-04T23:39:45.476501Z","iopub.status.idle":"2023-01-04T23:39:45.848373Z","shell.execute_reply.started":"2023-01-04T23:39:45.476472Z","shell.execute_reply":"2023-01-04T23:39:45.846711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\n\nfrom torch.optim import SGD\nfrom torch.nn import CrossEntropyLoss\n\nfrom torch.utils.data import TensorDataset\nfrom torch.utils.data import DataLoader","metadata":{"execution":{"iopub.status.busy":"2023-01-04T23:39:45.850132Z","iopub.execute_input":"2023-01-04T23:39:45.850543Z","iopub.status.idle":"2023-01-04T23:39:45.856922Z","shell.execute_reply.started":"2023-01-04T23:39:45.850504Z","shell.execute_reply":"2023-01-04T23:39:45.855389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.nn.functional as F","metadata":{"execution":{"iopub.status.busy":"2023-01-04T23:39:45.858211Z","iopub.execute_input":"2023-01-04T23:39:45.858595Z","iopub.status.idle":"2023-01-04T23:39:45.869343Z","shell.execute_reply.started":"2023-01-04T23:39:45.858565Z","shell.execute_reply":"2023-01-04T23:39:45.868333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Net(nn.Module):\n    def __init__(self):\n        super().__init__()\n    \n        self.fc1 = nn.Linear(41, 100)\n        self.fc2 = nn.Linear(100, 50)\n        self.fc3 = nn.Linear(50, 1)\n        \n    def forward(self, x):\n        x = F.relu(self.fc1(x))\n        x = F.relu(self.fc2(x))\n        x = F.sigmoid(self.fc3(x))\n        \n        return x","metadata":{"execution":{"iopub.status.busy":"2023-01-04T23:39:45.870820Z","iopub.execute_input":"2023-01-04T23:39:45.871161Z","iopub.status.idle":"2023-01-04T23:39:45.880750Z","shell.execute_reply.started":"2023-01-04T23:39:45.871137Z","shell.execute_reply":"2023-01-04T23:39:45.880137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Net()","metadata":{"execution":{"iopub.status.busy":"2023-01-04T23:39:45.883851Z","iopub.execute_input":"2023-01-04T23:39:45.885599Z","iopub.status.idle":"2023-01-04T23:39:45.893369Z","shell.execute_reply.started":"2023-01-04T23:39:45.885571Z","shell.execute_reply":"2023-01-04T23:39:45.892286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.optim import Adam","metadata":{"execution":{"iopub.status.busy":"2023-01-04T23:39:45.896194Z","iopub.execute_input":"2023-01-04T23:39:45.896627Z","iopub.status.idle":"2023-01-04T23:39:45.904467Z","shell.execute_reply.started":"2023-01-04T23:39:45.896592Z","shell.execute_reply":"2023-01-04T23:39:45.903263Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"criterion = nn.BCELoss()\n\noptimizer = Adam(model.parameters(), lr=0.01)","metadata":{"execution":{"iopub.status.busy":"2023-01-04T23:39:45.906282Z","iopub.execute_input":"2023-01-04T23:39:45.906706Z","iopub.status.idle":"2023-01-04T23:39:45.915765Z","shell.execute_reply.started":"2023-01-04T23:39:45.906663Z","shell.execute_reply":"2023-01-04T23:39:45.914689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = torch.tensor(train_df)\ntarget = torch.tensor(target.values).float()\n\ntest_df = torch.tensor(test_df.values)","metadata":{"execution":{"iopub.status.busy":"2023-01-04T23:39:45.917768Z","iopub.execute_input":"2023-01-04T23:39:45.918121Z","iopub.status.idle":"2023-01-04T23:39:46.098000Z","shell.execute_reply.started":"2023-01-04T23:39:45.918089Z","shell.execute_reply":"2023-01-04T23:39:46.096713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = train_df.float()\ntest_df = test_df.float()","metadata":{"execution":{"iopub.status.busy":"2023-01-04T23:39:46.099520Z","iopub.execute_input":"2023-01-04T23:39:46.099843Z","iopub.status.idle":"2023-01-04T23:39:46.219105Z","shell.execute_reply.started":"2023-01-04T23:39:46.099816Z","shell.execute_reply":"2023-01-04T23:39:46.217675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, x_val, y_train, y_val = train_test_split(train_df, target)","metadata":{"execution":{"iopub.status.busy":"2023-01-04T23:39:46.220269Z","iopub.execute_input":"2023-01-04T23:39:46.220603Z","iopub.status.idle":"2023-01-04T23:39:46.491692Z","shell.execute_reply.started":"2023-01-04T23:39:46.220575Z","shell.execute_reply":"2023-01-04T23:39:46.490314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_datasets = TensorDataset(x_train, y_train)\ntrain_dataloader = DataLoader(train_datasets, batch_size=256, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2023-01-04T23:39:46.493277Z","iopub.execute_input":"2023-01-04T23:39:46.493678Z","iopub.status.idle":"2023-01-04T23:39:46.506129Z","shell.execute_reply.started":"2023-01-04T23:39:46.493640Z","shell.execute_reply":"2023-01-04T23:39:46.505099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(300):\n    loss_sum = 0\n    for x, target in train_dataloader:\n        loss = criterion(model(x).view((-1,)), target)\n        optimizer.zero_grad()\n        loss.backward()\n        optimizer.step()\n        \n        loss_sum += loss.item()\n        \n    loss_average = loss_sum / len(train_dataloader)\n    print(f'{i} loss_average {loss_average}')","metadata":{"execution":{"iopub.status.busy":"2023-01-04T23:39:46.507597Z","iopub.execute_input":"2023-01-04T23:39:46.507839Z","iopub.status.idle":"2023-01-05T00:11:42.402961Z","shell.execute_reply.started":"2023-01-04T23:39:46.507815Z","shell.execute_reply":"2023-01-05T00:11:42.401997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_pred = model(x_train)","metadata":{"execution":{"iopub.status.busy":"2023-01-05T00:11:42.404156Z","iopub.execute_input":"2023-01-05T00:11:42.406177Z","iopub.status.idle":"2023-01-05T00:11:42.861498Z","shell.execute_reply.started":"2023-01-05T00:11:42.406142Z","shell.execute_reply":"2023-01-05T00:11:42.860309Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import roc_auc_score\n\nroc_auc_score(y_train.detach().numpy(), y_train_pred.detach().numpy())","metadata":{"execution":{"iopub.status.busy":"2023-01-05T00:11:42.863051Z","iopub.execute_input":"2023-01-05T00:11:42.863456Z","iopub.status.idle":"2023-01-05T00:11:43.079366Z","shell.execute_reply.started":"2023-01-05T00:11:42.863420Z","shell.execute_reply":"2023-01-05T00:11:43.078285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_val_pred = model(x_val)\n\nroc_auc_score(y_val.detach().numpy(), y_val_pred.detach().numpy())","metadata":{"execution":{"iopub.status.busy":"2023-01-05T00:11:43.081103Z","iopub.execute_input":"2023-01-05T00:11:43.081938Z","iopub.status.idle":"2023-01-05T00:11:43.323427Z","shell.execute_reply.started":"2023-01-05T00:11:43.081879Z","shell.execute_reply":"2023-01-05T00:11:43.322653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"((y_val_pred > 0.5).int().view((-1,)) == y_val).sum() / y_val.shape[0]","metadata":{"execution":{"iopub.status.busy":"2023-01-05T00:11:43.324617Z","iopub.execute_input":"2023-01-05T00:11:43.325075Z","iopub.status.idle":"2023-01-05T00:11:43.332484Z","shell.execute_reply.started":"2023-01-05T00:11:43.325042Z","shell.execute_reply":"2023-01-05T00:11:43.331746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test = torch.tensor(x_test)","metadata":{"execution":{"iopub.status.busy":"2023-01-05T00:14:15.910316Z","iopub.execute_input":"2023-01-05T00:14:15.910675Z","iopub.status.idle":"2023-01-05T00:14:15.972698Z","shell.execute_reply.started":"2023-01-05T00:14:15.910644Z","shell.execute_reply":"2023-01-05T00:14:15.971696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test = model(x_test.float())","metadata":{"execution":{"iopub.status.busy":"2023-01-05T00:14:32.251971Z","iopub.execute_input":"2023-01-05T00:14:32.252329Z","iopub.status.idle":"2023-01-05T00:14:32.786765Z","shell.execute_reply.started":"2023-01-05T00:14:32.252298Z","shell.execute_reply":"2023-01-05T00:14:32.785795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.read_csv('/kaggle/input/emsi-5iir-ds/sample_submission.csv')\nsubmission.target = y_test.detach().numpy()\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-01-05T00:14:51.640086Z","iopub.execute_input":"2023-01-05T00:14:51.640443Z","iopub.status.idle":"2023-01-05T00:14:52.682130Z","shell.execute_reply.started":"2023-01-05T00:14:51.640413Z","shell.execute_reply":"2023-01-05T00:14:52.680735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2023-01-05T00:14:41.085699Z","iopub.execute_input":"2023-01-05T00:14:41.086873Z","iopub.status.idle":"2023-01-05T00:14:41.100316Z","shell.execute_reply.started":"2023-01-05T00:14:41.086834Z","shell.execute_reply":"2023-01-05T00:14:41.098929Z"},"trusted":true},"execution_count":null,"outputs":[]}]}