Kaggle competition: TReNDS Neuroimaging
<SOS> metric: WMAE Kaggle data source: trends-neuroimaging<define_variables>
!cp.. /input/rapids/rapids.0.13.0 /opt/conda/envs/rapids.tar.gz
!cd /opt/conda/envs/ && tar -xzvf rapids.tar.gz > /dev/null
sys.path = ["/opt/conda/envs/rapids/lib/python3.6/site-packages"] + sys.path
sys.path = ["/opt/conda/envs/rapids/lib/python3.6"] + sys.path
sys.path = ["/opt/conda/envs/rapids/lib"] + sys.path
!cp /opt/conda/envs/rapids/lib/libxgboost.so /opt/conda/lib/
probabilities = np.zeros(( all_probabilities[0].shape))
for j in range(no_of_models):
probabilities = probabilities + all_probabilities[j]
predictions = np.argmax(probabilities, axis =-1)
 display_batch_of_images(( images, labels), predictions )<load_pretrained>
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))
val_probs = [cm_correct_labels, cm_predictions, val_probabilities[0], val_probabilities[1], test_probabilities[0], test_probabilities[1]]
filename = this_run_file_prefix + 'tests_vals_01.pkl'
pklfile = open(filename, 'ab')
 pickle.dump(val_probs, pklfile)
 pklfile.close()<categorify>
fnc_df = cudf.read_csv(".. /input/trends-assessment-prediction/fnc.csv")
 loading_df = cudf.read_csv(".. /input/trends-assessment-prediction/loading.csv")
 fnc_features, loading_features = list(fnc_df.columns[1:]), list(loading_df.columns[1:])
 df = fnc_df.merge(loading_df, on="Id")
 labels_df = cudf.read_csv(".. /input/trends-assessment-prediction/train_scores.csv")
 labels_df["is_train"] = True
df = df.merge(labels_df, on="Id", how="left")
 test_df = df[df["is_train"] != True].copy()
df = df[df["is_train"] == True].copy()
use_correct_labels = cm_correct_labels
use_val_predictions = best_predictions<define_variables>
FNC_SCALE = 1/600
df[fnc_features] *= FNC_SCALE
test_df[fnc_features] *= FNC_SCALE
correct_labels_cnt = 0
incorrect_labels_cnt = 0
correct_labels = []
incorrect_labels = []
vals_actual_true = {}
vals_tp = {}
vals_fn = {}
vals_fp = {}
for i in range(len(CLASSES)) :
vals_actual_true[i] = 0
vals_tp[i] = 0
vals_fn[i] = 0
vals_fp[i] = 0
for i in range(len(use_correct_labels)) :
correct_label = use_correct_labels[i]
predict_label = use_val_predictions[i]
vals_actual_true[correct_label] = vals_actual_true[correct_label] + 1
if use_val_predictions[i] != use_correct_labels[i]:
incorrect_labels_cnt = incorrect_labels_cnt + 1
incorrect_labels.append(i)
 vals_fn[correct_label] = vals_fn[correct_label] + 1
vals_fp[predict_label] = vals_fp[predict_label] + 1
else:
correct_labels_cnt = correct_labels_cnt + 1
correct_labels.append(i)
 vals_tp[correct_label] = vals_tp[correct_label] + 1
print('Number of correct_labels is {}, incorrect_labels is {}'.format(correct_labels_cnt, incorrect_labels_cnt))
print('Incorrect labels', incorrect_labels)
<define_variables>
%%time
warnings.filterwarnings("ignore", message="Expected column")
 df_model1 = df.copy()
df_model2 = df.copy()
df_model3 = df.copy()
NUM_FOLDS = 7
kf = KFold(n_splits=NUM_FOLDS, shuffle=True, random_state=0)
 features = loading_features + fnc_features
