{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np \nimport pandas as pd","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"default_path = '../input/'\n\ndf_train = pd.read_csv(default_path+'train.csv')\ndf_test  = pd.read_csv(default_path+'test.csv')\ndf_struct = pd.read_csv(default_path+'structures.csv')\nunique_type = list(set(df_test['type']))\n\nelements = []\nfor i in unique_type: \n    elements.append(i[2])\n    elements.append(i[3])\nunique_elements = list(set(elements))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Gives a normalized list of values for each of the TYPE of coupling\ndef give_uniqueId(lis): \n    l = np.size(lis)\n    fin = np.zeros((l,1))\n    for i in range(0,l):\n        fin[i] = (unique_type.index(lis[i])+1)\n    return fin\n\ndef give_uniqueElement(lis):\n    element = np.frompyfunc(lambda x:x[3:4],1,1)(lis)\n    uniqueID = np.frompyfunc(lambda x:unique_elements.index(x)-1,1,1)(element)\n    return uniqueID\ndef giveFirst(lis):\n    return np.frompyfunc(lambda x:int(x[0:1])-1,1,1)(lis)\n    \n# Returns dataframe merging xyz values\ndef map_atom_info(df, atom_idx):\n    df = pd.merge(df, df_struct, how = 'left',\n                  left_on  = ['molecule_name', f'atom_index_{atom_idx}'],\n                  right_on = ['molecule_name',  'atom_index'])\n    \n    df = df.drop('atom_index', axis=1)\n    df = df.rename(columns={'atom': f'atom_{atom_idx}',\n                            'x': f'x_{atom_idx}',\n                            'y': f'y_{atom_idx}',\n                            'z': f'z_{atom_idx}'})\n    return df","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Merging the x,y,z values into the train and test dataframes\ntrain_df = map_atom_info(map_atom_info(df_train,0),1)\ntest_df = map_atom_info(map_atom_info(df_test,0),1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"#First parameter of the type\ntrain_df['typeval']= giveFirst(train_df['type'].values)\ntest_df['typeval']= giveFirst(test_df['type'].values)\n# Giving last 2nd element as a parameter as first element is always hydrogen\ntrain_df['elem'] = give_uniqueElement(train_df['type'].values)\ntest_df['elem']  = give_uniqueElement(test_df['type'].values)\n# Assigning unique numerical values to the type \ntrain_df['type'] = give_uniqueId(train_df['type'].values)\ntest_df['type'] = give_uniqueId(test_df['type'].values)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.head(4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"test_df.head(3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df['dx'] = train_df['x_0']-train_df['x_1']\ntrain_df['dy'] = train_df['y_0']-train_df['y_1']\ntrain_df['dz'] = train_df['z_0']-train_df['z_1']\ntest_df['dx'] = test_df['x_0']-test_df['x_1']\ntest_df['dy'] = test_df['y_0']-test_df['y_1']\ntest_df['dz'] = test_df['z_0']-test_df['z_1']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_df.head(4)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Assigning features and lables\nX_train = train_df[['x_0','y_0','z_0','x_1','y_1','z_1','dx','dy','dz','elem','typeval','type']].values\ny_train = train_df['scalar_coupling_constant'].values\nX_test = test_df[['x_0','y_0','z_0','x_1','y_1','z_1','dx','dy','dz','elem','typeval','type']].values","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X_train","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Preprocessing : Making the mean of features to 0\nfrom sklearn import preprocessing\nX_train = preprocessing.scale(X_train)\nX_test = preprocessing.scale(X_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Importing requirements for dnn\nimport tensorflow as tf","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"n_cols = X_train.shape[1]\nmodel = tf.keras.models.Sequential([\n     \n    tf.keras.layers.Dense(5000, activation='relu', input_shape=(n_cols,)),\n    tf.keras.layers.Dropout(0.5),  # Since the network is dense and big, using dropout\n    tf.keras.layers.Dense(1000, activation='relu'),\n    tf.keras.layers.Dropout(0.05),\n    tf.keras.layers.Dense(500, activation='relu'),\n    tf.keras.layers.Dense(500, activation='relu'),\n    tf.keras.layers.Dropout(0.05),\n    tf.keras.layers.Dense(200, activation='relu'),\n    tf.keras.layers.Dense(1, activation='linear'),\n    ])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model.compile(optimizer='adam', loss='mean_squared_error')\nmodel.summary()\nmodel.fit(X_train, y_train,epochs=7)# not using validation set as regression will usually not have exact answer","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pred = model.predict(X_test)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def submit(predictions):\n    submit = pd.read_csv(default_path+'sample_submission.csv')\n    print(len(submit), len(predictions))   \n    submit[\"scalar_coupling_constant\"] = predictions\n    submit.to_csv(\"submission.csv\", index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"submit(pred)","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}