Kaggle competition: Mechanisms of Action (MoA) Prediction
df = train
df = df[df['type'] == 'train']<create_dataframe>
warnings.filterwarnings('ignore' )
plt_data = pd.DataFrame(index=column_types[np.dtype('bool')])
 plt_data['True per Task'] = [len(df[df[column]])for column in column_types[np.dtype('bool')]]
plt_data['True group by Tasks'] = [len(df.groupby('task_id')[column].min() [df.groupby('task_id')[column].min() ])for column in column_types[np.dtype('bool')]]
plt_data<feature_engineering>
from sklearn.preprocessing import QuantileTransformer
test_idx = test[test['type']=='test'].index
test.loc[test_idx, 'output'] = test.loc[test_idx, 'submission_id'].apply(lambda x: validation[validation['submission_id'] == x]['output'].values[0] if x in validation['submission_id'].values else None )<prepare_output>
train_features = pd.read_csv('.. /input/lish-moa/train_features.csv')
 train_targets_scored = pd.read_csv('.. /input/lish-moa/train_targets_scored.csv')
 train_targets_nonscored = pd.read_csv('.. /input/lish-moa/train_targets_nonscored.csv')
 test_features = pd.read_csv('.. /input/lish-moa/test_features.csv')
 sample_submission = pd.read_csv('.. /input/lish-moa/sample_submission.csv' )
def get_prediction_column(df, idx):

 next_prediction = 'prediction_3'
for idx, val in df.loc[idx, ['prediction_1', 'prediction_2', 'prediction_3']].iteritems() :
if np.array_equal(val, DEFAULT_PREDICTION)or val is None:
next_prediction = idx
break
return next_prediction
for df in [train, validation, test]:
df['prediction_1'] = [DEFAULT_PREDICTION] * df.shape[0]
df['prediction_2'] = [DEFAULT_PREDICTION] * df.shape[0]
df['prediction_3'] = [DEFAULT_PREDICTION] * df.shape[0]<compute_test_metric>
GENES = [col for col in train_features.columns if col.startswith('g-')]
CELLS = [col for col in train_features.columns if col.startswith('c-')]
def check_startpoint(frame_in, frame_out, startpoint):

 if startpoint == 'top_left':
return np.array_equal(frame_in, frame_out[:frame_in.shape[0],:frame_in.shape[1]])
 elif startpoint == 'top_right':
return np.array_equal(frame_in, frame_out[:frame_in.shape[0],-frame_in.shape[1]:])
 elif startpoint == 'bottom_left':
return np.array_equal(frame_in, frame_out[-frame_in.shape[0]:,:frame_in.shape[1]])
 elif startpoint == 'bottom_right':
return np.array_equal(frame_in, frame_out[-frame_in.shape[0]:,-frame_in.shape[1]:])
 else:
return False
def rotations(frame, horizontal, vertical, start, hmethod='repeat', vmethod='repeat'):

 result = frame
tmp = frame
for f in range(int(horizontal)) :
if hmethod == 'repeat':
tmp = tmp
elif hmethod == 'flip':
tmp = np.flip(tmp)
 elif hmethod == 'fliplr':
tmp = np.fliplr(tmp)
 elif hmethod == 'flipud':
tmp = np.flipud(tmp)
 elif hmethod == 'rot90_3' and frame.shape[0] == frame.shape[1]:
tmp = np.rot90(tmp, 3)
 elif hmethod == 'rot90_1' and frame.shape[0] == frame.shape[1]:
tmp = np.rot90(tmp, 1)
 else:
tmp = tmp
if 'left' in start:
result = np.append(result, tmp, axis=1)
 elif 'right' in start:
result = np.append(tmp, result, axis=1)
 tmp = result
for f in range(int(vertical)) :
if vmethod == 'repeat':
tmp = tmp
elif vmethod == 'flip':
tmp = np.flip(tmp)
 elif vmethod == 'flipud':
tmp = np.flipud(tmp)
 elif vmethod == 'fliplr':
tmp = np.fliplr(tmp)
 elif vmethod == 'rot90_2':
tmp = np.rot90(tmp, 2)
 elif vmethod == 'test' and frame.shape[0] == frame.shape[1]:
tmp = np.rot90(frame, 2)
 tmp = np.append(tmp, np.rot90(frame, 3), axis=1)
 else:
tmp = tmp
if 'top' in start:
result = np.append(result, tmp, axis=0)
 elif 'bottom' in start:
result = np.append(tmp, result, axis=0)
 return result<feature_engineering>
for col in(GENES + CELLS):
transformer = QuantileTransformer(n_quantiles=100,random_state=0, output_distribution="normal")
 vec_len = len(train_features[col].values)
 vec_len_test = len(test_features[col].values)
 raw_vec = train_features[col].values.reshape(vec_len, 1)
 transformer.fit(raw_vec)
 train_features[col] = transformer.transform(raw_vec ).reshape(1, vec_len)[0]