blend_weights = {"age": [0.4, 0.55, 0.05],
"domain1_var1": [0.55, 0.15, 0.3],
"domain1_var2": [0.45, 0.0, 0.55],
"domain2_var1": [0.55, 0.15, 0.3],
"domain2_var2": [0.5, 0.05, 0.45]}
overall_score = 0
for target, c, w in [("age", 60, 0.3),("domain1_var1", 12, 0.175),("domain1_var2", 8, 0.175),("domain2_var1", 9, 0.175),("domain2_var2", 12, 0.175)]:
y_oof = np.zeros(df.shape[0])
 y_oof_model_1 = np.zeros(df.shape[0])
 y_oof_model_2 = np.zeros(df.shape[0])
 y_oof_model_3 = 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_1 = SVR(C=c, cache_size=3000.0)
 model_1.fit(train_df[features].values, train_df[target].values)
 model_2 = Ridge(alpha = 0.0001)
 model_2.fit(train_df[features].values, train_df[target].values)
 model_3 = BaggingRegressor(Ridge(alpha = 0.0001), n_estimators=30, random_state=42, max_samples=0.3, max_features=0.3)
 model_3.fit(train_df.to_pandas() [features].values, train_df.to_pandas() [target].values)
 val_pred_1 = model_1.predict(val_df[features])
 val_pred_2 = model_2.predict(val_df[features])
 val_pred_3 = model_3.predict(val_df.to_pandas() [features])
 val_pred_3 = cudf.from_pandas(pd.Series(val_pred_3))
test_pred_1 = model_1.predict(test_df[features])
 test_pred_2 = model_2.predict(test_df[features])
 test_pred_3 = model_3.predict(test_df.to_pandas() [features])
 test_pred_3 = cudf.from_pandas(pd.Series(test_pred_3))
val_pred = blend_weights[target][0]*val_pred_1+blend_weights[target][1]*val_pred_2+blend_weights[target][2]*val_pred_3
val_pred = cp.asnumpy(val_pred.values.flatten())
 test_pred = blend_weights[target][0]*test_pred_1+blend_weights[target][1]*test_pred_2+blend_weights[target][2]*test_pred_3
test_pred = cp.asnumpy(test_pred.values.flatten())
 y_oof[val_ind] = val_pred
y_oof_model_1[val_ind] = val_pred_1
y_oof_model_2[val_ind] = val_pred_2
y_oof_model_3[val_ind] = val_pred_3
y_test[:, f] = test_pred
df["pred_{}".format(target)] = y_oof
df_model1["pred_{}".format(target)] = y_oof_model_1
df_model2["pred_{}".format(target)] = y_oof_model_2
df_model3["pred_{}".format(target)] = y_oof_model_3
test_df[target] = y_test.mean(axis=1)
 score = metric(df[df[target].notnull() ][target].values, df[df[target].notnull() ]["pred_{}".format(target)].values)
 overall_score += w*score
score_model1 = metric(df_model1[df_model1[target].notnull() ][target].values, df_model1[df_model1[target].notnull() ]["pred_{}".format(target)].values)
 score_model2 = metric(df_model2[df_model2[target].notnull() ][target].values, df_model2[df_model1[target].notnull() ]["pred_{}".format(target)].values)
 score_model3 = metric(df_model3[df_model3[target].notnull() ][target].values, df_model3[df_model1[target].notnull() ]["pred_{}".format(target)].values)
 print(f"For {target}:")
 print("SVR:", np.round(score_model1, 6))
print("Ridge:", np.round(score_model2, 6))
print("BaggingRegressor:", np.round(score_model3, 6))
print("Ensemble:", np.round(score, 6))
print()
print("Overall score:", np.round(overall_score, 6))
def display_my_batch_of_images(databatch, rows = 0, cols = 0, predictions=None):

 images, labels = databatch
if labels is None:
labels = [None for _ in enumerate(images)]
if rows == 0 or cols == 0:
rows = int(math.sqrt(len(images)))
 cols =(len(images)+ rows - 1)//rows
print('Total number of images is {}, rows is {}, cols is {}'.format(len(images), rows, cols))
FIGSIZE = 20.0
SPACING = 0.1
subplot=(rows,cols,1)
 if rows < cols:
plt.figure(figsize=(FIGSIZE,FIGSIZE/cols*rows))
else:
plt.figure(figsize=(FIGSIZE/rows*cols,FIGSIZE))
for i,(image, label)in enumerate(zip(images[:rows*cols], labels[:rows*cols])) :
title = '' if label is None else CLASSES[label]
correct = True
if predictions is not None:
title, correct = title_from_label_and_target(predictions[i], label)
 dynamic_titlesize = FIGSIZE*SPACING/max(rows,cols)*40+3
subplot = display_one_flower(image, title, subplot, not correct, titlesize=dynamic_titlesize)
 plt.tight_layout()
if label is None and predictions is None:
plt.subplots_adjust(wspace=0, hspace=0)
 else:
plt.subplots_adjust(wspace=SPACING, hspace=SPACING)
 plt.show()<define_variables>
sub_df = cudf.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 )
<load_from_csv><EOS> 
sub_df.to_csv("submission_rapids_ensemble_with_baggingregressor.csv", index=False )