Kaggle competition: House Prices - Advanced Regression Techniques
usefull_test = test[(test['target'] == 1)|(test['target'] == 0)]
new_train = pd.concat([train, usefull_test] ).reset_index(drop=True)
 new_train.loc[oof > 0.99, 'target'] = 1
new_train.loc[oof < 0.01, 'target'] = 0<normalization>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
oof2 = np.zeros(len(train))
preds2 = np.zeros(len(test))
for i in tqdm_notebook(range(512)) :
train2 = new_train[new_train['wheezy-copper-turtle-magic']==i]
test2 = test[test['wheezy-copper-turtle-magic']==i]
idx1 = train[train['wheezy-copper-turtle-magic']==i].index; idx2 = test2.index
train2.reset_index(drop=True,inplace=True)
 data = pd.concat([pd.DataFrame(train2[cols]), pd.DataFrame(test2[cols])])
 data2 = StandardScaler().fit_transform(VarianceThreshold(threshold=2 ).fit_transform(data[cols]))
train3 = data2[:train2.shape[0]]; test3 = data2[train2.shape[0]:]
skf = StratifiedKFold(n_splits=11, random_state=42)
 for train_index, test_index in skf.split(train2, train2['target']):
oof_test_index = [t for t in test_index if t < len(idx1)]
clf = QuadraticDiscriminantAnalysis(0.5)
 clf.fit(train3[train_index,:],train2.loc[train_index]['target'])
 if len(oof_test_index)> 0:
oof2[idx1[oof_test_index]] = clf.predict_proba(train3[oof_test_index,:])[:,1]
preds2[idx2] += clf.predict_proba(test3)[:,1] / skf.n_splits
auc = roc_auc_score(train['target'], oof2)
 print(f'AUC: {auc:.5}' )<save_to_csv>
print(train['SaleCondition'].unique())
 for i in range(train.shape[1]):
if train.iloc[:,i].dtypes == object:
lbl = LabelEncoder()
lbl.fit(list(train.iloc[:,i].values)+ list(test.iloc[:,i].values))
train.iloc[:,i] = lbl.transform(list(train.iloc[:,i].values))
test.iloc[:,i] = lbl.transform(list(test.iloc[:,i].values))
print(train['SaleCondition'].unique() )
sub = pd.read_csv('.. /input/sample_submission.csv')
 sub['target'] = preds2
sub.to_csv('submission.csv',index=False )<set_options>
train_ID = train['Id']
test_ID = test['Id']
y_train = train['SalePrice']
X_train = train.drop(['Id','SalePrice'], axis=1)
 X_test = test.drop('Id', axis=1)
 Xmat = pd.concat([X_train, X_test] )
warnings.filterwarnings('ignore' )<load_from_csv>
Xmat['TotalSF'] = Xmat['TotalBsmtSF'] + Xmat['1stFlrSF'] + Xmat['2ndFlrSF']
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 cols = [c for c in train.columns if c not in ['id', 'target', 'wheezy-copper-turtle-magic']]<create_dataframe>
X_train = Xmat[:X_train.shape[0]]
X_test = Xmat[X_train.shape[0]:]
X_train["Interaction"] = X_train["TotalSF"] * X_train["OverallQual"]
X_test["Interaction"] = X_test["TotalSF"] * X_test["OverallQual"]
X_train.head()
oof = np.zeros(len(train))
preds = np.zeros(len(test))
for i in tqdm_notebook(range(512)) :
train2 = train[train['wheezy-copper-turtle-magic']==i]
test2 = test[test['wheezy-copper-turtle-magic']==i]
idx1 = train2.index; idx2 = test2.index
train2.reset_index(drop=True,inplace=True)
 data = pd.concat([pd.DataFrame(train2[cols]), pd.DataFrame(test2[cols])])
 data2 = VarianceThreshold(threshold=2 ).fit_transform(data[cols])
 train3 = data2[:train2.shape[0]]; test3 = data2[train2.shape[0]:]
