Kaggle competition: Predicting Molecular Properties
<SOS> metric: GroupMeanLogMAE Kaggle data source: predicting-molecular-properties<load_from_csv>
warnings.filterwarnings("ignore")
 warnings.filterwarnings(action="ignore",category=DeprecationWarning)
 warnings.filterwarnings(action="ignore",category=FutureWarning)
 %cd /kaggle/input/champs-scalar-coupling
print(os.listdir("."))
train = pd.read_csv('.. /input/bike-sharing-demand/train.csv')
 test = pd.read_csv('.. /input/bike-sharing-demand/test.csv' )<concatenate>
df_train=pd.read_csv('train.csv')
 df_test=pd.read_csv('test.csv')
 df_struct=pd.read_csv('structures.csv')
 df_train_sub_charge=pd.read_csv('mulliken_charges.csv')
 df_train_sub_tensor=pd.read_csv('magnetic_shielding_tensors.csv')
tt = train.append(test)
 tt.shape<drop_column>
def make_features(df):
df['dx']=df['x_1']-df['x_0']
df['dy']=df['y_1']-df['y_0']
df['dz']=df['z_1']-df['z_0']
df['distance']=(df['dx']**2+df['dy']**2+df['dz']**2)**(1/2)
 return df
df_train=make_features(df_train)
 df_test=make_features(df_test)
 test_prediction=np.zeros(len(df_test))
show_ram_usage()
print(df_train.shape, df_test.shape)
 def get_dist(df):
df_temp=df.loc[:,["molecule_name","atom_index_0","atom_index_1","distance","x_0","y_0","z_0","x_1","y_1","z_1"]].copy()
df_temp_=df_temp.copy()
df_temp_= df_temp_.rename(columns={'atom_index_0': 'atom_index_1',
'atom_index_1': 'atom_index_0',
'x_0': 'x_1',
'y_0': 'y_1',
'z_0': 'z_1',
'x_1': 'x_0',
'y_1': 'y_0',
'z_1': 'z_0'})
 df_temp_all=pd.concat(( df_temp,df_temp_),axis=0)
 df_temp_all["min_distance"]=df_temp_all.groupby(['molecule_name', 'atom_index_0'])['distance'].transform('min')
 df_temp_all["max_distance"]=df_temp_all.groupby(['molecule_name', 'atom_index_0'])['distance'].transform('max')
 df_temp= df_temp_all[df_temp_all["min_distance"]==df_temp_all["distance"]].copy()
df_temp=df_temp.drop(['x_0','y_0','z_0','min_distance'], axis=1)
 df_temp= df_temp.rename(columns={'atom_index_0': 'atom_index',
'atom_index_1': 'atom_index_closest',
'distance': 'distance_closest',
'x_1': 'x_closest',
'y_1': 'y_closest',
'z_1': 'z_closest'})
 for atom_idx in [0,1]:
df = map_atom_info(df,df_temp, atom_idx)
 df = df.rename(columns={'atom_index_closest': f'atom_index_closest_{atom_idx}',
'distance_closest': f'distance_closest_{atom_idx}',
'x_closest': f'x_closest_{atom_idx}',
'y_closest': f'y_closest_{atom_idx}',
'z_closest': f'z_closest_{atom_idx}'})
 df_temp= df_temp_all[df_temp_all["max_distance"]==df_temp_all["distance"]].copy()
df_temp=df_temp.drop(['x_0','y_0','z_0','max_distance'], axis=1)
 df_temp= df_temp.rename(columns={'atom_index_0': 'atom_index',
'atom_index_1': 'atom_index_farthest',
'distance': 'distance_farthest',
'x_1': 'x_farthest',
'y_1': 'y_farthest',
'z_1': 'z_farthest'})
 for atom_idx in [0,1]:
df = map_atom_info(df,df_temp, atom_idx)
 df = df.rename(columns={'atom_index_farthest': f'atom_index_farthest_{atom_idx}',
'distance_farthest': f'distance_farthest_{atom_idx}',
'x_farthest': f'x_farthest_{atom_idx}',
'y_farthest': f'y_farthest_{atom_idx}',
'z_farthest': f'z_farthest_{atom_idx}'})
 return df
df_test=(get_dist(df_test))
df_train=(get_dist(df_train))
print(df_train.shape, df_test.shape)
 show_ram_usage()
