Kaggle competition: ASHRAE - Great Energy Predictor III
<SOS> metric: RMSLE Kaggle data source: ashrae-great-energy-predictor-iii<compute_test_metric>
%matplotlib inline
mean_error = []
for day in range(2, train_baseline.Day.max() + 1):
val = train_baseline[train_baseline.Day == day]
p = val.Last_Week_Sales.values
error = rmspe(val.Sales.values, p)
 mean_error.append(error)
 print('Mean Error = %.5f' % np.mean(mean_error))<categorify>
root = Path('.. /input/ashrae-feather-format-for-fast-loading')
 test_df = pd.read_feather(root/'test.feather')
 building_meta_df = pd.read_feather(root/"building_metadata.feather" )
class LabelEncoderPipelineFriendly(LabelEncoder):
def fit(self, X, y=None):

 super(LabelEncoderPipelineFriendly, self ).fit(X)
 def transform(self, X, y=None):
return super(LabelEncoderPipelineFriendly, self ).transform(X ).reshape(-1, 1)
 def fit_transform(self, X, y=None):
return super(LabelEncoderPipelineFriendly, self ).fit(X ).transform(X ).reshape(-1, 1 )<categorify>
leak_df = pd.read_feather('.. /input/ashrae-leak-data-station/leak.feather')
 leak_df.fillna(0, inplace=True)
 leak_df = leak_df[(leak_df.timestamp.dt.year > 2016)&(leak_df.timestamp.dt.year < 2019)]
leak_df.loc[leak_df.meter_reading < 0, 'meter_reading'] = 0
leak_df = leak_df[leak_df.building_id!=245]
def prepare_pipeline(df):
def get_DayOfWeek(df):
return df["DayOfWeek"]
def get_Open(df):
return df[["Open"]]
def get_Promo(df):
return df[["Promo"]]
def get_StateHoliday(df):
return df["StateHoliday"]
def get_SchoolHoliday(df):
return df[["SchoolHoliday"]]
def get_StoreType(df):
return df["StoreType"]
def get_Assortment(df):
return df["Assortment"]
def get_Promo2Active(df):
return df[["Promo2Active"]]
def get_CompetitionActive(df):
return df[["CompetitionActive"]]
def get_CompetitionDistance(df):
return df[["CompetitionDistance"]]
def get_DayOfYear(df):
return df["DayOfYear"]
p = make_union(*[
make_pipeline(FunctionTransformer(get_DayOfWeek, validate=False),
LabelEncoderPipelineFriendly() ,
OneHotEncoder()),
make_pipeline(FunctionTransformer(get_Open, validate=False),
Imputer(strategy="most_frequent")) ,
make_pipeline(FunctionTransformer(get_Promo, validate=False)) ,
make_pipeline(FunctionTransformer(get_StateHoliday, validate=False),
LabelEncoderPipelineFriendly() ,
OneHotEncoder()),
make_pipeline(FunctionTransformer(get_SchoolHoliday, validate=False)) ,
make_pipeline(FunctionTransformer(get_StoreType, validate=False),
LabelEncoderPipelineFriendly() ,
OneHotEncoder()),
make_pipeline(FunctionTransformer(get_Assortment, validate=False),
LabelEncoderPipelineFriendly() ,
OneHotEncoder()),
make_pipeline(FunctionTransformer(get_Promo2Active, validate=False)) ,
make_pipeline(FunctionTransformer(get_CompetitionActive, validate=False)) ,
make_pipeline(FunctionTransformer(get_CompetitionDistance, validate=False),
Imputer() ,
StandardScaler()),
make_pipeline(FunctionTransformer(get_DayOfYear, validate=False),
LabelEncoderPipelineFriendly() ,
OneHotEncoder())
 ])
 return p<define_variables>
print(leak_df.duplicated().sum() )
pipeline = prepare_pipeline(train )<train_model>
print(leak_df.isnull().sum() )
x_train, y_train = pipeline.fit_transform(train), train.Sales
x_test = pipeline.transform(test )<init_hyperparams>
gc.collect()
params = {"boosting_type" : ["gbdt"],
"learning_rate": [0.1],
"n_estimators": [200],
"objective": ["regression"],
"reg_alpha": [1.0],
"reg_lambda": [1.0],
"random_state": [0],
"n_jobs": [-1]
}<train_on_grid>
kfold_submission = pd.read_csv('.. /input/ashrae-kfold-lightgbm-without-leak-1-08/submission.csv', index_col=0)
 halfhalf_submission = pd.read_csv('.. /input/ashrae-half-and-half/submission.csv', index_col=0)
 highway_submission = pd.read_csv('.. /input/ashrae-highway-kernel-route4/submission.csv', index_col=0)
 gep3_submission = pd.read_csv(".. /input/ashrae-great-energy-predictor-iii-eda-model/Normal.csv", index_col=0)
 simple_submission = pd.read_csv(".. /input/ashrae-simple-data-cleanup-lb-1-08-no-leaks/submission.csv", index_col=0 )
gs = GridSearchCV(LGBMRegressor() , params, scoring=rmspe_scorer, cv=2, n_jobs=1)
 gs.fit(x_train, y_train )<predict_on_test>
