Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<define_variables>
print(os.listdir(".. /input"))
random_sample_percent = 5/8
random_state = 15
number_of_folds = 5
stop_after_fold_number = 1
shuffle = True<randomize_order>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
if random_sample_percent is not None:
train = train.sample(frac=random_sample_percent, random_state=random_state )<prepare_x_and_y>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
train_y = train['HasDetections']<drop_column>
train.drop(['Id'], axis=1, inplace=True)
 test.drop(['Id'], axis=1, inplace=True )
train = reduce_mem_usage(train )<load_from_csv>
train = train[train.GrLivArea < 4500]
train.reset_index(drop=True, inplace=True)
 train["SalePrice"] = np.log1p(train["SalePrice"])
 y = train['SalePrice'].reset_index(drop=True )
test = pd.read_csv('.. /input/test.csv', dtype=dtypes)
 test = reduce_mem_usage(test )<drop_column>
train_features = train.drop(['SalePrice'], axis=1)
 test_features = test
features = pd.concat([train_features, test_features] ).reset_index(drop=True )
<categorify>
features['MSSubClass'] = features['MSSubClass'].apply(str)
 features['YrSold'] = features['YrSold'].astype(str)
 features['MoSold'] = features['MoSold'].astype(str)
 features['Functional'] = features['Functional'].fillna('Typ')
 features['Electrical'] = features['Electrical'].fillna("SBrkr")
 features['KitchenQual'] = features['KitchenQual'].fillna("TA")
 features["PoolQC"] = features["PoolQC"].fillna("None")
 features['Exterior1st'] = features['Exterior1st'].fillna(features['Exterior1st'].mode() [0])
 features['Exterior2nd'] = features['Exterior2nd'].fillna(features['Exterior2nd'].mode() [0])
 features['SaleType'] = features['SaleType'].fillna(features['SaleType'].mode() [0])
def encode_categorical_columns(x_train, x_test, columns, sort=True):
train_length = x_train.shape[0]
for col in tqdm(columns):
if col == 'MachineIdentifier' or col == 'HasDetections':
continue
combined_data = pd.concat([x_train[col], x_test[col]])
 combined_data, _ = pd.factorize(combined_data, sort=sort)
 combined_data = pd.Series(combined_data ).astype('int32')
 x_train[col] = combined_data.iloc[:train_length].values
x_test[col] = combined_data.iloc[train_length:].values
x_train[col] = x_train[col].fillna(0)
 x_test[col] = x_test[col].fillna(0)
 del combined_data
return x_train, x_test<categorify>
for col in('GarageYrBlt', 'GarageArea', 'GarageCars'):
features[col] = features[col].fillna(0)
 for col in ['GarageType', 'GarageFinish', 'GarageQual', 'GarageCond']:
features[col] = features[col].fillna('None')
 for col in('BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2'):
features[col] = features[col].fillna('None')
train, test = encode_categorical_columns(train, test, categorical_columns)
 train = reduce_mem_usage(train)
 test = reduce_mem_usage(test )<predict_on_test>
features['MSZoning'] = features.groupby('MSSubClass')['MSZoning'].transform(lambda x: x.fillna(x.mode() [0]))
def predict_chunk(model, test):
initial_idx = 0
chunk_size = 1000000
current_pred = np.zeros(len(test))
while initial_idx < test.shape[0]:
final_idx = min(initial_idx + chunk_size, test.shape[0])
 idx = range(initial_idx, final_idx)
 current_pred[idx] = model.predict(test.iloc[idx], num_iteration=model.best_iteration)
 initial_idx = final_idx
return current_pred<train_model>
objects = []
for i in features.columns:
if features[i].dtype == object:
objects.append(i)
 features.update(features[objects].fillna('None'))
print(objects)
def train_model(x, y, lgb_params,
number_of_folds=5,
evaluation_metric='auc',
save_feature_importances=False,
early_stopping_rounds=50,
num_round = 50,
identifier_columns=['MachineIdentifier'],
stop_after_fold_number=None):
cross_validator = StratifiedKFold(n_splits=number_of_folds,
random_state=random_state,
shuffle=shuffle)
 validation_scores = []