tt.reset_index().drop('index', axis=1, inplace=True)
 tt.head()<data_type_conversions>
def add_features(df):
df["distance_center0"]=(( df['x_0']-df['c_x'])**2+(df['y_0']-df['c_y'])**2+(df['z_0']-df['c_z'])**2)**(1/2)
 df["distance_center1"]=(( df['x_1']-df['c_x'])**2+(df['y_1']-df['c_y'])**2+(df['z_1']-df['c_z'])**2)**(1/2)
 df["distance_c0"]=(( df['x_0']-df['x_closest_0'])**2+(df['y_0']-df['y_closest_0'])**2+(df['z_0']-df['z_closest_0'])**2)**(1/2)
 df["distance_c1"]=(( df['x_1']-df['x_closest_1'])**2+(df['y_1']-df['y_closest_1'])**2+(df['z_1']-df['z_closest_1'])**2)**(1/2)
 df["distance_f0"]=(( df['x_0']-df['x_farthest_0'])**2+(df['y_0']-df['y_farthest_0'])**2+(df['z_0']-df['z_farthest_0'])**2)**(1/2)
 df["distance_f1"]=(( df['x_1']-df['x_farthest_1'])**2+(df['y_1']-df['y_farthest_1'])**2+(df['z_1']-df['z_farthest_1'])**2)**(1/2)
 df["vec_center0_x"]=(df['x_0']-df['c_x'])/(df["distance_center0"]+1e-10)
 df["vec_center0_y"]=(df['y_0']-df['c_y'])/(df["distance_center0"]+1e-10)
 df["vec_center0_z"]=(df['z_0']-df['c_z'])/(df["distance_center0"]+1e-10)
 df["vec_center1_x"]=(df['x_1']-df['c_x'])/(df["distance_center1"]+1e-10)
 df["vec_center1_y"]=(df['y_1']-df['c_y'])/(df["distance_center1"]+1e-10)
 df["vec_center1_z"]=(df['z_1']-df['c_z'])/(df["distance_center1"]+1e-10)
 df["vec_c0_x"]=(df['x_0']-df['x_closest_0'])/(df["distance_c0"]+1e-10)
 df["vec_c0_y"]=(df['y_0']-df['y_closest_0'])/(df["distance_c0"]+1e-10)
 df["vec_c0_z"]=(df['z_0']-df['z_closest_0'])/(df["distance_c0"]+1e-10)
 df["vec_c1_x"]=(df['x_1']-df['x_closest_1'])/(df["distance_c1"]+1e-10)
 df["vec_c1_y"]=(df['y_1']-df['y_closest_1'])/(df["distance_c1"]+1e-10)
 df["vec_c1_z"]=(df['z_1']-df['z_closest_1'])/(df["distance_c1"]+1e-10)
 df["vec_f0_x"]=(df['x_0']-df['x_farthest_0'])/(df["distance_f0"]+1e-10)
 df["vec_f0_y"]=(df['y_0']-df['y_farthest_0'])/(df["distance_f0"]+1e-10)
 df["vec_f0_z"]=(df['z_0']-df['z_farthest_0'])/(df["distance_f0"]+1e-10)
 df["vec_f1_x"]=(df['x_1']-df['x_farthest_1'])/(df["distance_f1"]+1e-10)
 df["vec_f1_y"]=(df['y_1']-df['y_farthest_1'])/(df["distance_f1"]+1e-10)
 df["vec_f1_z"]=(df['z_1']-df['z_farthest_1'])/(df["distance_f1"]+1e-10)
 df["vec_x"]=(df['x_1']-df['x_0'])/df["distance"]
