Kaggle competition: House Prices - Advanced Regression Techniques
!pip install -U git+https://github.com/albu/albumentations<set_options>
def create_download_link(df, title = "Download CSV file", filename = "data.csv"):
csv = df.to_csv()
b64 = base64.b64encode(csv.encode())
 payload = b64.decode()
html = '<a download="{filename}" href="data:text/csv;base64,{payload}" target="_blank">{title}</a>'
html = html.format(payload=payload,title=title,filename=filename)
 return HTML(html)
 df = pd.DataFrame(np.random.randn(50, 4), columns=list('ABCD'))
warnings.filterwarnings("ignore" )<import_modules>
train_data = pd.read_csv(".. /input/train.csv")
 test_data = pd.read_csv(".. /input/test.csv" )
import albumentations as A<statistical_test>
train_ID = train_data['Id']
test_ID = test_data['Id']
A.MultiplicativeNoise()<install_modules>
train_data.drop("Id", axis = 1, inplace = True)
 test_data.drop("Id", axis = 1, inplace = True )
!pip install git+https://github.com/qubvel/segmentation_models.pytorch > /dev/null 2>&1<import_modules>
transform_log = np.log1p(train_data["SalePrice"])
 plot_dist_norm(transform_log, 'log(SalePrice)Distribution')
 train_data["SalePrice"] = transform_log
import segmentation_models_pytorch as smp<categorify>
train_data[train_data.GrLivArea > 4500]
def run_length_decode(rle, height=1024, width=1024, fill_value=1):
component = np.zeros(( height, width), np.float32)
 component = component.reshape(-1)
 rle = np.array([int(s)for s in rle.strip().split(' ')])
 rle = rle.reshape(-1, 2)
 start = 0
for index, length in rle:
start = start+index
end = start+length
component[start: end] = fill_value
start = end
component = component.reshape(width, height ).T
return component
def run_length_encode(component):
component = component.T.flatten()
start = np.where(component[1:] > component[:-1])[0]+1
end = np.where(component[:-1] > component[1:])[0]+1
length = end-start
rle = []
for i in range(len(length)) :
if i == 0:
rle.extend([start[0], length[0]])
 else:
rle.extend([start[i]-end[i-1], length[i]])
 rle = ' '.join([str(r)for r in rle])
 return rle<categorify>
train_data = train_data[train_data.GrLivArea < 4500]
class SIIMDataset(Dataset):
def __init__(self, df, fnames, data_folder, size, mean, std, phase):
self.df = df
self.root = data_folder
self.size = size
self.mean = mean
self.std = std
self.phase = phase
self.transforms = get_transforms(phase, size, mean, std)
 self.gb = self.df.groupby('ImageId')
 self.fnames = fnames
def __getitem__(self, idx):
image_id = self.fnames[idx]
df = self.gb.get_group(image_id)
 annotations = df[' EncodedPixels'].tolist()
image_path = os.path.join(self.root, image_id + ".png")
 image = cv2.imread(image_path)
 mask = np.zeros([1024, 1024])
 if annotations[0] != '-1':
for rle in annotations:
mask += run_length_decode(rle)
 mask =(mask >= 1 ).astype('float32')
 augmented = self.transforms(image=image, mask=mask)
 image = augmented['image']
mask = augmented['mask']
return image, mask
def __len__(self):
return len(self.fnames)
 def get_transforms(phase, size, mean, std):
list_transforms = []
if phase == "train":
list_transforms.extend(
 [
HorizontalFlip(p=0.5),
ShiftScaleRotate(
 shift_limit=0,
scale_limit=0.1,
rotate_limit=10,
p=0.5,
border_mode=cv2.BORDER_CONSTANT 
),
GaussNoise() ,
A.MultiplicativeNoise(multiplier=1.5, p=1),
] 
)
 list_transforms.extend(
 [
Resize(size, size),
Normalize(mean=mean, std=std, p=1),
ToTensor() ,
] 
)
 list_trfms = Compose(list_transforms)
 return list_trfms
