Kaggle competition: Predicting Molecular Properties
<SOS> metric: GroupMeanLogMAE Kaggle data source: predicting-molecular-properties<create_dataframe>
%matplotlib inline
original_same = train_df.pivot_table(values=['file'], columns=['label'], index=['original'], fill_value=0, aggfunc='count')
 original_same = original_same[(original_same[('file', 'FAKE')] != 0)&(original_same[('file', 'REAL')] != 0)]
print(f'Number of file having both FAKE and REAL: {len(original_same)}')
 original_same<feature_engineering>
DATA_PATH = '.. /input'
SUBMISSIONS_PATH = './'
ATOMIC_NUMBERS = {
'H': 1,
'C': 6,
'N': 7,
'O': 8,
'F': 9
}
train_df['label'] = train_df['label'].apply(lambda x: 1 if x=='FAKE' else 0 )<categorify>
pd.set_option('display.max_colwidth', -1)
 pd.set_option('display.max_rows', 120)
 pd.set_option('display.max_columns', 120 )
def transfer(label):
if label==0:
return "real"
else:
return "fake"
def display_mtcnn(number_frame=3, number_video=2):
fake_real = original_same[(original_same[('file', 'FAKE')] == 1)&(original_same[('file', 'REAL')] == 1)].index.tolist()
original_images = random.sample(fake_real, number_video)
 for original_image in original_images:
real_video = train_df[(train_df['label']==0)&(train_df['original']==original_image)]['file'].values[0]
fake_video = train_df[(train_df['label']==1)&(train_df['original']==original_image)]['file'].values[0]
if(real_video in os.listdir(train_video_path)) and(fake_video in os.listdir(train_video_path)) :
real_path = os.path.join(train_video_path, real_video)
 fake_path = os.path.join(train_video_path, fake_video)
 fig, axes = plt.subplots(number_frame, 2, figsize=(20, 20))
for ind, path in enumerate([real_path, fake_path]):
cap = cv2.VideoCapture(path)
 frame_index = 0
ax_ix = 0
while True:
ret, frame = cap.read()
if cv2.waitKey(1)& 0xFF == 27:
break
if ret:
if frame_index%24==0:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
 frame = Image.fromarray(frame)
 boxes, scores = box_mtcnn(frame, False)
 box = boxes[scores.argmax() ]
frame_crop = frame.crop(box)
 boxes, scores, landmarks = box_mtcnn(frame_crop)
 if landmarks is not None:
landmark = landmarks[scores.argmax() ]
axes[ax_ix, ind].scatter(landmark[:, 0], landmark[:, 1], c='red', s=8)
 axes[ax_ix, ind].imshow(frame_crop)
 axes[ax_ix, ind].xaxis.set_visible(False)
 axes[ax_ix, ind].yaxis.set_visible(False)
 axes[ax_ix, ind].set_title(f'Frame: {frame_index}_{transfer(ind)}')
 fig.tight_layout()
ax_ix += 1
if ax_ix == number_frame:
break
else:
break
frame_index+=1
fig.suptitle(original_image, color='b', size=20, y=1)
 display_mtcnn(number_frame=3, number_video=3 )<define_variables>
train_dtypes = {
'molecule_name': 'category',
'atom_index_0': 'int8',
'atom_index_1': 'int8',
'type': 'category',
'scalar_coupling_constant': 'float32'
}
train_csv = pd.read_csv(f'{DATA_PATH}/train.csv', index_col='id', dtype=train_dtypes)
 train_csv['molecule_index'] = train_csv.molecule_name.str.replace('dsgdb9nsd_', '' ).astype('int32')
 train_csv = train_csv[['molecule_index', 'atom_index_0', 'atom_index_1', 'type', 'scalar_coupling_constant']]
train_csv.head(10 )
class VideoDataset(Dataset):
def __init__(self, df, path_video, num_frame=5, is_train=True):
super(VideoDataset, self ).__init__()
self.df = df
self.num_frame = num_frame
self.is_train = is_train
self.path_video = path_video
index_list = deque()
for index in tqdm(range(len(self.df))):
video_name = self.df.loc[index, 'file']
video_path = os.path.join(self.path_video, video_name)
 if self.landmark_mtcnn(video_path)is not None:
index_list.append(index)
 index_list = list(index_list)
 self.df = self.df[self.df.index.isin(index_list)]
self.df.reset_index(inplace=True, drop=True)
 def __len__(self):
return len(self.df)
 def __getitem__(self, idx):