skf = StratifiedKFold(n_splits=24, random_state=24)
 for train_index, test_index in skf.split(train2, train2['target']):
clf = QuadraticDiscriminantAnalysis(0.5)
 clf.fit(train3[train_index,:],train2.loc[train_index]['target'])
 oof[idx1[test_index]] = clf.predict_proba(train3[test_index,:])[:,1]
preds[idx2] += clf.predict_proba(test3)[:,1] / skf.n_splits
auc = roc_auc_score(train['target'], oof)
 print(f'AUC: {auc:.5}' )<feature_engineering>
import lightgbm as lgb
test['target'] = preds
test.loc[test['target'] > 0.99, 'target'] = 1
test.loc[test['target'] < 0.01, 'target'] = 0<feature_engineering>
lgbParams = {"num_leaves": 41,
"min_data_in_leaf": 10,
"objective":'regression',
"metric": 'rmse',
"num_iterations": 5000,
"max_depth": -1,
"max_bin": 255,
"learning_rate": 0.005,
"num_iterations": 500,
"boosting": "gbdt",
"feature_fraction": 0.9,
"bagging_fraction": 0.9,
"bagging_freq": 3,
"lambda_l1": 0.1,
"verbosity": -1,
"random_state": 42,
}
usefull_test = test[(test['target'] == 1)|(test['target'] == 0)]
new_train = pd.concat([train, usefull_test] ).reset_index(drop=True)
 new_train.loc[oof > 0.99, 'target'] = 1
new_train.loc[oof < 0.01, 'target'] = 0<normalization>
n_folds = 5
kf = KFold(n_splits=n_folds, shuffle=True, random_state=42)
 y_pred_lgb = np.zeros(np.shape(X_train)[0])
 predictions_lgb = np.zeros(np.shape(X_test)[0])
 for train_idx, test_idx in kf.split(X_train):
trainX, testX = X_train.iloc[train_idx], X_train.iloc[test_idx]
trainY, testY = y_train.iloc[train_idx], y_train.iloc[test_idx]
d_train = lgb.Dataset(trainX, trainY)
 d_eval = lgb.Dataset(testX, testY, reference=d_train)
 LGBmodel = lgb.train(lgbParams, d_train, valid_sets=d_eval, early_stopping_rounds=100, verbose_eval=1000)
 y_pred_lgb[test_idx] = LGBmodel.predict(testX, num_iteration=LGBmodel.best_iteration)
 predictions_lgb += LGBmodel.predict(X_test)/ n_folds
oof2 = np.zeros(len(train))
preds2 = np.zeros(len(test))
for i in tqdm_notebook(range(512)) :
train2 = new_train[new_train['wheezy-copper-turtle-magic']==i]
test2 = test[test['wheezy-copper-turtle-magic']==i]
idx1 = train[train['wheezy-copper-turtle-magic']==i].index; idx2 = test2.index
train2.reset_index(drop=True,inplace=True)
 data = pd.concat([pd.DataFrame(train2[cols]), pd.DataFrame(test2[cols])])
 data2 = StandardScaler().fit_transform(VarianceThreshold(threshold=2 ).fit_transform(data[cols]))
train3 = data2[:train2.shape[0]]; test3 = data2[train2.shape[0]:]
skf = StratifiedKFold(n_splits=24, random_state=24)
 for train_index, test_index in skf.split(train2, train2['target']):
oof_test_index = [t for t in test_index if t < len(idx1)]
clf = QuadraticDiscriminantAnalysis(0.5)
 clf.fit(train3[train_index,:],train2.loc[train_index]['target'])
 if len(oof_test_index)> 0:
oof2[idx1[oof_test_index]] = clf.predict_proba(train3[oof_test_index,:])[:,1]
preds2[idx2] += clf.predict_proba(test3)[:,1] / skf.n_splits
auc = roc_auc_score(train['target'], oof2)
 print(f'AUC: {auc:.5}' )<save_to_csv>
