Kaggle competition: Predicting Molecular Properties
<SOS> metric: GroupMeanLogMAE Kaggle data source: predicting-molecular-properties<split>
%matplotlib inline
X_short = pd.DataFrame({'ind': list(X.index), 'type': X['type'].values, 'oof': [0] * len(X), 'target': y_fc.values})
 X_short_test = pd.DataFrame({'ind': list(X_test.index), 'type': X_test['type'].values, 'prediction': [0] * len(X_test)})
 result_dict_lgb_oof = {}
for t in X['type'].unique() :
print(f'Training of type {t}')
 X_t = X.loc[X['type'] == t]
X_test_t = X_test.loc[X_test['type'] == t]
y_t = X_short.loc[X_short['type'] == t, 'target']
result_dict_lgb_oof = train_model_regression(X=X_t, X_test=X_test_t, y=y_t, params=params, folds=folds, model_type='lgb', eval_metric='group_mae', plot_feature_importance=False,
verbose=500, early_stopping_rounds=200, n_estimators=n_estimators_default)
 X_short.loc[X_short['type'] == t, 'oof'] = result_dict_lgb_oof['oof']
X_short_test.loc[X_short_test['type'] == t, 'prediction'] = result_dict_lgb_oof['prediction']<feature_engineering>
DATA_PATH = '.. /input'
SUBMISSIONS_PATH = './'
ATOMIC_NUMBERS = {
'H': 1,
'C': 6,
'N': 7,
'O': 8,
'F': 9
}
X['oof_fc'] = X_short['oof']
X_test['oof_fc'] = X_short_test['prediction']<define_variables>
pd.set_option('display.max_colwidth', -1)
 pd.set_option('display.max_rows', 120)
 pd.set_option('display.max_columns', 120 )
good_columns = ['type','oof_fc',
'bond_lengths_mean_y',
'bond_lengths_median_y',
'bond_lengths_std_y',
'bond_lengths_mean_x',
'molecule_atom_index_0_dist_min_div',
'molecule_atom_index_0_dist_std_div',
'molecule_atom_index_0_dist_mean',
'molecule_atom_index_0_dist_max',
'dist_y',
'molecule_atom_index_1_dist_std_diff',
'z_0',
'molecule_type_dist_min',
'molecule_atom_index_0_y_1_mean_div',
'dist_x',
'x_0',
'y_0',
'molecule_type_dist_std',
'molecule_atom_index_0_y_1_std',
'molecule_dist_mean',
'molecule_atom_index_0_dist_std_diff',
'dist_z',
'molecule_atom_index_0_dist_std',
'molecule_atom_index_0_x_1_std',
'molecule_type_dist_std_diff',
'molecule_type_0_dist_std',
'dist',
'molecule_atom_index_0_dist_mean_diff',
'molecule_atom_index_1_dist_min_div',
'molecule_atom_index_1_dist_mean_diff',
'y_1',
'molecule_type_dist_mean_div',
'molecule_dist_max',
'molecule_atom_index_0_dist_mean_div',
'z_1',
'molecule_atom_index_0_z_1_std',
'molecule_atom_index_1_dist_mean_div',
'molecule_atom_index_1_dist_min_diff',
'molecule_atom_index_1_dist_mean',
'molecule_atom_index_1_dist_min',
'molecule_atom_index_1_dist_max',
'molecule_type_0_dist_std_diff',
'molecule_atom_index_0_dist_min_diff',
'molecule_type_dist_mean_diff',
'x_1',
'molecule_atom_index_0_y_1_max',
'molecule_atom_index_0_y_1_mean_diff',
'molecule_atom_1_dist_std_diff',
'molecule_atom_index_0_y_1_mean',
'molecule_atom_1_dist_std',
'molecule_type_dist_max']
X = X[good_columns].copy()
X_test = X_test[good_columns].copy()<init_hyperparams>
train_csv = pd.read_csv('.. /input/data-of-distance-qm9-giba/fin_train.csv')
 test_csv = pd.read_csv('.. /input/data-of-distance-qm9-giba/fin_test.csv' )