video_name = self.df.loc[idx, 'file']
video_path = os.path.join(self.path_video, video_name)
 list_landmark = self.landmark_mtcnn(video_path)
 if self.is_train:
label = self.df.loc[idx, 'label']
return torch.from_numpy(list_landmark), torch.tensor(label, dtype=torch.float)
 else:
return video_name, torch.from_numpy(list_landmark)
 def landmark_mtcnn(self, video_path):
cap = cv2.VideoCapture(video_path)
 frame_index = 0
list_landmark = deque()
while len(list_landmark)< 10*self.num_frame:
ret, frame = cap.read()
if cv2.waitKey(1)& 0xFF == 27:
break
if ret:
if frame_index % 24 == 0:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
 frame = Image.fromarray(frame)
 boxes, scores, landmarks = box_mtcnn(frame)
 if scores[0]:
index_max = np.argmax(scores)
 landmark = landmarks[index_max]
list_landmark.extend(landmark.flatten())
 else:
break
frame_index+=1
list_landmark = list(list_landmark)
 if len(list_landmark)== 10*self.num_frame:
list_landmark = np.array(list_landmark ).reshape(self.num_frame, 10)
 return list_landmark
return None
dataset = VideoDataset(train_df, train_video_path )<load_pretrained>
submission_csv = pd.read_csv(f'{DATA_PATH}/sample_submission.csv', index_col='id' )
test_size = 0.2
index_split = int(len(dataset)*test_size)
 list_index =(list(range(len(dataset))))
random.shuffle(list_index)
 train_idx = list_index[index_split:]
val_idx = list_index[:index_split]
train_dataset = Subset(dataset, train_idx)
 val_dataset = Subset(dataset, val_idx)
 train_ld = DataLoader(train_dataset, batch_size=8, shuffle=True)
 val_ld = DataLoader(val_dataset, batch_size=8, shuffle=True )<normalization>
test_csv = pd.read_csv(f'{DATA_PATH}/test.csv', index_col='id', dtype=train_dtypes)
 test_csv['molecule_index'] = test_csv['molecule_name'].str.replace('dsgdb9nsd_', '' ).astype('int32')
 test_csv = test_csv[['molecule_index', 'atom_index_0', 'atom_index_1', 'type']]
test_csv.head(10 )
class swish(Module):
def __init__(self):
super(swish, self ).__init__()
self.sigmoid = nn.Sigmoid()
def forward(self, x):
return x*self.sigmoid(x)
 class small_model(Module):
def __init__(self, num_class=1):
super(small_model, self ).__init__()
self.conv = nn.Sequential(nn.Conv2d(1, 32, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.Conv2d(64, 64, kernel_size=1, stride=1, padding=0),
nn.BatchNorm2d(64),
nn.Dropout(0.2))
self.fc = nn.Sequential(nn.Linear(64*10*5, 128),
nn.ReLU(inplace=True),
nn.BatchNorm1d(128),
nn.Dropout(0.2),
nn.Linear(128, num_class),
nn.Sigmoid())
 def forward(self, x):
x = self.conv(x)
 x = torch.flatten(x, 1)
 x = self.fc(x)
 return x
model = small_model().to(device)
 model.eval()<choose_model_class>
structures_dtypes = {
'molecule_name': 'category',
'atom_index': 'int8',
'atom': 'category',
'x': 'float32',
'y': 'float32',
'z': 'float32'
}
structures_csv = pd.read_csv(f'{DATA_PATH}/structures.csv', dtype=structures_dtypes)
 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 )
class Trainer(object):
def __init__(self, model):
self.model = model
self.creation = nn.MSELoss()
self.optimizer = optim.AdamW([
{'params': model.conv.parameters() , 'lr': 1e-4},
{'params': model.fc.parameters() , 'lr': 1e-3}], lr=0.001)
 self.scheduler = ReduceLROnPlateau(self.optimizer, mode='min', factor=0.15)
 def train_process(self, train_ld, val_ld, epochs):
score_max = 0
check_step = 0
loss_min = 1
check_number = 13
self.model.train()
for epoch in range(epochs):
train_loss, val_loss = 0, 0
for crop, label in tqdm(train_ld):
crop = crop.unsqueeze(1)
 crop, label = crop.float().to(device), label.to(device)
 self.optimizer.zero_grad()
output = self.model(crop ).squeeze(1)
 loss = self.creation(output, label)
 loss.backward()
self.optimizer.step()
self.scheduler.step(train_loss)
 train_loss += loss.item()
del crop, label
train_loss = train_loss/len(train_ld)
 torch.cuda.empty_cache()