test_features[col] = transformer.transform(test_features[col].values.reshape(vec_len_test, 1)).reshape(1, vec_len_test)[0]
if True:
for df in [train, validation, test]:
tasks = df[(df['type'] == 'test')&(df['input_in_output'])&(np.floor(df['increased_height'])== np.ceil(df['increased_height'])) &(np.floor(df['increased_width'])== np.ceil(df['increased_width'])) ]['task_id'].unique()
df_test = df[(df['type'] == 'test')&(df['task_id'].isin(tasks)) ].copy()
for task in tasks:
df_temp = df[(df['type'] != 'test')&(df['task_id'] == task)].copy()
df_temp['top_left'] = df_temp.apply(lambda x: check_startpoint(x['input'], x['output'], 'top_left'), axis=1)
 df_temp['top_right'] = df_temp.apply(lambda x: check_startpoint(x['input'], x['output'], 'top_right'), axis=1)
 df_temp['bottom_left'] = df_temp.apply(lambda x: check_startpoint(x['input'], x['output'], 'bottom_left'), axis=1)
 df_temp['bottom_right'] = df_temp.apply(lambda x: check_startpoint(x['input'], x['output'], 'bottom_right'), axis=1)
 starts = df_temp[['top_left', 'top_right', 'bottom_left', 'bottom_right']].min()
starts = list(starts[starts].index)
 if len(starts)== 0:
df_test = df_test[df_test['task_id'] != task]
continue
found = None
for start in starts:
for hmethod in ['repeat', 'flip', 'fliplr', 'flipud', 'rot90_3', 'rot90_1']:
for vmethod in ['repeat', 'flip', 'fliplr', 'flipud', 'rot90_2', 'test']:
df_temp[f'pred_{start}'] = df_temp.apply(lambda x: rotations(x['input'], x['increased_width'], x['increased_height'], start, hmethod, vmethod), axis=1)
 df_temp[f'pred_{start}'] = df_temp.apply(lambda x: np.array_equal(x[f'pred_{start}'], x['output']), axis=1)
 if df_temp[f'pred_{start}'].min() :
found = True
break
if found:
break
if found:
for idx, row in df_test[df_test['task_id'] == task].iterrows() :
prediction = rotations(row['input'], row['increased_width'], row['increased_height'], start, hmethod, vmethod)
 df_test.loc[idx, get_prediction_column(df_test, idx)] = [prediction]
