Kaggle competition: Histopathologic Cancer Detection
leak_score1 = 0
leak_df = pd.read_pickle(ucl_root/'site1.pkl')
 leak_df['meter_reading'] = leak_df.meter_reading_scraped
leak_df.drop(['meter_reading_scraped'], axis=1, inplace=True)
 leak_df.fillna(0, inplace=True)
 leak_df = leak_df[leak_df.timestamp.dt.year > 2016]
leak_df.loc[leak_df.meter_reading < 0, 'meter_reading'] = 0
for bid in leak_df.building_id.unique() :
temp_df = leak_df[(leak_df.building_id == bid)]
for m in temp_df.meter.unique() :
v0 = sample_submission.loc[(test_df.building_id == bid)&(test_df.meter==m), 'meter_reading'].values
v1 = temp_df[temp_df.meter==m].meter_reading.values
leak_score1 += mean_squared_error(np.log1p(v0), np.log1p(v1)) * len(v0)
 sample_submission.loc[(test_df.building_id == bid)&(test_df.meter==m), 'meter_reading'] = temp_df[temp_df.meter==m].meter_reading.values
leak_score1 /= len(leak_df )<feature_engineering>
from fastai.vision import *
leak_score2 = 0
leak_df = pd.read_csv('/kaggle/input/asu-buildings-energy-consumption/asu_2016-2018.csv')
 leak_df['timestamp'] = pd.to_datetime(leak_df['timestamp'])
 leak_df.fillna(0, inplace=True)
 leak_df.loc[leak_df.meter_reading < 0, 'meter_reading'] = 0
leak_df = leak_df[leak_df.timestamp.dt.year > 2016]
for bid in leak_df.building_id.unique() :
temp_df = leak_df[(leak_df.building_id == bid)]
for m in temp_df.meter.unique() :
v0 = sample_submission.loc[(test_df.building_id == bid)&(test_df.meter==m), 'meter_reading'].values
v1 = temp_df[temp_df.meter==m].meter_reading.values
leak_score2 += mean_squared_error(np.log1p(v0), np.log1p(v1)) * len(v0)
 sample_submission.loc[(test_df.building_id == bid)&(test_df.meter==m), 'meter_reading'] = temp_df[temp_df.meter==m].meter_reading.values
leak_score2 /= len(leak_df )<save_to_csv>
tfms = get_transforms(flip_vert=True, max_warp=0, max_zoom=0, p_affine=0, max_rotate=0)
 tfms[0][0].kwargs = {}
tfms[0][1] = dihedral()
tfms
if not debug:
sample_submission.to_csv('submission_ucf_replaced.csv', index=False, float_format='%.4f' )<compute_test_metric>
def data_gen(n_folds):
il = ImageItemList.from_csv(csv_name='train_labels.csv', path='input', folder='train', suffix='.tif')
 idxs = array(range(len(il)))
 np.random.shuffle(idxs)
 kfold = KFold(n_splits=n_folds, shuffle=True)
 for curr_fold in kfold.split(idxs):
val_idx = idxs[curr_fold[1]]
db_split =(il 
.split_by_idx(val_idx)
.label_from_df() 
.transform(tfms, size=96)
.add_test_folder('test')
.databunch(bs=64)
.normalize(imagenet_stats))
yield db_split
def get_output(db):
learn = create_cnn(db, models.resnet50, metrics=[error_rate])
 learn.fit_one_cycle(4, max_lr=1e-2)
 learn.unfreeze()
learn.fit_one_cycle(2, max_lr=slice(1e-5,1e-4))
probs, _ = learn.TTA(ds_type=DatasetType.Test)
 preds = probs[:,1]
return preds
print('UCF score = ', np.sqrt(leak_score0))
print('UCL score = ', np.sqrt(leak_score1))
print('ASU score = ', np.sqrt(leak_score2))<set_options>
n_folds = 4
sum_preds = None
gen = data_gen(n_folds)
 for db in gen:
preds = get_output(db)
 if sum_preds is None:
sum_preds = preds
else:
sum_preds += preds
py.init_notebook_mode(connected=True)
<set_options>
preds = sum_preds / n_folds
def reduce_mem_usage(df, use_float16=False):

 start_mem = df.memory_usage().sum() / 1024**2
print('Memory usage of dataframe is {:.2f} MB'.format(start_mem))
for col in df.columns:
if is_datetime(df[col])or is_categorical_dtype(df[col]):
continue
col_type = df[col].dtype
if col_type != object:
c_min = df[col].min()
c_max = df[col].max()
if str(col_type)[:3] == 'int':
if c_min > np.iinfo(np.int8 ).min and c_max < np.iinfo(np.int8 ).max:
df[col] = df[col].astype(np.int8)
 elif c_min > np.iinfo(np.int16 ).min and c_max < np.iinfo(np.int16 ).max:
df[col] = df[col].astype(np.int16)
 elif c_min > np.iinfo(np.int32 ).min and c_max < np.iinfo(np.int32 ).max:
df[col] = df[col].astype(np.int32)
 elif c_min > np.iinfo(np.int64 ).min and c_max < np.iinfo(np.int64 ).max:
df[col] = df[col].astype(np.int64)
 else:
if use_float16 and c_min > np.finfo(np.float16 ).min and c_max < np.finfo(np.float16 ).max:
df[col] = df[col].astype(np.float16)
 elif c_min > np.finfo(np.float32 ).min and c_max < np.finfo(np.float32 ).max:
df[col] = df[col].astype(np.float32)
 else:
df[col] = df[col].astype(np.float64)
 else:
df[col] = df[col].astype('category')
 end_mem = df.memory_usage().sum() / 1024**2
print('Memory usage after optimization is: {:.2f} MB'.format(end_mem))
print('Decreased by {:.1f}%'.format(100 *(start_mem - end_mem)/ start_mem))
return df<load_from_disk>
test_df = pd.read_csv('./input/sample_submission.csv')
 test_df['id'] = [i.stem for i in db.test_ds.items]
test_df['label'] = preds
<feature_engineering><EOS> 
test_df.to_csv('submission.csv', index=False )