gc.collect()
self.model.eval()
val_score = 0
with torch.no_grad() :
for crop, label in tqdm(val_ld):
crop = crop.unsqueeze(1)
 crop, label = crop.float().to(device), label.to(device)
 output = self.model(crop ).squeeze(1)
 loss = self.creation(output, label)
 val_loss += loss.item()
val_score += torch.sum(( output>0.5 ).float() == label ).item() /len(label)
 val_loss = val_loss/len(val_ld)
 val_score = val_score/len(val_ld)
 self.scheduler.step(val_loss)
 if val_score > score_max:
print(f'Epoch: {epoch}, train loss: {train_loss:.5f}, val_loss: {val_loss:.5f}.
 Validation score increased from {score_max:.5f} to {val_score:.5f}')
 score_max = val_score
loss_min = val_loss
torch.save(self.model.state_dict() , 'model.pth')
 print('Saving model!')
 check_step = 0
elif val_score == score_max:
if val_loss < loss_min:
print(f'Epoch: {epoch}, train loss: {train_loss:.5f}, val_loss: {val_loss:.5f}, val_score: {val_score:.5f}.
 Validation loss decreased from {loss_min:.5f} to {val_loss:.5f}')
 loss_min = val_loss
torch.save(self.model.state_dict() , 'model.pth')
 print('Saving model!')
 check_step = 0
else:
check_step += 1
print(f'Epoch: {epoch}, train loss: {train_loss:.5f}, val_loss: {val_loss:.5f}, val_score: {val_score:.5f}.
 Model not improve in {str(check_step)} step')
 if check_step > check_number:
print('Stop trainning!')
 break
else:
check_step += 1
print(f'Epoch: {epoch}, train loss: {train_loss:.5f}, val_loss: {val_loss:.5f}.
 Validation score not increased from {val_score:.5f} in {str(check_step)} step')
 if check_step > check_number:
print('Stop trainning!')
 break
trainer = Trainer(model)
 trainer.train_process(train_ld=train_ld, val_ld=val_ld, epochs=20 )<set_options>
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
%matplotlib inline 
<create_dataframe>
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
frames_per_vid = [17, 25, 30, 32, 35, 36, 40]
public_LB = [0.46788, 0.46776, 0.46611, 0.46542, 0.46643, 0.46484, 0.46635]
df_viz = pd.DataFrame({'frames_per_vid': frames_per_vid, 'public_LB':public_LB} )<define_variables>
def add_atoms(base, atoms):
df = pd.merge(base, atoms, how='inner',
on=['molecule_index', 'atom_index_0', 'atom_index_1'])
 return df
test_dir = "/kaggle/input/deepfake-detection-challenge/test_videos/"
test_videos = sorted([x for x in os.listdir(test_dir)if x[-4:] == ".mp4"])
 frame_h = 5
frame_l = 5
len(test_videos )<import_modules>
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
print("PyTorch version:", torch.__version__)
 print("CUDA version:", torch.version.cuda)
 print("cuDNN version:", torch.backends.cudnn.version() )<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))
gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 gpu<load_pretrained>
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 )
facedet = BlazeFace().to(gpu)
 facedet.load_weights("/kaggle/input/blazeface-pytorch/blazeface.pth")
 facedet.load_anchors("/kaggle/input/blazeface-pytorch/anchors.npy")
 _ = facedet.train(False )<load_pretrained>
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 )
frames_per_video = 38
video_reader = VideoReader()
video_read_fn = lambda x: video_reader.read_frames(x, num_frames=frames_per_video)
 face_extractor = FaceExtractor(video_read_fn, facedet )<define_variables>
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
input_size = 224<normalization>
def take_n_atoms(df, n_atoms, four_start=4):
labels = []
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]
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize_transform = Normalize(mean, std )<choose_model_class>
%%time
def type_select(types = '1JHN'):
full = build_couple_dataframe(train_csv, structures_csv, types, n_atoms=10)
 print(full.shape)
 df = take_n_atoms(full, 7)
 df = df.fillna(0)
 X_data = df.drop(['scalar_coupling_constant'], axis=1 ).values.astype('float32')
 y_data = df['scalar_coupling_constant'].values.astype('float32')
 X_train, X_val, y_train, y_val = train_test_split(X_data, y_data, test_size=0.2, random_state=128)
 print(X_train.shape, X_val.shape, y_train.shape, y_val.shape)
 return X_train, X_val, y_train, y_val
