Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<load_pretrained>
%matplotlib inline
warnings.filterwarnings("ignore" )
model = RanzcrModel()
model.load(os.path.join(MODEL_PATH, "effnet5_fold_0.bin"))<predict_on_test>
df_houses_train = pd.read_csv('.. /input/train.csv')
 df_houses_test = pd.read_csv('.. /input/test.csv')
 id_train = df_houses_train['Id']
id_test = df_houses_test['Id']
df_houses_train.drop(['Id'], axis=1, inplace=True)
 df_houses_test.drop(['Id'], axis=1, inplace=True )
final_preds = None
for j in range(2):
test_dataset = ImageDataset(
 image_paths=test_image_paths,
targets=[0]*len(test_image_paths),
augmentations=test_aug,
grayscale=True, 
)
 preds = model.predict(test_dataset, batch_size=32, n_jobs=-1, device="cuda")
 temp_preds = None
for p in preds:
if temp_preds is None:
temp_preds = p
else:
temp_preds = np.vstack(( temp_preds, p))
if final_preds is None:
final_preds = temp_preds
else:
final_preds += temp_preds
final_preds /= 2<feature_engineering>
del df_houses_train['1stFlrSF']
del df_houses_train['TotRmsAbvGrd']
del df_houses_train['GarageArea']
del df_houses_train['GarageYrBlt']
del df_houses_test['1stFlrSF']
del df_houses_test['TotRmsAbvGrd']
del df_houses_test['GarageArea']
del df_houses_test['GarageYrBlt']
target_cols = df.columns[1:]
for i in range(final_preds.shape[1]):
df.loc[:, target_cols[i]] = final_preds[:, i]<save_to_csv>
def get_avg_totalbsmt(col):
mean = df_houses_train['TotalBsmtSF'].mean()
return col if col != 0 else mean
df.to_csv('submission.csv', index=False)
 df.head()<import_modules>
df_houses_train["SalePrice"] = np.log1p(df_houses_train["SalePrice"] )
import numpy as np
import pandas as pd
import os
import random
from tqdm.auto import tqdm
import matplotlib.pyplot as plt
import cv2 
<import_modules>
train_size = len(df_houses_train)
 y_train = df_houses_train['SalePrice']
all_houses = pd.concat(( df_houses_train, df_houses_test)).reset_index(drop=True)
 all_houses.drop(['SalePrice'], axis=1, inplace=True)
 all_houses.fillna(np.nan, inplace=True)
 all_houses.head()
from sklearn.metrics import mean_absolute_error
from sklearn.model_selection import KFold, RepeatedKFold
import tensorflow as tf
import tensorflow.keras.backend as K
import tensorflow.keras.layers as L
import tensorflow.keras.models as M<define_variables>
all_houses.isnull().sum().sort_values(ascending=False ).head(35 )
seed = 123
def seed_everything(seed=123):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 tf.random.set_seed(seed)
 seed_everything(seed )<init_hyperparams>
columns = ['PoolQC', 'MiscFeature', 'Alley', 'Fence', 'FireplaceQu',
'GarageType', 'GarageFinish', 'GarageQual', 'GarageCond',
'MasVnrType',
'BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2']
for col in columns:
all_houses[col] = all_houses[col].fillna('None' )
mean = -0.13859619186395233
std = 0.1546567782480763
num_cv_folds = 4
num_repeats = 50<load_from_csv>
columns = ['GarageCars', 'BsmtFinSF1', 'BsmtFinSF2', 'BsmtUnfSF',
'TotalBsmtSF', 'BsmtFullBath', 'BsmtHalfBath', 'MasVnrArea']
for col in columns:
all_houses[col] = all_houses[col].fillna(0 )
train_data = pd.read_csv(".. /input/osic-pulmonary-fibrosis-progression/train.csv")
 print(train_data.shape)
 base_df = train_data.copy() [['Patient', 'FVC', 'Percent', 'Weeks']].sort_values(['Patient','Weeks'] ).groupby('Patient' ).head(1)
 base_df.columns = ['Patient', 'base_FVC', 'base_Percent', 'base_Weeks']
