Kaggle competition: Google Analytics Customer Revenue Prediction
class TrainGlobalConfig:
num_workers = 2
batch_size = 4
n_epochs = 14
lr = 0.0004
folder = 'plabel_model'
verbose = True
verbose_step = 1
step_scheduler = False
validation_scheduler = True
SchedulerClass = torch.optim.lr_scheduler.ReduceLROnPlateau
scheduler_params = dict(
 mode='min',
factor=0.5,
patience=1,
verbose=False,
threshold=0.0001,
threshold_mode='abs',
cooldown=0,
min_lr=1e-8,
eps=1e-08
)<data_type_conversions>
warnings.simplefilter('error', SettingWithCopyWarning)
 gc.enable()
%matplotlib inline
DATA_ROOT_PATH = '.. /input/global-wheat-detection/test'
class DatasetRetriever(Dataset):
def __init__(self, image_ids, transforms=None):
super().__init__()
self.image_ids = image_ids
self.transforms = transforms
def __getitem__(self, index: int):
image_id = self.image_ids[index]
image = cv2.imread(f'{DATA_ROOT_PATH}/{image_id}.jpg', cv2.IMREAD_COLOR)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB ).astype(np.float32)
 image /= 255.0
if self.transforms:
sample = {'image': image}
sample = self.transforms(**sample)
 image = sample['image']
return image, image_id
def __len__(self)-> int:
return self.image_ids.shape[0]<load_pretrained>
train = pd.read_csv('.. /input/create-extracted-json-fields-dataset/extracted_fields_train.gz',
dtype={'date': str, 'fullVisitorId': str, 'sessionId':str}, nrows=None)
 test = pd.read_csv('.. /input/create-extracted-json-fields-dataset/extracted_fields_test.gz',
dtype={'date': str, 'fullVisitorId': str, 'sessionId':str}, nrows=None)
 train.shape, test.shape
dataset = DatasetRetriever(
 image_ids=np.array([path.split('/')[-1][:-4] for path in glob(f'{DATA_ROOT_PATH}/*.jpg')]),
transforms=get_test_transforms() 
)
 def collate_fn(batch):
return tuple(zip(*batch))
data_loader = DataLoader(
 dataset,
batch_size=1,
shuffle=False,
num_workers=4,
drop_last=False,
collate_fn=collate_fn
)<load_pretrained>
y_reg = train['totals.transactionRevenue'].fillna(0)
 del train['totals.transactionRevenue']
if 'totals.transactionRevenue' in test.columns:
del test['totals.transactionRevenue']
def load_net(checkpoint_path):
config = get_efficientdet_config('tf_efficientdet_d5')
 net = EfficientDet(config, pretrained_backbone=False)
 config.num_classes = 1
config.image_size=512
net.class_net = HeadNet(config, num_outputs=config.num_classes, norm_kwargs=dict(eps=.001, momentum=.01))
checkpoint = torch.load(checkpoint_path)
 net.load_state_dict(checkpoint['model_state_dict'])
 del checkpoint
gc.collect()
net = DetBenchEval(net, config)
 net.eval() ;
return net.cuda()
net = load_net('/kaggle/input/google-weight/last-checkpoint.bin' )<categorify>
for df in [train, test]:
df['date'] = pd.to_datetime(df['visitStartTime'], unit='s')
 df['sess_date_dow'] = df['date'].dt.dayofweek
df['sess_date_hours'] = df['date'].dt.hour
df['sess_date_dom'] = df['date'].dt.day
class BaseWheatTTA:

 image_size = 512
def augment(self, image):
raise NotImplementedError
def batch_augment(self, images):
raise NotImplementedError
def deaugment_boxes(self, boxes):
raise NotImplementedError
class TTAHorizontalFlip(BaseWheatTTA):

 def augment(self, image):
return image.flip(1)
 def batch_augment(self, images):
return images.flip(2)
 def deaugment_boxes(self, boxes):
boxes[:, [1,3]] = self.image_size - boxes[:, [3,1]]
return boxes
class TTAVerticalFlip(BaseWheatTTA):

 def augment(self, image):
return image.flip(2)
 def batch_augment(self, images):
return images.flip(3)
 def deaugment_boxes(self, boxes):
boxes[:, [0,2]] = self.image_size - boxes[:, [2,0]]
return boxes
class TTARotate90(BaseWheatTTA):

 def augment(self, image):
return torch.rot90(image, 1,(1, 2))
def batch_augment(self, images):
return torch.rot90(images, 1,(2, 3))
def deaugment_boxes(self, boxes):
res_boxes = boxes.copy()
res_boxes[:, [0,2]] = self.image_size - boxes[:, [1,3]]
res_boxes[:, [1,3]] = boxes[:, [2,0]]
return res_boxes
class TTACompose(BaseWheatTTA):

 def __init__(self, transforms):
self.transforms = transforms
def augment(self, image):
for transform in self.transforms:
image = transform.augment(image)
 return image
def batch_augment(self, images):
for transform in self.transforms:
images = transform.batch_augment(images)
 return images
def prepare_boxes(self, boxes):
result_boxes = boxes.copy()
result_boxes[:,0] = np.min(boxes[:, [0,2]], axis=1)
 result_boxes[:,2] = np.max(boxes[:, [0,2]], axis=1)
 result_boxes[:,1] = np.min(boxes[:, [1,3]], axis=1)
 result_boxes[:,3] = np.max(boxes[:, [1,3]], axis=1)
 return result_boxes
def deaugment_boxes(self, boxes):
for transform in self.transforms[::-1]:
boxes = transform.deaugment_boxes(boxes)
 return self.prepare_boxes(boxes )<concatenate>
excluded_features = [
'date', 'fullVisitorId', 'sessionId', 'totals.transactionRevenue',
'visitId', 'visitStartTime'
]
categorical_features = [
_f for _f in train.columns
if(_f not in excluded_features)&(train[_f].dtype == 'object')
 ]
tta_transforms = []
for tta_combination in product([TTAHorizontalFlip() , None],
[TTAVerticalFlip() , None],
[TTARotate90() , None]):
tta_transforms.append(TTACompose([tta_transform for tta_transform in tta_combination if tta_transform])) 
<find_best_model_class>
for f in categorical_features:
train[f], indexer = pd.factorize(train[f])
 test[f] = indexer.get_indexer(test[f] )
def make_tta_predictions(images, score_threshold=0.25):
with torch.no_grad() :
images = torch.stack(images ).float().cuda()
predictions = []
for tta_transform in tta_transforms:
result = []
det = net(tta_transform.batch_augment(images.clone()), torch.tensor([1]*images.shape[0] ).float().cuda())
 for i in range(images.shape[0]):
