{"cells":[{"metadata":{},"cell_type":"markdown","source":"# CNN-LSTM without features"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"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 in \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 \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\nfrom tqdm import tqdm\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.svm import NuSVR\nfrom sklearn.metrics import mean_absolute_error","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"train = pd.read_csv('../input/train.csv', dtype={'acoustic_data': np.float32, 'time_to_failure': np.float32}).values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train [0:150000, 0 ] .mean(axis=0)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# pandas doesn't show us all the decimals\npd.options.display.precision = 15","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"rows = 150_000\nsegments = int(np.floor(train.shape[0] / rows))\nprint('train.shape',train.shape)\nsegments\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# for data set without features"},{"metadata":{"trusted":true},"cell_type":"code","source":"n_steps=150\nstep_length=1000\n\n   #n_features = create_X(train [0:150000,0]).shape\n   #create_X(train[:, 0], last_index=8388, n_steps=150000, step_length=1)\n    \ndef create_X(x, last_index=None, n_steps=n_steps, step_length=step_length):\n    if last_index == None:\n        last_index=len(x)\n       \n  #  assert last_index - n_steps * step_length >= 0\n    if last_index - n_steps * step_length < 0:\n        value=int((last_index - n_steps * step_length )* (-1))\n        temp = (x[(int(last_index) + value - n_steps * step_length):(int(last_index)+value)].reshape(n_steps,step_length,1 ).astype(np.float32) - 5 ) / 3  \n    else:\n    # Reshaping and approximate standardization with mean 5 and std 3.\n        temp = (x[(int(last_index) - n_steps * step_length):int(last_index)].reshape(n_steps,step_length,1 ).astype(np.float32) - 5 ) / 3   \n    # convert (150000) to [150 1000 ]\n    # then extract feature from each row of length 1000. so total 150 \n    \n    # Extracts features of sequences of full length 1000, of the last 100 values and finally also \n    # of the last 10 observations. \n    \n    return temp\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"last_index=110000.0\nvalue=int((last_index - n_steps * step_length )* (-1))\n(int(last_index) + value - n_steps * step_length)\nint(last_index)+value","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"[](http://)"},{"metadata":{},"cell_type":"markdown","source":"# Creating Training Data without features"},{"metadata":{"trusted":true},"cell_type":"code","source":"# Query \"create_X\" to figure out the number of features\nn_features = create_X(train [0:150000,0]).shape\nprint(\"Output segment shape\", n_features)     # 18 features each row of segment ie 150x18 features of 150000 chunk input\n\n\nmaxsize=train .shape[0]        #629145480\nC = int(np.floor(maxsize / (n_steps*step_length))) #4194\nbatch_size = seg-1   # (4193,) \nxx=350\n\n\n##############################################################################################\nrows_initialize = np.zeros((seg), dtype=float)  #4194\nprint(rows_initialize.shape)\n\nfor seg1 in tqdm(range(1,seg)) :      # for loop from 1 to 4194 segment value\n    rows_initialize [seg1] = seg1 * (n_steps*step_length) \n\nrows=np.delete(rows_initialize,0)    # (4193,)\n\nprint(rows.shape)\n\n########################################################################################\nbatch_size=batch_size-xx    # training data\n#batch_size=xx              # validation data\nsplit_point=xx\nsecond_earthquake = rows[xx]\n\n\n\n##########################################################################################\n\nif batch_size < 1000  :    # validation set \n               rows_1 = rows[:split_point+1]    #  0:350 \n        \nif batch_size > 1000:   # training set\n               rows_1 = rows[split_point+1 :]    # (351,) ie 351:4193    \n            \n\n       \n    # Initialize feature matrices and targets\nsamples_tr= np.zeros((rows_1.shape[0], n_features[0], n_features[1], 1), dtype=float)   #  for validation (350,150000)  for training ( 3842, 150000) \ntargets_tr = np.zeros(rows_1.shape[0], )    # (16,)  for validation (350)    for training ( 3843)\n        \nfor j, row in enumerate(rows_1):             # 16 for validation (350)    for training ( 3843)\n    samples_tr[j] = create_X(train[:, 0], last_index=row, n_steps=n_steps, step_length=step_length)\n    targets_tr[j] = train[int(row - 1), 1]         \n    \n    \n################################################################################################\n\nprint('samples_tr shape', samples_tr.shape)\nprint('targets_tr shape', targets_tr.shape)\n\nsamples_tr.shape\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## overlaping window\n\n# Query \"create_X\" to figure out the number of features\nn_features = create_X(train [0:150000,0]).shape\nprint(\"Output segment shape\", n_features)     # 18 features each row of segment ie 150x18 features of 150000 chunk input\n\n\nmaxsize=train .shape[0]        #629145480\nC = int(np.floor(maxsize / (n_steps*step_length))) #4194\nseg=int(np.floor(maxsize / (n_steps*step_length)))\nbatch_size = seg-1   # (4193,) \nxx=350\n\n\nstride=110000\noutput= int((maxsize-150000)/stride)+1  #41934\nseg=output  #41934\nbatch_size= output  #41934\nxx=int (output*0.1) #8386\n\n##############################################################################################\nrows_initialize = np.zeros((seg), dtype=float)  #41934\nprint(rows_initialize.shape) #41934\n\nfor seg1 in tqdm(range(1,seg)) :      # for loop from 1 to #41934 segment value\n    #rows_initialize [seg1] = seg1 * (n_steps*step_length) \n    rows_initialize [seg1] = int(seg1*stride)\n    \nrows=np.delete(rows_initialize,0)    # (#41934,)\n\nprint(rows.shape)\n\n########################################################################################\nbatch_size=batch_size-xx    # training data\n#batch_size=xx              # validation data\nsplit_point=xx\nsecond_earthquake = rows[xx]\n\n\n\n##########################################################################################\n\nif batch_size < 1000  :    # validation set   41934\n               rows_1 = rows[:split_point+1]    #  0:8386\n \n        \nif batch_size > 1000  :   # training set     41934\n               rows_1 = rows[split_point+1 :]    #  ie 8386 : 41934  \n        \nrows_1 = rows    #  ie removing validation data \nprint (batch_size)\nprint (xx)\n       \n    # Initialize feature matrices and targets\nsamples_tr= np.zeros((rows_1.shape[0], n_features[0], n_features[1], 1), dtype=float)   #  for validation (350,150000)  for training ( 3842, 150000) \ntargets_tr = np.zeros(rows_1.shape[0], )    # (16,)  for validation (350)    for training ( 3843)\n        \nfor j, row in enumerate(rows_1):             # 16 for validation (350)    for training ( 3843)\n    samples_tr[j] = create_X(train[:, 0], last_index=row, n_steps=n_steps, step_length=step_length)\n    targets_tr[j] = train[int(row - 1), 1]         \n    \n   # create_X(train[:, 0], last_index=41934, n_steps=150000, step_length=1)\n################################################################################################\n\nprint('samples_tr shape', samples_tr.shape)\nprint('targets_tr shape', targets_tr.shape)\n\n#samples_tr.shape\n#rows_initialize.shape\n#row","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"samples_tr","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Creating Validation data without features"},{"metadata":{"trusted":true},"cell_type":"code","source":"#batch_size=batch_size-xx    # training data\nbatch_size=xx              # validation data\nsplit_point=xx\nsecond_earthquake = rows[xx]\n\n##########################################################################################\n\nif batch_size < 1000  :    # validation set \n               rows_1 = rows[:split_point+1]    #  0:350 \n        \nif batch_size > 1000 :   # training set\n               rows_1 = rows[split_point+1 :]    # (351,) ie 351:4193    \n            \n\n       \n    # Initialize feature matrices and targets\nsamples_vd= np.zeros((rows_1.shape[0], n_features[0], n_features[1], 1), dtype=float)    #  for validation (350,150000)  for training ( 3842, 150000) \ntargets_vd = np.zeros(rows_1.shape[0], )    # (16,)  for validation (350)    for training ( 3843)\n        \nfor j, row in enumerate(rows_1):             # 16 for validation (350)    for training ( 3843)\n    samples_vd[j] = create_X(train[:, 0], last_index=row, n_steps=n_steps, step_length=step_length)\n    targets_vd[j] = train[int(row - 1), 1]         \n    \n    \n################################################################################################\n\n    \nprint('samples_tr shape', samples_tr.shape)\nprint('targets_tr shape',targets_tr.shape) \n    \nprint('samples_vd shape', samples_vd.shape)\nprint('targets_vd shape',targets_vd.shape)  \n#print('rows_1 shape',rows_1.shape[0])\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## overlaping window\n\n# Query \"create_X\" to figure out the number of features\nn_features = create_X(train [0:150000,0]).shape\nprint(\"Output segment shape\", n_features)     # 18 features each row of segment ie 150x18 features of 150000 chunk input\n\n\nmaxsize=train .shape[0]        #629145480\nC = int(np.floor(maxsize / (n_steps*step_length))) #4194\nseg=int(np.floor(maxsize / (n_steps*step_length)))\nbatch_size = seg-1   # (4193,) \nxx=350\n\n\nstride=80000\noutput= int((maxsize-150000)/stride)+1  #41934\nseg=output  #41934\nbatch_size= output  #41934\nxx=int (output*0.1) #8386\n\n##############################################################################################\nrows_initialize = np.zeros((seg), dtype=float)  #41934\nprint(rows_initialize.shape) #41934\n\nfor seg1 in tqdm(range(1,seg)) :      # for loop from 1 to #41934 segment value\n    #rows_initialize [seg1] = seg1 * (n_steps*step_length) \n    rows_initialize [seg1] = int(seg1*stride)\n    \nrows=np.delete(rows_initialize,0)    # (#41934,)\n\nprint(rows.shape)\n\n########################################################################################\nbatch_size=xx               # training data\n#batch_size=xx              # validation data\nsplit_point=xx\nsecond_earthquake = rows[xx]\nprint (batch_size)\nprint (xx)\n\n\n##########################################################################################\n\nif batch_size < 1000  :    # validation set   41934\n               rows_1 = rows[:split_point+1]    #  0:8386\n \n        \nif batch_size > 1000  :   # training set     41934\n               rows_1 = rows[split_point+1 :]    #  ie 8386 : 41934  \n            \n\n       \n    # Initialize feature matrices and targets\nsamples_tr= np.zeros((rows_1.shape[0], n_features[0], n_features[1]), dtype=float)   #  for validation (350,150000)  for training ( 3842, 150000) \ntargets_tr = np.zeros(rows_1.shape[0], )    # (16,)  for validation (350)    for training ( 3843)\n        \nfor j, row in enumerate(rows_1):             # 16 for validation (350)    for training ( 3843)\n    samples_tr[j] = create_X(train[:, 0], last_index=row, n_steps=n_steps, step_length=step_length)\n    targets_tr[j] = train[int(row - 1), 1]         \n    \n   # create_X(train[:, 0], last_index=41934, n_steps=150000, step_length=1)\n################################################################################################\n\nprint('samples_tr shape', samples_tr.shape)\nprint('targets_tr shape', targets_tr.shape)\n\nsamples_tr.shape\nrows_initialize.shape\nrow","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"split_point+1","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Define the model"},{"metadata":{"trusted":true},"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense, CuDNNGRU, SimpleRNN, LSTM ,  Dropout, Activation, Flatten, Input, Conv1D, MaxPooling1D, Reshape,  Conv2D, MaxPooling2D, Reshape, Flatten\nfrom keras.optimizers import adam\nfrom keras.callbacks import ModelCheckpoint\nfrom keras.optimizers import RMSprop\nfrom keras.layers.advanced_activations import LeakyReLU, Softmax\nfrom keras.utils import plot_model\n\n\n# Shared Input Layer\nfrom keras.utils import plot_model\nfrom keras.models import Model\nfrom keras.layers import Input\nfrom keras.layers import Dense\nfrom keras.layers import Flatten\nfrom keras.layers.convolutional import Conv2D\nfrom keras.layers.pooling import MaxPooling2D\nfrom keras.layers.merge import concatenate","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n# working fast cnn2 vgg\n\ni = (n_features[0],n_features[1] ,n_features[2])\n#i = (X_train.shape[1])\ni\nmodel = Sequential ()\n\n#model.add(Reshape((1500,100,1), input_shape=i))\nmodel.add(Conv2D(16, (3, 3), strides = (2,2),  input_shape=i, kernel_initializer='he_normal', padding='same'))\nmodel.add(LeakyReLU(0.1))\nmodel.add(MaxPooling2D())\nmodel.add(Dropout(0.3))\n\nmodel.add(Conv2D(32, (3, 3), strides = (2,2), kernel_initializer='he_normal', padding='same'))\nmodel.add(LeakyReLU(0.2))\nmodel.add(MaxPooling2D())\nmodel.add(Dropout(0.3))\n\nmodel.add(Conv2D(64, (3, 3), strides = (2,2), kernel_initializer='he_normal', padding='same'))\nmodel.add(LeakyReLU(0.2))\nmodel.add(MaxPooling2D())\nmodel.add(Dropout(0.3))\n\n#model.add(Conv2D(30, (3, 3), strides = (2,2), kernel_initializer='he_normal', padding='same'))\n#model.add(LeakyReLU(0.2))\n#model.add(MaxPooling2D())\n#model.add(Dropout(0.3))\n\nmodel.add(Reshape((30,64)))\n#model.add(Flatten())\n\nmodel.add(LSTM(64,  return_sequences=True))\nmodel.add(Dropout(0.2))\nmodel.add(LSTM(64))\nmodel.add(Dropout(0.2))\n\n#model.add(Dense(60))\n#model.add(Dense(30))\nmodel.add(Dense(1))\n\nmodel.summary()\n#plot_model(model, to_file='model.png')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"\n# working fast cnn2 vgg\n\ni = (n_features[0],n_features[1] ,n_features[2])\n#i = (X_train.shape[1])\ni\nmodel = Sequential ()\n\n#model.add(Reshape((1500,100,1), input_shape=i))\nmodel.add(Conv2D(16, (3, 3), strides = (2,2),  input_shape=i, kernel_initializer='he_normal', padding='same'))\nmodel.add(LeakyReLU(0.1))\nmodel.add(Conv2D(16, (3, 3), strides = (2,2), kernel_initializer='he_normal', padding='same'))\nmodel.add(LeakyReLU(0.1))\nmodel.add(MaxPooling2D())\nmodel.add(Dropout(0.3))\n\nmodel.add(Conv2D(32, (3, 3), strides = (2,2), kernel_initializer='he_normal', padding='same'))\nmodel.add(LeakyReLU(0.2))\nmodel.add(Conv2D(32, (3, 3), strides = (2,2), kernel_initializer='he_normal', padding='same'))\nmodel.add(LeakyReLU(0.2))\nmodel.add(MaxPooling2D())\nmodel.add(Dropout(0.3))\n\nmodel.add(Reshape((23,32)))\n#model.add(Flatten())\n\nmodel.add(LSTM(32,  return_sequences=True))\nmodel.add(Dropout(0.2))\nmodel.add(LSTM(32))\nmodel.add(Dropout(0.2))\n\nmodel.add(Dense(60))\nmodel.add(Dense(30))\nmodel.add(Dense(1))\n\nmodel.summary()\n#plot_model(model, to_file='model.png')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# cnn2 ALEX NET\n\ni = (n_features[0],n_features[1] ,n_features[2])\ni\nmodel = Sequential ()\n\n# input layer\nvisible = Input(shape=i)\n# first feature extractor\nconv1 = Conv2D(16, kernel_size=3, activation='relu')(visible)\npool1 = MaxPooling2D()(conv1)\nflat1 = Flatten()(pool1)\n# second feature extractor\nconv2 = Conv2D(16, kernel_size=3, activation='relu')(visible)\npool2 = MaxPooling2D()(conv2)\nflat2 = Flatten()(pool2)\n# merge feature extractors\nmerge1 = concatenate([pool1, pool2])\n\n# interpretation layer\n#merge1_1 =Reshape(( 19199744,1))(merge1)\n\n# first feature extractor\nconv3 = Conv2D(32, kernel_size=3, activation='relu')(merge1)\npool3 = MaxPooling2D()(conv3)\nflat3 = Flatten()(pool3)\n# second feature extractor\nconv4 = Conv2D(32, kernel_size=3, activation='relu')(merge1)\npool4 = MaxPooling2D()(conv4)\nflat4 = Flatten()(pool4)\n# merge feature extractors\nmerge2 = concatenate([pool3 , pool4] )\n\n\n# first feature extractor\n#conv5 = Conv2D(64, kernel_size=3, activation='relu')(merge2)\n#pool5 = MaxPooling2D()(conv5)\n#flat5 = Flatten()(pool5)\n# second feature extractor\n#conv6 = Conv2D(64, kernel_size=3, activation='relu')(merge2)\n#pool6 = MaxPooling2D()(conv6)\n#flat6 = Flatten()(pool6)\n# merge feature extractors\n#merge3 = concatenate([pool5 , pool6] )\n\n#merge3_3 =Reshape((1850,128))(merge3)\nmerge3_3 =Reshape((8579,64))(merge2)\nLSTM1= LSTM(64,  return_sequences=True)(merge3_3)\n#Dropout=Dropout(0.2)(LSTM1)\nLSTM2= LSTM(64)(LSTM1)\n#Dropout=Dropout(0.2)(LSTM2)\n\n#output1=Dense(62)(LSTM2)\noutput2=Dense(1)(LSTM2)\n\nmodel = Model(inputs=visible, outputs=output2)\nprint(model.summary())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"## CNN combined with LSTM Model sssssssssssssssssss\ni = (n_features[0],n_features[1])\nmodel = Sequential ()\n\nmodel.add(Conv1D (kernel_size = (3), filters = 8, strides=2, input_shape=i, kernel_initializer='he_normal')) \nmodel.add(LeakyReLU())\nmodel.add(Conv1D (kernel_size = (3), filters = 8, strides=2, kernel_initializer='he_normal'))\nmodel.add(LeakyReLU())\nmodel.add(Dropout(0.2))\nmodel.add(MaxPooling1D())\n\nmodel.add(Conv1D (kernel_size = (3), filters = 16, strides=2, kernel_initializer='he_normal')) \nmodel.add(LeakyReLU())\nmodel.add(Conv1D (kernel_size = (3), filters = 16, strides=2, kernel_initializer='he_normal')) \nmodel.add(LeakyReLU())\nmodel.add(Dropout(0.2))\nmodel.add(MaxPooling1D())\n\nmodel.add(Conv1D (kernel_size = (3), filters = 32, strides=2, kernel_initializer='he_normal')) \nmodel.add(LeakyReLU())\nmodel.add(Conv1D (kernel_size = (3), filters = 32, strides=2, kernel_initializer='he_normal')) \nmodel.add(LeakyReLU())\nmodel.add(Dropout(0.2))\nmodel.add(MaxPooling1D())\n\nmodel.add(Conv1D (kernel_size = (3), filters = 64, strides=2, kernel_initializer='he_normal')) \nmodel.add(LeakyReLU())\nmodel.add(Conv1D (kernel_size = (3), filters = 64, strides=2, kernel_initializer='he_normal')) \nmodel.add(LeakyReLU())\nmodel.add(Dropout(0.2))\nmodel.add(MaxPooling1D())\n\n#model.add(Flatten())\n#model.add(Dense (250, activation='relu', kernel_initializer='he_normal'))\n#model.add(BatchNormalization())\n#model.add(Dropout(0.5))\n    \nmodel.add(LSTM(64,  return_sequences=True))\nmodel.add(Dropout(0.2))\nmodel.add(LSTM(32))\nmodel.add(Dropout(0.2))\n\nmodel.add(Dense(32))\nmodel.add(Dense(1))\n\nmodel.summary()\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# VGG 16\n\ni = (n_features[0],n_features[1])\n#i = (X_train.shape[1])\ni\nmodel = Sequential ()\n\n#model.add(Reshape((1500,100,1), input_shape=i))\nmodel.add(Conv1D(16, 3, strides = 2, kernel_initializer='he_normal',input_shape=i, padding='same'))\nmodel.add(LeakyReLU(0.1))\nmodel.add(Conv1D(16, 3, strides = 2, padding='same'))\nmodel.add(LeakyReLU(0.1))\nmodel.add(MaxPooling1D())\n#model.add(Dropout(0.3))\n\nmodel.add(Conv1D(32, 3, strides = 2, padding='same'))\nmodel.add(LeakyReLU(0.1))\nmodel.add(Conv1D(32, 3, strides = 2, padding='same'))\nmodel.add(LeakyReLU(0.1))\nmodel.add(MaxPooling1D())\n#model.add(Dropout(0.3))\n\nmodel.add(Conv1D(64, 3, strides = 2, padding='same'))\nmodel.add(LeakyReLU(0.1))\nmodel.add(Conv1D(64, 3, strides = 2, padding='same'))\nmodel.add(LeakyReLU(0.1))\nmodel.add(MaxPooling1D())\n#model.add(Dropout(0.3))\n\n#model.add(Conv1D(128, 3, strides = 2, kernel_initializer='he_normal', padding='same'))\n#model.add(LeakyReLU(0.1))\n#model.add(Conv1D(128, 3, strides = 2, kernel_initializer='he_normal', padding='same'))\n#model.add(LeakyReLU(0.1))\n#model.add(MaxPooling1D())\n\n#model.add(Flatten())\n#model.add(Softmax())\n#model.add(Reshape((18752,1)))\n#model.add(Dense(60))\n#model.add(Dense(1))\n\n\n#model.add(Conv2D(30, (3, 3), strides = (2,2), kernel_initializer='he_normal', padding='same'))\n#model.add(LeakyReLU(0.2))\n#model.add(MaxPooling2D())\n#model.add(Dropout(0.3))\n\n#model.add(Reshape((23,60)))\n#model.add(Flatten())\n\nmodel.add(LSTM(32,  return_sequences=True))\nmodel.add(Dropout(0.2))\nmodel.add(LSTM(32))\nmodel.add(Dropout(0.2))\n\n#model.add(Dense(60))\nmodel.add(Dense(30))\nmodel.add(Dense(1))\n\nmodel.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# ALEX NET\n\ni = (n_features[0],n_features[1])\n#i = (X_train.shape[1])\ni\nmodel = Sequential ()\n\n# input layer\nvisible = Input(shape=i)\n# first feature extractor\nconv1 = Conv1D(128, kernel_size=3, activation='relu')(visible)\npool1 = MaxPooling1D()(conv1)\nflat1 = Flatten()(pool1)\n# second feature extractor\nconv2 = Conv1D(128, kernel_size=3, activation='relu')(visible)\npool2 = MaxPooling1D()(conv2)\nflat2 = Flatten()(pool2)\n# merge feature extractors\nmerge1 = concatenate([flat1, flat2])\n# interpretation layer\nmerge1_1 =Reshape(( 19199744,1))(merge1)\n\n# first feature extractor\nconv3 = Conv1D(64, kernel_size=3, activation='relu')(merge1_1)\npool3 = MaxPooling1D()(conv3)\nflat3 = Flatten()(pool3)\n# second feature extractor\nconv4 = Conv1D(64, kernel_size=3, activation='relu')(merge1_1)\npool4 = MaxPooling1D()(conv4)\nflat4 = Flatten()(pool4)\n# merge feature extractors\nmerge2 = concatenate([flat3 , flat4] )\nmerge2_2 =Reshape(( 1228783488,1))(merge2)\n\n\n## interpretation layer\n#hidden1 = Dense(100, activation='relu')(merge1)\n## prediction output\n#output = Dense(1, activation='relu')(hidden1)\n\n\nLSTM1= LSTM(128,  return_sequences=True)(merge2_2)\n#Dropout=Dropout(0.2)(LSTM1)\nLSTM2= LSTM(128)(LSTM1)\n#Dropout=Dropout(0.2)(LSTM2)\n\n#output1=Dense(62)(LSTM2)\noutput2=Dense(1)(LSTM2)\n\n\n\n\nmodel = Model(inputs=visible, outputs=output2)\n# summarize layers\nprint(model.summary())\n# plot graph\n#plot_model(model, to_file='shared_input_layer.png')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Compile and fit model"},{"metadata":{"trusted":true},"cell_type":"code","source":"# vggg 16\nimport keras\nfrom keras.optimizers import RMSprop\nopt = keras.optimizers.adam(lr=.005)\n\nmodel.compile(loss=\"mae\",\n              optimizer=opt, metrics=['mean_absolute_error'])\n             # metrics=['accuracy'])\n\n\nbatch_size = 32 # mini-batch with 32 examples\nepochs = 50\nhistory = model.fit(\n    samples_tr, targets_tr,\n    batch_size=batch_size,\n    epochs=epochs,\n    verbose=1)\n   #validation_data=(samples_vd  ,targets_vd ))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# alex net\nimport keras\nfrom keras.optimizers import RMSprop\nopt = keras.optimizers.adam(lr=.005)\n\nmodel.compile(loss=\"mae\",\n              optimizer=opt, metrics=['mean_absolute_error'])\n             # metrics=['accuracy'])\n\n\nbatch_size = 32 # mini-batch with 32 examples\nepochs = 50\nhistory = model.fit(\n    samples_tr, targets_tr,\n    batch_size=batch_size,\n    epochs=epochs,\n    verbose=1,\n   validation_data=(samples_vd  ,targets_vd ))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"markdown","source":"# Load submission file\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = pd.read_csv('../input/sample_submission.csv', index_col='seg_id', dtype={\"time_to_failure\": np.float32})","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Prepare submission data\nLoad each test data, create the feature matrix, get numeric prediction"},{"metadata":{"trusted":true},"cell_type":"code","source":"for i, seg_id in enumerate(tqdm(submission.index)):\n  #  print(i)\n    seg = pd.read_csv('../input/test/' + seg_id + '.csv')\n    x = seg['acoustic_data'].values\n    submission.time_to_failure[i] = model.predict(np.expand_dims(create_X(x), 0))\n\nsubmission.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Save submission file"},{"metadata":{"trusted":true},"cell_type":"code","source":"submission.to_csv('submission_without features new model VGG 16.csv')","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# OTHER TECHNIQUES"},{"metadata":{"trusted":true},"cell_type":"code","source":"x.mean()  ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"scaler = StandardScaler()\nscaler.fit(X_train)\nX_train_scaled = scaler.transform(X_train)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train_scaled","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_train.values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_train.values.flatten()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.ensemble import RandomForestRegressor\n\n## Train the  Model\nmodel = RandomForestRegressor(n_estimators=200)\nmodel.fit(X_train_scaled, y_train.values.flatten())      # .fit used for training\ny_pred = model.predict(X_train_scaled)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# number support vector regressor\n\n# svm = NuSVR()\n# svm.fit(X_train_scaled, y_train.values.flatten())\n# y_pred = svm.predict(X_train_scaled)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y_pred.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.figure(figsize=(6, 6))\nplt.scatter(y_train.values.flatten(), y_pred)\nplt.xlim(0, 20)\nplt.ylim(0, 20)\nplt.xlabel('actual', fontsize=12)\nplt.ylabel('predicted', fontsize=12)\nplt.plot([(0, 0), (20, 20)], [(0, 0), (20, 20)])\nplt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"score = mean_absolute_error(y_train.values.flatten(), y_pred)\nprint(f'Score: {score:0.3f}')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submission = pd.read_csv('../input/sample_submission.csv', index_col='seg_id')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_test = pd.DataFrame(columns=X_train.columns, dtype=np.float64, index=submission.index)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for seg_id in X_test.index:\n    seg = pd.read_csv('../input/test/' + seg_id + '.csv')\n    \n    x = seg['acoustic_data'].values\n    \n    X_test.loc[seg_id, 'ave'] = x.mean()\n    X_test.loc[seg_id, 'std'] = x.std()\n    X_test.loc[seg_id, 'max'] = x.max()\n    X_test.loc[seg_id, 'min'] = x.min()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_test_scaled = scaler.transform(X_test)\nsubmission['time_to_failure'] = svm.predict(X_test_scaled)\nsubmission.to_csv('submission.csv')","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}