def provider(
 fold,
total_folds,
data_folder,
df_path,
phase,
size,
mean=None,
std=None,
batch_size=8,
num_workers=2, 
):
df_all = pd.read_csv(df_path)
 df = df_all.drop_duplicates('ImageId')
 df_with_mask = df[df[" EncodedPixels"] != "-1"]
df_with_mask['has_mask'] = 1
df_without_mask = df[df[" EncodedPixels"] == "-1"]
df_without_mask['has_mask'] = 0
df_without_mask_sampled = df_without_mask.sample(len(df_with_mask)+1500, random_state=2019)
 df = pd.concat([df_with_mask, df_without_mask_sampled])
 kfold = StratifiedKFold(total_folds, shuffle=True, random_state=43)
 train_idx, val_idx = list(kfold.split(df["ImageId"], df["has_mask"])) [fold]
train_df, val_df = df.iloc[train_idx], df.iloc[val_idx]
df = train_df if phase == "train" else val_df
fnames = df['ImageId'].values
image_dataset = SIIMDataset(df_all, fnames, data_folder, size, mean, std, phase)
 dataloader = DataLoader(
 image_dataset,
batch_size=batch_size,
num_workers=num_workers,
pin_memory=True,
shuffle=True, 
)
 return dataloader<define_variables>
transform_log = transform_log.drop([523 , 1298], axis=0 )
sample_submission_path = '.. /input/siimmy/stage_2_sample_submission.csv'
train_rle_path = '.. /input/mysiim/train-rle.csv'
data_folder = ".. /input/siimpng/siimpng/train_png"
test_data_folder = ".. /input/siim_stage2_png"<set_options>
all_data = pd.concat(( train_data, test_data)).reset_index(drop=True )
ab = glob.glob('.. /input/siimpng/siimpng/train_png/*.png')
 len(ab )<load_from_csv>
all_data.drop(['SalePrice'], axis=1, inplace=True )
a= pd.read_csv(train_rle_path)
 len(a )<define_variables>
total = all_data.isnull().sum().sort_values(ascending=False)
 percent =(all_data.isnull().sum() /all_data.isnull().count() ).sort_values(ascending=False)
 missing_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing_data[total > 0]
dataloader = provider(
 fold=0,
total_folds=5,
data_folder=data_folder,
df_path=train_rle_path,
phase="train",
size=512,
mean =(0.485, 0.456, 0.406),
std =(0.229, 0.224, 0.225),
batch_size=16,
num_workers=2,
)<define_variables>
for col in ['SaleType', 'KitchenQual' , 'Exterior2nd' , 'Exterior1st' , 'Electrical' , 'Functional' , 'MSZoning' ] :
all_data[col] = all_data[col].fillna(all_data[col].mode() [0])
 for col in ['BsmtFinSF1', 'BsmtFinSF2' , 'GarageCars' , 'GarageArea' , 'TotalBsmtSF' , 'BsmtUnfSF' , 'BsmtHalfBath' , 'BsmtFullBath' , 'MasVnrArea' ]:
all_data[col]=all_data[col].fillna(0)
 for col in ['MasVnrType','FireplaceQu' , 'Fence' , 'PoolQC' , 'MiscFeature' ,'Alley' , 'BsmtFinType1', 'BsmtFinType2', 'BsmtQual', 'BsmtCond', 'BsmtExposure', 'GarageType', 'GarageFinish' , 'GarageCond' , 'GarageQual'] :
all_data[col]=all_data[col].fillna('None' )
batch = next(iter(dataloader))
images, masks = batch<compute_test_metric>
all_data.drop(['Utilities', 'Street' , 'PoolQC'], axis=1, inplace=True )
def dice_loss(input, target):
input = torch.sigmoid(input)
 smooth = 1.0
iflat = input.view(-1)
 tflat = target.view(-1)
 intersection =(iflat * tflat ).sum()