classifier_models = []
feature_importance_df = pd.DataFrame()
for fold_index,(train_index, validation_index)in enumerate(cross_validator.split(x, y)) :
x_train, x_validation = x.iloc[train_index], x.iloc[validation_index]
y_train, y_validation = y.iloc[train_index], y.iloc[validation_index]
x_train.drop(identifier_columns, axis=1, inplace=True)
 validation_identifier_data = x_validation[identifier_columns]
x_validation.drop(identifier_columns, axis=1, inplace=True)
 x_train_columns = x_train.columns
trn_data = lgb.Dataset(x_train,
label=y_train, 
)
 del x_train
del y_train
val_data = lgb.Dataset(x_validation,
label=y_validation, 
)
 classifier_model = lgb.train(lgb_params,
trn_data,
num_round,
valid_sets=[trn_data, val_data],
verbose_eval=100,
early_stopping_rounds=early_stopping_rounds,
feval=eval_auc 
)
 classifier_models.append(classifier_model)
 predictions = classifier_model.predict(x_validation, num_iteration=classifier_model.best_iteration)
 false_positive_rate, recall, thresholds = metrics.roc_curve(y_validation, predictions)
 score = metrics.auc(false_positive_rate, recall)
 validation_scores.append(score)
 fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = x_train_columns
fold_importance_df["importance"] = classifier_model.feature_importance(importance_type='gain')
 fold_importance_df["fold"] = fold_index + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 if stop_after_fold_number == fold_index + 1:
break
if save_feature_importances:
cols =(feature_importance_df[["feature", "importance"]] 
.groupby("feature")
.mean() 
.sort_values(by="importance", ascending=False)[:1000].index)
 best_features = feature_importance_df.loc[feature_importance_df.feature.isin(cols)]
plt.figure(figsize=(14, 25))
sns.barplot(x="importance",
y="feature",
data=best_features.sort_values(by="importance",
ascending=False))
plt.title('LightGBM Features(avg over folds)')
 plt.tight_layout()
plt.savefig('lgbm_importances.png')
 best_features.sort_values(by="importance", ascending=False)\ 
.groupby("feature")\ 
.mean() \ 
.sort_values(by="importance", ascending=False)\ 
.to_csv('feature_importances_new.csv', index=True)
 score = sum(validation_scores)/ len(validation_scores)
 return classifier_models, score<train_model>
features['LotFrontage'] = features.groupby('Neighborhood')['LotFrontage'].transform(lambda x: x.fillna(x.median()))
numeric_dtypes = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']
numerics = []
for i in features.columns:
if features[i].dtype in numeric_dtypes:
numerics.append(i)
 features.update(features[numerics].fillna(0))
numerics[1:10]
models, validation_score = train_model(train.drop('HasDetections', axis=1),
train_y, base_params,
num_round=5120,
early_stopping_rounds=200,
stop_after_fold_number=stop_after_fold_number,
save_feature_importances=True)
 del train<predict_on_test>
numeric_dtypes = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']
numerics2 = []
for i in features.columns:
if features[i].dtype in numeric_dtypes:
numerics2.append(i)
 skew_features = features[numerics2].apply(lambda x: skew(x)).sort_values(ascending=False)
 high_skew = skew_features[skew_features > 0.5]
skew_index = high_skew.index
for i in skew_index:
features[i] = boxcox1p(features[i], boxcox_normmax(features[i] + 1))
submission_data = test[['MachineIdentifier']]
predictions = np.zeros(len(test))
test = test.drop('MachineIdentifier', axis=1)
 chunk_size = 1000000
for classifier_model in tqdm(models):
current_pred = np.zeros(len(test))
initial_idx = 0
while initial_idx < test.shape[0]:
final_idx = min(initial_idx + chunk_size, test.shape[0])
 idx = range(initial_idx, final_idx)
 current_pred[idx] = classifier_model.predict(test.iloc[idx],
num_iteration=classifier_model.best_iteration)
 initial_idx = final_idx