boxes = det[i].detach().cpu().numpy() [:,:4]
scores = det[i].detach().cpu().numpy() [:,4]
indexes = np.where(scores > score_threshold)[0]
boxes = boxes[indexes]
boxes[:, 2] = boxes[:, 2] + boxes[:, 0]
boxes[:, 3] = boxes[:, 3] + boxes[:, 1]
boxes = tta_transform.deaugment_boxes(boxes.copy())
 result.append({
'boxes': boxes,
'scores': scores[indexes],
})
 predictions.append(result)
 return predictions
def run_wbf(predictions, image_index, image_size=512, iou_thr=0.44, skip_box_thr=0.43, weights=None):
boxes = [(prediction[image_index]['boxes']/(image_size-1)).tolist() for prediction in predictions]
scores = [prediction[image_index]['scores'].tolist() for prediction in predictions]
labels = [np.ones(prediction[image_index]['scores'].shape[0] ).astype(int ).tolist() for prediction in predictions]
boxes, scores, labels = ensemble_boxes.ensemble_boxes_wbf.weighted_boxes_fusion(boxes, scores, labels, weights=None, iou_thr=iou_thr, skip_box_thr=skip_box_thr)
 boxes = boxes*(image_size-1)
 return boxes, scores, labels<define_variables>
folds = get_folds(df=train, n_splits=5)
 train_features = [_f for _f in train.columns if _f not in excluded_features]
print(train_features)
 importances = pd.DataFrame()
oof_reg_preds = np.zeros(train.shape[0])
 sub_reg_preds = np.zeros(test.shape[0])
 for fold_,(trn_, val_)in enumerate(folds):
trn_x, trn_y = train[train_features].iloc[trn_], y_reg.iloc[trn_]
val_x, val_y = train[train_features].iloc[val_], y_reg.iloc[val_]
reg = lgb.LGBMRegressor(
 num_leaves=31,
learning_rate=0.03,
n_estimators=1000,
subsample=.9,
colsample_bytree=.9,
random_state=1 
)
 reg.fit(
 trn_x, np.log1p(trn_y),
eval_set=[(val_x, np.log1p(val_y)) ],
early_stopping_rounds=50,
verbose=100,
eval_metric='rmse' 
)
 imp_df = pd.DataFrame()
imp_df['feature'] = train_features
imp_df['gain'] = reg.booster_.feature_importance(importance_type='gain')
 imp_df['fold'] = fold_ + 1
importances = pd.concat([importances, imp_df], axis=0, sort=False)
 oof_reg_preds[val_] = reg.predict(val_x, num_iteration=reg.best_iteration_)
 oof_reg_preds[oof_reg_preds < 0] = 0
_preds = reg.predict(test[train_features], num_iteration=reg.best_iteration_)
 _preds[_preds < 0] = 0
sub_reg_preds += np.expm1(_preds)/ len(folds)
 mean_squared_error(np.log1p(y_reg), oof_reg_preds)**.5
def format_prediction_string(boxes, scores):
pred_strings = []
for j in zip(scores, boxes):
pred_strings.append("{0:.4f} {1} {2} {3} {4}".format(j[0], j[1][0], j[1][1], j[1][2], j[1][3]))
return " ".join(pred_strings )<categorify>
train['predictions'] = np.expm1(oof_reg_preds)
 test['predictions'] = sub_reg_preds
results_plabel = []
for images, image_ids in data_loader:
predictions = make_tta_predictions(images)
 for i, image in enumerate(images):
image_id = image_ids[i]
image_ = cv2.imread(f'{DATA_ROOT_PATH}/{image_id}.jpg', cv2.IMREAD_COLOR)
 h,w,_ = np.shape(image_)
 boxes, scores, labels = run_wbf(predictions, image_index=i)
 boxes =(boxes*2 ).astype(np.int32 ).clip(min=0, max=1023)
 image_id = image_ids[i]
boxes[:, 2] = boxes[:, 2] - boxes[:, 0]
boxes[:, 3] = boxes[:, 3] - boxes[:, 1]
for box in boxes:
result_p = {
'image_id': image_id,
'width':w,
'height':h,
'source':'usask_1',
'x':box[0],
'y':box[1],
'w':box[2],
'h':box[3],
}
results_plabel.append(result_p )<prepare_output>
trn_data = train[train_features + ['fullVisitorId']].groupby('fullVisitorId' ).mean()
results_df = pd.DataFrame(results_plabel, columns=['image_id', 'width','height','source','x','y','w','h'])
 results_df.head()<feature_engineering>
%%time
trn_pred_list = train[['fullVisitorId', 'predictions']].groupby('fullVisitorId')\ 
.apply(lambda df: list(df.predictions)) \ 
.apply(lambda x: {'pred_'+str(i): pred for i, pred in enumerate(x)} )
results_df['image_id'] = results_df['image_id'].apply(lambda x: DATA_ROOT_PATH+'/'+ x+'.jpg' )<feature_engineering>
trn_all_predictions = pd.DataFrame(list(trn_pred_list.values), index=trn_data.index)
 trn_feats = trn_all_predictions.columns
trn_all_predictions['t_mean'] = np.log1p(trn_all_predictions[trn_feats].mean(axis=1))
trn_all_predictions['t_median'] = np.log1p(trn_all_predictions[trn_feats].median(axis=1))
trn_all_predictions['t_sum_log'] = np.log1p(trn_all_predictions[trn_feats] ).sum(axis=1)
 trn_all_predictions['t_sum_act'] = np.log1p(trn_all_predictions[trn_feats].fillna(0 ).sum(axis=1))
trn_all_predictions['t_nb_sess'] = trn_all_predictions[trn_feats].isnull().sum(axis=1)
 full_data = pd.concat([trn_data, trn_all_predictions], axis=1)
 del trn_data, trn_all_predictions
gc.collect()
full_data.shape
TRAIN_ROOT_PATH = '.. /input/global-wheat-detection/train'
marking['image_id'] = marking['image_id'].apply(lambda x: TRAIN_ROOT_PATH+'/'+ x+'.jpg' )<concatenate>
%%time
sub_pred_list = test[['fullVisitorId', 'predictions']].groupby('fullVisitorId')\ 
.apply(lambda df: list(df.predictions)) \ 
.apply(lambda x: {'pred_'+str(i): pred for i, pred in enumerate(x)} )
train_data_plabel = pd.concat([results_df,marking], axis=0 )<data_type_conversions>
sub_data = test[train_features + ['fullVisitorId']].groupby('fullVisitorId' ).mean()
sub_all_predictions = pd.DataFrame(list(sub_pred_list.values), index=sub_data.index)
 for f in trn_feats:
if f not in sub_all_predictions.columns:
sub_all_predictions[f] = np.nan
sub_all_predictions['t_mean'] = np.log1p(sub_all_predictions[trn_feats].mean(axis=1))
sub_all_predictions['t_median'] = np.log1p(sub_all_predictions[trn_feats].median(axis=1))
sub_all_predictions['t_sum_log'] = np.log1p(sub_all_predictions[trn_feats] ).sum(axis=1)
 sub_all_predictions['t_sum_act'] = np.log1p(sub_all_predictions[trn_feats].fillna(0 ).sum(axis=1))
sub_all_predictions['t_nb_sess'] = sub_all_predictions[trn_feats].isnull().sum(axis=1)
 sub_full_data = pd.concat([sub_data, sub_all_predictions], axis=1)
 del sub_data, sub_all_predictions
gc.collect()
sub_full_data.shape
skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
 df_folds = train_data_plabel[['image_id']].copy()
df_folds.loc[:, 'bbox_count'] = 1
df_folds = df_folds.groupby('image_id' ).count()
df_folds.loc[:, 'source'] = marking[['image_id', 'source']].groupby('image_id' ).min() ['source']
df_folds.loc[:, 'stratify_group'] = np.char.add(
 df_folds['source'].values.astype(str),
df_folds['bbox_count'].apply(lambda x: f'_{x // 15}' ).values.astype(str)
)
 df_folds.loc[:, 'fold'] = 0
for fold_number,(train_index, val_index)in enumerate(skf.split(X=df_folds.index, y=df_folds['stratify_group'])) :
df_folds.loc[df_folds.iloc[val_index].index, 'fold'] = fold_number<import_modules>
train['target'] = y_reg
trn_user_target = train[['fullVisitorId', 'target']].groupby('fullVisitorId' ).sum()
class DatasetRetriever(Dataset):
def __init__(self, marking, image_ids, transforms=None, test=False):
super().__init__()
self.image_ids = image_ids
self.marking = marking
self.transforms = transforms
self.test = test
def __getitem__(self, index: int):
image_id = self.image_ids[index]
if self.test or random.random() > 0:
image, boxes = self.load_image_and_boxes(index)
 else:
image, boxes = self.load_cutmix_image_and_boxes(index)
 labels = torch.ones(( boxes.shape[0],), dtype=torch.int64)
 target = {}
target['boxes'] = boxes
target['labels'] = labels
target['image_id'] = torch.tensor([index])
 if self.transforms:
for i in range(10):
sample = self.transforms(**{
'image': image,
'bboxes': target['boxes'],
'labels': labels
})
 if len(sample['bboxes'])> 0:
image = sample['image']
target['boxes'] = torch.stack(tuple(map(torch.tensor, zip(*sample['bboxes'])))).permute(1, 0)
 target['boxes'][:,[0,1,2,3]] = target['boxes'][:,[1,0,3,2]]
break
return image, target, image_id
def __len__(self)-> int:
return self.image_ids.shape[0]
def load_image_and_boxes(self, index):
image_id = self.image_ids[index]
image = cv2.imread(image_id, cv2.IMREAD_COLOR)
 image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB ).astype(np.float32)
 image /= 255.0
records = self.marking[self.marking['image_id'] == image_id]
boxes = records[['x', 'y', 'w', 'h']].values
boxes[:, 2] = boxes[:, 0] + boxes[:, 2]
boxes[:, 3] = boxes[:, 1] + boxes[:, 3]
return image, boxes
def load_cutmix_image_and_boxes(self, index, imsize=1024):

 w, h = imsize, imsize
s = imsize // 2
xc, yc = [int(random.uniform(imsize * 0.25, imsize * 0.75)) for _ in range(2)]
indexes = [index] + [random.randint(0, self.image_ids.shape[0] - 1)for _ in range(3)]
result_image = np.full(( imsize, imsize, 3), 1, dtype=np.float32)
 result_boxes = []
for i, index in enumerate(indexes):
image, boxes = self.load_image_and_boxes(index)
 if i == 0:
x1a, y1a, x2a, y2a = max(xc - w, 0), max(yc - h, 0), xc, yc
x1b, y1b, x2b, y2b = w -(x2a - x1a), h -(y2a - y1a), w, h
elif i == 1:
x1a, y1a, x2a, y2a = xc, max(yc - h, 0), min(xc + w, s * 2), yc
x1b, y1b, x2b, y2b = 0, h -(y2a - y1a), min(w, x2a - x1a), h
elif i == 2:
x1a, y1a, x2a, y2a = max(xc - w, 0), yc, xc, min(s * 2, yc + h)
 x1b, y1b, x2b, y2b = w -(x2a - x1a), 0, max(xc, w), min(y2a - y1a, h)
 elif i == 3:
x1a, y1a, x2a, y2a = xc, yc, min(xc + w, s * 2), min(s * 2, yc + h)
 x1b, y1b, x2b, y2b = 0, 0, min(w, x2a - x1a), min(y2a - y1a, h)
 result_image[y1a:y2a, x1a:x2a] = image[y1b:y2b, x1b:x2b]
padw = x1a - x1b
padh = y1a - y1b
boxes[:, 0] += padw
boxes[:, 1] += padh
boxes[:, 2] += padw
boxes[:, 3] += padh
result_boxes.append(boxes)
 result_boxes = np.concatenate(result_boxes, 0)
 np.clip(result_boxes[:, 0:], 0, 2 * s, out=result_boxes[:, 0:])
 result_boxes = result_boxes.astype(np.int32)
 result_boxes = result_boxes[np.where(( result_boxes[:,2]-result_boxes[:,0])*(result_boxes[:,3]-result_boxes[:,1])> 0)]
return result_image, result_boxes<create_dataframe>
df = pd.concat([full_data,sub_full_data],sort=False)
 df = df.reset_index(drop=False)
 for c in df.columns[1:]:
if(( df[c].min() >=0)&(df[c].max() >=10)) :
df[c] = np.log1p(df[c])
 elif(( df[c].min() <0)&(( df[c].max() -df[c].min())>=10)) :
df.loc[df[c]!=0,c] = np.sign(df.loc[df[c]!=0,c])*np.log(np.abs(df.loc[df[c]!=0,c]))
for c in df.columns[1:]:
ss = StandardScaler()
df.loc[~np.isfinite(df[c]),c] = np.nan
df.loc[~df[c].isnull() ,c] = ss.fit_transform(df.loc[~df[c].isnull() ,c].values.reshape(-1,1))
df.fillna(-99999,inplace=True)
 gp_trn_users = df[:full_data.shape[0]].copy().set_index('fullVisitorId')
 gp_trn_users['target'] = np.log1p(trn_user_target['target'].values)
 gp_trn_users.target /= gp_trn_users.target.max()