cvscore = np.sqrt(((y_pred_lgb - y_train.values)** 2 ).mean())
 print("CV score = " + str(cvscore))
sub = pd.read_csv('.. /input/sample_submission.csv')
 sub['target'] = preds2
sub.to_csv('submission.csv',index=False )<save_to_csv>
import xgboost as xgb
with open('.. /input/prediction//prediction.pkl', 'rb')as f:
preds = pickle.load(f)
 sub = pd.read_csv('.. /input/instant-gratification/sample_submission.csv')
 if sub.shape[0] != 131073:
sub['target'] = preds
sub.to_csv('submission.csv',index=False )<import_modules>
xgbParams = {'learning_rate': 0.005,
'max_depth': 10,
'sabsample': 0.9,
'colsample_bytree': 0.9,
'n_estimators': 1000,
'objective': 'reg:linear',
'eval_metric': 'rmse',
'alpha': 0.1,
'verbosity': 0,
}
from PIL import Image
import math
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader, Subset, TensorDataset
from sklearn.model_selection import train_test_split
from tqdm import notebook
import torchvision
from torchvision import transforms
from torchvision import models
from collections import Counter
from pathlib import Path
from sklearn.metrics import confusion_matrix, recall_score
import seaborn as sn
import pyarrow.parquet as pq
from skimage.filters import threshold_otsu
from skimage.transform import AffineTransform, SimilarityTransform, warp, resize<set_options>
trainX, testX, trainY, testY = train_test_split(X_train, y_train, test_size=0.33, random_state=42)
 d_train = xgb.DMatrix(trainX, trainY)
 d_eval = xgb.DMatrix(testX, testY)
 watchlist = [(d_eval, 'eval'),(d_train, 'train')]
n_rounds = 5000
XGBmodel = xgb.train(xgbParams, d_train, n_rounds, watchlist, early_stopping_rounds=100, verbose_eval=1000)
 importance = np.asarray(list(XGBmodel.get_score(importance_type='gain' ).values()))
ranking = np.argsort(-importance)
 fig, ax = plt.subplots(figsize=(11, 9))
sns.barplot(x=importance[ranking], y=X_train.columns.values[ranking], orient='h')
 ax.set_xlabel("feature importance")
 plt.tight_layout()
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu' )<load_from_csv>
y_pred_xgb = np.zeros(np.shape(X_train)[0])
 predictions_xgb = np.zeros(np.shape(X_test)[0])
 for train_idx, test_idx in kf.split(X_train):
trainX, testX = X_train.iloc[train_idx], X_train.iloc[test_idx]
trainY, testY = y_train.iloc[train_idx], y_train.iloc[test_idx]
d_train = xgb.DMatrix(trainX, trainY)
 d_eval = xgb.DMatrix(testX, testY)
 watchlist = [(d_eval, 'eval'),(d_train, 'train')]
XGBmodel = xgb.train(xgbParams, d_train, n_rounds, watchlist, early_stopping_rounds=100, verbose_eval=1000)
 y_pred_xgb[test_idx] = XGBmodel.predict(xgb.DMatrix(testX))
predictions_xgb += XGBmodel.predict(xgb.DMatrix(X_test)) / n_folds
df_test = pd.read_csv('/kaggle/input/bengaliai-cv19/test.csv' )<data_type_conversions>
cvscore = np.sqrt(((y_pred_xgb - y_train.values)** 2 ).mean())
 print("CV score = " + str(cvscore))
def make_tensordataset_from_dfs(parquet_locs, label_loc=None):
ids = []
X = []
for parquet_loc in parquet_locs:
df = pd.read_parquet(parquet_loc)
 ids.extend(df.image_id.tolist())
 x = df.iloc[:, 1:].to_numpy(dtype=np.uint8)
 del df
X.append(x)
 X = np.vstack(X)
 X = X.reshape(-1, 1, 137, 236)
 X = torch.from_numpy(X)
 ids = dict(( s,i)for(i,s)in enumerate(ids))