test_df["pred1"] = kfold_submission.meter_reading
test_df["pred2"] = halfhalf_submission.meter_reading
test_df["pred3"] = highway_submission.meter_reading
test_df["pred4"] = gep3_submission.meter_reading
test_df["pred5"] = simple_submission.meter_reading
del kfold_submission, halfhalf_submission, highway_submission, gep3_submission
del simple_submission
gc.collect()
prediction = gs.predict(x_test )<save_to_csv>
test_df.loc[test_df.pred3 < 0, "pred3"] = 0
test_df.loc[test_df.pred4 < 0, "pred4"] = 0
test_df.loc[test_df.pred5 < 0, "pred5"] = 0
test_df = reduce_mem_usage(test_df)
 leak_df = reduce_mem_usage(leak_df )
pd.DataFrame({"Id": test.Id, "Sales": prediction} ).to_csv("submission.csv", sep=",", index=False )<something_strange>
leak_df = leak_df.merge(test_df[['building_id', 'meter', 'timestamp', 'pred1', 'pred2', 'pred3', 'pred4', 'pred5', 'row_id']],
left_on = ['building_id','meter', 'timestamp'], right_on = ['building_id', 'meter', 'timestamp'],
how = "left")
 leak_df = leak_df.merge(building_meta_df[['building_id', 'site_id']], on='building_id', how='left' )
def prepare_pipeline_ts(df, min_shift, max_shift):
def get_shifted_date(df, for_sales=False):
return(df.Date.min() + pd.DateOffset(days_to_shift))
def get_DayOfWeek(df):
return df["DayOfWeek"]
def get_Open(df):
return df[["Open"]]
def get_Promo(df):
return df[["Promo"]]
def get_StateHoliday(df):
return df["StateHoliday"]
def get_SchoolHoliday(df):
return df[["SchoolHoliday"]]
def get_StoreType(df):
return df["StoreType"]
def get_Assortment(df):
return df["Assortment"]
def get_Promo2Active(df):
return df[["Promo2Active"]]
def get_CompetitionActive(df):
return df[["CompetitionActive"]]
def get_CompetitionDistance(df):
return df[["CompetitionDistance"]]
def get_DayOfYear(df):
return df["DayOfYear"]
def get_previous_sales(df):
sales = df[["Store", "Sales"]].copy()
for day in range(min_shift, max_shift + 1):
sales["Last-{}_Day_Sales".format(day)] = sales.groupby("Store")["Sales"].shift(day)
 sales["Last-{}_Day_Diff".format(day)] = sales.groupby("Store")["Last-{}_Day_Sales".format(day)].diff()
return sales.drop(["Store", "Sales"], axis=1)
 p = make_union(*[
make_pipeline(FunctionTransformer(get_DayOfWeek, validate=False),
LabelEncoderPipelineFriendly() ,
OneHotEncoder()),
make_pipeline(FunctionTransformer(get_Open, validate=False),
Imputer(strategy="most_frequent")) ,
make_pipeline(FunctionTransformer(get_Promo, validate=False)) ,
make_pipeline(FunctionTransformer(get_StateHoliday, validate=False),
LabelEncoderPipelineFriendly() ,
OneHotEncoder()),
make_pipeline(FunctionTransformer(get_SchoolHoliday, validate=False)) ,
make_pipeline(FunctionTransformer(get_StoreType, validate=False),
LabelEncoderPipelineFriendly() ,
OneHotEncoder()),
make_pipeline(FunctionTransformer(get_Assortment, validate=False),
LabelEncoderPipelineFriendly() ,
OneHotEncoder()),
make_pipeline(FunctionTransformer(get_Promo2Active, validate=False)) ,
make_pipeline(FunctionTransformer(get_CompetitionActive, validate=False)) ,
make_pipeline(FunctionTransformer(get_CompetitionDistance, validate=False),
Imputer() ,
StandardScaler()),
make_pipeline(FunctionTransformer(get_DayOfYear, validate=False),
LabelEncoderPipelineFriendly() ,
OneHotEncoder()),
make_pipeline(FunctionTransformer(get_previous_sales, validate=False),
Imputer() ,
StandardScaler())
 ])
 return p<define_variables>
leak_df["pred1_l1p"] = np.log1p(leak_df.pred1)
 leak_df["pred2_l1p"] = np.log1p(leak_df.pred2)
 leak_df["pred3_l1p"] = np.log1p(leak_df.pred3)
 leak_df["pred4_l1p"] = np.log1p(leak_df.pred4)
 leak_df["pred5_l1p"] = np.log1p(leak_df.pred5)
 leak_df["meter_reading_l1p"] = np.log1p(leak_df.meter_reading )
test_size = len(test)
 min_shift =(test.Date.max() - test.Date.min() ).days
max_shift = 180
to_drop = len(train[train.Date < train.Date.min() + pd.DateOffset(max_shift)] )<prepare_x_and_y>
leak_df.isnull().sum()
full = pd.concat([train, test], ignore_index=True)
 pipeline_ts = prepare_pipeline_ts(train, min_shift, max_shift)
 full_transformed = pipeline_ts.fit_transform(full)
 x_train_ts, y_train_ts = full_transformed[to_drop:-test_size], train.Sales[to_drop:]
