Kaggle competition: TReNDS Neuroimaging
frames = [train_data_translate, xnli_valid_df, mnli_sample]
train_data_def = pd.concat(frames )<train_model>
def metric(y_true, y_pred):
return np.mean(np.sum(np.abs(y_true - y_pred), axis=0)/np.sum(y_true, axis=0))
fnc_df = pd.read_csv(".. /input/trends-assessment-prediction/fnc.csv")
 fnc_df
tf.tpu.experimental.initialize_tpu_system(tpu )<categorify>
loading_df = pd.read_csv(".. /input/trends-assessment-prediction/loading.csv")
 fnc_features, loading_features = list(fnc_df.columns[1:]), list(loading_df.columns[1:])
 loading_df
train_text_xml_roberta = train_data_def[['premise', 'hypothesis']].values.tolist()
test_text_xml_roberta = test[['premise', 'hypothesis']].values.tolist()
train_encoded_xml_roberta = tokenizer_xml_roberta.batch_encode_plus(
 train_text_xml_roberta,
pad_to_max_length=True,
max_length=max_len 
)
 test_encoded_xml_roberta = tokenizer_xml_roberta.batch_encode_plus(
 test_text_xml_roberta,
pad_to_max_length=True,
max_length=max_len
)<split>
df = fnc_df.merge(loading_df, on="Id")
 df
x_train, x_valid, y_train, y_valid = train_test_split(
 train_encoded_xml_roberta['input_ids'], train_data_def.label.values,
test_size=0.2, random_state=2020 
)
 x_test = test_encoded_xml_roberta['input_ids']<split>
labels_df = pd.read_csv(".. /input/trends-assessment-prediction/train_scores.csv")
 labels_df
auto = tf.data.experimental.AUTOTUNE
train_dataset =(
 tf.data.Dataset 
.from_tensor_slices(( x_train, y_train)) 
.repeat() 
.shuffle(2048)
.batch(batch_size)
.prefetch(auto)
)
 valid_dataset =(
 tf.data.Dataset 
.from_tensor_slices(( x_valid, y_valid)) 
.batch(batch_size)
.cache() 
.prefetch(auto)
)
 test_dataset =(
 tf.data.Dataset 
.from_tensor_slices(x_test)
.batch(batch_size)
 )<load_pretrained>
labels_df["is_train"] = True
df = df.merge(labels_df, on="Id", how="left")
 df
response = requests.get('https://miro.medium.com/max/1100/1*zfdW5zAyQxge85gA_mFPYg.png')
 image_bytes = io.BytesIO(response.content)
 img = PIL.Image.open(image_bytes)
 img<load_pretrained>
FNC_SCALE = 1/500
df[fnc_features] *= FNC_SCALE
test_df[fnc_features] *= FNC_SCALE
response = requests.get('https://paperswithcode.com/media/methods/Screen_Shot_2020-05-28_at_6.15.37_PM_apRrZCo.png')
 image_bytes = io.BytesIO(response.content)
 img = PIL.Image.open(image_bytes)
 img<load_pretrained>
%%time
NUM_FOLDS = 7
kf = KFold(n_splits=NUM_FOLDS, shuffle=True, random_state=0)
 features = loading_features + fnc_features
overal_score = 0
for target, c, w in [("age", 100, 0.3),("domain1_var1", 10, 0.175),("domain1_var2", 10, 0.175),("domain2_var1", 10, 0.175),("domain2_var2", 10, 0.175)]:
y_oof = np.zeros(df.shape[0])
 y_test = np.zeros(( test_df.shape[0], NUM_FOLDS))
for f,(train_ind, val_ind)in enumerate(kf.split(df, df)) :
train_df, val_df = df.iloc[train_ind], df.iloc[val_ind]
train_df = train_df[train_df[target].notnull() ]
model = SVR(C=c, cache_size=3000.0)
 model.fit(train_df[features], train_df[target])
 y_oof[val_ind] = model.predict(val_df[features])
 y_test[:, f] = model.predict(test_df[features])
 df["pred_{}".format(target)] = y_oof
test_df[target] = y_test.mean(axis=1)
 score = metric(df[df[target].notnull() ][target].values, df[df[target].notnull() ]["pred_{}".format(target)].values)
 overal_score += w*score
print(target, np.round(score, 4))
print()
print("Overal score:", np.round(overal_score, 4))
response = requests.get('https://www.jeremyjordan.me/content/images/2018/02/Screen-Shot-2018-02-24-at-11.47.09-AM.png')
 image_bytes = io.BytesIO(response.content)
 img = PIL.Image.open(image_bytes)
 img<load_from_csv>
sub_df = pd.melt(test_df[["Id", "age", "domain1_var1", "domain1_var2", "domain2_var1", "domain2_var2"]], id_vars=["Id"], value_name="Predicted")
 sub_df["Id"] = sub_df["Id"].astype("str")+ "_" + sub_df["variable"].astype("str")
 sub_df = sub_df.drop("variable", axis=1 ).sort_values("Id")
 assert sub_df.shape[0] == test_df.shape[0]*5
sub_df.head(10 )
fine_tuning_lr = pd.read_csv(".. /input/fine-tuning/table tuning lr.csv", sep = ';')
 fine_tuning_lr<load_from_csv>
sub_df.to_csv("submission.csv", index=False )