train_data = train_data.merge(base_df, on='Patient')
 print(train_data.shape)
 train_data['Weeks_passed'] = train_data.Weeks-train_data.base_Weeks
train_data = train_data[train_data.Weeks_passed>0]
train_data = train_data.drop_duplicates()
print(train_data.shape)
 train_data['est_Percent'] = train_data.apply(lambda x: mean*(x.Weeks-x.base_Weeks)+ x.base_Percent, axis=1)
 train_data['est_FVC'] = train_data['base_FVC']/train_data['base_Percent']*train_data['est_Percent']
train_data = train_data[['Patient', 'Weeks', 'FVC', 'Percent', 'Age', 'Sex', 'SmokingStatus',
'base_FVC', 'base_Percent', 'base_Weeks', 'Weeks_passed',
'est_Percent', 'est_FVC',
]].drop_duplicates()
train_data['WHERE'] = 'train'
print(train_data.shape)
 train_data.head()<load_from_csv>
df_houses_train['TotalBsmtSF_AVG'] = df_houses_train['TotalBsmtSF'].apply(get_avg_totalbsmt )
sample_sub = pd.read_csv(".. /input/osic-pulmonary-fibrosis-progression/sample_submission.csv")
 sample_sub['Patient'] = sample_sub['Patient_Week'].apply(lambda x: x.split("_")[0])
 sample_sub['Weeks'] = sample_sub['Patient_Week'].apply(lambda x: x.split("_")[1])
 sample_sub.head()<load_from_csv>
columns = ['MSZoning', 'Functional', 'Utilities', 'Exterior1st', 'Exterior2nd',
'KitchenQual', 'SaleType', 'Electrical']
for col in columns:
print('Column [ {0} ] - Mode: {1}'.format(col, all_houses[col].mode() [0]))
all_houses[col] = all_houses[col].fillna(all_houses[col].mode() [0] )
test_data = pd.read_csv('.. /input/osic-pulmonary-fibrosis-progression/test.csv')
 test_data = test_data.groupby('Patient' ).first() [['FVC', 'Percent', 'Weeks', 'Percent', 'Age', 'Sex', 'SmokingStatus',]]
test_data.columns = ['base_FVC', 'base_Percent', 'base_Weeks', 'Percent', 'Age', 'Sex', 'SmokingStatus',]
test_data = test_data.merge(sample_sub[['Patient','Weeks']], on='Patient')
 test_data['Weeks'] = test_data.Weeks.astype(int)
 test_data['Weeks_passed'] = test_data.Weeks-test_data.base_Weeks
print(test_data.shape)
 test_data['est_Percent'] = test_data.apply(lambda x: mean*(x.Weeks-x.base_Weeks)+ x.base_Percent, axis=1)
 test_data['est_FVC'] = test_data['base_FVC']/test_data['base_Percent']*test_data['est_Percent']
print(test_data.shape)
 test_data = test_data[['Patient', 'Weeks', 'Age', 'Sex', 'SmokingStatus',
'base_FVC', 'base_Percent', 'base_Weeks', 'Weeks_passed',
'est_Percent', 'est_FVC',]].drop_duplicates()
test_data['WHERE'] = 'test'
print(test_data.shape)
 test_data.head()<normalization>
all_houses['LotFrontage'] = all_houses.groupby('Neighborhood')['LotFrontage'].transform(
 lambda lotFrontage: lotFrontage.fillna(lotFrontage.median()))
def calculate_height(row):
if row['Sex'] == 'Male':
return row['base_FVC'] /(27.63 - 0.112 * row['Age'])
 else:
return row['base_FVC'] /(21.78 - 0.101 * row['Age'])
 def scale_feature(series):
return(series - series.min())/(series.max() - series.min() )<categorify>
all_houses = all_houses.replace({'MoSold': {1: 'Jan', 2: 'Feb', 3: 'Mar', 4: 'Apr', 5: 'May', 6: 'Jun',
7: 'Jul', 8: 'Aug', 9: 'Sep', 10: 'Oct', 11: 'Nov',12: 'Dec'}} )
all_data = train_data.append(test_data)
 all_data['Height'] = all_data.apply(calculate_height, axis=1)
 all_data = pd.concat([
all_data,
pd.get_dummies(all_data.Sex),
pd.get_dummies(all_data.SmokingStatus)
 ], axis=1)
 all_data = all_data.drop(columns=['Sex', 'SmokingStatus'])
 all_data.head()<normalization>