class MyResNeXt(models.resnet.ResNet):
def __init__(self, training=True):
super(MyResNeXt, self ).__init__(block=models.resnet.Bottleneck,
layers=[3, 4, 6, 3],
groups=32,
width_per_group=4)
 self.fc = nn.Linear(2048, 1 )<load_pretrained>
model_params = {
'1JHN': 7,
'1JHC': 10,
'2JHH': 9,
'2JHN': 9,
'2JHC': 9,
'3JHH': 9,
'3JHC': 10,
'3JHN': 10
}
model_params.keys()
checkpoint = torch.load("/kaggle/input/deepfakes-inference-demo/resnext.pth", map_location=gpu)
 model = MyResNeXt().to(gpu)
 model.load_state_dict(checkpoint)
 _ = model.eval()
del checkpoint<predict_on_test>
X_train, X_val, y_train, y_val = type_select(types = '3JHN' )
def predict_on_video(video_path, batch_size):
try:
faces = face_extractor.process_video(video_path)
 face_extractor.keep_only_best_face(faces)
 if len(faces)> 0:
x = np.zeros(( batch_size, input_size, input_size, 3), dtype=np.uint8)
 n = 0
for frame_data in faces:
for face in frame_data["faces"]:
resized_face = isotropically_resize_image(face, input_size)
 resized_face = make_square_image(resized_face)
 if n < batch_size:
x[n] = resized_face
n += 1
else:
print("WARNING: have %d faces but batch size is %d" %(n, batch_size))
if n > 0:
x = torch.tensor(x, device=gpu ).float()
x = x.permute(( 0, 3, 1, 2))
for i in range(len(x)) :
x[i] = normalize_transform(x[i] / 255.)
with torch.no_grad() :
y_pred = model(x)
 y_pred = torch.sigmoid(y_pred.squeeze())
 return y_pred[:n].mean().item()
except Exception as e:
print("Prediction error on video %s: %s" %(video_path, str(e)))
 return 0.5<predict_on_test>
%%time
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
}
LGB_PARAMS={'bagging_seed': 14, 'colsample_bytree': 1.0, 'learning_rate': 0.14848931924611134, 'max_depth': -1, 'min_child_samples': 80, 'categorical_feature':categorical_feature, 'num_leaves': 129, 'random_state': 42, 'reg_alpha': 0.1, 'reg_lambda': 0.3, 'subsample': 0.89, 'subsample_freq': 1, 'verbosity': -1}
model = LGBMRegressor(**LGB_PARAMS, n_estimators=3000, n_jobs = -1)
 model.fit(X_train, y_train,
eval_set=[(X_train, y_train),(X_val, y_val)], eval_metric='mae',
verbose=400, early_stopping_rounds=500)
 y_pred = model.predict(X_val)
 np.log(mean_absolute_error(y_val, y_pred))
def predict_on_video_set(videos, num_workers):
def process_file(i):
filename = videos[i]
y_pred = predict_on_video(os.path.join(test_dir, filename), batch_size=frames_per_video)
 return y_pred
with ThreadPoolExecutor(max_workers=num_workers)as ex:
predictions = ex.map(process_file, range(len(videos)))
 return list(predictions )<define_variables>
categorical_feature=[0,1,2,3,4]
speed_test = False<predict_on_test>
LGB_PARAMS_3JHN={'bagging_seed': 14, 'colsample_bytree': 1.0, 'learning_rate': 0.14548931924611134, 'max_depth': 14, 'min_child_samples': 80, 'num_leaves': 129, 'random_state': 42, 'reg_alpha': 0.1, 'reg_lambda': 0.3, 'subsample': 0.89, 'subsample_freq': 1, 'verbosity': -1}
if speed_test:
start_time = time.time()
speedtest_videos = test_videos[:5]
predictions = predict_on_video_set(speedtest_videos, num_workers=4)
 elapsed = time.time() - start_time
print("Elapsed %f sec.Average per video: %f sec." %(elapsed, elapsed / len(speedtest_videos)) )<predict_on_test>
predictions = predict_on_video_set(test_videos, num_workers=4 )<save_to_csv>
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
submission_df = pd.DataFrame({"filename": test_videos, "label": predictions})
 submission_df.to_csv("submission.csv", index=False )<import_modules>
def train_and_predict_for_one_coupling_type(coupling_type, submission, n_atoms, n_folds=4, n_splits=4, 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)
 print('fitting {} with {} LGB_PARAMS'.format(coupling_type,LGB_PARAMS))
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= 9000, 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=500)
 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