if label_loc is None:
return TensorDataset(X)
 else:
graphemes = torch.zeros(X.shape[0], dtype=torch.long)
 vowel_diacs = torch.zeros(X.shape[0], dtype=torch.long)
 consonant_diacs = torch.zeros(X.shape[0], dtype=torch.long)
 lbl_df = pd.read_csv(label_loc)
 for row in lbl_df.itertuples() :
if row.image_id not in ids:
continue
idx = ids[row.image_id]
graphemes[idx] = row.grapheme_root
vowel_diacs[idx] = row.vowel_diacritic
consonant_diacs[idx] = row.consonant_diacritic
return TensorDataset(X, graphemes, vowel_diacs, consonant_diacs )<categorify>
from catboost import CatBoostRegressor
class DatasetWithImageTransforms(Dataset):
def __init__(self, ds, transforms):
super(DatasetWithImageTransforms, self ).__init__()
self.ds = ds
self.tr = transforms
self.nt = len(self.ds[0])
 self.ln = len(self.ds)
 def __getitem__(self, index):
img, *rest = self.ds[index]
img = self.tr(img)
 return(img,)+ tuple(rest)
 def __len__(self):
return self.ln<categorify>
Catmodel = CatBoostRegressor(iterations=2000,
learning_rate = 0.005,
use_best_model = True,
eval_metric = 'RMSE',
loss_function = 'RMSE',
boosting_type = 'Ordered',
verbose = 0 )
def thresh(img):
thresh_val = int(threshold_otsu(img))
img =(img > thresh_val)
 return img
def bounding_box(img):
img = thresh(img ).astype(np.uint8)
 col_min_val = np.min(img, axis=0)
 row_min_val = np.min(img, axis=1)
 col = np.argwhere(1 - col_min_val ).flatten()
row = np.argwhere(1 - row_min_val ).flatten()
return row.min() , row.max() , col.min() , col.max()
def scale_to_bb(img):
height = img.shape[0]
width = img.shape[1]
t, b, l, r = bounding_box(img)
 box_width = r - l
box_height = b - t
t, l = max(0, t - 10), max(0, l - 10)
 b, r = min(height, b + 10), min(width, r + 10)
 img = resize(img[t:b, l:r], output_shape=(256, 256), preserve_range=True, order=3, cval=1.0)
 return img
def random_scale(img):
height = img.shape[0]
width = img.shape[1]
t, b, l, r = bounding_box(img)
 box_width = r - l
box_height = b - t
max_width_scale =(box_width + min(l, width - r)) / box_width
max_height_scale =(box_height + min(t, height - b)) / box_height
max_scale = min(max_width_scale, max_height_scale)
 min_scale = min(1.0, 0.25 * max(( height / box_height),(width / box_width)))
 scale = random.uniform(min_scale, max_scale)
 tfm = SimilarityTransform(
 scale=(scale, scale), 
)
 img = warp(img, tfm.inverse, cval=1.0, order=3)
 return img
def random_translate(img):
height = img.shape[0]
width = img.shape[1]
t, b, l, r = bounding_box(img)
 box_width = r - l
box_height = b - t
translate_height = random.uniform(-t, height - b)
 translate_width = random.uniform(-l, width - r)
 tfm = SimilarityTransform(
 translation=(translate_width, translate_height), 
)
 img = warp(img, tfm.inverse, cval=1.0, order=3)
 return img
def random_rotate_and_shear(img):
max_theta = math.pi / 16
theta = random.uniform(-max_theta, max_theta)
 max_shear_theta = math.pi / 8
shear_theta = random.uniform(-max_shear_theta, max_shear_theta)
 tfm = AffineTransform(rotation=theta, shear=shear_theta)
 img = warp(img, tfm.inverse, cval=1.0, order=3)
 return img
def invert_color(t):
t.mul_(-1)
 t.add_(255)
 return t