all_houses['YrSold'] = all_houses['YrSold'].astype(str )
all_data['normalized_Weeks'] = scale_feature(all_data['Weeks'])
 all_data['normalized_base_Percent'] = scale_feature(all_data['base_Percent'])
 all_data['normalized_Age'] = scale_feature(all_data['Age'])
 all_data['normalized_base_Weeks'] = scale_feature(all_data['base_Weeks'])
 all_data['normalized_base_FVC'] = scale_feature(all_data['base_FVC'])
 all_data['normalized_Weeks_passed'] = scale_feature(all_data['Weeks_passed'])
 all_data['normalized_Height'] = scale_feature(all_data['Height'])
 all_data['normalized_est_FVC'] = scale_feature(all_data['est_FVC'])
 all_data['normalized_est_Percent'] = scale_feature(all_data['est_Percent'])
 all_data.head()<define_variables>
cat_features = ['BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2', 'BsmtQual',
'ExterCond', 'ExterQual', 'Fence', 'FireplaceQu', 'Functional', 'GarageCond',
'GarageFinish', 'GarageQual', 'HeatingQC', 'KitchenQual', 'PavedDrive', 'PoolQC', 'Utilities']
for feature in cat_features:
all_houses[feature] = all_houses[feature].astype("category")
 all_houses = all_houses.replace({
"BsmtExposure" : {"None" : 0, "Mn" : 1, "Av": 2, "Gd" : 3},
"BsmtFinType1" : {"None" : 0, "Unf" : 1, "LwQ": 2, "Rec" : 3, "BLQ" : 4, "ALQ" : 5, "GLQ" : 6},
"BsmtFinType2" : {"None" : 0, "Unf" : 1, "LwQ": 2, "Rec" : 3, "BLQ" : 4, "ALQ" : 5, "GLQ" : 6},
"Fence" : {"None" : 0, "MnWw" : 1, "GdWo": 2, "MnPrv" : 3, "GdPrv" : 4},
"Functional" : {"Sal" : 1, "Sev" : 2, "Maj2" : 3, "Maj1" : 4, "Mod": 5, "Min2" : 6, "Min1" : 7, "Typ" : 8},
"GarageFinish" : {"None" : 0, "Unf" : 1, "RFn" : 2, "Fin" : 3},
"PavedDrive" : {"N" : 0, "P" : 1, "Y" : 2},
"Utilities" : {"ELO" : 1, "NoSeWa" : 2, "NoSewr" : 3, "AllPub" : 4}})
 all_houses['CentralAir'].replace({'Y':1,'N':0}, inplace=True )
FEATURE_COLS = ['Female', 'Male',
'Currently smokes', 'Ex-smoker', 'Never smoked',
'normalized_Weeks', 'normalized_base_Weeks', 'normalized_Weeks_passed',
'normalized_base_Percent',
'normalized_base_FVC',
'normalized_Age',
]<compute_test_metric>
columns = ['ExterQual','ExterCond','BsmtQual','BsmtCond', 'HeatingQC','KitchenQual',
'FireplaceQu','GarageQual', 'GarageCond','PoolQC']
values = {'Ex':5,'Gd':4,'TA':3,'Fa':2,'Po':1,'NA':0}
for col in columns:
all_houses[col].replace(values, inplace=True )
C1, C2 = tf.constant(70, dtype='float32'), tf.constant(1000, dtype="float32")
 def score(y_true, y_pred):
tf.dtypes.cast(y_true, tf.float32)
 tf.dtypes.cast(y_pred, tf.float32)
 sigma = y_pred[:, 2] - y_pred[:, 0]
fvc_pred = y_pred[:, 1]
sigma_clip = tf.maximum(sigma, C1)
 delta = tf.abs(y_true[:, 0] - fvc_pred)
 delta = tf.minimum(delta, C2)
 sq2 = tf.sqrt(tf.dtypes.cast(2, dtype=tf.float32))