params = {'num_leaves': 50,
'min_child_samples': 79,
'min_data_in_leaf': 100,
'objective': 'regression',
'max_depth': 9,
'learning_rate': 0.3,
"boosting_type": "gbdt",
"subsample_freq": 1,
"subsample": 0.9,
"bagging_seed": 11,
"metric": 'mae',
"verbosity": -1,
'reg_alpha': 0.1,
'reg_lambda': 0.3,
'colsample_bytree': 1.0
} 
<choose_model_class>
train_csv = reduce_mem_usage(train_csv,verbose = True)
 test_csv = reduce_mem_usage(test_csv,verbose = True )
X_short = pd.DataFrame({'ind': list(X.index), 'type': X['type'].values, 'oof': [0] * len(X), 'target': y.values})
 X_short_test = pd.DataFrame({'ind': list(X_test.index), 'type': X_test['type'].values, 'prediction': [0] * len(X_test)})
 for t in X['type'].unique() :
print(f'Training of type {t}')
 X_t = X.loc[X['type'] == t]
X_test_t = X_test.loc[X_test['type'] == t]
y_t = X_short.loc[X_short['type'] == t, 'target']
result_dict_lgb3 = train_model_regression(X=X_t, X_test=X_test_t, y=y_t, params=params, folds=folds, model_type='lgb', eval_metric='group_mae', plot_feature_importance=False,
verbose=500, early_stopping_rounds=200, n_estimators=n_estimators_default)
 X_short.loc[X_short['type'] == t, 'oof'] = result_dict_lgb3['oof']
X_short_test.loc[X_short_test['type'] == t, 'prediction'] = result_dict_lgb3['prediction']<save_to_csv>
train_csv.set_index('id',inplace = True)
 test_csv.set_index('id',inplace = True )
sub['scalar_coupling_constant'] = X_short_test['prediction']
sub.to_csv('submission_type.csv', index=False)
 sub.head() 
<set_options>
structures_csv = pd.read_csv(f'{DATA_PATH}/champs-scalar-coupling/structures.csv')
 structures_csv['molecule_index'] = structures_csv.molecule_name.str.replace('dsgdb9nsd_', '' ).astype('int32')
 structures_csv = structures_csv[['molecule_index', 'atom_index', 'atom', 'x', 'y', 'z']]
structures_csv['atom'] = structures_csv['atom'].replace(ATOMIC_NUMBERS ).astype('int8')
 structures_csv.head(10 )
warnings.filterwarnings("ignore")
 warnings.filterwarnings(action="ignore",category=DeprecationWarning)
 warnings.filterwarnings(action="ignore",category=FutureWarning)
 %cd /kaggle/input/champs-scalar-coupling
print(os.listdir(".")) 
<load_from_csv>
def build_type_dataframes(base, structures, coupling_type):
base = base[base['type'] == coupling_type].drop('type', axis=1 ).copy()
base = base.reset_index()
base['id'] = base['id'].astype('int32')
 structures = structures[structures['molecule_index'].isin(base['molecule_index'])]
return base, structures
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')
<feature_engineering>
def add_coordinates(base, structures, index):
df = pd.merge(base, structures, how='inner',
left_on=['molecule_index', f'atom_index_{index}'],
right_on=['molecule_index', 'atom_index'] ).drop(['atom_index'], axis=1)
 df = df.rename(columns={
'atom': f'atom_{index}',
'x': f'x_{index}',
'y': f'y_{index}',
'z': f'z_{index}'
})
 return df
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()<feature_engineering>
def add_atoms(base, atoms):
df = pd.merge(base, atoms, how='inner',
on=['molecule_index', 'atom_index_0', 'atom_index_1'])
 return df
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()<define_search_model>
def merge_all_atoms(base, structures):
df = pd.merge(base, structures, how='left',
left_on=['molecule_index'],
right_on=['molecule_index'])
 df = df[(df.atom_index_0 != df.atom_index)&(df.atom_index_1 != df.atom_index)]