def affine_transforms(img):
img = img.reshape(137, 236 ).numpy()
img = thresh(img ).astype(np.float32)
 img = random_translate(random_scale(img))
img = img.reshape(1, 137, 236)
 return torch.from_numpy(img)
 def tfms(img):
img = img.reshape(137, 236 ).numpy()
img = thresh(img ).astype(np.float32)
 img = scale_to_bb(img)
 img = img.reshape(1, 256, 256)
 return torch.from_numpy(img)
 def va_tfms(img):
img = img.reshape(137, 236 ).numpy()
img = thresh(img ).astype(np.float32)
 img = img.reshape(1, 137, 236)
 return torch.from_numpy(img)
 affine_transforms = transforms.Lambda(affine_transforms)
 mult = transforms.Lambda(lambda img: img * 255)
 to_float = transforms.Lambda(lambda img: img.float())
 invert_color = transforms.Lambda(invert_color)
 tfms = transforms.Compose([
tfms,
to_float,
] )<define_variables>
y_pred_cat = np.zeros(np.shape(X_train)[0])
 predictions_cat = np.zeros(np.shape(X_test)[0])
 for train_idx, test_idx in kf.split(X_train):
trainX, testX = X_train.iloc[train_idx], X_train.iloc[test_idx]
trainY, testY = y_train.iloc[train_idx], y_train.iloc[test_idx]
Catmodel.fit(trainX, trainY, eval_set=[(testX, testY)], early_stopping_rounds=100)
 y_pred_cat[test_idx] = Catmodel.predict(testX)
 predictions_cat += Catmodel.predict(X_test)/ n_folds
n_graphemes, n_vowel_diacs, n_consonant_diacs = 168, 11, 7<choose_model_class>
cvscore = np.sqrt(((y_pred_cat - y_train.values)** 2 ).mean())
 print("CV score(CatBoost)= " + str(cvscore))
def make_linear_block_old(in_size, out_size):
block = nn.Sequential(
 nn.Linear(in_size, out_size),
nn.ReLU() ,
nn.BatchNorm1d(num_features=out_size), 
)
 nn.init.xavier_normal_(block[0].weight.data)
 nn.init.zeros_(block[0].bias.data)
 return block
def make_ff_predictor_old(in_size, intermediate_size, out_size, layer_count):
layers = [make_linear_block_old(in_size, intermediate_size)]
for i in range(layer_count):
layers.append(make_linear_block_old(intermediate_size, intermediate_size))
layers.append(make_linear_block_old(intermediate_size, out_size))
layers = nn.Sequential(*layers)
 return layers
def make_squeeze_predictor(in_size, out_size):
return nn.Sequential(
 nn.Dropout(p=0.5),
nn.Conv2d(in_size, out_size, kernel_size=(1, 1)) ,
nn.ReLU() ,
nn.AdaptiveAvgPool2d(output_size=(1,1)) ,
nn.Flatten() 
)
 class BanglaHandwrittenGraphemeNN(nn.Module):
def __init__(self):
super(BanglaHandwrittenGraphemeNN, self ).__init__()
base = models.resnet18(pretrained=False)
 base.fc = nn.Identity()
self.base = base
feature_size = 512
self.grapheme_predictor = make_ff_predictor_old(feature_size, 512, n_graphemes, 2)
 self.vowel_diac_predictor = make_ff_predictor_old(feature_size, 512, n_vowel_diacs, 1)
 self.consonant_diacs = make_ff_predictor_old(feature_size, 512, n_consonant_diacs, 1)
 def convert_to_grayscale(self):
with torch.no_grad() :
conv1 = nn.Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
 conv1.weight.data = torch.sum(model.base.conv1.weight.data, dim=1, keepdim=True)
 self.base.conv1 = conv1