return(( 2.0 * intersection + smooth)/(iflat.sum() + tflat.sum() + smooth))
class FocalLoss(nn.Module):
def __init__(self, gamma):
super().__init__()
self.gamma = gamma
def forward(self, input, target):
if not(target.size() == input.size()):
raise ValueError("Target size({})must be the same as input size({})" 
.format(target.size() , input.size()))
max_val =(-input ).clamp(min=0)
 loss = input - input * target + max_val + \ 
(( -max_val ).exp() +(-input - max_val ).exp() ).log()
invprobs = F.logsigmoid(-input *(target * 2.0 - 1.0))
loss =(invprobs * self.gamma ).exp() * loss
return loss.mean()
class MixedLoss(nn.Module):
def __init__(self, alpha, gamma):
super().__init__()
self.alpha = alpha
self.focal = FocalLoss(gamma)
 def forward(self, input, target):
loss = self.alpha*self.focal(input, target)- torch.log(dice_loss(input, target))
return loss.mean()<predict_on_test>
all_data['Total_sqr_footage'] =(all_data['BsmtFinSF1'] + all_data['BsmtFinSF2'] + all_data['1stFlrSF'] + all_data['2ndFlrSF'])
 all_data['Total_Bathrooms'] =(all_data['FullBath'] +(0.5 * all_data['HalfBath'])+ all_data['BsmtFullBath'] +(0.5 * all_data['BsmtHalfBath']))
all_data['Total_porch_sf'] =(all_data['OpenPorchSF'] + all_data['3SsnPorch'] + all_data['EnclosedPorch'] + all_data['ScreenPorch'] + all_data['WoodDeckSF'])
def predict(X, threshold):
X_p = np.copy(X)
 preds =(X_p > threshold ).astype('uint8')
 return preds
def metric(probability, truth, threshold=0.5, reduction='none'):


batch_size = len(truth)
 with torch.no_grad() :
probability = probability.view(batch_size, -1)
 truth = truth.view(batch_size, -1)
 assert(probability.shape == truth.shape)
 p =(probability > threshold ).float()
t =(truth > 0.5 ).float()
t_sum = t.sum(-1)
 p_sum = p.sum(-1)
 neg_index = torch.nonzero(t_sum == 0)
 pos_index = torch.nonzero(t_sum >= 1)
 dice_neg =(p_sum == 0 ).float()
dice_pos = 2 *(p*t ).sum(-1)/(( p+t ).sum(-1))
dice_neg = dice_neg[neg_index]
dice_pos = dice_pos[pos_index]
dice = torch.cat([dice_pos, dice_neg])
 dice_neg = np.nan_to_num(dice_neg.mean().item() , 0)
 dice_pos = np.nan_to_num(dice_pos.mean().item() , 0)
 dice = dice.mean().item()
num_neg = len(neg_index)
 num_pos = len(pos_index)
 return dice, dice_neg, dice_pos, num_neg, num_pos
class Meter:

 def __init__(self, phase, epoch):
self.base_threshold = 0.5
self.base_dice_scores = []
self.dice_neg_scores = []
self.dice_pos_scores = []
self.iou_scores = []
def update(self, targets, outputs):
probs = torch.sigmoid(outputs)
 dice, dice_neg, dice_pos, _, _ = metric(probs, targets, self.base_threshold)
 self.base_dice_scores.append(dice)
 self.dice_pos_scores.append(dice_pos)
 self.dice_neg_scores.append(dice_neg)
 preds = predict(probs, self.base_threshold)
 iou = compute_iou_batch(preds, targets, classes=[1])
 self.iou_scores.append(iou)
 def get_metrics(self):
dice = np.mean(self.base_dice_scores)
 dice_neg = np.mean(self.dice_neg_scores)
 dice_pos = np.mean(self.dice_pos_scores)
 dices = [dice, dice_neg, dice_pos]
iou = np.nanmean(self.iou_scores)
 return dices, iou
def epoch_log(phase, epoch, epoch_loss, meter, start):

 dices, iou = meter.get_metrics()