df["vec_y"]=(df['y_1']-df['y_0'])/df["distance"]
df["vec_z"]=(df['z_1']-df['z_0'])/df["distance"]
df["cos_c0_c1"]=df["vec_c0_x"]*df["vec_c1_x"]+df["vec_c0_y"]*df["vec_c1_y"]+df["vec_c0_z"]*df["vec_c1_z"]
df["cos_f0_f1"]=df["vec_f0_x"]*df["vec_f1_x"]+df["vec_f0_y"]*df["vec_f1_y"]+df["vec_f0_z"]*df["vec_f1_z"]
df["cos_center0_center1"]=df["vec_center0_x"]*df["vec_center1_x"]+df["vec_center0_y"]*df["vec_center1_y"]+df["vec_center0_z"]*df["vec_center1_z"]
df["cos_c0"]=df["vec_c0_x"]*df["vec_x"]+df["vec_c0_y"]*df["vec_y"]+df["vec_c0_z"]*df["vec_z"]
df["cos_c1"]=df["vec_c1_x"]*df["vec_x"]+df["vec_c1_y"]*df["vec_y"]+df["vec_c1_z"]*df["vec_z"]
df["cos_f0"]=df["vec_f0_x"]*df["vec_x"]+df["vec_f0_y"]*df["vec_y"]+df["vec_f0_z"]*df["vec_z"]
df["cos_f1"]=df["vec_f1_x"]*df["vec_x"]+df["vec_f1_y"]*df["vec_y"]+df["vec_f1_z"]*df["vec_z"]
df["cos_center0"]=df["vec_center0_x"]*df["vec_x"]+df["vec_center0_y"]*df["vec_y"]+df["vec_center0_z"]*df["vec_z"]
df["cos_center1"]=df["vec_center1_x"]*df["vec_x"]+df["vec_center1_y"]*df["vec_y"]+df["vec_center1_z"]*df["vec_z"]
df=df.drop(['vec_c0_x','vec_c0_y','vec_c0_z','vec_c1_x','vec_c1_y','vec_c1_z',
'vec_f0_x','vec_f0_y','vec_f0_z','vec_f1_x','vec_f1_y','vec_f1_z',
'vec_center0_x','vec_center0_y','vec_center0_z','vec_center1_x','vec_center1_y','vec_center1_z',
'vec_x','vec_y','vec_z'], axis=1)
 return df
df_train=add_features(df_train)
 df_test=add_features(df_test)
 print(df_train.shape, df_test.shape)
 show_ram_usage()
pd.DatetimeIndex(train['datetime'] )<feature_engineering>
def create_nn_model(input_shape):
inp = Input(shape=(input_shape,))
x = Dense(256 )(inp)
 x = BatchNormalization()(x)
 x = LeakyReLU(alpha=0.05 )(x)
 x = Dropout(0.4 )(x)
 x = Dense(1024 )(x)
 x = BatchNormalization()(x)
 x = LeakyReLU(alpha=0.05 )(x)
 x = Dropout(0.2 )(x)
 x = Dense(1024 )(x)
 x = BatchNormalization()(x)
 x = LeakyReLU(alpha=0.05 )(x)
 x = Dropout(0.2 )(x)
 x = Dense(512 )(x)
 x = BatchNormalization()(x)
 x = LeakyReLU(alpha=0.05 )(x)
 x = Dropout(0.4 )(x)
 x = Dense(512 )(x)
 x = BatchNormalization()(x)
 x = LeakyReLU(alpha=0.05 )(x)
 x = Dense(256 )(x)
 x = BatchNormalization()(x)
 x = LeakyReLU(alpha=0.05 )(x)
 x = Dropout(0.4 )(x)
 out1 = Dense(2, activation="linear" )(x)
 out2 = Dense(6, activation="linear" )(x)
 out3 = Dense(12, activation="linear" )(x)
 x = Dense(128 )(x)
 x = BatchNormalization()(x)
 x = LeakyReLU(alpha=0.05 )(x)
 x = Dropout(0.2 )(x)
 x = Dense(128 )(x)
 x = BatchNormalization()(x)
 x = LeakyReLU(alpha=0.05 )(x)
 x = Dense(64 )(x)
 x = BatchNormalization()(x)
 x = LeakyReLU(alpha=0.05 )(x)
 x = Dropout(0.2 )(x)
 out = Dense(1, activation="linear" )(x)
 model = Model(inputs=inp, outputs=[out,out1,out2,out3])
 return model
temp = pd.DatetimeIndex(train['datetime'])
 train['year'] = temp.year
train['date'] = temp.date
train['hour'] = temp.hour<feature_engineering>
mol_types=df_train["type"].unique()
cv_score=[]
cv_score_total=0
epoch_n = 300
verbose = 0
batch_size = 2048
retrain =False
config = tf.ConfigProto(device_count = {'GPU': 1 , 'CPU': 2})
 config.gpu_options.allow_growth = True
config.gpu_options.per_process_gpu_memory_fraction = 0.6
sess = tf.Session(config=config)
 K.set_session(sess)
 start_time=datetime.now()
