{"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\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-10-10T15:03:16.931361Z","iopub.execute_input":"2021-10-10T15:03:16.931808Z","iopub.status.idle":"2021-10-10T15:03:16.944156Z","shell.execute_reply.started":"2021-10-10T15:03:16.93176Z","shell.execute_reply":"2021-10-10T15:03:16.943129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport torch\nfrom sklearn.preprocessing import StandardScaler\nfrom tqdm.notebook import tqdm\n\nfrom sklearn.metrics import r2_score\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2021-10-10T15:03:24.422705Z","iopub.execute_input":"2021-10-10T15:03:24.423036Z","iopub.status.idle":"2021-10-10T15:03:24.428094Z","shell.execute_reply.started":"2021-10-10T15:03:24.423008Z","shell.execute_reply":"2021-10-10T15:03:24.426927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_X=pd.read_csv(\"/kaggle/input/2021-ai-w6-p1/pca_train.csv\")\ntrain_Y=pd.read_csv(\"/kaggle/input/2021-ai-w6-p1/train_label.csv\").label\ntest_X=pd.read_csv(\"/kaggle/input/2021-ai-w6-p1/pca_test.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-10-10T15:03:25.322848Z","iopub.execute_input":"2021-10-10T15:03:25.323177Z","iopub.status.idle":"2021-10-10T15:03:27.991834Z","shell.execute_reply.started":"2021-10-10T15:03:25.323147Z","shell.execute_reply":"2021-10-10T15:03:27.99086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, x_val, y_train, y_val = train_test_split(train_X, train_Y, random_state=777)","metadata":{"execution":{"iopub.status.busy":"2021-10-10T15:03:27.993352Z","iopub.execute_input":"2021-10-10T15:03:27.993735Z","iopub.status.idle":"2021-10-10T15:03:28.052729Z","shell.execute_reply.started":"2021-10-10T15:03:27.993696Z","shell.execute_reply":"2021-10-10T15:03:28.051832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# scaler=StandardScaler()\ntrain_X_scale=np.array(x_train)#scaler.fit_transform(x_train)\nval_X_scale=np.array(x_val)#scaler.transform(x_val)\ntest_X_scale=np.array(test_X)#scaler.transform(test_X)","metadata":{"execution":{"iopub.status.busy":"2021-10-10T15:03:28.054327Z","iopub.execute_input":"2021-10-10T15:03:28.054682Z","iopub.status.idle":"2021-10-10T15:03:28.08664Z","shell.execute_reply.started":"2021-10-10T15:03:28.054654Z","shell.execute_reply":"2021-10-10T15:03:28.085782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_X_tensor=torch.FloatTensor(train_X_scale).cuda()\nval_X_tensor=torch.FloatTensor(val_X_scale).cuda()\ntest_X_tensor=torch.FloatTensor(test_X_scale).cuda()\n","metadata":{"execution":{"iopub.status.busy":"2021-10-10T15:03:28.743308Z","iopub.execute_input":"2021-10-10T15:03:28.743679Z","iopub.status.idle":"2021-10-10T15:03:28.778403Z","shell.execute_reply.started":"2021-10-10T15:03:28.743649Z","shell.execute_reply":"2021-10-10T15:03:28.77757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_Y_tensor=torch.LongTensor(y_train).cuda()","metadata":{"execution":{"iopub.status.busy":"2021-10-10T15:03:29.558156Z","iopub.execute_input":"2021-10-10T15:03:29.558495Z","iopub.status.idle":"2021-10-10T15:03:29.566604Z","shell.execute_reply.started":"2021-10-10T15:03:29.558463Z","shell.execute_reply":"2021-10-10T15:03:29.565793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_X_tensor.shape","metadata":{"execution":{"iopub.status.busy":"2021-10-10T15:11:57.912861Z","iopub.execute_input":"2021-10-10T15:11:57.913182Z","iopub.status.idle":"2021-10-10T15:11:57.92059Z","shell.execute_reply.started":"2021-10-10T15:11:57.913152Z","shell.execute_reply":"2021-10-10T15:11:57.919704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_Y_tensor.max()","metadata":{"execution":{"iopub.status.busy":"2021-10-10T15:20:00.802508Z","iopub.execute_input":"2021-10-10T15:20:00.802892Z","iopub.status.idle":"2021-10-10T15:20:00.814129Z","shell.execute_reply.started":"2021-10-10T15:20:00.802862Z","shell.execute_reply":"2021-10-10T15:20:00.813122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.nn as nn\nimport torch.nn.functional as F\n\n\nclass Net(nn.Module):\n    def __init__(self):\n        super().__init__()\n        drop_prob = 0.3\n        self.fc1 = nn.Linear(256, 198,bias=True)\n        self.fc2 = nn.Linear(198, 198,bias=True)\n        self.fc3 = nn.Linear(198, 198,bias=True)\n\n        self.fc4 = nn.Linear(198, 198)\n        self.relu = torch.nn.Sigmoid()\n        self.dropout = torch.nn.Dropout(p=drop_prob)\n        ## initialize weight\n        for m in self.modules():\n            if isinstance(m, nn.Linear):\n                nn.init.xavier_uniform_(m.weight.data)\n                if m.bias is not None:\n                    m.bias.data.zero_() \n    def forward(self, x):\n        x = self.fc1(x)\n        x = self.fc2(x)\n        x = self.fc3(x)\n        x = self.fc4(x)\n        \n        return x\n\n\n","metadata":{"execution":{"iopub.status.busy":"2021-10-10T15:03:30.355349Z","iopub.execute_input":"2021-10-10T15:03:30.355708Z","iopub.status.idle":"2021-10-10T15:03:30.364627Z","shell.execute_reply.started":"2021-10-10T15:03:30.355676Z","shell.execute_reply":"2021-10-10T15:03:30.363041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.optim as optim\nnet = Net()\ncriterion = nn.CrossEntropyLoss().cuda()\noptimizer = optim.SGD(net.parameters(), lr=0.01)","metadata":{"execution":{"iopub.status.busy":"2021-10-10T15:13:29.468663Z","iopub.execute_input":"2021-10-10T15:13:29.468982Z","iopub.status.idle":"2021-10-10T15:13:29.569215Z","shell.execute_reply.started":"2021-10-10T15:13:29.468955Z","shell.execute_reply":"2021-10-10T15:13:29.568153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_Y_tensor.shape","metadata":{"execution":{"iopub.status.busy":"2021-10-10T15:13:29.751324Z","iopub.execute_input":"2021-10-10T15:13:29.751653Z","iopub.status.idle":"2021-10-10T15:13:29.75763Z","shell.execute_reply.started":"2021-10-10T15:13:29.751625Z","shell.execute_reply":"2021-10-10T15:13:29.756624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\nepoch=2000\nnet=net.cuda()\nfrom tqdm import tqdm\n\nfor i in tqdm(range(2000)):\n    optimizer.zero_grad()\n    # forward + backward + optimize\n    outputs = net(train_X_tensor)\n    loss = criterion(outputs, train_Y_tensor)\n    loss.backward()\n    optimizer.step()\n\n    with torch.no_grad():\n        val_predict=net(val_X_tensor)\n        _, val_predicted = torch.max(val_predict, 1)\n        score=accuracy_score(val_predicted.detach().cpu(),y_val)\n    if i%100==0:\n        print(i,\" = \",score)\n","metadata":{"execution":{"iopub.status.busy":"2021-10-10T15:21:35.345424Z","iopub.execute_input":"2021-10-10T15:21:35.34591Z","iopub.status.idle":"2021-10-10T15:21:42.075826Z","shell.execute_reply.started":"2021-10-10T15:21:35.345869Z","shell.execute_reply":"2021-10-10T15:21:42.074377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"score","metadata":{"execution":{"iopub.status.busy":"2021-10-06T08:20:04.432105Z","iopub.execute_input":"2021-10-06T08:20:04.432392Z","iopub.status.idle":"2021-10-06T08:20:04.439486Z","shell.execute_reply.started":"2021-10-06T08:20:04.432365Z","shell.execute_reply":"2021-10-06T08:20:04.438382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_Y_tensor.shape","metadata":{"execution":{"iopub.status.busy":"2021-10-06T07:57:41.782507Z","iopub.execute_input":"2021-10-06T07:57:41.782831Z","iopub.status.idle":"2021-10-06T07:57:41.788838Z","shell.execute_reply.started":"2021-10-06T07:57:41.782801Z","shell.execute_reply":"2021-10-06T07:57:41.78762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict=net(test_X_tensor)\n_, predicted = torch.max(predict, 1)\npredicted=predicted.detach().cpu().numpy()","metadata":{"execution":{"iopub.status.busy":"2021-10-06T04:19:05.315652Z","iopub.execute_input":"2021-10-06T04:19:05.315968Z","iopub.status.idle":"2021-10-06T04:19:05.322358Z","shell.execute_reply.started":"2021-10-06T04:19:05.315938Z","shell.execute_reply":"2021-10-06T04:19:05.321184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_={\"label\":predicted}\nsubmission = pd.DataFrame(data=pred_)\nsubmission.index = [i for i in range(test_X_tensor.shape[0])]\nsubmission.index.name = 'id'\n\n\nsubmission.to_csv('DNN_Pred.csv', index=True) ","metadata":{"execution":{"iopub.status.busy":"2021-10-06T04:19:05.507344Z","iopub.execute_input":"2021-10-06T04:19:05.50763Z","iopub.status.idle":"2021-10-06T04:19:05.549443Z","shell.execute_reply.started":"2021-10-06T04:19:05.507604Z","shell.execute_reply":"2021-10-06T04:19:05.548741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2021-09-07T11:55:36.745302Z","iopub.execute_input":"2021-09-07T11:55:36.745554Z","iopub.status.idle":"2021-09-07T11:55:36.758474Z","shell.execute_reply.started":"2021-09-07T11:55:36.74553Z","shell.execute_reply":"2021-09-07T11:55:36.757684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predict","metadata":{"execution":{"iopub.status.busy":"2021-09-07T10:15:25.616371Z","iopub.execute_input":"2021-09-07T10:15:25.61674Z","iopub.status.idle":"2021-09-07T10:15:25.671914Z","shell.execute_reply.started":"2021-09-07T10:15:25.616707Z","shell.execute_reply":"2021-09-07T10:15:25.670343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}