Kaggle competition: TReNDS Neuroimaging
<SOS> metric: WMAE Kaggle data source: trends-neuroimaging<compute_test_metric>
import numpy as np
import pandas as pd
%%time
val_loss, val_acc = eval(model, train_dataloader, device )<compute_test_metric>
algorithm = ['lasso', 'elastic', 'ridge', 'lightgbm', 'xgboost', 'svr', 'linear', 'tweedie']
columns = ['age', 'domain1_var1', 'domain1_var2', 'domain2_var1', 'domain2_var2']
print(f'Loss : {val_loss:.5}, Accuracy : {val_acc:.2%}' )<find_best_params>
DATA_PATH = '.. /input/brain-models/'
df_pred = pd.read_csv(DATA_PATH + 'training_ridge.csv')['Id']
model.eval()
with torch.no_grad() :
input_ids = batch['input_ids'].to(device)
 attention_mask = batch['attention_mask'].to(device)
 outputs = model(input_ids=input_ids, attention_mask=attention_mask)[0]
print(f'Output : {torch.argmax(outputs, -1 ).cpu().detach() }')
 print('Target : {}'.format(batch['label']))<load_from_csv>
for a in algorithm:
p = pd.read_csv(DATA_PATH + 'training_{}.csv'.format(a))
for c in p.columns[1:]:
p['{}_{}'.format(a,c[5:])] = p[c]
p = p.drop(c, axis=1)
 df_pred = pd.merge(df_pred, p, on='Id' )
train = pd.read_csv('.. /input/contradictory-my-dear-watson/train.csv', nrows=1024 )<create_dataframe>
df_sub = pd.read_csv(DATA_PATH + 'submission_ridge.csv')['Id']
for a in algorithm:
p = pd.read_csv(DATA_PATH + 'submission_{}.csv'.format(a))
for c in p.columns[1:]:
p['{}_{}'.format(a,c)] = p[c]
p = p.drop(c, axis=1)
 df_sub = pd.merge(df_sub, p, on='Id' )
train_dataset = nlp.Dataset.from_pandas(train )<define_variables>
def extract_algo(df, col):
for c in df.columns[1:]:
if col not in c:
df = df.drop(c, axis=1)
 for c in df.columns[1:]:
s = c.split('_')[0]
df[s] = df[c]
df = df.drop(c, axis=1)
 return df
train_dataset.num_rows<load_pretrained>
extract_algo(df_pred, 'age' )
train_dataloader = DataLoader(
 train_dataset,
batch_size=batch_size,
num_workers=0,
collate_fn=tokenizer.pad
)<compute_test_metric>
score_pd = pd.read_csv("/kaggle/input/trends-assessment-prediction/train_scores.csv" )
%%time
val_loss, val_acc = eval(model, train_dataloader, device )<compute_test_metric>
df = extract_algo(df_pred, 'domain1_var1')
 df = df.merge(score_pd, on="Id", how="left")
 test_df = extract_algo(df_sub, 'domain1_var1')
 sub_df = test_df.copy()
print(f'Loss : {val_loss:.5}, Accuracy : {val_acc:.2%}' )<load_from_csv>
params = {
'max_depth': -1,
'num_leaves': 3,
'min_data_in_leaf': 4,
}
sample_submission = pd.read_csv('.. /input/contradictory-my-dear-watson/sample_submission.csv' )<load_from_csv>
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))
test_dataset = nlp.load_dataset('csv', data_files={'test': '.. /input/contradictory-my-dear-watson/test.csv'})
 test_dataset = test_dataset.map(convert_to_features, batched=True)
 test_dataset.set_format("torch", columns=['input_ids', 'attention_mask'] )<load_pretrained>
from sklearn.svm import SVR
test_dataloader = DataLoader(
 test_dataset['test'],
batch_size=batch_size,
drop_last=False,
num_workers=0,
shuffle=False,
collate_fn=tokenizer.pad
)<predict_on_test>
NUM_FOLDS = 5
kf = KFold(n_splits=NUM_FOLDS, shuffle=True, random_state=0)
 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)]:
df = extract_algo(df_pred, target)
 df = df.merge(score_pd, on="Id", how="left")
 test_df = extract_algo(df_sub, target)
 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(kernel='rbf')
 model.fit(train_df[algorithm], train_df[target])
 y_oof[val_ind] = model.predict(val_df[algorithm])
 y_test[:, f] = model.predict(test_df[algorithm])
 df["pred_{}".format(target)] = y_oof
sub_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))
def predict(model, dataloader, device):
model.eval()
val_loss = 0
correct = 0
test_preds = np.array([])
 with torch.no_grad() :
for batch in dataloader:
input_ids = batch['input_ids'].to(device)
 attention_mask = batch['attention_mask'].to(device)
 outputs = model(input_ids=input_ids, attention_mask=attention_mask)[0]
test_preds = np.concatenate(( test_preds, torch.argmax(outputs, -1 ).cpu().detach().numpy()))
sample_submission.prediction = test_preds.astype(int)
 sample_submission.prediction.replace([0, 2], [2, 0], inplace=True)
 sample_submission.to_csv('submission.csv', index=False)
 return sample_submission<predict_on_test>
sub_df = pd.melt(sub_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" )
%%time
predict(model, test_dataloader, device )<set_options>
sub_df.to_csv("submission.csv", index=False )