for mol_type in mol_types:
model_name_rd =('.. /keras-neural-net-for-champs/molecule_model_%s.hdf5' % mol_type)
 model_name_wrt =('/kaggle/working/molecule_model_%s.hdf5' % mol_type)
 print('Training %s' % mol_type, 'out of', mol_types, '
')
 df_train_=df_train[df_train["type"]==mol_type]
df_test_=df_test[df_test["type"]==mol_type]
input_features=["x_0","y_0","z_0","x_1","y_1","z_1","c_x","c_y","c_z",
'x_closest_0','y_closest_0','z_closest_0','x_closest_1','y_closest_1','z_closest_1',
"distance","distance_center0","distance_center1", "distance_c0","distance_c1","distance_f0","distance_f1",
"cos_c0_c1","cos_f0_f1","cos_center0_center1","cos_c0","cos_c1","cos_f0","cos_f1","cos_center0","cos_center1",
"atom_n"
]
input_data=StandardScaler().fit_transform(pd.concat([df_train_.loc[:,input_features],df_test_.loc[:,input_features]]))
target_data=df_train_.loc[:,"scalar_coupling_constant"].values
target_data_1=df_train_.loc[:,["charge_0","charge_1"]]
target_data_2=df_train_.loc[:,["XX_0","YY_0","ZZ_0","XX_1","YY_1","ZZ_1"]]
target_data_3=df_train_.loc[:,["YX_0","ZX_0","XY_0","ZY_0","XZ_0","YZ_0","YX_1","ZX_1","XY_1","ZY_1","XZ_1","YZ_1"]]
m1=1
m2=4
m3=1
target_data_1=m1*(StandardScaler().fit_transform(target_data_1))
target_data_2=m2*(StandardScaler().fit_transform(target_data_2))
target_data_3=m3*(StandardScaler().fit_transform(target_data_3))
train_index, cv_index = train_test_split(np.arange(len(df_train_)) ,random_state=111, test_size=0.1)
 train_input=input_data[train_index]
cv_input=input_data[cv_index]
train_target=target_data[train_index]
cv_target=target_data[cv_index]
train_target_1=target_data_1[train_index]
cv_target_1=target_data_1[cv_index]
train_target_2=target_data_2[train_index]
cv_target_2=target_data_2[cv_index]
train_target_3=target_data_3[train_index]
cv_target_3=target_data_3[cv_index]
test_input=input_data[len(df_train_):,:]
nn_model=create_nn_model(train_input.shape[1])
 if not retrain:
nn_model = load_model(model_name_rd)
 nn_model.compile(loss='mae', optimizer=Adam())
 es = callbacks.EarlyStopping(monitor='val_loss', min_delta=0.0001, patience=8,verbose=1, mode='auto', restore_best_weights=True)
 rlr = callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.1,patience=7, min_lr=1e-6, mode='auto', verbose=1)
 sv_mod = callbacks.ModelCheckpoint(model_name_wrt, monitor='val_loss', save_best_only=True, period=1)
 history = nn_model.fit(train_input,[train_target,train_target_1,train_target_2,train_target_3],
validation_data=(cv_input,[cv_target,cv_target_1,cv_target_2,cv_target_3]),
callbacks=[es, rlr, sv_mod], epochs=epoch_n, batch_size=batch_size, verbose=verbose)
 cv_predict=nn_model.predict(cv_input)
 plot_history(history, mol_type)
 accuracy=np.mean(np.abs(cv_target-cv_predict[0][:,0]))
cv_score.append(np.log(accuracy))
cv_score_total+=np.log(accuracy)
 test_predict=nn_model.predict(test_input)
 test_prediction[df_test["type"]==mol_type]=test_predict[0][:,0]
K.clear_session()
cv_score_total/=len(mol_types)
pd.DatetimeIndex(train.date ).dayofweek<feature_engineering>
def submit(predictions):
submit = pd.read_csv('sample_submission.csv')
 print(len(submit), len(predictions))
submit["scalar_coupling_constant"] = predictions
submit.to_csv("/kaggle/working/workingsubmission-test.csv", index=False)
 submit(test_prediction)
 print('Total training time: ', datetime.now() - start_time)
 i=0
for mol_type in mol_types:
print(mol_type,": cv score is ",cv_score[i])
 i+=1
print("total cv score is",cv_score_total )
<data_type_conversions><EOS> 
print(keras.__version__ )