metric =(delta / sigma_clip)*sq2 + tf.math.log(sigma_clip* sq2)
 return -K.mean(metric)
 def qloss(y_true, y_pred):
qs = [0.2, 0.50, 0.8]
q = tf.constant(np.array([qs]), dtype=tf.float32)
 e = y_true - y_pred
v = tf.maximum(q*e,(q-1)*e)
 return K.mean(v)
 def mloss(_lambda):
def loss(y_true, y_pred):
return _lambda * qloss(y_true, y_pred)+(1 - _lambda)*score(y_true, y_pred)
 return loss
def make_model(nh):
z = L.Input(( nh,), name="Patient")
 x = L.Dense(100, activation="relu", name="d1" )(z)
 x = L.Dense(100, activation="relu", name="d2" )(x)
 p1 = L.Dense(3, activation="linear", name="p1" )(x)
 p2 = L.Dense(3, activation="relu", name="p2" )(x)
 preds = L.Lambda(lambda x: x[0] + tf.cumsum(x[1], axis=1),
name="preds" )([p1, p2])
 model = M.Model(z, preds, name="CNN")
 model.compile(loss=mloss(0.65), optimizer=tf.keras.optimizers.Adam(lr=0.1, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.01, amsgrad=False), metrics=[score])
 return model<find_best_params>
string_features = all_houses.select_dtypes(include=['object'] ).columns
numeric_features = all_houses.select_dtypes(include=['int64', 'float64'] ).columns
nh = len(FEATURE_COLS)
 net = make_model(nh)
 print(net.summary() )<define_variables>
all_houses[numeric_features] = all_houses[numeric_features].apply(normal_minmax )
NOISE_ON_FEATURES = ['FVC',
'normalized_Weeks', 'normalized_base_Weeks', 'normalized_Weeks_passed',
'normalized_base_Percent', 'normalized_base_FVC',
'normalized_Age', 'normalized_Height',
]
NOISE_MAX_PCT = 0.05
noise_generator = lambda x: x*(1+(np.random.randint(-NOISE_MAX_PCT*1000, NOISE_MAX_PCT*1000)/1000))
N_ARTIFICIAL_SAMPLES = len(tr )<define_variables>
skewed_features = all_houses[numeric_features].apply(lambda x: skew(x))
skewed_features = skewed_features[abs(skewed_features)> 0.75]
BATCH_SIZE = 128
EPOCHS = 200<split>
all_houses[skewed_features] = np.log1p(all_houses[skewed_features] )
pred = np.zeros(( len(tr), 3))
pe = np.zeros(( len(te[FEATURE_COLS]), 3))
rkf = RepeatedKFold(n_splits=num_cv_folds, n_repeats=num_repeats, random_state=seed)
 fold = 0
for tr_idx, val_idx in rkf.split(tr):
print(f"FOLD {fold}")
 X_tr = tr.loc[tr_idx]
X_tr_artif = X_tr.sample(n=N_ARTIFICIAL_SAMPLES, replace=True)
 X_tr_artif[NOISE_ON_FEATURES] = X_tr_artif[NOISE_ON_FEATURES].apply(noise_generator)
 X_tr = X_tr.append(X_tr_artif)
 y_tr = X_tr['FVC'].values
X_tr = X_tr[FEATURE_COLS].values
X_val = tr.loc[val_idx]
y_val = X_val['FVC'].values
X_val = X_val[FEATURE_COLS].values
X_te = te[FEATURE_COLS].values
net = make_model(nh)
 net.fit(X_tr, y_tr,
batch_size=BATCH_SIZE, epochs=EPOCHS,
validation_data=(X_val, y_val), verbose=0)
 print("train", net.evaluate(X_tr, y_tr, verbose=0, batch_size=BATCH_SIZE))
print("val", net.evaluate(X_val, y_val, verbose=0, batch_size=BATCH_SIZE))
print("predict val...", end=" ")
 pred[val_idx] += net.predict(X_val, batch_size=BATCH_SIZE, verbose=0)
 print("done")
 print("predict test...", end=" ")
 pe += net.predict(X_te, batch_size=BATCH_SIZE, verbose=0)/(num_cv_folds*num_repeats)
 print("done")
 fold+=1