def freeze(self):
for p in self.base.parameters() :
p.requires_grad = False
def unfreeze(self):
for p in self.base.parameters() :
p.requires_grad = True
def forward(self, x):
with torch.no_grad() :
features = self.base(x)
 g_pred = self.grapheme_predictor(features)
 v_pred = self.vowel_diac_predictor(features)
 c_pred = self.consonant_diacs(features)
 return g_pred, v_pred, c_pred<define_search_model>
X1 = pd.DataFrame({'LGB': y_pred_lgb, 'XGB': y_pred_xgb, 'CAT': y_pred_cat})
 X1.head(7 )
def make_linear_block(in_size, out_size, dropout, bn):
if dropout:
if bn:
block = nn.Sequential(
 nn.Linear(in_size, out_size),
nn.Dropout(0.5),
nn.ReLU() ,
nn.BatchNorm1d(num_features=out_size), 
)
 else:
block = nn.Sequential(
 nn.Linear(in_size, out_size),
nn.Dropout(0.5),
nn.ReLU() , 
)
 else:
if bn:
block = nn.Sequential(
 nn.Linear(in_size, out_size),
nn.ReLU() ,
nn.BatchNorm1d(num_features=out_size), 
)
 else:
block = nn.Sequential(
 nn.Linear(in_size, out_size),
nn.ReLU() , 
)
 nn.init.xavier_normal_(block[0].weight.data)
 nn.init.zeros_(block[0].bias.data)
 return block
def make_ff_predictor(in_size, intermediate_size, out_size, layer_count, dropout, bn):
layers = [make_linear_block(in_size, intermediate_size, dropout, bn)]
for i in range(layer_count):
layers.append(make_linear_block(intermediate_size, intermediate_size, dropout, bn))
layers.append(nn.Linear(intermediate_size, out_size))
layers = nn.Sequential(*layers)
 return layers
def make_squeeze_predictor(in_size, out_size):
return nn.Sequential(
 nn.Dropout(p=0.5),
nn.Conv2d(in_size, out_size, kernel_size=(1, 1)) ,
nn.ReLU() ,
nn.AdaptiveAvgPool2d(output_size=(1,1)) ,
nn.Flatten() , 
)
 class MultiTaskNNR18(nn.Module):
def __init__(self, n_classes_tasks, depth_tasks, dropout=False, bn=False):
super(MultiTaskNNR18, self ).__init__()
base, feature_size = models.resnet18(pretrained=False), 512
base.fc = nn.Identity()
self.base = base
self.task_predictors = nn.ModuleList([
make_ff_predictor(feature_size, 512, n_classes, depth, dropout, bn)
 for n_classes, depth in zip(n_classes_tasks, depth_tasks)
 ])
 self.n_classes_tasks = n_classes_tasks