predictions += current_pred / len(models)
 del test<save_to_csv>
features = features.drop(['Utilities', 'Street', 'PoolQC',], axis=1)
 features['YrBltAndRemod']=features['YearBuilt']+features['YearRemodAdd']
features['TotalSF']=features['TotalBsmtSF'] + features['1stFlrSF'] + features['2ndFlrSF']
features['Total_sqr_footage'] =(features['BsmtFinSF1'] + features['BsmtFinSF2'] +
features['1stFlrSF'] + features['2ndFlrSF'])
 features['Total_Bathrooms'] =(features['FullBath'] +(0.5 * features['HalfBath'])+
features['BsmtFullBath'] +(0.5 * features['BsmtHalfBath']))
features['Total_porch_sf'] =(features['OpenPorchSF'] + features['3SsnPorch'] +
features['EnclosedPorch'] + features['ScreenPorch'] +
features['WoodDeckSF'])
submission_data['HasDetections'] = predictions
filename = 'submission_{:.6f}_{}_folds_{}_data.csv'.format(validation_score,
dt.now().strftime('%Y-%m-%d-%H-%M'),
len(models))
submission_data.to_csv('single_{}'.format(filename), index=False )<set_options>
features['haspool'] = features['PoolArea'].apply(lambda x: 1 if x > 0 else 0)
 features['has2ndfloor'] = features['2ndFlrSF'].apply(lambda x: 1 if x > 0 else 0)
 features['hasgarage'] = features['GarageArea'].apply(lambda x: 1 if x > 0 else 0)
 features['hasbsmt'] = features['TotalBsmtSF'].apply(lambda x: 1 if x > 0 else 0)
 features['hasfireplace'] = features['Fireplaces'].apply(lambda x: 1 if x > 0 else 0 )
sns.set_style('whitegrid')
 %matplotlib inline 
<feature_engineering>
final_features = pd.get_dummies(features ).reset_index(drop=True)
 final_features.shape
homesite_df = pd.read_csv(".. /input/train.csv")
 test_df = pd.read_csv(".. /input/test.csv")
 homesite_df = homesite_df.drop(['QuoteNumber'], axis=1)
 homesite_df['Year'] = homesite_df['Original_Quote_Date'].apply(lambda x: int(str(x)[:4]))
homesite_df['Month'] = homesite_df['Original_Quote_Date'].apply(lambda x: int(str(x)[5:7]))
homesite_df['Week'] = homesite_df['Original_Quote_Date'].apply(lambda x: int(str(x)[8:10]))
test_df['Year'] = test_df['Original_Quote_Date'].apply(lambda x: int(str(x)[:4]))
test_df['Month'] = test_df['Original_Quote_Date'].apply(lambda x: int(str(x)[5:7]))
test_df['Week'] = test_df['Original_Quote_Date'].apply(lambda x: int(str(x)[8:10]))
homesite_df.drop(['Original_Quote_Date'], axis=1,inplace=True)
 test_df.drop(['Original_Quote_Date'], axis=1,inplace=True)
 for f in homesite_df.columns:
if homesite_df[f].dtype=='object':
lbl = preprocessing.LabelEncoder()
lbl.fit(np.unique(list(homesite_df[f].values)+ list(test_df[f].values)))
 homesite_df[f] = lbl.transform(list(homesite_df[f].values))
test_df[f] = lbl.transform(list(test_df[f].values))<prepare_x_and_y>
outliers = [30, 88, 462, 631, 1322]
X = X.drop(X.index[outliers])
 y = y.drop(y.index[outliers])
 overfit = []
for i in X.columns:
counts = X[i].value_counts()
zeros = counts.iloc[0]
if zeros / len(X)* 100 > 99.94:
overfit.append(i)
 overfit = list(overfit)
 X = X.drop(overfit, axis=1)
 X_sub = X_sub.drop(overfit, axis=1)
 overfit