return df
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<set_options>
def add_center(df):
df['x_c'] =(( df['x_1'] + df['x_0'])* np.float32(0.5))
df['y_c'] =(( df['y_1'] + df['y_0'])* np.float32(0.5))
df['z_c'] =(( df['z_1'] + df['z_0'])* np.float32(0.5))
def add_distance_to_center(df):
df['d_c'] =(( 
(df['x_c'] - df['x'])**np.float32(2)+ 
(df['y_c'] - df['y'])**np.float32(2)+ 
(df['z_c'] - df['z'])**np.float32(2)
)**np.float32(0.5))
def add_distance_between(df, suffix1, suffix2):
df[f'd_{suffix1}_{suffix2}'] =(( 
(df[f'x_{suffix1}'] - df[f'x_{suffix2}'])**np.float32(2)+ 
(df[f'y_{suffix1}'] - df[f'y_{suffix2}'])**np.float32(2)+ 
(df[f'z_{suffix1}'] - df[f'z_{suffix2}'])**np.float32(2)
)**np.float32(0.5))
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)
<save_to_csv>
def add_distances(df):
n_atoms = 1 + max([int(c.split('_')[1])for c in df.columns if c.startswith('x_')])
 for i in range(1, n_atoms):
for vi in range(min(4, i)) :
add_distance_between(df, i, vi )
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 )<import_modules>
def add_n_atoms(base, structures):
dfs = structures['molecule_index'].value_counts().rename('n_atoms' ).to_frame()
return pd.merge(base, dfs, left_on='molecule_index', right_index=True )
print(keras.__version__ )<set_options>
def build_couple_dataframe(some_csv, structures_csv, coupling_type, n_atoms=10):
base, structures = build_type_dataframes(some_csv, structures_csv, coupling_type)
 base = add_coordinates(base, structures, 0)
 base = add_coordinates(base, structures, 1)
 base = base.drop(['atom_0', 'atom_1'], axis=1)
 atoms = base.drop('id', axis=1 ).copy()
if 'scalar_coupling_constant' in some_csv:
atoms = atoms.drop(['scalar_coupling_constant'], axis=1)
 add_center(atoms)
 atoms = atoms.drop(['x_0', 'y_0', 'z_0', 'x_1', 'y_1', 'z_1'], axis=1)
 atoms = merge_all_atoms(atoms, structures)
 add_distance_to_center(atoms)
 atoms = atoms.drop(['x_c', 'y_c', 'z_c', 'atom_index'], axis=1)
 atoms.sort_values(['molecule_index', 'atom_index_0', 'atom_index_1', 'd_c'], inplace=True)
 atom_groups = atoms.groupby(['molecule_index', 'atom_index_0', 'atom_index_1'])
 atoms['num'] = atom_groups.cumcount() + 2
atoms = atoms.drop(['d_c'], axis=1)
 atoms = atoms[atoms['num'] < n_atoms]
atoms = atoms.set_index(['molecule_index', 'atom_index_0', 'atom_index_1', 'num'] ).unstack()
atoms.columns = [f'{col[0]}_{col[1]}' for col in atoms.columns]
atoms = atoms.reset_index()
for col in atoms.columns:
if col.startswith('atom_'):
atoms[col] = atoms[col].fillna(0 ).astype('int8')
 atoms['molecule_index'] = atoms['molecule_index'].astype('int32')
 full = add_atoms(base, atoms)
 add_distances(full)
 full.sort_values('id', inplace=True)
 return full
warnings.filterwarnings('ignore')
 warnings.filterwarnings('ignore' )<load_from_csv>
def take_n_atoms(df, n_atoms, four_start=4):
labels = [
'linkM0',
'linkM1', 'inv_dist0R', 'mean_molecule_atom_0_dist_xyz',
'max_molecule_atom_0_dist_xyz', 'sd_molecule_atom_0_dist_xyz',
'mean_molecule_atom_1_dist_xyz', 'max_molecule_atom_1_dist_xyz',
'sd_molecule_atom_1_dist_xyz', 'coulomb_C.x', 'coulomb_H.x',
'coulomb_N.x', 'coulomb_O.x', 'yukawa_H.x', 'coulomb_C.y',
'coulomb_H.y', 'coulomb_O.y', 'yukawa_C.y', 'yukawa_H.y', 'vander_C.y',
'distC0', 'distC1', 'adH1', 'adC2', 'adC3', 'adC4',
'molecule_index', 'mulliken_min', 'mulliken_max', 'mulliken_atom_0',
'mulliken_atom_1'
]
for i in range(2, n_atoms):
label = f'atom_{i}'
labels.append(label)
 for i in range(n_atoms):