x_test_ts = full_transformed[-test_size:]<train_on_grid>
print(leak_df[leak_df.pred1_l1p.isnull() ])
 print(leak_df[leak_df.pred4_l1p.isnull() ])
 print(leak_df[leak_df.pred5_l1p.isnull() ] )
gs_ts = GridSearchCV(LGBMRegressor() , params, scoring=rmspe_scorer, cv=2, n_jobs=1)
 gs_ts.fit(x_train_ts, y_train_ts )<predict_on_test>
del building_meta_df
gc.collect()
prediction_ts = gs_ts.predict(x_test_ts )<save_to_csv>
all_combinations = list(np.linspace(0.15,0.4,15))
all_combinations
pd.DataFrame({"Id": test.Id, "Sales": prediction_ts} ).to_csv("submission_ts.csv", sep=",", index=False )<install_modules>
import itertools
!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/<compute_test_metric>
l = [all_combinations, all_combinations, all_combinations, all_combinations, all_combinations]
all_l = list(itertools.product(*l))
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))<load_from_csv>
gc.collect()
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()<normalization>
filtered_combis = [l for l in all_l if l[0] + l[1] + l[2] + l[3] + l[4] > 0.95 and l[0] + l[1] + l[2] + l[3] + l[4] < 1.03]
FNC_SCALE = 1/600
df[fnc_features] *= FNC_SCALE
test_df[fnc_features] *= FNC_SCALE<import_modules>
best_combi = []
for i, combi in enumerate(filtered_combis):
score1 = combi[0]
score2 = combi[1]
score3 = combi[2]
score4 = combi[3]
score5 = combi[4]
v = score1 * leak_df['pred1'].values + score2 * leak_df['pred3'].values + score3 * leak_df['pred2'].values + score4 * leak_df['pred4'] + score5 * leak_df['pred5']
vl1p = np.log1p(v)
 curr_score = np.sqrt(mean_squared_error(vl1p, leak_df.meter_reading_l1p))
if best_combi:
prev_score = best_combi[0][1]
if curr_score < prev_score:
best_combi[:] = []
best_combi += [(i, curr_score)]
else:
best_combi += [(i, curr_score)]
score = best_combi[0][1]
print(score )
import nilearn as nl
import nibabel as nib
from nilearn import image
from nilearn import plotting
from nilearn import datasets
from nilearn import surface
import nilearn.plotting as nlplt<define_variables>
fmri_mask = '.. /input/trends-assessment-prediction/fMRI_mask.nii'<define_search_space>
submission_form = pd.read_feather(os.path.join(root, 'sample_submission.feather'))
final_combi = filtered_combis[best_combi[0][0]]
w1 = final_combi[0]
w2 = final_combi[1]
w3 = final_combi[2]
w4 = final_combi[3]
w5 = final_combi[4]
print("Best weight is w1(kfold):{}, w2(halfhalf):{}, w3(highway):{}, w4(gep3):{}, w5(simple):{}".format(w1, w2, w3, w4, w5))
submission_form['meter_reading'] = w1 * test_df.pred1 + w2 * test_df.pred3 + w3 * test_df.pred2 + w4 * test_df.pred4 + w5 * test_df.pred5
submission_form.loc[submission_form.meter_reading < 0, 'meter_reading'] = 0
smri = 'ch2better.nii'
mask_img = nl.image.load_img(fmri_mask)
 def load_subject(filename, mask_img):
subject_data = None
with h5py.File(filename, 'r')as f:
subject_data = f['SM_feature'][() ]
subject_data = np.moveaxis(subject_data, [0,1,2,3], [3,2,1,0])
 subject_img = nl.image.new_img_like(mask_img, subject_data, affine=mask_img.affine, copy_header=True)
 return subject_img
files = random.choices(os.listdir('.. /input/trends-assessment-prediction/fMRI_train/'), k = 3)
 for file in files:
subject = os.path.join('.. /input/trends-assessment-prediction/fMRI_train/', file)
 subject_img = load_subject(subject, mask_img)
 print("Image shape is %s" %(str(subject_img.shape)))
 num_components = subject_img.shape[-1]
print("Detected {num_components} spatial maps".format(num_components=num_components))
rsn = subject_img
first_rsn = image.index_img(rsn, 0)
 print(first_rsn.shape)
 plotting.plot_glass_brain(first_rsn,display_mode='lyrz')
 print("-"*50 )<create_dataframe>
gc.collect()
%%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))<data_type_conversions>
leak_df = leak_df[['meter_reading', 'row_id']].set_index('row_id' ).dropna()
print(len(leak_df))
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 )<save_to_csv>
submission_form.loc[leak_df.index, 'meter_reading'] = leak_df['meter_reading']
sub_df.to_csv("submission_rapids_ensemble_with_baggingregressor.csv", index=False )<compute_test_metric>
gc.collect()
<load_from_csv><EOS> 
submission_form.to_csv('submission.csv', index=False, float_format='%.4f' )