dice, dice_neg, dice_pos = dices
print("Loss: %0.4f | dice: %0.4f | dice_neg: %0.4f | dice_pos: %0.4f | IoU: %0.4f" %(epoch_loss, dice, dice_neg, dice_pos, iou))
return dice, iou
def compute_ious(pred, label, classes, ignore_index=255, only_present=True):

 pred[label == ignore_index] = 0
ious = []
for c in classes:
label_c = label == c
if only_present and np.sum(label_c)== 0:
ious.append(np.nan)
 continue
pred_c = pred == c
intersection = np.logical_and(pred_c, label_c ).sum()
union = np.logical_or(pred_c, label_c ).sum()
if union != 0:
ious.append(intersection / union)
 return ious if ious else [1]
def compute_iou_batch(outputs, labels, classes=None):

 ious = []
preds = np.copy(outputs)
 labels = np.array(labels)
 for pred, label in zip(preds, labels):
ious.append(np.nanmean(compute_ious(pred, label, classes)))
 iou = np.nanmean(ious)
 return iou 
<choose_model_class>
all_data['haspool'] = all_data['PoolArea'].apply(lambda x: 1 if x > 0 else 0)
 all_data['has2ndfloor'] = all_data['2ndFlrSF'].apply(lambda x: 1 if x > 0 else 0)
 all_data['hasgarage'] = all_data['GarageArea'].apply(lambda x: 1 if x > 0 else 0)
 all_data['hasbsmt'] = all_data['TotalBsmtSF'].apply(lambda x: 1 if x > 0 else 0)
 all_data['hasfireplace'] = all_data['Fireplaces'].apply(lambda x: 1 if x > 0 else 0 )
model = smp.FPN('inceptionresnetv2', encoder_weights="imagenet" )<set_options>
all_data['GarageYrBlt'][all_data['GarageYrBlt']>2150]
class RAdam(Optimizer):
def __init__(self, params, lr=1e-3, betas=(0.9, 0.999), eps=1e-8, weight_decay=0):
defaults = dict(lr=lr, betas=betas, eps=eps, weight_decay=weight_decay)
 self.buffer = [[None, None, None] for ind in range(10)]
super(RAdam, self ).__init__(params, defaults)
 def __setstate__(self, state):
super(RAdam, self ).__setstate__(state)
 def step(self, closure=None):
loss = None
if closure is not None:
loss = closure()
for group in self.param_groups:
for p in group['params']:
if p.grad is None:
continue
grad = p.grad.data.float()
if grad.is_sparse:
raise RuntimeError('RAdam does not support sparse gradients')
 p_data_fp32 = p.data.float()
state = self.state[p]
if len(state)== 0:
state['step'] = 0
state['exp_avg'] = torch.zeros_like(p_data_fp32)
 state['exp_avg_sq'] = torch.zeros_like(p_data_fp32)
 else:
state['exp_avg'] = state['exp_avg'].type_as(p_data_fp32)
 state['exp_avg_sq'] = state['exp_avg_sq'].type_as(p_data_fp32)
 exp_avg, exp_avg_sq = state['exp_avg'], state['exp_avg_sq']
beta1, beta2 = group['betas']
exp_avg_sq.mul_(beta2 ).addcmul_(1 - beta2, grad, grad)
 exp_avg.mul_(beta1 ).add_(1 - beta1, grad)
 state['step'] += 1
buffered = self.buffer[int(state['step'] % 10)]
if state['step'] == buffered[0]:
N_sma, step_size = buffered[1], buffered[2]
else:
buffered[0] = state['step']
beta2_t = beta2 ** state['step']
N_sma_max = 2 /(1 - beta2)- 1
N_sma = N_sma_max - 2 * state['step'] * beta2_t /(1 - beta2_t)
 buffered[1] = N_sma
if N_sma >= 5:
step_size = group['lr'] * math.sqrt(( 1 - beta2_t)*(N_sma - 4)/(N_sma_max - 4)*(N_sma - 2)/ N_sma * N_sma_max /(N_sma_max - 2)) /(1 - beta1 ** state['step'])
 else:
step_size = group['lr'] /(1 - beta1 ** state['step'])
 buffered[2] = step_size