num = min(i, 4)if i < four_start else 4
for j in range(num):
labels.append(f'd_{i}_{j}')
 if 'scalar_coupling_constant' in df:
labels.append('scalar_coupling_constant')
 return df[labels]
train = pd.read_csv(".. /input/jigsawtraintest/train_jigsaw.csv")
 test= pd.read_csv(".. /input/jigsawtraintest/test_jigsaw.csv")
 pos_df = pd.read_csv(".. /input/pos-neg-files/positive words.txt", sep="
", header=None)
 neg_df = pd.read_csv(".. /input/pos-neg-files/Negative words.txt", sep="
", header=None, encoding = "ISO-8859-1")
 pos_df.columns = ['words']
neg_df.columns = ['words']<count_missing_values>
LGB_PARAMS = {
'objective': 'regression',
'metric': 'mae',
'verbosity': -1,
'boosting_type': 'gbdt',
'learning_rate': 0.1455,
'num_leaves': 129,
'min_child_samples': 78,
'max_depth': 13,
'subsample_freq': 1,
'subsample': 0.88,
'bagging_seed': 15,
'reg_alpha': 0.10107001,
'reg_lambda': 0.300132,
'colsample_bytree': 1.0
}
for df in [train, test]:
print(df.isna().sum() )<define_variables>
submission_csv = pd.read_csv(f'{DATA_PATH}/champs-scalar-coupling/sample_submission.csv', index_col='id' )
labels = ['toxic', 'severe_toxic', 'obscene', 'threat', 'insult', 'identity_hate']<count_values>
def build_x_y_data(some_csv, coupling_type, n_atoms):
full = build_couple_dataframe(some_csv, structures_csv, coupling_type, n_atoms=n_atoms)
 df = take_n_atoms(full, n_atoms)
 df = df.fillna(0)
 print(df.columns)
 if 'scalar_coupling_constant' in df:
X_data = df.drop(['scalar_coupling_constant'], axis=1 ).values.astype('float32')
 y_data = df['scalar_coupling_constant'].values.astype('float32')
 else:
X_data = df.values.astype('float32')
 y_data = None
return X_data, y_data
for col in labels:
print(train[col].value_counts())
 print("
" )<feature_engineering>
def train_and_predict_for_one_coupling_type(coupling_type, submission, n_atoms, n_folds=5, n_splits=5, random_state=128):
print(f'*** Training Model for {coupling_type} ***')
 X_data, y_data = build_x_y_data(train_csv, coupling_type, n_atoms)
 X_test, _ = build_x_y_data(test_csv, coupling_type, n_atoms)
 y_pred = np.zeros(X_test.shape[0], dtype='float32')
 cv_score = 0
if n_folds > n_splits:
n_splits = n_folds
kfold = KFold(n_splits=n_splits, shuffle=True, random_state=random_state)
 for fold,(train_index, val_index)in enumerate(kfold.split(X_data, y_data)) :
if fold >= n_folds:
break
X_train, X_val = X_data[train_index], X_data[val_index]
y_train, y_val = y_data[train_index], y_data[val_index]
model = LGBMRegressor(**LGB_PARAMS, n_estimators=8000, n_jobs = -1)
 model.fit(X_train, y_train,
eval_set=[(X_train, y_train),(X_val, y_val)], eval_metric='mae',
verbose=1000, early_stopping_rounds=200)
 y_val_pred = model.predict(X_val)
 val_score = np.log(mean_absolute_error(y_val, y_val_pred))
print(f'{coupling_type} Fold {fold}, logMAE: {val_score}')
 cv_score += val_score / n_folds
y_pred += model.predict(X_test)/ n_folds
plt.scatter(y_val, y_val_pred)
 plt.show()
submission.loc[test_csv['type'] == coupling_type, 'scalar_coupling_constant'] = y_pred
return cv_score
stop_words = set(stopwords.words('english'))
for df in [train, test]:
df['words_length'] = df['comment_text'].apply(lambda x: len(x)) 
<groupby>
model_params = {
'1JHN': 7,
'1JHC': 10,
'2JHH': 9,
'2JHN': 9,
'2JHC': 9,
'3JHH': 9,
'3JHC': 10,
'3JHN': 10
}
N_FOLDS = 5
submission = submission_csv.copy()
cv_scores = {}
for coupling_type in model_params.keys() :
cv_score = train_and_predict_for_one_coupling_type(
 coupling_type, submission, n_atoms=model_params[coupling_type], n_folds=N_FOLDS)
 cv_scores[coupling_type] = cv_score
for col in labels:
print(train.groupby([col])['words_length'].mean())
 print("
" )<define_variables>
pd.DataFrame({'type': list(cv_scores.keys()), 'cv_score': list(cv_scores.values())} )
contractions = {
"ain't": "am not ",
"aren't": "are not",
"can't": "cannot",
"can't've": "cannot have",
"'cause": "because",
"could've": "could have",
"couldn't": "could not",
"couldn't've": "could not have",
"didn't": "did not",
"doesn't": "does not",
"don't": "do not",
"hadn't": "had not",
"hadn't've": "had not have",
"hasn't": "has not",
"haven't": "have not",
"he'd": "he would",
"he'd've": "he would have",
"he'll": "he will",
"he'll've": "he shall have",
"he's": "he is",
"how'd": "how did",
"how'd'y": "how do you",
"how'll": "how will",
"how's": "how is",
"i'd": "I would",
"i'd've": "I would have",
"i'll": "I will",
"i'll've": "I shall have",
"i'm": "I am",
"i've": "I have",
"isn't": "is not",
"it'd": "it would",
"it'd've": "it would have",
"it'll": "it will",
"it'll've": "it will have",
"it's": "it is",
"let's": "let us",
"ma'am": "madam",
"mayn't": "may not",
"might've": "might have",
"mightn't": "might not",
"mightn't've": "might not have",
"must've": "must have",
"mustn't": "must not",
"mustn't've": "must not have",
"needn't": "need not",
"needn't've": "need not have",
"o'clock": "of the clock",
"oughtn't": "ought not",
"oughtn't've": "ought not have",
"shan't": "shall not",
"sha'n't": "shall not",
"shan't've": "shall not have",
"she'd": "she would",
"she'd've": "she would have",
"she'll": "she will",
"she'll've": "she will have",
"she's": "she is",
"should've": "should have",
"shouldn't": "should not",
"shouldn't've": "should not have",
"so've": "so have",
"so's": "so is",
"that'd": "that would",
"that'd've": "that would have",
"that's": "that is",
"there'd": "there would",
"there'd've": "there would have",
"there's": "there is",
"they'd": "they would",
"they'd've": "they would have",
"they'll": "they will",
"they'll've": "they will have",
"they're": "they are",
"they've": "they have",
"to've": "to have",
"wasn't": "was not",
"we'd": "we would",
"we'd've": "we would have",
"we'll": "we will",
"we'll've": "we will have",
"we're": "we are",
"we've": "we have",
"weren't": "were not",
"what'll": "what will",
"what'll've": "what will have",
"what're": "what are",
"what's": "what is",
"what've": "what have",
"when's": "when is",
"when've": "when have",
"where'd": "where did",
"where's": "where is",
"where've": "where have",
"who'll": "who will",
"who'll've": "who will have",
"who's": "who is",
"who've": "who have",
"why's": "why is",
"why've": "why have",
"will've": "will have",
"won't": "will not",
"won't've": "will not have",
"would've": "would have",
"wouldn't": "would not",
"wouldn't've": "would not have",
"y'all": "you all",
"y'all'd": "you all would",
"y'all'd've": "you all would have",
"y'all're": "you all are",
"y'all've": "you all have",
"you'd": "you would",
"you'd've": "you would have",
"you'll": "you will",
"you'll've": "you will have",
"you're": "you are",
"you've": "you have"
}<string_transform>
submission.to_csv(f'{SUBMISSIONS_PATH}/submission.csv' )