gp_sub_users = df[full_data.shape[0]:].copy().set_index('fullVisitorId')
 newcols = [x.replace('.','_')for x in gp_trn_users.columns]
gp_trn_users.columns = newcols
newcols = [x.replace('.','_')for x in gp_sub_users.columns]
gp_sub_users.columns = newcols
fold_number = 0
train_dataset = DatasetRetriever(
 image_ids=df_folds.index.values,
marking=train_data_plabel,
transforms=get_train_transforms() ,
test=False, 
)
 validation_dataset = DatasetRetriever(
 image_ids=df_folds[df_folds['fold'] == fold_number].index.values,
marking=train_data_plabel,
transforms=get_valid_transforms() ,
test=True,
)<load_pretrained>
def GP1(data):
return(((( data["t_mean"])+(data["totals_hits"])))+ 
(np.maximum(((((( data["pred_2"])+(data["pred_2"])) /2.0))),(( -3.0)))) + 
(np.maximum(((data["t_mean"])) ,(( -1.0)))) + 
(np.minimum(((1.0)) ,(((4.81583929061889648)))))+ 
(np.maximum(((-2.0)) ,(( data["t_sum_act"])))) + 
(((1.0)*(data["geoNetwork_metro"])))+ 
(np.maximum(((((( data["pred_0"])+(((( data["t_median"])+(np.maximum(((np.maximum(((data["t_sum_log"])) ,(( data["pred_0"]))))),(( data["t_mean"])))))/2.0)))/2.0))),(( data["t_sum_log"])))) + 
(((data["t_sum_log"])+(np.minimum(((data["t_mean"])) ,(( data["t_sum_log"])))))) + 
(((((( -1.0)-(-2.0)))+(data["t_sum_act"])) /2.0)) + 
(((( 0.0)>(((( data["trafficSource_referralPath"])<(data["trafficSource_referralPath"])) *1.))) *1.))+ 
(((data["t_mean"])+(1.0)))+ 
(((data["t_sum_log"])+(data["t_median"])))+ 
(((data["t_sum_act"])-(0.0)))+ 
(np.maximum(((data["pred_3"])) ,(((( 3.0)*(data["pred_0"])))))) + 
(((( data["pred_2"])+(-2.0)) /2.0)) + 
(((data["t_sum_log"])*(data["t_mean"])))+ 
(((data["t_sum_act"])+(data["t_sum_log"])))+ 
(((((( data["totals_hits"])-(3.0)))>(data["geoNetwork_metro"])) *1.))+ 
(((3.0)*(data["t_sum_log"])))+ 
(((( data["geoNetwork_continent"])>(-1.0)) *1.))+ 
(((( data["geoNetwork_subContinent"])<(((data["device_browser"])+(-2.0)))) *1.))+ 
(np.minimum(((np.minimum(((2.0)) ,(( data["geoNetwork_metro"]))))),(( data["geoNetwork_metro"])))) + 
(np.minimum(((1.0)) ,(( np.tanh(((( 2.0)* 2.0)))))))+ 
(((( data["geoNetwork_subContinent"])>(-3.0)) *1.))+ 
(((data["t_sum_log"])-(-1.0)))+ 
(np.minimum(((-2.0)) ,(( data["trafficSource_referralPath"])))) + 
(np.maximum(((data["t_sum_log"])) ,(( -3.0)))) + 
(np.minimum(((np.where(((( data["pred_0"])>(data["trafficSource_referralPath"])) *1.) <0, data["totals_hits"], -3.0))),(((( data["geoNetwork_subContinent"])/ 2.0)))))+ 
(((( 2.0)+(data["pred_1"])) /2.0)) + 
(np.tanh(( np.tanh(( 2.0)))))+ 
(np.tanh(((-1.0*(( -3.0)))))) + 
(((-2.0)+(data["t_sum_log"])))+ 
(((( data["geoNetwork_country"])<(data["geoNetwork_metro"])) *1.))+ 
(((( 3.0)+(( -1.0*(( -2.0)))))/2.0)) + 
(((((((data["t_sum_log"])*(data["geoNetwork_continent"])))*(((data["t_sum_log"])-(data["visitNumber"])))))*(np.minimum(((data["t_sum_act"])) ,(( data["t_sum_log"])))))) + 
(((((( data["totals_hits"])/ 2.0)) +(data["t_sum_log"])) /2.0)) + 
(((((np.tanh(( data["pred_1"])))+(data["t_sum_act"])))+(((data["t_sum_act"])+(((data["t_sum_log"])+(-2.0)))))))+ 
(((2.0)-(np.where(data["t_mean"]>0, data["t_nb_sess"],(((data["t_mean"])<(data["geoNetwork_continent"])) *1.))))) + 
(np.where(data["t_mean"]>0, data["t_sum_log"], -2.0)) + 
(((( data["t_mean"])+(((( data["geoNetwork_subContinent"])+(((np.tanh(((12.19428730010986328)))) * 2.0)))/2.0)))/2.0)) + 
(np.where(((((((data["pred_0"])* 2.0)) +(data["visitNumber"])))/ 2.0)< -99998,(((-1.0)>(2.0)) *1.) , np.tanh(( data["totals_hits"])))) + 
(np.where(((( -1.0)>(1.0)) *1.) <0,(((( 1.0)) +(-1.0)) /2.0), data["t_sum_act"])) + 
(((( -1.0*(((((((((( data["t_median"])+(((data["t_sum_log"])+(data["t_nb_sess"])))) /2.0)) +(data["geoNetwork_continent"])) /2.0)) +(data["t_sum_log"])))))) / 2.0)) + 
(np.minimum(((np.minimum(((np.where(data["trafficSource_referralPath"]<0,(((data["trafficSource_referralPath"])+(np.tanh(( data["t_sum_log"])))) /2.0), -3.0))),(( data["geoNetwork_country"]))))),(( data["t_sum_log"])))) + 
(((((( data["t_sum_log"])+(np.where(data["visitNumber"]<0, -2.0, data["pred_1"])))/2.0)) +(data["t_sum_act"])))+ 
(((((np.where(data["geoNetwork_country"]>0, data["t_sum_act"], data["geoNetwork_country"])) +(((data["geoNetwork_country"])/ 2.0)))) +(-1.0)))+ 
(np.where(data["t_mean"]<0, -2.0, np.where(((data["geoNetwork_metro"])+(data["geoNetwork_continent"])) <0, -2.0,(( data["geoNetwork_continent"])+(data["geoNetwork_continent"])))))+ 
(((((( data["geoNetwork_continent"])<(data["t_sum_act"])) *1.))+(((np.minimum(((((((data["totals_hits"])/ 2.0)) / 2.0))),(( data["pred_0"])))) +(data["t_sum_log"])))))+ 
(((data["t_sum_act"])+(((-2.0)-(np.minimum(((data["geoNetwork_metro"])) ,(((( data["geoNetwork_metro"])*(np.minimum(((data["t_sum_act"])) ,(( data["t_mean"])))))))))))))+ 
(np.where(-3.0<0,(( np.where(2.0 < -99998, data["t_sum_log"],(((0.0)<(data["totals_pageviews"])) *1.))) * 2.0),(6.0)))+ 
(((( data["t_sum_log"])+(((((( -1.0)-(((data["visitNumber"])+(data["t_mean"])))))+(np.tanh(( data["t_sum_log"])))) /2.0)))/2.0)) + 
(((((( 4.0)) *(np.where(((( data["totals_pageviews"])>(data["totals_hits"])) *1.) >0, data["t_sum_act"], -3.0)))) * 2.0)) + 
(np.minimum(((((data["t_sum_log"])+(( -1.0*(( data["t_nb_sess"]))))))),(( np.where(np.tanh(( data["t_mean"])) >0,(-1.0*(( data["t_nb_sess"]))), -3.0)))))+ 
(((((((-3.0)+(((( 11.04013633728027344)) *(data["t_sum_log"])))))+(((( -3.0)+(data["t_sum_log"])) /2.0)))) * 2.0)) + 
(np.minimum(((np.tanh(( np.tanh(( data["totals_hits"])))))) ,(( data["pred_1"])))) + 
(((( np.minimum(((np.tanh(((((( data["pred_0"])* 2.0)) / 2.0))))),(((4.91354322433471680)))))+(-2.0)) /2.0)) + 
(((-3.0)+(((data["t_sum_act"])*(((( 13.85095119476318359)) -(-2.0)))))))+ 
(((((((((data["t_sum_log"])* 2.0)) * 2.0)) * 2.0)) * 2.0)) + 
(((((( np.maximum(((np.minimum(((data["t_median"])) ,(( data["geoNetwork_metro"]))))),(((((data["t_mean"])>(data["t_sum_log"])) *1.))))) +(-3.0)) /2.0)) +(data["t_mean"])))+ 
(np.minimum(((((1.0)* 2.0))),(((((-2.0)+(((0.0)+(data["trafficSource_referralPath"])))) /2.0)))))+ 
(((np.minimum(((data["t_sum_log"])) ,(( 0.0)))) +(np.minimum(((((( data["t_mean"])+(data["totals_pageviews"])) /2.0))),(( data["t_mean"])))))) + 
(((( 3.0)>(data["t_sum_act"])) *1.))+ 
(((( data["trafficSource_referralPath"])<(3.0)) *1.))+ 
(((0.0)+(-1.0)))+ 
(np.minimum(((data["geoNetwork_metro"])) ,(((13.91542816162109375)))))+ 
(((data["t_sum_act"])+(((-3.0)+(((data["t_nb_sess"])+(((data["t_sum_act"])+(data["t_sum_act"])))))))))+ 
(( -1.0*(((((data["t_nb_sess"])+(((((data["pred_1"])*(((((((data["t_mean"])>(data["t_sum_log"])) *1.))<(-1.0)) *1.))))* 2.0)))/2.0)))))+ 
(((data["t_median"])+(((((((data["t_median"])+(((data["totals_pageviews"])+(-1.0)))))-(data["trafficSource_referralPath"])))+(-1.0)))))+ 
(((((0.0)+(((((((data["t_mean"])<(data["trafficSource_referralPath"])) *1.))>(1.0)) *1.))))*(-3.0)))+ 
(((data["totals_pageviews"])-(((( data["visitNumber"])+(((((data["totals_hits"])-(( -1.0*(( data["t_nb_sess"])))))) * 2.0)))/2.0)))) + 
(((((((data["geoNetwork_metro"])-(((((10.93749332427978516)) >(((data["t_sum_log"])* 2.0)))*1.))))-(np.tanh(( data["geoNetwork_metro"])))))*(data["geoNetwork_metro"])))+ 
(((data["t_nb_sess"])*(data["channelGrouping"])))+ 
(((-2.0)+(0.0)))+ 
(((( -1.0)>(1.0)) *1.))+ 
(((((-3.0)+(0.0)))+(-3.0)))+ 
(((((data["totals_hits"])-(np.where(data["pred_1"] < -99998, 1.0, data["totals_pageviews"])))) +(((data["totals_hits"])-(data["totals_pageviews"])))))+ 
(((((data["t_mean"])*(((( data["geoNetwork_continent"])+(( -1.0*(( data["geoNetwork_subContinent"])))))/2.0)))) *(2.0)))+ 
(np.maximum(((data["t_sum_act"])) ,(( data["visitNumber"])))) + 
(((( data["totals_hits"])<(3.0)) *1.))+ 
(np.minimum(((((( data["t_median"])+(data["visitNumber"])) /2.0))),(( np.minimum(((data["t_sum_act"])) ,(( data["totals_hits"])))))))+ 
(np.maximum(((data["totals_hits"])) ,(((( data["geoNetwork_continent"])/ 2.0)))))+ 
(np.maximum(((-3.0)) ,(( np.maximum(((data["geoNetwork_continent"])) ,(( data["t_nb_sess"])))))))+ 
(((( 2.0)>(data["visitNumber"])) *1.))+ 
(((((( data["channelGrouping"])+(data["geoNetwork_continent"])))<(((( data["geoNetwork_continent"])>(((data["t_nb_sess"])-(data["channelGrouping"])))) *1.))) *1.))+ 
(((( -2.0)>(np.maximum(((((data["t_sum_log"])-(( 2.97587943077087402))))),(((((data["pred_0"])+(( 3.84451842308044434)))/2.0)))))) *1.))+ 
(np.minimum(((np.minimum(((data["geoNetwork_country"])) ,(((((((( 1.0)) -(data["geoNetwork_country"])))+(((( 1.0)) -(data["geoNetwork_subContinent"])))) /2.0)))))) ,(( data["t_sum_act"])))) + 
(((((((10.0)) *(data["t_nb_sess"])))>(np.minimum(((( 3.0))),(((( data["geoNetwork_continent"])*(( 0.13566258549690247)))))))) *1.))+ 
(((( data["geoNetwork_continent"])>(( 3.89537310600280762)))*1.))+ 
(np.minimum(((data["t_mean"])) ,(( 2.0)))) + 
(np.minimum(((np.minimum(((-3.0)) ,(((( data["t_sum_log"])-(data["t_median"]))))))),(((( data["t_sum_log"])+(data["t_sum_act"])))))) + 
(((data["totals_pageviews"])-(data["t_mean"])))+ 
(np.maximum(((1.0)) ,(( data["t_sum_act"])))) + 
(np.where(0.0<0, data["geoNetwork_metro"], data["visitNumber"])) + 
(((data["visitNumber"])*(data["geoNetwork_continent"])))+ 
(((( 0.0)<(data["geoNetwork_subContinent"])) *1.))+ 
(np.minimum(((((( -3.0)+(((( data["t_sum_log"])+(data["geoNetwork_subContinent"])) /2.0)))/2.0))),(((( data["t_sum_act"])-(np.minimum(((data["t_sum_log"])) ,(( data["t_mean"])))))))))+ 
(np.minimum(((3.0)) ,(( data["geoNetwork_metro"])))) + 
(((-3.0)*(((data["t_median"])*(-1.0)))))+ 
(((2.0)*(((( 9.0)) *(( 9.0)))))) + 
(np.minimum(((np.minimum(((data["t_median"])) ,(( data["t_sum_log"]))))),(((((((data["totals_hits"])-(data["visitNumber"])))+(((data["totals_hits"])+(data["totals_hits"])))) /2.0))))))
def GP2(data):
return(((((( np.tanh(( data["totals_hits"])))+(data["pred_1"])))* 2.0)) + 
(((( 8.08461284637451172)) -(data["t_sum_log"])))+ 
(((( np.tanh(( data["t_sum_log"])))+(((data["t_sum_log"])+(data["t_mean"])))) /2.0)) + 
(((((data["t_sum_log"])+(0.0)))+(data["t_sum_act"])))+ 
(np.minimum(((data["t_nb_sess"])) ,(((((-2.0)+(3.0)) /2.0)))))+ 
(((data["geoNetwork_continent"])+(data["t_sum_log"])))+ 
(np.minimum(((data["t_sum_act"])) ,(( np.maximum(((( 5.0))),(( data["t_sum_act"])))))))+ 
(((data["totals_hits"])*(((data["t_sum_act"])-(( 8.14568805694580078)))))) + 
(((data["t_sum_act"])+(data["pred_3"])))+ 
(( -1.0*(((-1.0*(( data["t_sum_act"])))))))+ 
(np.maximum(((((3.0)-(-2.0)))) ,(( 3.0)))) + 
(np.maximum(((-2.0)) ,(( data["t_sum_log"])))) + 
(((((data["t_sum_act"])* 2.0)) *(( 1.0)))) + 
(((data["t_sum_log"])+(data["t_sum_log"])))+ 
(((data["pred_1"])-(( 5.53632402420043945)))) + 
(((( data["t_sum_log"])<(data["geoNetwork_metro"])) *1.))+ 
(((data["pred_1"])*(1.0)))+ 
(np.maximum(((data["totals_pageviews"])) ,(((-1.0*(( np.tanh(( 0.0)))))))))+ 
(((((((0.0)) *(-3.0)))>(-1.0)) *1.))+ 
(np.maximum(((data["totals_pageviews"])) ,(( -2.0)))) + 
(((data["t_sum_log"])*(data["t_sum_log"])))+ 
(np.minimum(((3.0)) ,(( data["t_sum_log"])))) + 
(((np.maximum(((data["visitNumber"])) ,(( 3.0)))) -(data["t_mean"])))+ 
(((data["t_sum_log"])-(( 14.92182254791259766)))) + 
(np.maximum(((data["t_median"])) ,(( 3.0)))) + 
(((( 3.0)>(data["t_mean"])) *1.))+ 
(((((data["geoNetwork_subContinent"])* 2.0)) * 2.0)) + 
(((np.where(1.0<0, np.tanh(( data["pred_1"])) , data["t_nb_sess"])) *(((data["t_sum_act"])* 2.0)))) + 
(((data["t_sum_act"])*(data["t_sum_act"])))+ 
(((-3.0)+(data["t_sum_log"])))+ 
(((( 9.0)) *(((-2.0)+(data["t_sum_act"])))))+ 
(((((((7.23600816726684570)) *(data["geoNetwork_continent"])))>(( 1.63156664371490479)))*1.))+ 
(((( data["t_sum_log"])<(1.0)) *1.))+ 
(((data["t_sum_act"])*(((((((( 7.0)) -(data["t_sum_act"])))*(((((7.0)) >(data["t_sum_act"])) *1.))))*(data["t_sum_act"])))))+ 
(((((((3.0)>(data["pred_0"])) *1.))>(((np.maximum(((np.minimum(((2.0)) ,(( data["visitNumber"]))))),(((( data["t_median"])* 2.0)))))/ 2.0)))*1.))+ 
(((((2.0)*(data["geoNetwork_subContinent"])))+(((((data["t_sum_act"])+(((data["t_sum_act"])-(data["pred_0"])))))*(data["t_sum_act"])))))+ 
(np.minimum(((((( data["t_sum_act"])<(data["pred_1"])) *1.))) ,(( data["geoNetwork_metro"])))) + 
(((data["geoNetwork_continent"])+(data["t_mean"])))+ 
(np.maximum(((((((( data["totals_hits"])* 2.0)) +(-2.0)) /2.0))),(((( data["visitNumber"])+(1.0)))))) + 
(((((( data["t_sum_act"])+(((( data["t_mean"])+(data["pred_1"])) /2.0)))/2.0)) +(((((( data["pred_1"])+(data["t_sum_act"])) /2.0)) -(data["visitNumber"])))))+ 
(np.minimum(((((( 10.0)) -(data["t_sum_log"])))) ,(((((((( data["geoNetwork_continent"])* 2.0)) *(data["t_sum_act"])))*(data["geoNetwork_continent"])))))) + 
(np.minimum(((data["geoNetwork_subContinent"])) ,(((((((((data["geoNetwork_subContinent"])+(-3.0)))-(2.0)))+(data["t_sum_log"])) /2.0)))))+ 
(((((((data["t_sum_log"])/ 2.0)) +(data["t_nb_sess"])))+(((((-3.0)+(data["t_sum_act"])))+(data["t_sum_act"])))))+ 
(((( np.tanh(((((( np.tanh(( np.tanh(( np.minimum(((data["geoNetwork_country"])) ,(( data["geoNetwork_country"])))))))) * 2.0)) * 2.0)))) +(( 7.0)))/2.0)) + 
(np.where(np.minimum(((2.0)) ,(((((-1.0)>(-2.0)) *1.))))>0, data["geoNetwork_subContinent"],(-1.0*(((5.0)))))) + 
(((data["geoNetwork_metro"])+(np.where(data["geoNetwork_metro"]>0, data["geoNetwork_metro"], data["t_sum_act"])))) + 
(((((( 6.0)) / 2.0)) -(((((( -3.0)+(data["visitNumber"])))<(np.tanh(( data["totals_pageviews"])))) *1.))))+ 
(((data["t_nb_sess"])+(((data["t_sum_log"])+(((np.where(data["t_sum_log"] < -99998, data["t_mean"], data["t_sum_log"])) -(3.0)))))))+ 
(((( data["t_median"])+(((np.maximum(((((( data["geoNetwork_metro"])+(-1.0)) /2.0))),(( -2.0)))) *(data["t_sum_act"])))) /2.0)) + 
(((((( data["t_median"])*(data["t_sum_act"])))+(np.tanh(((( np.minimum(((-1.0)) ,(( 2.0)))) +(data["t_sum_act"])))))) /2.0)) + 
(np.maximum(((data["visitNumber"])) ,(( np.minimum(((data["totals_hits"])) ,(( data["t_mean"])))))))+ 
(np.where(data["t_median"]<0,(( data["t_median"])* 2.0),(( data["t_sum_act"])* 2.0)))+ 
(((((( np.minimum(((np.minimum(((3.0)) ,(( data["totals_pageviews"]))))),(( data["t_mean"])))) +(data["totals_pageviews"])) /2.0)) * 2.0)) + 
(np.maximum(((data["totals_hits"])) ,(( np.maximum(((((((( data["totals_pageviews"])+(data["t_mean"])))+(data["t_sum_log"])) /2.0))),(( data["t_sum_act"])))))))+ 
(((((np.where(data["t_sum_log"]<0, data["pred_1"], data["geoNetwork_metro"])) +(np.tanh(( np.tanh(( data["t_sum_log"])))))))* 2.0)) + 
(((np.minimum(((((data["geoNetwork_metro"])-(data["channelGrouping"])))) ,(((((13.51332950592041016)) *(data["t_sum_log"])))))) * 2.0)) + 
(((( -1.0*(( data["geoNetwork_country"])))) / 2.0)) + 
(((( np.tanh(((( data["pred_1"])+(((( 1.0)>(data["channelGrouping"])) *1.))))))>(((( data["t_sum_act"])+(data["t_nb_sess"])) /2.0)))*1.))+ 
(((data["t_sum_log"])+(((data["t_sum_log"])+(((data["t_sum_log"])+(((data["t_sum_act"])+(data["t_sum_act"])))))))))+ 
(((((data["totals_pageviews"])+(((data["totals_pageviews"])-(3.0)))))+(((data["t_sum_log"])-(data["t_sum_act"])))))+ 
(((data["trafficSource_referralPath"])-(np.maximum(((( 2.04302835464477539))),(( 0.0)))))) + 
(((( data["geoNetwork_continent"])+(data["totals_pageviews"])) /2.0)) + 
(np.minimum(((( 12.72106933593750000))),(((( data["geoNetwork_continent"])-(data["device_browser"])))))) + 
(((np.minimum(((((data["t_sum_log"])*(( 4.04873085021972656))))),(((( data["totals_pageviews"])-(np.minimum(((data["totals_hits"])) ,(( data["totals_hits"])))))))))*(( 4.04873085021972656)))) + 
(((3.0)+(data["geoNetwork_metro"])))+ 
(((( -2.0)<(np.minimum(((( 4.73788738250732422))),(( data["t_median"])))))*1.))+ 
(np.minimum(((-1.0)) ,(( np.minimum(((0.0)) ,(( 3.0)))))))+ 
(((np.maximum(((( 0.0))),(( -3.0)))) *(((((((2.0)>(data["t_median"])) *1.))>(0.0)) *1.))))+ 
(np.minimum(((((( data["geoNetwork_metro"])>(( 4.0)))*1.))) ,(( np.minimum(((data["t_mean"])) ,(( data["t_sum_log"])))))))+ 
(((np.minimum(((((( 6.11185455322265625)) *(((np.tanh(( data["t_median"])))-(( -1.0*(( data["t_sum_act"]))))))))),(( data["t_sum_log"])))) +(data["totals_hits"])))+ 
(((( np.minimum(((data["t_nb_sess"])) ,(( -3.0)))) >(1.0)) *1.))+ 
(((((((np.tanh(((-1.0*(( data["pred_1"])))))) +(data["trafficSource_referralPath"])) /2.0)) <(0.0)) *1.))+ 
(((data["t_sum_act"])*(((( data["t_sum_act"])<(( 7.0)))*1.))))+ 
(((data["t_sum_log"])+(np.minimum(((data["t_mean"])) ,(((( data["channelGrouping"])*(data["t_mean"])))))))) + 
(np.maximum(((((( data["totals_hits"])>(0.0)) *1.))) ,(( data["totals_pageviews"])))) + 
(((( data["geoNetwork_continent"])>(((data["device_browser"])+(((data["trafficSource_referralPath"])*(((( data["pred_0"])>(data["t_nb_sess"])) *1.))))))) *1.))+ 
(np.where(data["t_mean"]<0, data["totals_pageviews"], data["geoNetwork_metro"])) + 
(((data["totals_pageviews"])-(np.maximum(((-1.0)) ,(( data["totals_hits"])))))) + 
(((( data["t_mean"])<(data["totals_pageviews"])) *1.))+ 
(((( 1.0)) +(((( data["t_median"])+(1.0)) /2.0)))) + 
(((( np.minimum(((((data["pred_0"])*(((np.minimum(((data["t_median"])) ,(((-1.0*(( data["device_browser"])))))))* 2.0))))),(( -2.0)))) +(data["totals_hits"])) /2.0)) + 
(np.minimum(((data["pred_0"])) ,(( data["t_median"])))) + 
(np.minimum(((((data["t_sum_act"])*(np.minimum(((((data["geoNetwork_subContinent"])-(data["geoNetwork_country"])))) ,(( data["geoNetwork_subContinent"]))))))),(((( data["geoNetwork_country"])-(data["trafficSource_referralPath"])))))) + 
(((3.0)-(data["geoNetwork_metro"])))+ 
(((np.maximum(((((data["pred_0"])*(data["pred_0"])))) ,(( data["channelGrouping"])))) *(((data["t_nb_sess"])*(data["t_nb_sess"])))))+ 
(((( -2.0)>(((( 0.0)<(data["t_median"])) *1.))) *1.))+ 
(np.maximum(((np.maximum(((data["trafficSource_referralPath"])) ,(( np.maximum(((((data["geoNetwork_subContinent"])+(((data["t_nb_sess"])+(data["geoNetwork_continent"])))))) ,(( data["channelGrouping"])))))))) ,(( data["trafficSource_referralPath"])))) + 
(np.minimum(((np.maximum(((data["trafficSource_referralPath"])) ,(( data["t_nb_sess"]))))),(((((((data["t_sum_log"])+(data["t_mean"])) /2.0)) +(data["t_sum_act"])))))) + 
(((((( data["totals_pageviews"])+(data["geoNetwork_city"])))<(2.0)) *1.))+ 
(np.minimum(((data["t_sum_act"])) ,(( 1.0)))) + 
(((-1.0)-(3.0)))+ 
(((((((((( 12.32176685333251953)) >(((( data["geoNetwork_metro"])+(data["geoNetwork_metro"])) /2.0)))*1.))+(data["channelGrouping"])) /2.0)) *(np.minimum(((data["t_mean"])) ,(( data["geoNetwork_metro"])))))) + 
(((np.minimum(((data["visitNumber"])) ,(( data["trafficSource_source"])))) * 2.0)) + 
(((1.0)*(data["device_browser"])))+ 
(((((( -1.0*(( 2.0)))) +(((-2.0)+(1.0)))))/ 2.0)) + 
(((((((((((data["t_nb_sess"])+(( 6.0)))/2.0)) -(data["t_nb_sess"])))+(data["geoNetwork_subContinent"])) /2.0)) +(data["t_nb_sess"])))+ 
(((((( data["t_sum_act"])*(data["totals_pageviews"])))<(data["pred_0"])) *1.))+ 
(((((((data["t_median"])-(data["totals_hits"])))+(data["geoNetwork_subContinent"])))+(((data["geoNetwork_subContinent"])+(((data["geoNetwork_subContinent"])+(data["geoNetwork_country"])))))))+ 
(np.minimum(((data["geoNetwork_subContinent"])) ,(( data["geoNetwork_metro"])))) + 
(((( np.minimum(((data["t_median"])) ,(( data["t_sum_log"])))) +(data["totals_hits"])) /2.0)) )
def collate_fn(batch):
return tuple(zip(*batch))
def run_training() :
device = torch.device('cuda:0')
 net.to(device)
 train_loader = torch.utils.data.DataLoader(
 train_dataset,
batch_size=TrainGlobalConfig.batch_size,
sampler=RandomSampler(train_dataset),
pin_memory=False,
drop_last=True,
num_workers=TrainGlobalConfig.num_workers,
collate_fn=collate_fn, 
)
 val_loader = torch.utils.data.DataLoader(
 validation_dataset,
batch_size=TrainGlobalConfig.batch_size,
num_workers=TrainGlobalConfig.num_workers,
shuffle=False,
sampler=SequentialSampler(validation_dataset),
pin_memory=False,
collate_fn=collate_fn, 
)
 fitter = Fitter(model=net, device=device, config=TrainGlobalConfig)
 fitter.fit(train_loader, val_loader )<choose_model_class>