homesite_df.fillna(homesite_df.mean() , inplace=True)
 test_df.fillna(test_df.mean() , inplace=True)
 X_train = homesite_df.drop("QuoteConversion_Flag",axis=1)
 Y_train = homesite_df["QuoteConversion_Flag"]
X_test = test_df.drop("QuoteNumber",axis=1 ).copy()
X_train = maxabs_scale(X_train)
 X_test = maxabs_scale(X_test )<predict_on_test>
kfolds = KFold(n_splits=10, shuffle=True, random_state=42)
 def rmsle(y, y_pred):
return np.sqrt(mean_squared_error(y, y_pred))
def cv_rmse(model, X=X):
rmse = np.sqrt(-cross_val_score(model, X, y, scoring="neg_mean_squared_error", cv=kfolds))
return(rmse )
params = {"objective": "binary:logistic"}
T_train_xgb = xgb.DMatrix(X_train, Y_train)
 X_test_xgb = xgb.DMatrix(X_test)
 gbm = xgb.train(params, T_train_xgb, 20)
 Y_pred = gbm.predict(X_test_xgb )<save_to_csv>
alphas_alt = [14.5, 14.6, 14.7, 14.8, 14.9, 15, 15.1, 15.2, 15.3, 15.4, 15.5]
alphas2 = [5e-05, 0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007, 0.0008]
e_alphas = [0.0001, 0.0002, 0.0003, 0.0004, 0.0005, 0.0006, 0.0007]
e_l1ratio = [0.8, 0.85, 0.9, 0.95, 0.99, 1]
submission = pd.DataFrame()
submission["QuoteNumber"] = test_df["QuoteNumber"]
submission["QuoteConversion_Flag"] = Y_pred
submission.to_csv('homesite.csv', index=False )<set_options>
ridge = make_pipeline(RobustScaler() , RidgeCV(alphas=alphas_alt, cv=kfolds))
lasso = make_pipeline(RobustScaler() , LassoCV(max_iter=1e7, alphas=alphas2, random_state=42, cv=kfolds))
elasticnet = make_pipeline(RobustScaler() , ElasticNetCV(max_iter=1e7, alphas=e_alphas, cv=kfolds, l1_ratio=e_l1ratio))
svr = make_pipeline(RobustScaler() , SVR(C= 20, epsilon= 0.008, gamma=0.0003,))
%reload_ext autoreload
%autoreload 2
%matplotlib inline
warnings.filterwarnings("ignore" )<load_from_csv>
gbr = 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 =42 )
HEIGHT = 137
WIDTH = 236
SIZE = 128
bs = 128
stats =(0.0692, 0.2051)
 arch = models.resnet18
MODEL = '/kaggle/input/grapheme-fast-ai-starter-using-resnet18/resnet18_model_fold_0.pth'
nworkers = 2
TEST = ['/kaggle/input/bengaliai-cv19/test_image_data_0.parquet',
'/kaggle/input/bengaliai-cv19/test_image_data_1.parquet',
'/kaggle/input/bengaliai-cv19/test_image_data_2.parquet',
'/kaggle/input/bengaliai-cv19/test_image_data_3.parquet']
LABELS = '.. /input/bengaliai-cv19/train.csv'
df = pd.read_csv(LABELS)
 nunique = list(df.nunique())[1:-1]<choose_model_class>
lightgbm = LGBMRegressor(objective='regression',
num_leaves=4,
learning_rate=0.01,
n_estimators=5000,
max_bin=200,
bagging_fraction=0.75,
bagging_freq=5,
bagging_seed=7,
feature_fraction=0.2,
feature_fraction_seed=7,
verbose=-1,
)
class Head(nn.Module):
def __init__(self, nc, n, ps=0.5):
super().__init__()
layers = [AdaptiveConcatPool2d() , Mish() , Flatten() ] + \
bn_drop_lin(nc*2, 512, True, ps, Mish())+ \
bn_drop_lin(512, n, True, ps)
 self.fc = nn.Sequential(*layers)
 self._init_weight()
def _init_weight(self):
for m in self.modules() :
if isinstance(m, nn.Conv2d):
torch.nn.init.kaiming_normal_(m.weight)
 elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1.0)
 m.bias.data.zero_()
