{"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 matplotlib.pyplot as plt\nimport pandas as pd\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.set_option('display.max_columns', 200)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = pd.read_csv(\"../input/amex-default-prediction/train_labels.csv\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data=pd.read_feather(\"/kaggle/input/amexfeather/train_data.ftr\")\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"layer = tf.keras.layers\nmodel = tf.keras.Sequential()\nmodel.add(layer.Input(shape=(13,186)))\nmodel.add(layer.LSTM(1028 , activation = \"relu\" ))\nmodel.add(layer.Dense(512,activation = \"relu\"))\nmodel.add(layer.Dense(256,activation = \"relu\"))\nmodel.add(layer.Dense(128,activation = \"relu\"))\nmodel.add(layer.Dense(64,activation = \"relu\"))\nmodel.add(layer.Dense(32,activation = \"relu\"))\nmodel.add(layer.Dense(1,activation = \"sigmoid\"))\nmodel.summary()\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head().style.background_gradient(cmap=\"Reds\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data[\"B_2\"].unique()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(\n    initial_learning_rate=1e-2,\n    decay_steps=10000,\n    decay_rate=0.9)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"    \n# detect and init the TPU\ntpu = tf.distribute.cluster_resolver.TPUClusterResolver.connect()\n\n# instantiate a distribution strategy\ntpu_strategy = tf.distribute.experimental.TPUStrategy(tpu)\n\n# instantiating the model in the strategy scope creates the model on the TPU\nwith tpu_strategy.scope():\n    model = tf.keras.Sequential([layer.Input(shape=(13,186)),layer.LSTM(1028 ,return_sequences = True, activation = \"relu\" ),layer.LSTM(512 , activation = \"relu\" ),layer.Dense(512,activation = \"relu\") , layer.Dense(256,activation = \"relu\"),layer.Dense(256,activation = \"relu\"),layer.Dense(128,activation = \"relu\"),layer.Dense(32,activation = \"relu\"),layer.Dense(1,activation = \"sigmoid\")]) # define your model normally\n    model.compile(loss = tf.keras.losses.BinaryCrossentropy(),optimizer = tf.keras.optimizers.Adam(learning_rate=lr_schedule),metrics = [\"accuracy\"])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"counts = train_data.groupby(\"customer_ID\").customer_ID.unique().to_numpy()\nheaders = list(train_data)\nunique = train_data[\"customer_ID\"].unique()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(unique.shape)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"unique30000 = unique[:30000]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = np.zeros((30000 , 13, 186))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i , v in enumerate(unique30000):\n    \n    print(i)\n    \n    data1 = train_data.loc[train_data[\"customer_ID\"] == v]\n\n    data2 = data1.iloc[:,2:-1]\n    \n    data3 = data2.loc[:,data2.columns != \"D_63\"]\n    \n    data4 = data3.loc[:,data3.columns != \"D_64\"]\n\n    data[i ,:data4.shape[0] , :] = data4","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.nan_to_num(data , copy = False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.save(\"./\" , data)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(data.shape)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels30000 = np.array(labels[:30000])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels30000 = labels30000[:,1]\nlabels30000 = np.asarray(labels30000).astype('float32')","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"if(np.isnan(data).any()):\n    print(\"The Array contain NaN values\")\n    \nelse:\n    print(\"The Array does not contain NaN values\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(data ,labels30000, epochs = 100)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save_weights(os.path.join(\"./\", 'weights.h5'), overwrite=True)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}