if group['weight_decay'] != 0:
p_data_fp32.add_(-group['weight_decay'] * group['lr'], p_data_fp32)
 if N_sma >= 5:
denom = exp_avg_sq.sqrt().add_(group['eps'])
 p_data_fp32.addcdiv_(-step_size, exp_avg, denom)
 else:
p_data_fp32.add_(-step_size, exp_avg)
 p.data.copy_(p_data_fp32)
 return loss<choose_model_class>
all_data['GarageYrBlt']=all_data['GarageYrBlt'].fillna(all_data['YearBuilt'][ all_data['GarageYrBlt'].isnull() ])
 all_data['GarageYrBlt'][all_data['GarageYrBlt']>2018] = all_data['YearBuilt'][all_data['GarageYrBlt']>2018]
class Trainer(object):

 def __init__(self, model):
self.fold = 1
self.total_folds = 5
self.num_workers = 4
self.batch_size = {"train": 4, "val": 4}
self.accumulation_steps = 32 // self.batch_size['train']
self.lr = 5e-4
self.num_epochs = 32
self.best_loss = float("inf")
 self.phases = ["train", "val"]
self.device = torch.device("cuda:0")
 torch.set_default_tensor_type("torch.cuda.FloatTensor")
 self.net = model
self.criterion = MixedLoss(10.0, 2.0)
 self.optimizer = RAdam(model.parameters() , lr=self.lr)
 self.scheduler = ReduceLROnPlateau(self.optimizer, mode="min", patience=3, verbose=True)
 self.net = self.net.to(self.device)
 cudnn.benchmark = True
self.dataloaders = {
phase: provider(
 fold=1,
total_folds=5,
data_folder=data_folder,
df_path=train_rle_path,
phase=phase,
size=512,
mean=(0.485, 0.456, 0.406),
std=(0.229, 0.224, 0.225),
batch_size=self.batch_size[phase],
num_workers=self.num_workers, 
)
 for phase in self.phases
}
self.losses = {phase: [] for phase in self.phases}
self.iou_scores = {phase: [] for phase in self.phases}
self.dice_scores = {phase: [] for phase in self.phases}
def forward(self, images, targets):
images = images.to(self.device)
 masks = targets.to(self.device)
 outputs = self.net(images)
 loss = self.criterion(outputs, masks)
 return loss, outputs
def iterate(self, epoch, phase):
meter = Meter(phase, epoch)
 start = time.strftime("%H:%M:%S")
 print(f"Starting epoch: {epoch} | phase: {phase} | ⏰: {start}")
 batch_size = self.batch_size[phase]
self.net.train(phase == "train")
 dataloader = self.dataloaders[phase]
running_loss = 0.0
total_batches = len(dataloader)
 self.optimizer.zero_grad()
for itr, batch in enumerate(dataloader):
images, targets = batch
loss, outputs = self.forward(images, targets)
 loss = loss / self.accumulation_steps
if phase == "train":
loss.backward()
if(itr + 1)% self.accumulation_steps == 0:
self.optimizer.step()
self.optimizer.zero_grad()
running_loss += loss.item()
outputs = outputs.detach().cpu()
meter.update(targets, outputs)
 epoch_loss =(running_loss * self.accumulation_steps)/ total_batches
dice, iou = epoch_log(phase, epoch, epoch_loss, meter, start)
 self.losses[phase].append(epoch_loss)
 self.dice_scores[phase].append(dice)
 self.iou_scores[phase].append(iou)
 torch.cuda.empty_cache()
return epoch_loss
def start(self):
for epoch in range(self.num_epochs):
self.iterate(epoch, "train")
 state = {
"epoch": epoch,
"best_loss": self.best_loss,
"state_dict": self.net.state_dict() ,
"optimizer": self.optimizer.state_dict() ,
}
val_loss = self.iterate(epoch, "val")
 self.scheduler.step(val_loss)
 if val_loss < self.best_loss:
print("******** New optimal found, saving state ********")
 state["best_loss"] = self.best_loss = val_loss