pred = pred/num_repeats<compute_test_metric>
all_houses = pd.get_dummies(all_houses, columns=string_features, drop_first=True )
X = tr[FEATURE_COLS].values
y = tr['FVC'].values
sigma_opt = mean_absolute_error(y, pred[:, 1])
 unc = pred[:,2] - pred[:, 0]
sigma_mean = np.mean(unc)
 print(sigma_opt, sigma_mean )<feature_engineering>
df_houses_train = all_houses[:train_size]
df_houses_test = all_houses[train_size:]
X_train = df_houses_train
X_test = df_houses_test
print('Train:', X_train.shape)
 print('Test :', X_test.shape )
tr['FVC_pred'] = pred[:, 1]
tr['Confidence'] = pred[:,2] - pred[:, 0]<feature_engineering>
seed = 42
n_folds = 5
kfold = KFold(n_folds, shuffle=True, random_state=seed ).get_n_splits(X_train )
def lb_metric(train):
train['sigma_clipped'] = train['Confidence'].apply(lambda x: max(x, 70))
train['diff'] = abs(train['FVC'] - train['FVC_pred'])
 train['delta'] = train['diff'].apply(lambda x: min(x, 1000))
train['score'] = -np.sqrt(2)*train['delta']/train['sigma_clipped'] - np.log(np.sqrt(2)*train['sigma_clipped'])
 score = train['score'].mean()
return score
score = lb_metric(tr)
 print(f'Local Score: {score}' )<feature_engineering>
def get_rmse(model, x, y):
rmse = np.sqrt(-cross_val_score(model,
x,
y=y,
scoring="neg_mean_squared_error",
cv=kfold))
return rmse
sub = te[['Patient', 'Weeks', 'base_FVC', 'base_Weeks']].copy()
sub['FVC'] = pe[:, 1]
c =(sub.loc[sub.base_Weeks == sub.Weeks, 'base_FVC']/sub.loc[sub.base_Weeks == sub.Weeks, 'FVC'] ).mean()
sub['FVC'] = c*pe[:, 1]
sub['Patient_Week'] = sub['Patient'] +"_"+sub['Weeks'].astype(str)
 sub.loc[sub.base_Weeks == sub.Weeks, 'FVC'] = sub.loc[sub.base_Weeks == sub.Weeks, 'base_FVC']
sub['Confidence'] = pe[:, 2] - pe[:, 0]
sub.loc[sub.base_Weeks == sub.Weeks, 'Confidence'] = 70
sub['Confidence'] = sub.Confidence.fillna(70 ).apply(lambda x: np.clip(x, 70, best_x+140))
sub = sub[['Patient_Week','FVC','Confidence']]
sub.head(20 )<save_to_csv>
ridgeCV = RidgeCV(alphas=np.arange(0.001, 0.1, 0.01))
lassoCV = LassoCV(alphas=np.arange(0.001, 0.1, 0.01), eps=np.arange(1e-7, 1e-5, 1e-6))
lassoLarsCV = LassoLarsCV(max_iter=1000, eps=1e-7)
 elasticNetCV = ElasticNetCV(eps=1e-15)
 gBoost = GradientBoostingRegressor(random_state=seed)
 xgbReg = xgb.XGBRegressor(random_state=seed)
 lgbmReg = lgb.LGBMRegressor()
clfs = dict()
clfs['RidgeCV'] = ridgeCV
clfs['LassoCV'] = lassoCV
clfs['LassoLarsCV'] = lassoLarsCV
clfs['ElasticNetCV'] = elasticNetCV
clfs['GBoosting'] = gBoost
clfs['XGBRegressor'] = xgbReg
clfs['LGBMRegressor'] = lgbmReg
sub.to_csv('submission.csv', index=False )<save_to_csv>
def tuning(model, param_grid, X, y):
grid_result = GridSearchCV(model,
param_grid=param_grid,
cv=kfold,
scoring="neg_mean_squared_error",
n_jobs=-1,
verbose=1)
 grid_result.fit(X, y)
 model_best = grid_result.best_estimator_