break
print(f'
DataFrame: {DF_NAME[id(df)]}')
 score = calculate_score(df_test['output'].values, df_test['prediction_1'].values, df_test['prediction_2'].values, df_test['prediction_3'].values)
 df.loc[df_test.index, ['prediction_1', 'prediction_2', 'prediction_3']] = df_test[['prediction_1', 'prediction_2', 'prediction_3']]<split>
def seed_everything(seed=1903):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
seed_everything(seed=1903 )
def split_frame(frame, split='vertical'):

 if split=='vertical':
mid = int(np.floor(frame.shape[1]/2))
frame1 = frame[:,:mid]
frame2 = frame[:,mid+1:]
else:
mid = int(np.floor(frame.shape[0]/2))
frame1 = frame[:mid,:]
frame2 = frame[mid+1:,:]
return frame1, frame2
def splitting(frame, split='vertical', aggmethod='min', flip_frame1=None, flip_frame2=None, color=None):

 frame1, frame2 = split_frame(frame, split)
 if flip_frame1 == 'flip':
frame1 = np.flip(frame1)
 elif flip_frame1 == 'fliplr':
frame1 = np.fliplr(frame1)
 elif flip_frame1 == 'flipud':
frame1 = np.flipud(frame1)
 if flip_frame2 == 'flip':
frame2 = np.flip(frame2)
 elif flip_frame2 == 'fliplr':
frame2 = np.fliplr(frame2)
 elif flip_frame2 == 'flipud':
frame2 = np.flipud(frame2)
 if aggmethod == 'min':
result = np.min(np.dstack(( frame1, frame2)) , axis=2)
 elif aggmethod == 'max':
result = np.max(np.dstack(( frame1, frame2)) , axis=2)
 elif aggmethod == 'none_match':
result = frame1 + frame2
result = np.where(result > np.max(np.append(frame1, frame2)) , 0, result)
 elif aggmethod == 'merge_2_in_1':
result = frame1 + frame2
if np.max(result)> np.max(np.append(frame1, frame2)) :
result = frame1
elif aggmethod == 'merge_1_in_2':
result = frame1 + frame2
if np.max(result)> np.max(np.append(frame1, frame2)) :
result = frame2
elif aggmethod == 'blanks':
result = np.max(np.dstack(( frame1, frame2)) , axis=2)
 result = np.where(result == 0, 1, 0)
 if color:
result[result > 0] = color
return result<categorify>
n_comp = 600
data = pd.concat([pd.DataFrame(train_features[GENES]), pd.DataFrame(test_features[GENES])])
 data_PCA = PCA(n_components=n_comp, random_state=42 ).fit(data[GENES][:train_features.shape[0]])
 data2 =(data_PCA.transform(data[GENES]))
train2 = data2[:train_features.shape[0]]; test2 = data2[-test_features.shape[0]:]
train2 = pd.DataFrame(train2, columns=[f'pca_G-{i}' for i in range(n_comp)])
 test2 = pd.DataFrame(test2, columns=[f'pca_G-{i}' for i in range(n_comp)])
 train_features = pd.concat(( train_features, train2), axis=1)
 test_features = pd.concat(( test_features, test2), axis=1 )
def fill_out(frame, method, mask_color=0):

 mask = np.where(frame == mask_color)
 if method == 'output_left_diagonal_symmetric':
transpose = frame.T
elif method == 'output_right_diagonal_symmetric':
transpose = np.flip(frame ).T
elif method == 'output_horizontal_symmetric':
transpose = np.flipud(frame)
 elif method == 'output_vertical_symmetric':
transpose = np.fliplr(frame)
 for i in range(len(mask[0])) :
frame[mask[0][i], mask[1][i]] = transpose[mask[0][i], mask[1][i]]
return frame<feature_engineering>
n_comp = 50
data = pd.concat([pd.DataFrame(train_features[CELLS]), pd.DataFrame(test_features[CELLS])])
 data_PCA =(PCA(n_components=n_comp, random_state=42 ).fit(data[CELLS][:train_features.shape[0]]))
data2 =(data_PCA.transform(data[CELLS]))
train2 = data2[:train_features.shape[0]]; test2 = data2[-test_features.shape[0]:]
train2 = pd.DataFrame(train2, columns=[f'pca_C-{i}' for i in range(n_comp)])
 test2 = pd.DataFrame(test2, columns=[f'pca_C-{i}' for i in range(n_comp)])
 train_features = pd.concat(( train_features, train2), axis=1)
 test_features = pd.concat(( test_features, test2), axis=1 )
if True:
for df in [train, validation, test]:
tasks = df[df['type'] == 'test'][df['fill_out_mask_color'] >= 0][(df['output_left_diagonal_symmetric'])|(df['output_right_diagonal_symmetric'])|(df['output_horizontal_symmetric'])|(df['output_vertical_symmetric'])]['task_id'].unique()
df_test = df[(df['type'] == 'test')&(df['task_id'].isin(tasks)) ].copy()
for task in tasks:
found = None
df_temp = df[(df['type'] != 'test')&(df['task_id'] == task)].copy()
df_temp['pred_output'] = df_temp['input']
methods = df_temp[['output_left_diagonal_symmetric', 'output_right_diagonal_symmetric', 'output_horizontal_symmetric', 'output_vertical_symmetric']].min()
methods = list(methods[methods].index)
 for method in methods*2:
df_temp['pred_output'] = df_temp.apply(lambda x: fill_out(x['pred_output'], method, x['fill_out_mask_color']), axis=1)
 df_temp['pred'] = df_temp.apply(lambda x: np.array_equal(x['pred_output'], x['output']), axis=1)
 if df_temp[f'pred'].min() :
found = True
break
if True:
for idx, row in df_test[df_test['task_id'] == task].iterrows() :
prediction = row['input'].copy()
for method in methods*2:
prediction = fill_out(prediction, method, row['fill_out_mask_color'])
 df_test.loc[idx, get_prediction_column(df_test, idx)] = [prediction]
print(f'
DataFrame: {DF_NAME[id(df)]}')
 score = calculate_score(df_test['output'].values, df_test['prediction_1'].values, df_test['prediction_2'].values, df_test['prediction_3'].values)
 df.loc[df_test.index, ['prediction_1', 'prediction_2', 'prediction_3']] = df_test[['prediction_1', 'prediction_2', 'prediction_3']]<categorify>
var_thresh = VarianceThreshold(0.8)
 data = train_features.append(test_features)
 data_transformed_var = var_thresh.fit(data.iloc[:, 4:][:train_features.shape[0]])
 data_transformed = data_transformed_var.transform(data.iloc[:, 4:])
 train_features_transformed = data_transformed[ : train_features.shape[0]]
test_features_transformed = data_transformed[-test_features.shape[0] : ]
train_features = pd.DataFrame(train_features[['sig_id','cp_type','cp_time','cp_dose']].values.reshape(-1, 4),\
columns=['sig_id','cp_type','cp_time','cp_dose'])
 train_features = pd.concat([train_features, pd.DataFrame(train_features_transformed)], axis=1)
 test_features = pd.DataFrame(test_features[['sig_id','cp_type','cp_time','cp_dose']].values.reshape(-1, 4),\
columns=['sig_id','cp_type','cp_time','cp_dose'])
 test_features = pd.concat([test_features, pd.DataFrame(test_features_transformed)], axis=1)
 train_features.shape
def get_color_mapping(frame_in, frame_out, color_mapping=None):

 if color_mapping == False:
return False
if color_mapping is None:
color_mapping = dict(zip(range(NUM_COLORS), range(NUM_COLORS)))
 for key in color_mapping.keys() :
val = color_mapping[key]
new_color = frame_out[np.where(frame_in == key)]
new_color = np.unique(new_color)
 if len(new_color)> 1:
return False
elif len(new_color)== 1:
new_color = new_color[0]
if key == val:
color_mapping[key] = new_color
elif val != new_color:
return False
return color_mapping
def change_colors(frame, color_mapping):

 result = frame.copy()
for key in color_mapping.keys() :
val = color_mapping[key]
result[np.where(frame == key)] = val
return result<categorify>
def fe_stats(train, test):
features_g = list(train.columns[4:776])
 features_c = list(train.columns[776:876])
 for df in train, test:
df['g_sum'] = df[features_g].sum(axis = 1)
 df['g_mean'] = df[features_g].mean(axis = 1)
 df['g_std'] = df[features_g].std(axis = 1)
 df['g_kurt'] = df[features_g].kurtosis(axis = 1)
 df['g_skew'] = df[features_g].skew(axis = 1)
 df['c_sum'] = df[features_c].sum(axis = 1)
 df['c_mean'] = df[features_c].mean(axis = 1)
 df['c_std'] = df[features_c].std(axis = 1)
 df['c_kurt'] = df[features_c].kurtosis(axis = 1)
 df['c_skew'] = df[features_c].skew(axis = 1)
 df['gc_sum'] = df[features_g + features_c].sum(axis = 1)
 df['gc_mean'] = df[features_g + features_c].mean(axis = 1)
 df['gc_std'] = df[features_g + features_c].std(axis = 1)
 df['gc_kurt'] = df[features_g + features_c].kurtosis(axis = 1)
 df['gc_skew'] = df[features_g + features_c].skew(axis = 1)
 return train, test
train_features,test_features=fe_stats(train_features,test_features )
if True:
for df in [train, validation, test]:
tasks = df[(df['type'] == 'test')&(df['color_change'])]['task_id'].unique()
df_test = df[(df['type'] == 'test')&(df['task_id'].isin(tasks)) ].copy()
for task in tasks:
color_mapping = dict(zip(range(NUM_COLORS), range(NUM_COLORS)))
 df_temp = df[(df['type'] != 'test')&(df['task_id'] == task)].copy()
df_temp['pred'] = df_temp.apply(lambda x: get_color_mapping(x['input'], x['output']), axis=1)
 for pred_color_mapping in df_temp['pred'].to_list() :
if pred_color_mapping == False:
color_mapping = False
break
for key in color_mapping.keys() :
val = color_mapping[key]
new_color = pred_color_mapping[key]
if key == val and key != new_color:
color_mapping[key] = new_color
elif val != new_color and key != new_color:
color_mapping = False
break
if color_mapping == False:
break
if color_mapping == False:
continue
df_temp['pred'] = df_temp['input'].apply(lambda x: change_colors(x, color_mapping))
df_temp['pred'] = df_temp.apply(lambda x: np.array_equal(x['pred'], x['output']), axis=1)
 if df_temp[f'pred'].min() :
for idx, row in df_test[df_test['task_id'] == task].iterrows() :
prediction = change_colors(row['input'], color_mapping)
 df_test.loc[idx, get_prediction_column(df_test, idx)] = [prediction]
print(f'
DataFrame: {DF_NAME[id(df)]}')
 score = calculate_score(df_test['output'].values, df_test['prediction_1'].values, df_test['prediction_2'].values, df_test['prediction_3'].values)
 df.loc[df_test.index, ['prediction_1', 'prediction_2', 'prediction_3']] = df_test[['prediction_1', 'prediction_2', 'prediction_3']]<categorify>
train = train_features.merge(train_targets_scored, on='sig_id')
 train = train[train['cp_type']!='ctl_vehicle'].reset_index(drop=True)
 test = test_features[test_features['cp_type']!='ctl_vehicle'].reset_index(drop=True)
 target = train[train_targets_scored.columns]
train = train.drop('cp_type', axis=1)
 test = test.drop('cp_type', axis=1 )
def object_detection(objects, method):

 objects = [v['obj'] for v in objects]
if method == 'most_common_obj':
str_objects = [str(obj.reshape(-1,)) for obj in objects]
uobj, ucount = np.unique(str_objects, return_counts=True)
 str_obj = uobj[np.argmax(ucount)]
result = objects[str_objects.index(str_obj)]
elif method == 'rarest_obj':
str_objects = [str(obj.reshape(-1,)) for obj in objects]
uobj, ucount = np.unique(str_objects, return_counts=True)
 str_obj = uobj[np.argmin(ucount)]
result = objects[str_objects.index(str_obj)]
elif method == 'rarest_color':
colors = [x for o in objects for x in np.unique(o)if x != 0]
ucol, ucount = np.unique(colors, return_counts=True)
 if len(colors)== len(objects)and len(np.where(ucount == 1)) == 1 and len(np.where(ucount == 1)[0])!= 0:
result = objects[colors.index(ucol[np.where(ucount == 1)][0])]
else:
result = objects[0]
elif method == 'smallest':
sizes = [x.shape[0]*x.shape[1] for x in objects]
result = objects[np.argmin(sizes)]
elif method == 'biggest':
sizes = [x.shape[0]*x.shape[1] for x in objects]
result = objects[np.argmax(sizes)]
return result<feature_engineering>
train_non_scored = train_features.merge(train_targets_nonscored, on='sig_id')
 train_non_scored = train_non_scored[train_non_scored['cp_type']!='ctl_vehicle'].reset_index(drop=True)
 test = test_features[test_features['cp_type']!='ctl_vehicle'].reset_index(drop=True)
 target_non_scored = train_non_scored[train_targets_nonscored.columns]
train_non_scored = train_non_scored.drop('cp_type', axis=1)
 test = test.drop('cp_type', axis=1)
if True:
for df in [train, validation, test]:
tasks = df[(df['type'] == 'test')&(df['output_in_input'])]['task_id'].unique()
df_test = df[(df['type'] == 'test')&(df['task_id'].isin(tasks)) ].copy()
for task in tasks:
df_temp = df[(df['type'] != 'test')&(df['task_id'] == task)].copy()
found = None
for method in ['most_common_obj', 'rarest_obj', 'rarest_color', 'smallest', 'biggest']:
df_temp['pred'] = df_temp.apply(lambda x: object_detection(x['input_objects'], method), axis=1)
 df_temp['pred'] = df_temp.apply(lambda x: np.array_equal(x['pred'], x['output']), axis=1)
 if df_temp[f'pred'].min() :
found = True
break
if found:
for idx, row in df_test[df_test['task_id'] == task].iterrows() :
prediction = object_detection(row['input_objects'], method)
 df_test.loc[idx, get_prediction_column(df_test, idx)] = [prediction]
print(f'
DataFrame: {DF_NAME[id(df)]}')
 score = calculate_score(df_test['output'].values, df_test['prediction_1'].values, df_test['prediction_2'].values, df_test['prediction_3'].values)
 df.loc[df_test.index, ['prediction_1', 'prediction_2', 'prediction_3']] = df_test[['prediction_1', 'prediction_2', 'prediction_3']]<compute_test_metric>
target_cols = target.drop('sig_id', axis=1 ).columns.values.tolist()
target_non_scored_cols = train_targets_nonscored.drop('sig_id', axis=1 ).columns.values.tolist()
for df in [train, validation, test]:
print(f'DataFrame: {DF_NAME[id(df)]}')
 df['match'] = df.apply(lambda x: np.array_equal(x['output'], x['prediction_1'])| np.array_equal(x['output'], x['prediction_2'])| np.array_equal(x['output'], x['prediction_3']), axis=1)
 df = df[df['type'] == 'test']
score = calculate_score(df['output'].values, df['prediction_1'].values, df['prediction_2'].values, df['prediction_3'].values )<save_to_csv>
folds = train.copy()
mskf = MultilabelStratifiedKFold(n_splits=7)
 for f,(t_idx, v_idx)in enumerate(mskf.split(X=train, y=target)) :
folds.loc[v_idx, 'kfold'] = int(f)
 folds['kfold'] = folds['kfold'].astype(int )
df = test[test['type'] == 'test'].copy()
df['submission'] = df.apply(lambda x: prediction_to_submission(x['prediction_1'], x['prediction_2'], x['prediction_3']), axis=1)
 df = df.set_index('submission_id')['submission']
id_check = len(set(submission['output_id'])& set(df.index))
print(f'{df.shape[0]} Test ids
{submission.shape[0]} Submssion ids
{id_check} Intersection')
 submission['output'] = submission['output_id'].apply(lambda x: df[x] if x in df.index else DEFAULT_SUBMISSION)
 submission.to_csv('submission.csv', index=False)
 submission[submission['output'] != DEFAULT_SUBMISSION]<save_to_csv>
folds_non_scored = train_non_scored.copy()
mskf = MultilabelStratifiedKFold(n_splits=7)
 for f,(t_idx, v_idx)in enumerate(mskf.split(X=train, y=target)) :
folds_non_scored.loc[v_idx, 'kfold'] = int(f)
 folds_non_scored['kfold'] = folds_non_scored['kfold'].astype(int )
df = pd.read_csv('/kaggle/input/abstraction-and-reasoning-challenge/sample_submission.csv')
 df.to_csv('submission.csv', index=False )<import_modules>
class MoADataset:
def __init__(self, features, targets):
self.features = features
self.targets = targets
def __len__(self):
return(self.features.shape[0])
 def __getitem__(self, idx):
dct = {
'x' : torch.tensor(self.features[idx, :], dtype=torch.float),
'y' : torch.tensor(self.targets[idx, :], dtype=torch.float)
 }
return dct
class TestDataset:
def __init__(self, features):
self.features = features
def __len__(self):
return(self.features.shape[0])
 def __getitem__(self, idx):
dct = {
'x' : torch.tensor(self.features[idx, :], dtype=torch.float)
 }
return dct
import numpy as np
import pandas as pd
import os
import json
from pathlib import Path
import matplotlib.pyplot as plt
from matplotlib import colors
import numpy as np<define_variables>
def train_fn(model, optimizer, scheduler, loss_fn, dataloader, device):
model.train()
final_loss = 0
for data in dataloader:
optimizer.zero_grad()
inputs, targets = data['x'].to(device), data['y'].to(device)
 outputs = model(inputs)
 loss = loss_fn(outputs, targets)
 loss.backward()
optimizer.step()
scheduler.step()
final_loss += loss.item()
final_loss /= len(dataloader)
 return final_loss
def valid_fn(model, loss_fn, dataloader, device):
model.eval()
final_loss = 0
valid_preds = []
for data in dataloader:
inputs, targets = data['x'].to(device), data['y'].to(device)
 outputs = model(inputs)
 loss = loss_fn(outputs, targets)
 final_loss += loss.item()
valid_preds.append(outputs.sigmoid().detach().cpu().numpy())
 final_loss /= len(dataloader)
 valid_preds = np.concatenate(valid_preds)
 return final_loss, valid_preds
def inference_fn(model, dataloader, device):
model.eval()
preds = []
for data in dataloader:
inputs = data['x'].to(device)
 with torch.no_grad() :
outputs = model(inputs)
 preds.append(outputs.sigmoid().detach().cpu().numpy())
 preds = np.concatenate(preds)
 return preds
data_path = Path('/kaggle/input/abstraction-and-reasoning-challenge/')
 training_path = data_path / 'training'
evaluation_path = data_path / 'evaluation'
test_path = data_path / 'test'<load_from_disk>
class Model(nn.Module):
def __init__(self, num_features, num_targets, hidden_size):
super(Model, self ).__init__()
self.batch_norm1 = nn.BatchNorm1d(num_features)
 self.dropout1 = nn.Dropout(0.2)
 self.dense1 = nn.utils.weight_norm(nn.Linear(num_features, hidden_size))
self.batch_norm2 = nn.BatchNorm1d(hidden_size)
 self.dropout2 = nn.Dropout(0.3)
 self.dense2 = nn.utils.weight_norm(nn.Linear(hidden_size, hidden_size))
self.batch_norm3 = nn.BatchNorm1d(hidden_size)
 self.dropout3 = nn.Dropout(0.25)
 self.dense3 = nn.utils.weight_norm(nn.Linear(hidden_size, num_targets))
def forward(self, x):
x = self.batch_norm1(x)
 x = self.dropout1(x)
 x = F.relu(self.dense1(x))
x = self.batch_norm2(x)
 x = self.dropout2(x)
 x = F.relu(self.dense2(x))
x = self.batch_norm3(x)
 x = self.dropout3(x)
 x = self.dense3(x)
 return x
class SmoothBCEwLogits(_WeightedLoss):
def __init__(self, weight=None, reduction='mean', smoothing=0.0):
super().__init__(weight=weight, reduction=reduction)
 self.smoothing = smoothing
self.weight = weight
self.reduction = reduction
@staticmethod
def _smooth(targets:torch.Tensor, n_labels:int, smoothing=0.0):
assert 0 <= smoothing < 1
with torch.no_grad() :
targets = targets *(1.0 - smoothing)+ 0.5 * smoothing
return targets
def forward(self, inputs, targets):
targets = SmoothBCEwLogits._smooth(targets, inputs.size(-1),
self.smoothing)
 loss = F.binary_cross_entropy_with_logits(inputs, targets,self.weight)
 if self.reduction == 'sum':
loss = loss.sum()
elif self.reduction == 'mean':
loss = loss.mean()
return loss
task_file = str(training_path / '00d62c1b.json')
 with open(task_file, 'r')as f:
task = json.load(f)
 print(task.keys() )<define_variables>
def process_data(data):
data = pd.get_dummies(data, columns=['cp_time','cp_dose'])
 return data
n_train_pairs = len(task['train'])
 n_test_pairs = len(task['test'])
 print(f'task contains {n_train_pairs} training pairs')
 print(f'task contains {n_test_pairs} test pairs' )<categorify>
feature_cols = [c for c in process_data(folds ).columns if c not in target_cols]
feature_cols = [c for c in feature_cols if c not in ['kfold','sig_id']]
len(feature_cols )
def getFillValue(im, out, maxValue=10):
Holes = findHoles(im)
 for FillValue in range(maxValue+1):
Filled = np.where(Holes, FillValue, im)
 if(Filled==out ).all() :
return FillValue
return 0<load_from_csv>
DEVICE =('cuda' if torch.cuda.is_available() else 'cpu')
 EPOCHS = 100
BATCH_SIZE = 128
LEARNING_RATE = 1e-3
WEIGHT_DECAY = 1e-5
NFOLDS = 7
EARLY_STOPPING_STEPS = 10
EARLY_STOP = True
num_features=len(feature_cols)
 num_targets=len(target_cols)
 num_non_scored_targets=len(target_non_scored_cols)
 hidden_size=2048
submission = pd.read_csv(data_path / 'sample_submission.csv', index_col='output_id')
 display(submission.head() )<categorify>
import copy
def flattener(pred):
str_pred = str([row for row in pred])
 str_pred = str_pred.replace(', ', '')
 str_pred = str_pred.replace('[[', '|')
 str_pred = str_pred.replace('][', '|')
 str_pred = str_pred.replace(']]', '|')
 return str_pred<concatenate>
def run_training(fold, seed, pretrain=False):
seed_everything(seed)
 train = process_data(folds)
 test_ = process_data(test)
 trn_idx = train[train['kfold'] != fold].index
val_idx = train[train['kfold'] == fold].index
train_df = train[train['kfold'] != fold].reset_index(drop=True)
 valid_df = train[train['kfold'] == fold].reset_index(drop=True)
 x_train, y_train = train_df[feature_cols].values, train_df[target_cols].values
x_valid, y_valid = valid_df[feature_cols].values, valid_df[target_cols].values
train_dataset = MoADataset(x_train, y_train)
 valid_dataset = MoADataset(x_valid, y_valid)
 trainloader = torch.utils.data.DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True)
 validloader = torch.utils.data.DataLoader(valid_dataset, batch_size=BATCH_SIZE, shuffle=False)
 if pretrain:
model = Model(
 num_features=num_features,
num_targets=num_non_scored_targets,
hidden_size=hidden_size, 
)
 model.load_state_dict(torch.load(f"seed{seed}FOLD{fold}_non_scored.pth"))
model.batch_norm3 = nn.BatchNorm1d(hidden_size)
 model.dropout3 = nn.Dropout(0.5)
 model.dense3 = nn.utils.weight_norm(nn.Linear(hidden_size, num_targets))
else:
model = Model(
 num_features=num_features,
num_targets=num_targets,
hidden_size=hidden_size, 
)
 model.to(DEVICE)
 optimizer = torch.optim.Adam(model.parameters() , lr=LEARNING_RATE, weight_decay=WEIGHT_DECAY)
 if pretrain:
scheduler = optim.lr_scheduler.OneCycleLR(optimizer=optimizer, pct_start=0.1, div_factor=1e3,
max_lr=1e-3, epochs=EPOCHS, steps_per_epoch=len(trainloader))
epochs = 100
else:
scheduler = optim.lr_scheduler.OneCycleLR(optimizer=optimizer, pct_start=0.1, div_factor=1e3,
max_lr=1e-2, epochs=EPOCHS, steps_per_epoch=len(trainloader))
epochs = copy.copy(EPOCHS)
 loss_fn = nn.BCEWithLogitsLoss()
loss_tr = SmoothBCEwLogits(smoothing =0.001)
 early_stopping_steps = EARLY_STOPPING_STEPS
early_step = 0
oof = np.zeros(( len(train), target.iloc[:, 1:].shape[1]))
best_loss = np.inf
print(epochs)
 for epoch in range(epochs):
train_loss = train_fn(model, optimizer,scheduler, loss_tr, trainloader, DEVICE)
 print(f"FOLD: {fold}, EPOCH: {epoch}, train_loss: {train_loss}")
 valid_loss, valid_preds = valid_fn(model, loss_fn, validloader, DEVICE)
 print(f"FOLD: {fold}, EPOCH: {epoch}, valid_loss: {valid_loss}")
 if valid_loss < best_loss:
best_loss = valid_loss
oof[val_idx] = valid_preds
torch.save(model.state_dict() , f"seed{seed}FOLD{fold}_.pth")
 elif(EARLY_STOP == True):
early_step += 1
if(early_step >= early_stopping_steps):
break
x_test = test_[feature_cols].values
testdataset = TestDataset(x_test)
 testloader = torch.utils.data.DataLoader(testdataset, batch_size=BATCH_SIZE, shuffle=False)
 model = Model(
 num_features=num_features,
num_targets=num_targets,
hidden_size=hidden_size, 
)
 model.load_state_dict(torch.load(f"seed{seed}FOLD{fold}_.pth"))
model.to(DEVICE)
 predictions = np.zeros(( len(test_), target.iloc[:, 1:].shape[1]))
predictions = inference_fn(model, testloader, DEVICE)
 return oof, predictions
example_grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
display(example_grid)
 print(flattener(example_grid))<predict_on_test>
def run_training_non_scored(fold, seed):
seed_everything(seed)
 train = process_data(folds_non_scored)
 test_ = process_data(test)
 trn_idx = train[train['kfold'] != fold].index
val_idx = train[train['kfold'] == fold].index
train_df = train[train['kfold'] != fold].reset_index(drop=True)
 valid_df = train[train['kfold'] == fold].reset_index(drop=True)
 x_train, y_train = train_df[feature_cols].values, train_df[target_non_scored_cols].values
x_valid, y_valid = valid_df[feature_cols].values, valid_df[target_non_scored_cols].values
train_dataset = MoADataset(x_train, y_train)
 valid_dataset = MoADataset(x_valid, y_valid)
 trainloader = torch.utils.data.DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True)
 validloader = torch.utils.data.DataLoader(valid_dataset, batch_size=BATCH_SIZE, shuffle=False)
 model = Model(
 num_features=num_features,
num_targets=num_non_scored_targets,
hidden_size=hidden_size, 
)
 model.to(DEVICE)
 optimizer = torch.optim.Adam(model.parameters() , lr=LEARNING_RATE, weight_decay=WEIGHT_DECAY)
 scheduler = optim.lr_scheduler.OneCycleLR(optimizer=optimizer, pct_start=0.1, div_factor=1e3,
max_lr=1e-4, epochs=EPOCHS, steps_per_epoch=len(trainloader))
loss_fn = nn.BCEWithLogitsLoss()
loss_tr = SmoothBCEwLogits(smoothing =0.001)
 early_stopping_steps = EARLY_STOPPING_STEPS
early_step = 0
oof = np.zeros(( len(train), target_non_scored.iloc[:, 1:].shape[1]))
best_loss = np.inf
for epoch in range(10):
train_loss = train_fn(model, optimizer,scheduler, loss_tr, trainloader, DEVICE)
 print(f"FOLD: {fold}, EPOCH: {epoch}, train_loss: {train_loss}")
 valid_loss, valid_preds = valid_fn(model, loss_fn, validloader, DEVICE)
 print(f"FOLD: {fold}, EPOCH: {epoch}, valid_loss: {valid_loss}")
 if valid_loss < best_loss:
best_loss = valid_loss
oof[val_idx] = valid_preds
torch.save(model.state_dict() , f"seed{seed}FOLD{fold}_non_scored.pth")
 elif(EARLY_STOP == True):
early_step += 1
if(early_step >= early_stopping_steps):
break
x_test = test_[feature_cols].values
testdataset = TestDataset(x_test)
 testloader = torch.utils.data.DataLoader(testdataset, batch_size=BATCH_SIZE, shuffle=False)
 model = Model(
 num_features=num_features,
num_targets=num_non_scored_targets,
hidden_size=hidden_size, 
)
 model.load_state_dict(torch.load(f"seed{seed}FOLD{fold}_non_scored.pth"))
model.to(DEVICE)
 predictions = np.zeros(( len(test_), target_non_scored.iloc[:, 1:].shape[1]))
predictions = inference_fn(model, testloader, DEVICE)
 return oof, predictions
def getDefaultPred(task, pair_id):
data = task['test'][pair_id]['input']
pred_1 = flattener(data)
 data = [[5 if i==0 else i for i in j] for j in data]
pred_2 = flattener(data)
 data = [[0 for i in j] for j in data]
pred_3 = flattener(data)
 pred = pred_1 + ' ' + pred_2 + ' ' + pred_3 + ' '
return pred
FillerCount = 0
for output_id in submission.index:
task_id = output_id.split('_')[0]
pair_id = int(output_id.split('_')[1])
 f = str(test_path / str(task_id + '.json'))
with open(f, 'r')as read_file:
task = json.load(read_file)
 if all([np.array(tr['input'] ).shape == np.array(tr['output'] ).shape for tr in task['train']]):
FillValues = np.unique([getFillValue(np.array(tr['input']), np.array(tr['output'])) for tr in task['train']])
 if len(FillValues)==1 and FillValues[0] > 0:
print('Found a Filler')
 FillerCount += 1
FillValue = FillValues[0]
im = np.array(task['test'][pair_id]['input'])
 Holes = findHoles(im)
 Filled = np.where(Holes, FillValue, im ).tolist()
pred = flattener(Filled)
 else:
pred = getDefaultPred(task, pair_id)
 else:
pred = getDefaultPred(task, pair_id)
 submission.loc[output_id, 'output'] = pred
submission.to_csv('submission.csv')
 print(f'{FillerCount} Fillers found in total' )<import_modules>
import numpy as np
import pandas as pd
import os
import json
from pathlib import Path
import matplotlib.pyplot as plt
from matplotlib import colors
import numpy as np<define_variables>
def run_k_fold(NFOLDS, seed, pretrain=False):
oof = np.zeros(( len(train), len(target_cols)))
 predictions = np.zeros(( len(test), len(target_cols)))
 for fold in range(NFOLDS):
oof_, pred_ = run_training(fold, seed, pretrain)
 predictions += pred_ / NFOLDS
oof += oof_
return oof, predictions
data_path = Path('/kaggle/input/abstraction-and-reasoning-challenge/')
 training_path = data_path / 'training'
evaluation_path = data_path / 'evaluation'
test_path = data_path / 'test'<load_from_disk>
def run_k_fold_none_scored(NFOLDS, seed):
oof = np.zeros(( len(train), len(target_non_scored_cols)))
 predictions = np.zeros(( len(test), len(target_non_scored_cols)))
 for fold in range(NFOLDS):
oof_, pred_ = run_training_non_scored(fold, seed)
 predictions += pred_ / NFOLDS
oof += oof_
return oof, predictions
task_file = str(training_path / '00d62c1b.json')
 with open(task_file, 'r')as f:
task = json.load(f)
 print(task.keys() )<define_variables>
SEED = [1903, 1881]
oof = np.zeros(( len(train), len(target_cols)))
 predictions = np.zeros(( len(test), len(target_cols)))
 for seed in SEED:
run_k_fold_none_scored(NFOLDS, seed)
 oof_, predictions_ = run_k_fold(NFOLDS, seed, pretrain=True)
 oof += oof_ / len(SEED)
 predictions += predictions_ / len(SEED)
 train[target_cols] = oof
test[target_cols] = predictions
n_train_pairs = len(task['train'])
 n_test_pairs = len(task['test'])
 print(f'task contains {n_train_pairs} training pairs')
 print(f'task contains {n_test_pairs} test pairs' )<load_from_csv>
valid_results = train_targets_scored.drop(columns=target_cols ).merge(train[['sig_id']+target_cols], on='sig_id', how='left' ).fillna(0)
 y_true = train_targets_scored[target_cols].values
y_pred = valid_results[target_cols].values
score = 0
for i in range(len(target_cols)) :
score_ = log_loss(y_true[:, i], y_pred[:, i])
 score += score_ / target.shape[1]
print("CV log_loss: ", score )
submission = pd.read_csv(data_path / 'sample_submission.csv', index_col='output_id')
 display(submission.head() )<categorify>
sub = sample_submission.drop(columns=target_cols ).merge(test[['sig_id']+target_cols], on='sig_id', how='left' ).fillna(0)
 sub.to_csv('submission.csv', index=False )