xtrain = GP1(gp_trn_users ).values
ytrain = GP2(gp_trn_users ).values
xtrain = np.sign(xtrain)*np.log1p(np.abs(xtrain))
ytrain = np.sign(ytrain)*np.log1p(np.abs(ytrain))
xtest = GP1(gp_sub_users ).values
ytest = GP2(gp_sub_users ).values
xtest = np.sign(xtest)*np.log1p(np.abs(xtest))
ytest = np.sign(ytest)*np.log1p(np.abs(ytest))
del gp_sub_users
del gp_trn_users
gc.collect()
def get_net() :
config = get_efficientdet_config('tf_efficientdet_d5')
 net = EfficientDet(config, pretrained_backbone=False)
 config.num_classes = 1
config.image_size = 512
net.class_net = HeadNet(config, num_outputs=config.num_classes, norm_kwargs=dict(eps=.001, momentum=.01))
checkpoint = torch.load('/kaggle/input/google-weight/last-checkpoint.bin')
 net.load_state_dict(checkpoint['model_state_dict'])
 return DetBenchTrain(net, config)
 net = get_net()<normalization>
full_data['gp1'] = xtrain
full_data['gp2'] = ytrain
sub_full_data['gp1'] = xtest
sub_full_data['gp2'] = ytest
class AverageMeter(object):

 def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count<train_model>