torch.save(state, "./model.pth")
 print()<choose_model_class>
all_data["LotFrontage"] = all_data.groupby("Neighborhood")["LotFrontage"].transform(lambda x: x.fillna(x.median()))
model_trainer = Trainer(model)
 model_trainer.start()<categorify>
all_data['MSSubClass'] = all_data['MSSubClass'].astype(str)
 all_data['OverallCond'] = all_data['OverallCond'].astype(str)
 all_data['BsmtFullBath'] = all_data['BsmtFullBath'].astype(str)
 all_data['BsmtHalfBath'] = all_data['BsmtHalfBath'].astype(str)
 all_data['YrSold'] = all_data['YrSold'].astype(str)
 all_data['MoSold'] = all_data['MoSold'].astype(str)
 all_data['GarageYrBlt'] = all_data['GarageYrBlt'].astype(str)
 all_data['YearBuilt'] = all_data['YearBuilt'].astype(str)
 all_data['YearRemodAdd'] = all_data['YearRemodAdd'].astype(str)
 all_data['GarageCars'] = all_data['GarageCars'].astype(str )
class TestDataset(Dataset):
def __init__(self, root, df, size, mean, std, tta=4):
self.root = root
self.size = size
self.fnames = list(df["ImageId"])
 self.num_samples = len(self.fnames)
 self.transform = Compose(
 [
Normalize(mean=mean, std=std, p=1),
Resize(size, size),
ToTensor() ,
] 
)
 def __getitem__(self, idx):
fname = self.fnames[idx]
path = os.path.join(self.root, fname + ".png")
 image = cv2.imread(path)
 images = self.transform(image=image)["image"]
return images
def __len__(self):
return self.num_samples
def post_process(probability, threshold, min_size):
mask = cv2.threshold(probability, threshold, 1, cv2.THRESH_BINARY)[1]
num_component, component = cv2.connectedComponents(mask.astype(np.uint8))
predictions = np.zeros(( 1024, 1024), np.float32)
 num = 0
for c in range(1, num_component):
p =(component == c)
 if p.sum() > min_size:
predictions[p] = 1
num += 1
return predictions, num<load_pretrained>
all_data['TotalSF']=all_data['TotalBsmtSF']+all_data['1stFlrSF']+all_data['2ndFlrSF']
size = 512
mean =(0.485, 0.456, 0.406)
 std =(0.229, 0.224, 0.225)
 num_workers = 8
batch_size = 16
best_threshold = 0.5
min_size = 3500
device = torch.device("cuda:0")
 df = pd.read_csv(sample_submission_path)
 testset = DataLoader(
 TestDataset(test_data_folder, df, size, mean, std),
batch_size=batch_size,
shuffle=False,
num_workers=num_workers,
pin_memory=True, 
)
 model = model_trainer.net
model.eval()
state = torch.load('./model.pth', map_location=lambda storage, loc: storage)
 model.load_state_dict(state["state_dict"])
 encoded_pixels = []
for i, batch in enumerate(tqdm(testset)) :
preds = torch.sigmoid(model(batch.to(device)))
 preds = preds.detach().cpu().numpy() [:, 0, :, :]
for probability in preds:
if probability.shape !=(1024, 1024):
probability = cv2.resize(probability, dsize=(1024, 1024), interpolation=cv2.INTER_LINEAR)
 predict, num_predict = post_process(probability, best_threshold, min_size)
 if num_predict == 0:
encoded_pixels.append('-1')
 else:
r = run_length_encode(predict)
 encoded_pixels.append(r)
 df['EncodedPixels'] = encoded_pixels
df.to_csv('submission.csv', columns=['ImageId', 'EncodedPixels'], index=False )<import_modules>
cols =('ExterCond','HeatingQC', 'BsmtFinType1',
'BsmtFinType2', 'Functional', 'Fence', 'BsmtExposure', 'GarageFinish', 'LandSlope',
'LotShape', 'PavedDrive', 'CentralAir', 'MSSubClass', 'OverallCond',
'YrSold', 'MoSold','GarageYrBlt','YearBuilt','YearRemodAdd', 'BsmtHalfBath','BsmtFullBath', 'GarageCars')
 for c in cols:
lbl = LabelEncoder()
lbl.fit(list(all_data[c].values))
all_data[c] = lbl.transform(list(all_data[c].values))
import torchvision
from fastai.vision import *
from fastai.metrics import error_rate
from fastai import *
import cv2 as cv
import numpy as np
import pandas as pd
import fastai<load_from_zip>
all_data.FireplaceQu = all_data.FireplaceQu.astype('category', ordered=True, categories=['None','Po','Fa','TA','Gd','Ex'] ).cat.codes
all_data.BsmtQual = all_data.BsmtQual.astype('category', ordered=True, categories=['None','Fa','TA','Gd','Ex'] ).cat.codes
all_data.BsmtCond = all_data.BsmtCond.astype('category', ordered=True, categories=['None','Po','Fa','TA','Gd'] ).cat.codes
all_data.GarageQual = all_data.GarageQual.astype('category', ordered=True, categories=['None','Po','Fa','TA','Gd','Ex'] ).cat.codes
all_data.GarageCond = all_data.GarageCond.astype('category', ordered=True, categories=['None','Po','Fa','TA','Gd','Ex'] ).cat.codes
all_data.ExterQual = all_data.ExterQual.astype('category', ordered=True, categories=['Fa','TA','Gd','Ex'] ).cat.codes
all_data.KitchenQual = all_data.KitchenQual.astype('category', ordered=True, categories=['Fa','TA','Gd','Ex'] ).cat.codes
os.listdir('.. /input')
 !tar -xf /kaggle/input/make-pneumothorax-classifer-data/classifier_data.tar.gz -C.
 !tar -xf /kaggle/input/make-pneumothorax-classifer-data/test_data.tar.gz -C .<load_pretrained>
for feat in skewness[abs(skewness)>0.5].index:
all_data[feat] = boxcox1p(all_data[feat], 0.15 )
tfms = get_transforms(do_flip=True, flip_vert=False, max_lighting=0.1, max_zoom=1.05,
max_warp=0.,
xtra_tfms=[rand_crop() , rand_zoom(1, 1.5),
symmetric_warp(magnitude=(-0.2, 0.2)) ])
 path = '/kaggle/working/classifier_data'
path_test = '/kaggle/working/test'
data =(ImageList.from_folder(path)
.split_by_rand_pct(seed=seed)
.label_from_folder() 
.transform(tfms, size=224)
.databunch().normalize(imagenet_stats))<define_variables>
all_data = pd.get_dummies(all_data)
 all_data.drop(["Neighborhood_Veenker", "RoofMatl_WdShngl", "RoofStyle_Shed" , "SaleCondition_Partial" , "SaleType_WD"], axis=1, inplace=True)
 train_data = all_data[:train_data.shape[0]]
test_data = all_data[train_data.shape[0]:]
y_train = transform_log
data.show_batch(rows=3, figsize=(12,9))<count_values>
from sklearn.linear_model import ElasticNet, Lasso, BayesianRidge, LassoLarsIC
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from sklearn.kernel_ridge import KernelRidge
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import RobustScaler
from sklearn.base import BaseEstimator, TransformerMixin, RegressorMixin, clone
from sklearn.model_selection import KFold, cross_val_score, train_test_split
from sklearn.metrics import mean_squared_error
import xgboost as xgb
import lightgbm as lgb
len(data.classes),data.c<install_modules>
n_folds = 10
def rmsle_cv(model):
kf = KFold(n_folds, shuffle=True, random_state=42 ).get_n_splits(train_data.values)
 rmse= np.sqrt(-cross_val_score(model, train_data.values, y_train, scoring="neg_mean_squared_error", cv = kf))