print('Best score:', np.round(grid_result.best_score_, 4))
print('Best estimator:', model_best)
 return model_best
sub.to_csv('submission.csv', index=False )<set_options>
parameters = {'n_estimators':[1800, 1900],
'learning_rate': [0.05],
'max_depth': [2, 3],
'min_samples_leaf': [14, 16],
'max_features': ['sqrt'],
'min_samples_split': [5, 6],
'loss': ['huber', 'ls']}
gBoost_best = tuning(clfs['GBoosting'], parameters, X_train, y_train )
cufflinks.go_offline()
cufflinks.set_config_file(world_readable=True, theme='pearl')
 Dense, Dropout, Activation, Flatten, Input, GlobalAveragePooling2D, Add, Conv2D, AveragePooling2D,
LeakyReLU, Concatenate 
)
 %matplotlib inline
sns.set(style='darkgrid', palette='Set2')
 warnings.filterwarnings('ignore')
 plt.style.use('fivethirtyeight')
 plt.show()
print(f"Numpy version : Numpy {np.__version__}")
 print(f"Pandas version : Pandas {pd.__version__}")
 print(f"Matplotlib version : Matplotlib {matplotlib.__version__}")
 print(f"Seaborn version : Seaborn {sns.__version__}")
 print(f"Tensorflow version : Tensorflow {tf.__version__}" )<install_modules>
parameters = {'learning_rate': [0.01, 0.04],
'max_depth': [2, 3],
'min_child_weight': [2, 3],
'silent': [1],
'subsample': [0.7],
'colsample_bytree': [0.7],
'n_estimators': [1300, 1500]}
xgbReg_best = tuning(clfs['XGBRegressor'], parameters, X_train, y_train )
!pip install.. /input/kerasapplications/keras-team-keras-applications-3b180cb -f./ --no-index
!pip install.. /input/efficientnet/efficientnet-1.1.0/ -f./ --no-index<import_modules>
parameters = {'objective': ['regression'],
'num_leaves': [4, 5],
'max_depth': [3, 4],
'bagging_freq': [5, 6],
'bagging_fraction': [0.8, 0.9],
'learning_rate': [0.01, 0.05],
'n_estimators': [700, 1000]}
lgbmReg_best = tuning(clfs['LGBMRegressor'], parameters, X_train, y_train )
import efficientnet.tfkeras as efn<define_variables>
clfs['GBoostingBest'] = gBoost_best
clfs['XGBRegressorBest'] = xgbReg_best
clfs['LGBMRegressorBest'] = lgbmReg_best
get_results(list(clfs.values()), list(clfs.keys()), X_train, y_train )
def seed_everything(seed=100):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 tf.random.set_seed(seed)
 seed_everything(101 )<define_variables>
class AveragingModels(BaseEstimator, RegressorMixin, TransformerMixin):
def __init__(self, models):
self.models = models
def fit(self, X, y):
self.models_ = [clone(x)for x in self.models]
for model in self.models_:
model.fit(X, y)
 return self
def predict(self, X):
predictions = np.column_stack([
model.predict(X)for model in self.models_
])
 return np.mean(predictions, axis=1 )
base_dir = ".. /input/osic-pulmonary-fibrosis-progression/"<load_from_csv>
avg_models1 = AveragingModels(models =(lassoLarsCV, elasticNetCV, lassoCV,
ridgeCV, gBoost_best, xgbReg_best, lgbmReg_best))
avg_models2 = AveragingModels(models =(lassoLarsCV, elasticNetCV, xgbReg_best, gBoost_best))
avg_models3 = AveragingModels(models =(lassoLarsCV, elasticNetCV))
train_df = pd.read_csv('.. /input/osic-pulmonary-fibrosis-progression/train.csv', encoding = 'latin-1')
 test_df = pd.read_csv('.. /input/osic-pulmonary-fibrosis-progression/test.csv', encoding = 'latin-1')
 submission_df = pd.read_csv('.. /input/osic-pulmonary-fibrosis-progression/sample_submission.csv', encoding = 'latin-1')
 print(colored('Training data set shape.......... : ','yellow'),train_df.shape)
 print(colored('Test data set shape...............: ','red'),test_df.shape)
 print(colored('Submission data set shape.........: ','blue'),submission_df.shape )<count_missing_values>