self.depth_tasks = depth_tasks
def convert_to_grayscale(self):
with torch.no_grad() :
conv1 = nn.Conv2d(1, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
 conv1.weight.data = torch.sum(self.base.conv1.weight.data, dim=1, keepdim=True)
 self.base.conv1 = conv1
def freeze(self):
for p in self.base.parameters() :
p.requires_grad = False
def unfreeze(self):
for p in self.base.parameters() :
p.requires_grad = True
def forward(self, x):
features = self.base(x)
 preds = [predictor(features)for predictor in self.task_predictors]
return preds<init_hyperparams>
linearModel1 = BayesianRidge()
linearModel1.fit(X1, y_train)
 X2 = pd.DataFrame({'LGB': predictions_lgb, 'XGB': predictions_xgb, 'CAT': predictions_cat})
 predictions1 = linearModel1.predict(X2 )
class MultiTaskNN(nn.Module):
def __init__(self, base, task_predictors):
super(MultiTaskNN, self ).__init__()
self.base = base
self.task_predictors = nn.ModuleList(task_predictors)
 def freeze(self):
for p in self.base.parameters() :
p.requires_grad = False
def unfreeze(self):
for p in self.base.parameters() :
p.requires_grad = True
def forward(self, x):
features = self.base(x)
 preds = [predictor(features)for predictor in self.task_predictors]
return preds<load_pretrained>
MLPmodel = MLPRegressor(random_state=1220, activation='relu', solver='sgd', learning_rate='adaptive', tol=1e-06, hidden_layer_sizes=(250,))
SVRmodel = NuSVR(kernel='rbf', degree=4, gamma='auto', nu=0.59, coef0=0.053, tol=1e-6)
 X_train =(X_train - np.mean(X_train)) / np.std(X_train)
 X_test =(X_test - np.mean(X_test)) / np.std(X_test)
 y_pred_nn = np.zeros(np.shape(X_train)[0])
 predictions_nn = np.zeros(np.shape(X_test)[0])
 y_pred_svr = np.zeros(np.shape(X_train)[0])
 predictions_svr = np.zeros(np.shape(X_test)[0])
 for train_idx, test_idx in kf.split(X_train):
trainX, testX = X_train.iloc[train_idx], X_train.iloc[test_idx]
trainY, testY = y_train.iloc[train_idx], y_train.iloc[test_idx]
MLPmodel.fit(trainX, trainY)
 SVRmodel.fit(trainX, trainY)
 y_pred_nn[test_idx] = MLPmodel.predict(testX)
 y_pred_svr[test_idx] = SVRmodel.predict(testX)
 predictions_nn += MLPmodel.predict(X_test)/ n_folds
predictions_svr += SVRmodel.predict(X_test)/ n_folds
base = torch.jit.load('/kaggle/input/bhgd-r152-trace/resnet152-corrected-more-transforms-base.trace')
 num_tasks = 3
task_predictors = [torch.jit.load('/kaggle/input/bhgd-r152-trace/resnet152-corrected-more-transforms-task-predictor-{}.trace'.format(i)) for i in range(num_tasks)]
model1 = MultiTaskNN(base, task_predictors )<load_pretrained>
cvscore = np.sqrt(((y_pred_nn - y_train.values)** 2 ).mean())
 print("CV score(Neural network)= " + str(cvscore))
cvscore = np.sqrt(((y_pred_svr - y_train.values)** 2 ).mean())
 print("CV score(SVR)= " + str(cvscore))
base, feature_size = torchvision.models.resnet152(pretrained=False), 2048
base.fc = nn.Identity()
conv1 = nn.Conv2d(3, 96, kernel_size=(3, 3), stride=(2, 2), bias=False)
 conv1.weight.data = torch.sum(base.conv1.weight.data, dim=1, keepdim=True)
 base.conv1 = conv1
n_classes_tasks = [n_graphemes, n_vowel_diacs, n_consonant_diacs]
task_predictors = [nn.Linear(feature_size, n_classes, bias=False)for n_classes in n_classes_tasks]
model2 = MultiTaskNN(base, task_predictors ).to(device)
 model2.load_state_dict(torch.load('/kaggle/input/bhgd-r152-itstrat-mixup-partial/resnet152-itstrat-mixup.pth', map_location=device))<choose_model_class>
X3 = pd.DataFrame({'MLP': y_pred_nn, 'SVR': y_pred_svr})
 linearModel2 = BayesianRidge()
linearModel2.fit(X3, y_train)
 X4 = pd.DataFrame({'MLP': predictions_nn, 'SVR': predictions_svr})
 predictions2 = linearModel2.predict(X4)
 X5 = pd.DataFrame({'LGB+XGB+CAT': linearModel1.predict(X1), 'NN+SVR': linearModel2.predict(X3)})
 linearModel3 = BayesianRidge()
linearModel3.fit(X5, y_train)
 X4 = pd.DataFrame({'LGB+XGB+CAT': predictions1, 'NN+SVR': predictions2})
 predictions = linearModel3.predict(X4 )
<categorify><EOS> 
predictions = np.expm1(predictions)
 submission = pd.DataFrame({
"Id": test_ID,
"SalePrice": predictions
})
 submission.to_csv('houseprice_stacked.csv', index=False )