def forward(self, x):
return self.fc(x)
 class Rnet_1ch(nn.Module):
def __init__(self, arch=arch, n=nunique, pre=True, ps=0.5):
super().__init__()
m = arch(True)if pre else arch()
conv = nn.Conv2d(1, 64, kernel_size=7, stride=2, padding=3, bias=False)
 w =(m.conv1.weight.sum(1)).unsqueeze(1)
 conv.weight = nn.Parameter(w)
 self.layer0 = nn.Sequential(conv, m.bn1, nn.ReLU(inplace=True))
self.layer1 = nn.Sequential(
 nn.MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False),
m.layer1)
 self.layer2 = nn.Sequential(m.layer2)
 self.layer3 = nn.Sequential(m.layer3)
 self.layer4 = nn.Sequential(m.layer4)
 self.avgpool = nn.AdaptiveAvgPool2d(( 1,1))
nc = 512
self.head1 = Head(nc,n[0])
 self.head2 = Head(nc,n[1])
 self.head3 = Head(nc,n[2])
 to_Mish(self.layer0), to_Mish(self.layer1), to_Mish(self.layer2)
 to_Mish(self.layer3), to_Mish(self.layer4)
 def forward(self, x):
x = self.layer0(x)
 x = self.layer1(x)
 x = self.layer2(x)
 x = self.layer3(x)
 x = self.layer4(x)
 x = self.avgpool(x)
 x1 = self.head1(x)
 x2 = self.head2(x)
 x3 = self.head3(x)
 return x1,x2,x3<find_best_params>
xgboost = XGBRegressor(learning_rate=0.01,n_estimators=3460,
max_depth=3, min_child_weight=0,
gamma=0, subsample=0.7,
colsample_bytree=0.7,
objective='reg:linear', nthread=-1,
scale_pos_weight=1, seed=27,
reg_alpha=0.00006 )
model = Rnet_1ch(pre=False ).cuda()
model.load_state_dict(torch.load(MODEL, map_location=torch.device('cpu')));
model.eval() ;<data_type_conversions>
stack_gen = StackingCVRegressor(regressors=(ridge, lasso, elasticnet, gbr, xgboost, lightgbm),
meta_regressor=xgboost,
use_features_in_secondary=True )
class GraphemeDataset(Dataset):
def __init__(self, fname):
self.df = pd.read_parquet(fname)
 self.data = 255 - self.df.iloc[:, 1:].values.reshape(-1, HEIGHT, WIDTH ).astype(np.uint8)
 def __len__(self):
return len(self.data)
 def __getitem__(self, idx):
name = self.df.iloc[idx,0]
img =(self.data[idx]*(255.0/self.data[idx].max())).astype(np.uint8)
 img = crop_resize(img)
 img =(img.astype(np.float32)/255.0 - stats[0])/stats[1]