folds = get_folds(df=full_data[['totals.pageviews']].reset_index() , n_splits=5)
 oof_preds = np.zeros(full_data.shape[0])
 sub_preds = np.zeros(sub_full_data.shape[0])
 vis_importances = pd.DataFrame()
for fold_,(trn_, val_)in enumerate(folds):
trn_x, trn_y = full_data.iloc[trn_], trn_user_target['target'].iloc[trn_]
val_x, val_y = full_data.iloc[val_], trn_user_target['target'].iloc[val_]
reg = lgb.LGBMRegressor(
 num_leaves=31,
learning_rate=0.03,
n_estimators=1000,
subsample=.9,
colsample_bytree=.9,
random_state=1 
)
 reg.fit(
 trn_x, np.log1p(trn_y),
eval_set=[(trn_x, np.log1p(trn_y)) ,(val_x, np.log1p(val_y)) ],
eval_names=['TRAIN', 'VALID'],
early_stopping_rounds=50,
eval_metric='rmse',
verbose=100 
)
 imp_df = pd.DataFrame()
imp_df['feature'] = trn_x.columns
imp_df['gain'] = reg.booster_.feature_importance(importance_type='gain')
 imp_df['fold'] = fold_ + 1
vis_importances = pd.concat([vis_importances, imp_df], axis=0, sort=False)
 oof_preds[val_] = reg.predict(val_x, num_iteration=reg.best_iteration_)
 oof_preds[oof_preds < 0] = 0
_preds = reg.predict(sub_full_data[full_data.columns], num_iteration=reg.best_iteration_)
 _preds[_preds < 0] = 0
sub_preds += _preds / len(folds)
 mean_squared_error(np.log1p(trn_user_target['target']), oof_preds)**.5
if len(os.listdir('.. /input/global-wheat-detection/test/')) <11:
pass
else:
run_training()<load_pretrained>
sub_full_data['PredictedLogRevenue'] = sub_preds
sub_full_data[['PredictedLogRevenue']].to_csv('new_test.csv', index=True )