import numpy as np
import pandas as pd
import os 
<load_from_csv>
model_params = {
'1JHN': 7,
'1JHC': 10,
'2JHH': 9,
'2JHN': 9,
'2JHC': 9,
'3JHH': 9,
'3JHC': 10,
'3JHN': 10
}
cat_code = {'1JHN': 5,'1JHC': 7,'2JHH': 7,
'2JHN': 7,'2JHC': 7,'3JHH': 7,
'3JHC': 8,'3JHN': 7}
categorical_feature = [0,1,2,3,4,5,6]
LGB_PARAMS_2JHC={'bagging_seed': 14, 'colsample_bytree': 1.0, 'learning_rate': 0.14548931824400012, 'max_depth': -1, 'min_child_samples': 81,'categorical_feature':categorical_feature, 'num_leaves': 129, 'random_state': 42, 'reg_alpha': 0.1, 'reg_lambda': 0.30, 'subsample': 0.89, 'subsample_freq': 1, 'verbosity': -1}
LGB_PARAMS_3JHN={'bagging_seed': 14, 'colsample_bytree': 1.0, 'learning_rate': 0.1454893092, 'max_depth': -1, 'min_child_samples': 81, 'num_leaves': 129, 'random_state': 42, 'reg_alpha': 0.1, 'reg_lambda': 0.3, 'subsample': 0.89, 'subsample_freq': 1, 'verbosity': -1}
LGB_PARAMS_2JHN={'bagging_seed': 14, 'colsample_bytree': 1.0, 'learning_rate': 0.1454893072, 'max_depth': -1, 'min_child_samples': 82, 'num_leaves': 130, 'random_state': 42, 'reg_alpha': 0.1, 'reg_lambda': 0.3, 'subsample': 0.89, 'subsample_freq': 1, 'verbosity': -1}
N_FOLDS = 5
submission = submission_csv.copy()
cv_scores = {}
for coupling_type in model_params.keys() :
if(coupling_type == '2JHC'):
categorical_feature = [0,1,2,3,4,5,6]
LGB_PARAMS = {'bagging_seed': 14, 'colsample_bytree': 1.0, 'learning_rate': 0.14548931824400012, 'max_depth': -1, 'min_child_samples': 81,'categorical_feature':categorical_feature, 'num_leaves': 129, 'random_state': 42, 'reg_alpha': 0.1, 'reg_lambda': 0.30, 'subsample': 0.89, 'subsample_freq': 1, 'verbosity': -1}
elif(coupling_type == '3JHN'):
categorical_feature = [0,1,2,3,4,5,6]
LGB_PARAMS = {'bagging_seed': 14, 'colsample_bytree': 1.0, 'learning_rate': 0.1454893092, 'max_depth': -1, 'min_child_samples': 81, 'num_leaves': 129, 'random_state': 42, 'reg_alpha': 0.1, 'reg_lambda': 0.3, 'subsample': 0.89, 'subsample_freq': 1, 'verbosity': -1}
elif(coupling_type == '2JHN'):
categorical_feature = [0,1,2,3,4,5,6]
LGB_PARAMS = {'bagging_seed': 14, 'colsample_bytree': 1.0, 'learning_rate': 0.1454893072, 'max_depth': -1, 'min_child_samples': 82, 'num_leaves': 130, 'random_state': 42, 'reg_alpha': 0.1, 'reg_lambda': 0.3, 'subsample': 0.89, 'subsample_freq': 1, 'verbosity': -1}
elif(coupling_type == '3JHC'):
categorical_feature = [0,1,2,3,4,5,6,7]
LGB_PARAMS={'bagging_seed': 14, 'colsample_bytree': 1.0, 'learning_rate': 0.14548931824400012, 'max_depth': -1, 'min_child_samples': 81,'categorical_feature':categorical_feature, 'num_leaves': 129, 'random_state': 42, 'reg_alpha': 0.1, 'reg_lambda': 0.30, 'subsample': 0.89, 'subsample_freq': 1, 'verbosity': -1}
elif(coupling_type == '1JHN'):
categorical_feature = [0,1,2,3,4]
LGB_PARAMS={'bagging_seed': 14, 'colsample_bytree': 1.0, 'learning_rate': 0.1454893092, 'max_depth': -1, 'min_child_samples': 81,'categorical_feature':categorical_feature, 'num_leaves': 129, 'random_state': 42, 'reg_alpha': 0.1, 'reg_lambda': 0.30, 'subsample': 0.89, 'subsample_freq': 1, 'verbosity': -1}
else:
LGB_PARAMS = LGB_PARAMS_2JHN
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
<load_from_csv><EOS> 
submission.to_csv(f'{SUBMISSIONS_PATH}/submission.csv' )