clfs['AVG Models 1'] = avg_models1
clfs['AVG Models 2'] = avg_models2
clfs['AVG Models 3'] = avg_models3
get_results([clfs[key] for key in list(clfs.keys())if 'AVG' in key],
[key for key in list(clfs.keys())if 'AVG' in key],
X_train, y_train )
print(colored('Missing values in Train Set !!', 'yellow'))
print(train_df.isnull().sum())
 print("
")
 print(colored('Missing values in Test Set !!', 'red'))
print(test_df.isnull().sum() )<sort_values>
clfs['LassoLarsCV'].fit(X_train, y_train )
train_df.groupby(['Sex','SmokingStatus'])['FVC'].agg(['mean','std','count'] ).sort_values(by=['Sex','count'],ascending=False )<count_unique_values>
X_train_new = X_train.copy()
X_test_new = X_test.copy()
for column in X_train.columns:
if column in coefs[coefs==0].index:
del X_train_new[column]
del X_test_new[column]
print(colored("Total Patients in Train set...: ", 'yellow'),train_df['Patient'].count())
 print(colored("Total Patients in Test set.... : ", 'red'),test_df['Patient'].count())
 print("
")
 print(colored("Unique Patients in Train set...: ", 'yellow'),train_df['Patient'].nunique())
 print(colored("Unique Patients in Test set.... : ", 'red'),test_df['Patient'].nunique() )<count_values>
for col1 in coefs.sort_values(ascending=False ).head(20 ).index:
for col2 in coefs.sort_values(ascending=False ).head(20 ).index:
new_col = col1 + '_' + col2
if new_col not in X_train_new.columns:
X_train_new[new_col] = X_train_new[col1] * X_train_new[col2]
X_test_new[new_col] = X_test_new[col1] * X_test_new[col2]
for col in coefs.sort_values(ascending=False ).head(20 ).index:
new_col = col + '_cubic'
X_train_new[new_col] = X_train_new[col] ** 3
X_test_new[new_col] = X_test_new[col] ** 3
print(colored("Few most repeated Patients in Train set: ", 'yellow'))
print(train_df['Patient'].value_counts().head())
 print("
")
 print(colored("Few most repeated Patients in Test set: ", 'red'))
print(test_df['Patient'].value_counts().head() )<count_duplicates>
kfold = KFold(n_folds, shuffle=True, random_state=seed ).get_n_splits(X_train_new)
 get_results([clfs[key] for key in list(clfs.keys())if 'AVG' in key],
[key for key in list(clfs.keys())if 'AVG' in key],
X_train_new, y_train )
train_df_unique = train_df[['Patient', 'Age', 'Sex', 'SmokingStatus']].drop_duplicates().reset_index()
print(colored("Shape of unique patient data set : ",'yellow'),train_df_unique.shape)
 train_df_unique.head()<merge>
clfs['AVG Models 1'].fit(X_train_new, y_train)
 clfs['AVG Models 2'].fit(X_train_new, y_train )
patient_feq = train_df.groupby(['Patient'])['Patient'].count()
patient_feq = pd.DataFrame({'Patient':patient_feq.index, 'Frequency':patient_feq.values})
 train_df_unique = pd.merge(train_df_unique,patient_feq,how='inner',on='Patient' )<sort_values>
y_hat_model1 = np.expm1(clfs['AVG Models 1'].predict(X_test_new))
y_hat_model2 = np.expm1(clfs['AVG Models 2'].predict(X_test_new))
train_df_unique.sort_values(by='Frequency', ascending=False ).head()<merge>
price_predict =(y_hat_model1*0.5)+(y_hat_model2*0.5 )
<merge><EOS> 
solution = pd.DataFrame()
solution['Id'] = id_test
solution['SalePrice'] = price_predict
solution.to_csv('solution_final_v1.csv',index=False )