return img, name<import_modules>
score = cv_rmse(ridge , X)
 score = cv_rmse(lasso , X)
 print("LASSO: {:.4f}({:.4f})
 ".format(score.mean() , score.std()), datetime.now() ,)
 score = cv_rmse(elasticnet)
 print("elastic net: {:.4f}({:.4f})
 ".format(score.mean() , score.std()), datetime.now() ,)
 score = cv_rmse(svr)
 print("SVR: {:.4f}({:.4f})
 ".format(score.mean() , score.std()), datetime.now() ,)
 score = cv_rmse(lightgbm)
 print("lightgbm: {:.4f}({:.4f})
 ".format(score.mean() , score.std()), datetime.now() ,)
 score = cv_rmse(gbr)
 print("gbr: {:.4f}({:.4f})
 ".format(score.mean() , score.std()), datetime.now() ,)
 score = cv_rmse(xgboost)
 print("xgboost: {:.4f}({:.4f})
 ".format(score.mean() , score.std()), datetime.now() , )
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
IMG_SIZE=64
N_CHANNELS=1
HEIGHT = 137
WIDTH = 236
SIZE = 64
CROP_SIZE = 64
def resize(df, size=64):
resized = {}
resize_size=64
for i in range(df.shape[0]):
image=df.loc[df.index[i]].values.reshape(137,236)
 _, thresh = cv2.threshold(image, 30, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
 contours, _ = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)[-2:]
idx = 0
ls_xmin = []
ls_ymin = []
ls_xmax = []
ls_ymax = []
for cnt in contours:
idx += 1
x,y,w,h = cv2.boundingRect(cnt)
 ls_xmin.append(x)
 ls_ymin.append(y)
 ls_xmax.append(x + w)
 ls_ymax.append(y + h)
 xmin = min(ls_xmin)
 ymin = min(ls_ymin)
 xmax = max(ls_xmax)
 ymax = max(ls_ymax)
 roi = image[ymin:ymax,xmin:xmax]
resized_roi = cv2.resize(roi,(resize_size, resize_size),interpolation=cv2.INTER_AREA)
 resized[df.index[i]] = resized_roi.reshape(-1)
 resized = pd.DataFrame(resized ).T
return resized
def get_dummies(df):
cols = []
for col in df:
cols.append(pd.get_dummies(df[col].astype(str)))
 return pd.concat(cols, axis=1)
 model = tf.keras.models.load_model('/kaggle/input/resnetfinal/resnet_final.h5')
 model.load_weights('/kaggle/input/resnetfinal/resnet_final.h5')
 preds_dict = {
'grapheme_root': [],
'vowel_diacritic': [],
'consonant_diacritic': []
}
components = ['consonant_diacritic', 'grapheme_root', 'vowel_diacritic']
target=[]
row_id=[]
for i in range(4):
df_test_img = pd.read_parquet('/kaggle/input/bengaliai-cv19/test_image_data_{}.parquet'.format(i))
df_test_img.set_index('image_id', inplace=True)
 X_test = resize(df_test_img)/255
X_test = X_test.values.reshape(-1, IMG_SIZE, IMG_SIZE, N_CHANNELS)
 preds = model.predict(X_test)
 for i, p in enumerate(preds_dict):
preds_dict[p] = np.argmax(preds[i], axis=1)
 for k,id in enumerate(df_test_img.index.values):
for i,comp in enumerate(components):
id_sample=id+'_'+comp
row_id.append(id_sample)
 target.append(preds_dict[comp][k])
 del df_test_img
del X_test
gc.collect()
df_sample = pd.DataFrame(
 {
'row_id': row_id,
'target':target
},
columns = ['row_id','target'] 
)
 df_sample.to_csv('submission.csv',index=False)
<load_from_csv>
print('START Fit')
 print('stack_gen')
 stack_gen_model = stack_gen.fit(np.array(X), np.array(y))
print('elasticnet')
 elastic_model_full_data = elasticnet.fit(X, y)
 print('Lasso')
 lasso_model_full_data = lasso.fit(X, y)
 print('Ridge')
 ridge_model_full_data = ridge.fit(X, y)
 print('Svr')
 svr_model_full_data = svr.fit(X, y)
 print('GradientBoosting')
 gbr_model_full_data = gbr.fit(X, y)
 print('xgboost')
 xgb_model_full_data = xgboost.fit(X, y)
 print('lightgbm')
 lgb_model_full_data = lightgbm.fit(X, y )
def load_raw_df() :
train_df = pd.read_csv(".. /input/train.csv", parse_dates=[2])
 store_df = pd.read_csv(".. /input/store.csv")
 test_df = pd.read_csv(".. /input/test.csv", parse_dates=[3])
 train_df.head()
store_df.head()
test_df.head()
display(train_df.isnull().sum() ,store_df.isnull().sum() ,test_df.isnull().sum())
 mereged_train_df = pd.merge(train_df, store_df, on='Store')
 merged_test_df = pd.merge(test_df, store_df, on='Store')
 return mereged_train_df, merged_test_df
def extract_linear_feature(df):
df.drop("Customers", 1)
 mappings = {'0': 0, 'a': 1, 'b': 2, 'c': 3, 'd': 4}
df["StoreType"] = df["StoreType"].map(mappings)
 df["Assortment"] = df["Assortment"].map(mappings)
 df["StateHoliday"].loc[df["StateHoliday"] == 0] = "0"
df["StateHoliday"] = df["StateHoliday"].map(mappings)
 df["CompetitionDistance"].fillna(df["CompetitionDistance"].median())
 df["year"] = df.Date.dt.year
df["month"] = df.Date.dt.month
df["day"] = df.Date.dt.day
df["year"] = df.Date.dt.year
df["month"] = df.Date.dt.month
df["day"] = df.Date.dt.day
df.drop(["Date", "PromoInterval", "Promo2SinceWeek", "Promo2SinceYear"], axis=1, inplace=True)
 return df
def get_train_test_label() :
train_df, test_df = load_raw_df()
train_df_2 = extract_linear_feature(train_df)
 train_df_2.head()
y_true = train_df_2.loc[:, 'Sales'].as_matrix(columns=None)
 train_df_2.drop("Sales", axis =1)
 test_df["Customers"] = 1
test_df = extract_linear_feature(test_df)
 test_df.head()
feature_list = list(train_df_2.columns)
 print(feature_list)
 return train_df_2,y_true,test_df
x_train, y_train, x_test = get_train_test_label() 
<load_from_csv>
def blend_models_predict(X):
return(( 0.1 * elastic_model_full_data.predict(X)) + \ 
(0.05 * lasso_model_full_data.predict(X)) + \ 
(0.1 * ridge_model_full_data.predict(X)) + \ 
(0.1 * svr_model_full_data.predict(X)) + \ 
(0.1 * gbr_model_full_data.predict(X)) + \ 
(0.15 * xgb_model_full_data.predict(X)) + \ 
(0.1 * lgb_model_full_data.predict(X)) + \ 
(0.3 * stack_gen_model.predict(np.array(X))))
train_df = pd.read_csv(".. /input/train.csv", parse_dates=[2])
 store_df = pd.read_csv(".. /input/store.csv")
 test_df = pd.read_csv(".. /input/test.csv", parse_dates=[3])
 features = ['Store', 'DayOfWeek', 'Promo', 'StateHoliday', 'SchoolHoliday', 'StoreType', 'Assortment','Promo2', 'WeekOfYear','year', 'month', 'day']
def extract_feature(df):
mappings = {'0': 0, 'a': 1, 'b': 2, 'c': 3, 'd': 4}
df["StoreType"] = df["StoreType"].map(mappings)
 df["Assortment"] = df["Assortment"].map(mappings)
 df["StateHoliday"].loc[df["StateHoliday"] == 0] = "0"
df["StateHoliday"] = df["StateHoliday"].map(mappings)
 df["CompetitionDistance"].fillna(df["CompetitionDistance"].median())
 df['WeekOfYear'] = df.Date.dt.weekofyear
df["year"] = df.Date.dt.year
df["month"] = df.Date.dt.month
df["day"] = df.Date.dt.day
return df
train_df = pd.merge(train_df, store_df, on='Store')
 test_df = pd.merge(test_df, store_df, on='Store')
 train_df = extract_feature(train_df)
 test_df = extract_feature(test_df)
 train_df = train_df[train_df["Open"] != 0]
train, valid = train_test_split(train_df, test_size=0.03)
 y_train = np.log1p(train.Sales)
 y_valid = np.log1p(valid.Sales)
 dtrain = xgb.DMatrix(train[features], y_train)
 dvalid = xgb.DMatrix(valid[features], y_valid)
 watchlist = [(dtrain, 'train'),(dvalid, 'eval')]
def rmspe(y, yhat):
return np.sqrt(np.mean(((y - yhat)/y)** 2))
def rmspe_xg(yhat, y):
y = np.expm1(y.get_label())
 yhat = np.expm1(yhat)
 return "rmspe", rmspe(y, yhat)
 params = {"objective": "reg:linear",
"booster" : "gbtree",
"eta": 0.1,
"max_depth": 10,
"subsample": 0.85,
"colsample_bytree": 0.4,
"min_child_weight": 6,
"thread": 1,
"seed": 10
}
num_boost_round = 1000
gbm = xgb.train(params, dtrain, num_boost_round, evals=watchlist, early_stopping_rounds=1000, \
feval=rmspe_xg, verbose_eval=True)
 print("Validating")
 yhat = gbm.predict(xgb.DMatrix(valid[features]))
error = rmspe(valid.Sales.values, np.expm1(yhat))
print('RMSPE: {:.6f}'.format(error))
print("Make predictions on the test set")
 dtest = xgb.DMatrix(test_df[features])
 test_probs = gbm.predict(dtest)
 print(test_probs[:5])
 result = pd.DataFrame({"Id": test_df["Id"], 'Sales': np.expm1(test_probs)})
 result.to_csv("xgb_v2.csv", index=False)
<feature_engineering>
print('RMSLE score on train data:')
 print(rmsle(y, blend_models_predict(X)) )
os.environ['http_proxy'] = ''
os.environ['https_proxy'] = ''
os.environ['NO_PROXY'] = 'localhost'
def transform(df_):

 df_['Date']=pd.to_datetime(df_['Date'])
 df_['month']=df_['Date'].dt.month
df_['year']=df_['Date'].dt.year
df_['Store']=pd.to_numeric(df_['Store'])
 return df_
store=pd.read_csv(".. /input/store.csv")
 train=pd.read_csv(".. /input/train.csv")
 test=pd.read_csv(".. /input/test.csv")
 train=train[train['Sales'] > 0]
train=pd.merge(train, store, on=['Store'])
 test=pd.merge(test, store, on=['Store'])
 train=transform(train)
 test=transform(test)
 train['logSales']=pd.to_numeric(np.log(train['Sales']))
test['logSales']=0
h2o.init(nthreads=-1, max_mem_size = "8G")
 train_hf = h2o.H2OFrame(train)
 test_hf = h2o.H2OFrame(test)
 rf_v1_model = H2ORandomForestEstimator(model_id="rf_covType_v1", ntrees=200, stopping_rounds=2, max_depth = 30, nbins_cats = 1115, score_each_iteration=True, seed=1000000)
 covtype_X=[col for col in train_hf.columns if col not in ["Id","Date","Sales","logSales","Customers"]]
covtype_y=train_hf.columns[-1]
rf_v1_model.train(x=covtype_X, y=covtype_y, training_frame=train_hf)
 test_result_hf = rf_v1_model.predict(test_hf)
 test_result_df = test_result_hf.as_data_frame()
test_result_df['predict']=np.exp(test_result_df['predict'])
 test_result_df.rename(columns={'predict': 'Sales'}, inplace=True)
 test_result_df.insert(loc=0, column='Id', value=test['Id'])
 test_result_df.set_index('Id')
 test_result_df.to_csv('python_h2o_rf.csv', header=True, index=False )<load_from_csv>
print('Predict submission')
 submission = pd.read_csv(".. /input/sample_submission.csv")
 submission.iloc[:,1] =(np.expm1(blend_models_predict(X_sub)) )
sns.set_style('whitegrid')
 %matplotlib inline
df_train = pd.read_csv(".. /input/train.csv")
 df_store = pd.read_csv(".. /input/store.csv")
 df_test = pd.read_csv(".. /input/test.csv")
 df_train['Year'] = df_train['Date'].apply(lambda x: int(x[:4]))
df_train['Month'] = df_train['Date'].apply(lambda x: int(x[5:7]))
df_train.head()<categorify>
<groupby><EOS> 
q1 = submission['SalePrice'].quantile(0.0042)
 q2 = submission['SalePrice'].quantile(0.99)
 submission['SalePrice'] = submission['SalePrice'].apply(lambda x: x if x > q1 else x*0.77)
 submission['SalePrice'] = submission['SalePrice'].apply(lambda x: x if x < q2 else x*1.1)
 submission.to_csv("submission.csv", index=False )