{"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":"import numpy as np\nimport pandas as pd\nfrom matplotlib import pyplot as plt\nimport seaborn as sns\nimport plotly.express as px\nimport tensorflow as tf\nimport keras\nimport keras.layers as L\nimport math\nfrom keras.utils import Sequence\nfrom keras.preprocessing import image\nfrom random import shuffle\nfrom sklearn.model_selection import train_test_split","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-07-01T00:33:06.558999Z","iopub.execute_input":"2021-07-01T00:33:06.559497Z","iopub.status.idle":"2021-07-01T00:33:15.867438Z","shell.execute_reply.started":"2021-07-01T00:33:06.559393Z","shell.execute_reply":"2021-07-01T00:33:15.866316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels = pd.read_csv('../input/g2net-gravitational-wave-detection/training_labels.csv')\nsample_submission = pd.read_csv('../input/g2net-gravitational-wave-detection/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-07-01T00:33:15.869111Z","iopub.execute_input":"2021-07-01T00:33:15.869449Z","iopub.status.idle":"2021-07-01T00:33:16.619506Z","shell.execute_reply.started":"2021-07-01T00:33:15.869415Z","shell.execute_reply":"2021-07-01T00:33:16.618315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-01T00:33:16.621637Z","iopub.execute_input":"2021-07-01T00:33:16.621969Z","iopub.status.idle":"2021-07-01T00:33:16.653563Z","shell.execute_reply.started":"2021-07-01T00:33:16.621911Z","shell.execute_reply":"2021-07-01T00:33:16.652504Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(x='target',data=train_labels,palette='Set2')","metadata":{"execution":{"iopub.status.busy":"2021-07-01T00:33:16.655582Z","iopub.execute_input":"2021-07-01T00:33:16.655954Z","iopub.status.idle":"2021-07-01T00:33:16.871069Z","shell.execute_reply.started":"2021-07-01T00:33:16.655889Z","shell.execute_reply":"2021-07-01T00:33:16.869944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = list(train_labels['id'])\nfor i in range(len(path)):\n    path[i] = '../input/g2net-gravitational-wave-detection/train/' +path[i][0]+'/'+path[i][1]+'/'+path[i][2]+'/' + path[i] + '.npy'","metadata":{"execution":{"iopub.status.busy":"2021-07-01T00:33:16.872746Z","iopub.execute_input":"2021-07-01T00:33:16.873212Z","iopub.status.idle":"2021-07-01T00:33:17.628471Z","shell.execute_reply.started":"2021-07-01T00:33:16.873168Z","shell.execute_reply":"2021-07-01T00:33:17.627358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path[0]","metadata":{"execution":{"iopub.status.busy":"2021-07-01T00:33:17.630103Z","iopub.execute_input":"2021-07-01T00:33:17.630558Z","iopub.status.idle":"2021-07-01T00:33:17.636725Z","shell.execute_reply.started":"2021-07-01T00:33:17.630508Z","shell.execute_reply":"2021-07-01T00:33:17.635710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example = np.load(path[0])","metadata":{"execution":{"iopub.status.busy":"2021-07-01T00:33:17.638245Z","iopub.execute_input":"2021-07-01T00:33:17.638547Z","iopub.status.idle":"2021-07-01T00:33:17.662351Z","shell.execute_reply.started":"2021-07-01T00:33:17.638516Z","shell.execute_reply":"2021-07-01T00:33:17.660639Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig,a =  plt.subplots(3,1)\na[0].plot(example[1],color='blue')\na[1].plot(example[1],color='black')\na[2].plot(example[1],color='red')\nfig.suptitle('1', fontsize=12)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-01T00:33:17.666213Z","iopub.execute_input":"2021-07-01T00:33:17.666750Z","iopub.status.idle":"2021-07-01T00:33:18.034953Z","shell.execute_reply.started":"2021-07-01T00:33:17.666698Z","shell.execute_reply":"2021-07-01T00:33:18.034014Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example = np.load(path[1])\nfig,a =  plt.subplots(3,1)\na[0].plot(example[1],color='blue')\na[1].plot(example[1],color='black')\na[2].plot(example[1],color='red')\nfig.suptitle('0', fontsize=12)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-01T00:33:18.037089Z","iopub.execute_input":"2021-07-01T00:33:18.037481Z","iopub.status.idle":"2021-07-01T00:33:18.361241Z","shell.execute_reply.started":"2021-07-01T00:33:18.037446Z","shell.execute_reply":"2021-07-01T00:33:18.360337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example = np.load(path[2])\nfig,a =  plt.subplots(3,1)\na[0].plot(example[1],color='blue')\na[1].plot(example[1],color='black')\na[2].plot(example[1],color='red')\nfig.suptitle('0', fontsize=12)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-01T00:33:18.362631Z","iopub.execute_input":"2021-07-01T00:33:18.363173Z","iopub.status.idle":"2021-07-01T00:33:18.688087Z","shell.execute_reply.started":"2021-07-01T00:33:18.363124Z","shell.execute_reply":"2021-07-01T00:33:18.687203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example = np.load(path[3])\nfig,a =  plt.subplots(3,1)\na[0].plot(example[1],color='blue')\na[1].plot(example[1],color='black')\na[2].plot(example[1],color='red')\nfig.suptitle('0', fontsize=12)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-01T00:33:18.689315Z","iopub.execute_input":"2021-07-01T00:33:18.689758Z","iopub.status.idle":"2021-07-01T00:33:19.003624Z","shell.execute_reply.started":"2021-07-01T00:33:18.689706Z","shell.execute_reply":"2021-07-01T00:33:19.002106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"example = np.load(path[4])\nfig,a =  plt.subplots(3,1)\na[0].plot(example[1],color='blue')\na[1].plot(example[1],color='black')\na[2].plot(example[1],color='red')\nfig.suptitle('1', fontsize=12)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-01T00:33:19.005283Z","iopub.execute_input":"2021-07-01T00:33:19.005628Z","iopub.status.idle":"2021-07-01T00:33:19.324457Z","shell.execute_reply.started":"2021-07-01T00:33:19.005594Z","shell.execute_reply":"2021-07-01T00:33:19.323145Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels","metadata":{"execution":{"iopub.status.busy":"2021-07-01T00:33:19.325865Z","iopub.execute_input":"2021-07-01T00:33:19.326223Z","iopub.status.idle":"2021-07-01T00:33:19.341256Z","shell.execute_reply.started":"2021-07-01T00:33:19.326186Z","shell.execute_reply":"2021-07-01T00:33:19.340209Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def id_to_path(idx,train=True):\n    path = '../input/g2net-gravitational-wave-detection/'\n    if train:\n        folder = 'train/'\n    else:\n        folder = 'test/'\n    path+=folder+idx[0]+'/'+idx[1]+'/'+idx[2]+'/'+idx+'.npy'\n    return path","metadata":{"execution":{"iopub.status.busy":"2021-07-01T00:33:19.342687Z","iopub.execute_input":"2021-07-01T00:33:19.343102Z","iopub.status.idle":"2021-07-01T00:33:19.353639Z","shell.execute_reply.started":"2021-07-01T00:33:19.343066Z","shell.execute_reply":"2021-07-01T00:33:19.352447Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Dataset(Sequence):\n    def __init__(self,idx,y=None,batch_size=512,shuffle=True):\n        self.idx = idx\n        self.batch_size = batch_size\n        self.shuffle = shuffle\n        if y is not None:\n            self.is_train=True\n        else:\n            self.is_train=False\n        self.y = y\n    def __len__(self):\n        return math.ceil(len(self.idx)/self.batch_size)\n    def __getitem__(self,ids):\n        batch_ids = self.idx[ids * self.batch_size:(ids + 1) * self.batch_size]\n        if self.y is not None:\n            batch_y = self.y[ids * self.batch_size: (ids + 1) * self.batch_size]\n            \n        list_x = [np.load(id_to_path(x, self.is_train)) for x in batch_ids]\n        batch_X = np.transpose(np.stack(list_x), \n                               (0,2,1))\n        if self.is_train:\n            return batch_X, batch_y\n        else:\n            return batch_X\n    \n    def on_epoch_end(self):\n        if self.shuffle and self.is_train:\n            ids_y = list(zip(self.idx, self.y))\n            shuffle(ids_y)\n            self.idx, self.y = list(zip(*ids_y))","metadata":{"execution":{"iopub.status.busy":"2021-07-01T00:33:19.355025Z","iopub.execute_input":"2021-07-01T00:33:19.355330Z","iopub.status.idle":"2021-07-01T00:33:19.371468Z","shell.execute_reply.started":"2021-07-01T00:33:19.355298Z","shell.execute_reply":"2021-07-01T00:33:19.370266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_idx =  train_labels['id'].values\ny = train_labels['target'].values\ntest_idx = sample_submission['id'].values","metadata":{"execution":{"iopub.status.busy":"2021-07-01T00:33:19.372698Z","iopub.execute_input":"2021-07-01T00:33:19.373172Z","iopub.status.idle":"2021-07-01T00:33:19.384209Z","shell.execute_reply.started":"2021-07-01T00:33:19.373135Z","shell.execute_reply":"2021-07-01T00:33:19.383217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train,x_valid,y_train,y_valid = train_test_split(train_idx,y,test_size=0.05,random_state=42)","metadata":{"execution":{"iopub.status.busy":"2021-07-01T00:33:19.385702Z","iopub.execute_input":"2021-07-01T00:33:19.386335Z","iopub.status.idle":"2021-07-01T00:33:19.552792Z","shell.execute_reply.started":"2021-07-01T00:33:19.386298Z","shell.execute_reply":"2021-07-01T00:33:19.551858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = Dataset(x_train,y_train)\nvalid_dataset = Dataset(x_valid,y_valid)\ntest_dataset = Dataset(test_idx)","metadata":{"execution":{"iopub.status.busy":"2021-07-01T00:33:19.554203Z","iopub.execute_input":"2021-07-01T00:33:19.554763Z","iopub.status.idle":"2021-07-01T00:33:19.559203Z","shell.execute_reply.started":"2021-07-01T00:33:19.554725Z","shell.execute_reply":"2021-07-01T00:33:19.558183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.Sequential([\nL.Dense(256,activation='relu',input_shape=(4096,3)),\nL.Reshape((-1,)),\nL.Dense(128,activation='relu'),\nL.Dense(64,activation='relu'),\nL.Dense(1,activation='sigmoid')\n])\nmodel.summary()\nmodel.compile(loss='binary_crossentropy', optimizer='adam', metrics=[keras.metrics.AUC()])","metadata":{"execution":{"iopub.status.busy":"2021-07-01T00:33:19.560628Z","iopub.execute_input":"2021-07-01T00:33:19.561159Z","iopub.status.idle":"2021-07-01T00:33:20.660721Z","shell.execute_reply.started":"2021-07-01T00:33:19.561079Z","shell.execute_reply":"2021-07-01T00:33:20.658555Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-07-01T00:33:20.662460Z","iopub.execute_input":"2021-07-01T00:33:20.662815Z","iopub.status.idle":"2021-07-01T00:33:20.673048Z","shell.execute_reply.started":"2021-07-01T00:33:20.662773Z","shell.execute_reply":"2021-07-01T00:33:20.671599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(train_dataset,epochs=1,validation_data=valid_dataset)","metadata":{"execution":{"iopub.status.busy":"2021-07-01T00:33:20.675116Z","iopub.execute_input":"2021-07-01T00:33:20.675495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = model.predict(test_dataset)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = preds.reshape(-1)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'id':sample_submission['id'],'target':preds})","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv',index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}