return(rmse )
!pip install pretrainedmodels 
<compute_train_metric>
KRR = KernelRidge(alpha=0.6, kernel='polynomial', degree=2, coef0=2.5)
 score = rmsle_cv(KRR)
 print("Kernel Ridge score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
class FocalLoss(nn.Module):
def __init__(self, alpha=1., gamma=2.) :
super().__init__()
self.alpha = alpha
self.gamma = gamma
def forward(self, inputs, targets, **kwargs):
CE_loss = nn.CrossEntropyLoss(reduction='none' )(inputs, targets)
 pt = torch.exp(-CE_loss)
 F_loss = self.alpha *(( 1-pt)**self.gamma)* CE_loss
return F_loss.mean()<choose_model_class>
lasso = make_pipeline(RobustScaler() , Lasso(alpha =0.0005, random_state=1))
score = rmsle_cv(lasso)
 print("Lasso score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
def resnext50_32x4d(pretrained=False):
pretrained = 'imagenet' if pretrained else None
model = pretrainedmodels.se_resnext50_32x4d(pretrained=pretrained)
 return nn.Sequential(*list(model.children()))<choose_model_class>
ENet = make_pipeline(RobustScaler() , ElasticNet(alpha=0.0005, l1_ratio=.9, random_state=3))
score = rmsle_cv(ENet)
 print("ElasticNet score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
learn = cnn_learner(data, resnext50_32x4d, pretrained=True, cut=-2,
split_on=lambda m:(m[0][3], m[1]),
metrics=[accuracy])
 learn.loss_fn = FocalLoss()<train_model>
GBoost = GradientBoostingRegressor(n_estimators=3000, learning_rate=0.05,
max_depth=4, max_features='sqrt',
min_samples_leaf=15, min_samples_split=10,
loss='huber', random_state =5)
 score = rmsle_cv(GBoost)
 print("Gradient Boosting score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
learn.fit_one_cycle(32, max_lr=slice(2e-2), wd=1e-5 )<find_best_params>
model_xgb = xgb.XGBRegressor(colsample_bytree=0.4603, gamma=0.0468,
learning_rate=0.05, max_depth=3,
min_child_weight=1.7817, n_estimators=2200,
reg_alpha=0.4640, reg_lambda=0.8571,
subsample=0.5213, silent=1,
random_state =7, nthread = -1)
 score = rmsle_cv(model_xgb)
 print("Xgboost score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
interp = ClassificationInterpretation.from_learner(learn)
 losses,idxs = interp.top_losses()
len(data.valid_ds)==len(losses)==len(idxs )<load_pretrained>
model_lgb = lgb.LGBMRegressor(objective='regression',num_leaves=5,
learning_rate=0.05, n_estimators=720,
max_bin = 55, bagging_fraction = 0.8,
bagging_freq = 5, feature_fraction = 0.2319,
feature_fraction_seed=9, bagging_seed=9,
min_data_in_leaf =6, min_sum_hessian_in_leaf = 11)
 score = rmsle_cv(model_lgb)
 print("LGBM score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))
learn.load('resnext50_32x4d_1');
learn.unfreeze() ;
learn = learn.clip_grad() ;<train_model>
LassoMd = lasso.fit(train_data.values,y_train)
 ENetMd = ENet.fit(train_data.values,y_train)
 KRRMd = KRR.fit(train_data.values,y_train)
 GBoostMd = GBoost.fit(train_data.values,y_train)
 XGBMd = model_xgb.fit(train_data.values, y_train)
 LGBMd = model_lgb.fit(train_data.values, y_train )
lr = [3e-3/100, 3e-3/20, 3e-3/10]
learn.fit_one_cycle(36, lr, wd=1e-7)
<save_model>
finalMd =(np.expm1(LassoMd.predict(test_data.values)) +
np.expm1(ENetMd.predict(test_data.values)) +
np.expm1(KRRMd.predict(test_data.values)) +
np.expm1(GBoostMd.predict(test_data.values)) +
np.expm1(XGBMd.predict(test_data.values)) +
np.expm1(LGBMd.predict(test_data.values)))/ 6
finalMd
<normalization><EOS> 
my_submission = pd.DataFrame({'Id': test_ID, 'SalePrice': finalMd})
 my_submission.to_csv('submission.csv', index=False )