Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<compute_train_metric>
print(os.listdir(".. /input"))
warnings.filterwarnings('ignore')
def cv(model,cv=10):
cvx= np.sqrt(cross_val_score(model, Ntrain, y, scoring="accuracy", cv=10))
return(cvx )<find_best_params>
train = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/train.csv")
 train.head()
ran_for = RandomForestClassifier(n_estimators=50)
 cv(ran_for, cv=10 ).mean()<choose_model_class>
test = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/test.csv")
 test.head()
xgb_cl= xgb.XGBClassifier(colsample_bytree= 0.70,learning_rate=0.03, max_depth= 5, min_child_weight= 4, n_estimators= 50, nthread= 4, subsample= 0.7)
 cv(xgb_cl ).mean()<choose_model_class>
missing_percentage(test )
gbm = xgb.XGBClassifier(
 n_estimators= 200,
max_depth= 4,
min_child_weight= 2,
gamma=0.9,
subsample=0.8,
colsample_bytree=0.8,
objective= 'binary:logistic',
nthread= -1,
scale_pos_weight=1)
 cv(gbm ).mean()<save_to_csv>
( train.corr() **2)["SalePrice"].sort_values(ascending = False)[1:]
sub = pd.DataFrame()
sub['PassengerId'] = test_ID
xgb_cl.fit(Ntrain,y)
 ran_for.fit(Ntrain,y)
 gbm.fit(Ntrain,y)
 sub['Survived'] =(0.6*xgb_cl.predict(test)+0.3*gbm.predict(test)+0.1*ran_for.predict(test))
sub['Survived'] =sub['Survived'].apply(lambda f: 1 if f>=0.5 else 0)
 sub.to_csv('ensbleX.csv',index=False )<import_modules>
train = train[train.GrLivArea < 4500]
train.reset_index(drop = True, inplace = True)
 previous_train = train.copy()
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.preprocessing import StandardScaler, RobustScaler
from sklearn.model_selection import StratifiedKFold, cross_validate
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
from lightgbm import LGBMClassifier
from sklearn.neural_network import MLPClassifier
from hyperopt import hp, tpe, Trials, fmin, STATUS_OK, space_eval<load_from_csv>
train.drop(columns=['Id'],axis=1, inplace=True)
 test.drop(columns=['Id'],axis=1, inplace=True)
 y = train['SalePrice'].reset_index(drop=True)
 previous_train = train.copy()
train = pd.read_csv('/kaggle/input/titanic/train.csv')
 train.head()<load_from_csv>
all_data = pd.concat(( train, test)).reset_index(drop = True)
 all_data.drop(['SalePrice'], axis = 1, inplace = True )
test = pd.read_csv('/kaggle/input/titanic/test.csv')
 test.head()<load_from_csv>
missing_percentage(all_data )
submission = pd.read_csv('/kaggle/input/titanic/gender_submission.csv' )<count_missing_values>
missing_val_col = ["Alley",
"PoolQC",
"MiscFeature",
"Fence",
"FireplaceQu",
"GarageType",
"GarageFinish",
"GarageQual",
"GarageCond",
'BsmtQual',
'BsmtCond',
'BsmtExposure',
'BsmtFinType1',
'BsmtFinType2',
'MasVnrType']
for i in missing_val_col:
all_data[i] = all_data[i].fillna('None' )
print('---- train null ----
', train.isnull().sum())
 print('
---- test null ----
', test.isnull().sum() )<set_options>
missing_val_col2 = ['BsmtFinSF1',
'BsmtFinSF2',
'BsmtUnfSF',
'TotalBsmtSF',
'BsmtFullBath',
'BsmtHalfBath',
'GarageYrBlt',
'GarageArea',
'GarageCars',
'MasVnrArea']
for i in missing_val_col2:
all_data[i] = all_data[i].fillna(0)
 all_data['LotFrontage'] = all_data.groupby('Neighborhood')['LotFrontage'].transform(lambda x: x.fillna(x.mean()))
sns.set_palette('rainbow')
 sns.set_style('darkgrid' )<concatenate>
all_data['MSSubClass'] = all_data['MSSubClass'].astype(str)
 all_data['MSZoning'] = all_data.groupby('MSSubClass')['MSZoning'].transform(lambda x: x.fillna(x.mode() [0]))
all_data['YrSold'] = all_data['YrSold'].astype(str)
 all_data['MoSold'] = all_data['MoSold'].astype(str )
combine = pd.concat([train, test], sort=False )<categorify>
all_data['Functional'] = all_data['Functional'].fillna('Typ')
 all_data['Utilities'] = all_data['Utilities'].fillna('AllPub')
 all_data['Exterior1st'] = all_data['Exterior1st'].fillna(all_data['Exterior1st'].mode() [0])
 all_data['Exterior2nd'] = all_data['Exterior2nd'].fillna(all_data['Exterior2nd'].mode() [0])
 all_data['KitchenQual'] = all_data['KitchenQual'].fillna("TA")
 all_data['SaleType'] = all_data['SaleType'].fillna(all_data['SaleType'].mode() [0])
 all_data['Electrical'] = all_data['Electrical'].fillna("SBrkr")
combine['Sex'].replace(['male', 'female'], [0, 1], inplace=True )<data_type_conversions>
missing_percentage(all_data )
combine['Age'].fillna(combine['Age'].median() , inplace=True )<normalization>
numeric_feats = all_data.dtypes[all_data.dtypes != "object"].index
skewed_feats = all_data[numeric_feats].apply(lambda x: skew(x)).sort_values(ascending=False)
 skewed_feats
standard = StandardScaler()
combine[['Age']] = standard.fit_transform(combine[['Age']] )<correct_missing_values>
def fixing_skewness(df):

 numeric_feats = df.dtypes[df.dtypes != "object"].index
skewed_feats = df[numeric_feats].apply(lambda x: skew(x)).sort_values(ascending=False)
 high_skew = skewed_feats[abs(skewed_feats)> 0.5]
skewed_features = high_skew.index
for feat in skewed_features:
df[feat] = boxcox1p(df[feat], boxcox_normmax(df[feat] + 1))
fixing_skewness(all_data )
combine['Fare'].fillna(combine['Fare'].median() , inplace=True )<normalization>
all_data['TotalSF'] =(all_data['TotalBsmtSF']
+ all_data['1stFlrSF']
+ all_data['2ndFlrSF'])
 all_data['YrBltAndRemod'] = all_data['YearBuilt'] + all_data['YearRemodAdd']
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'] 
)
rscaler = RobustScaler(quantile_range=(25., 75.))
 combine[['Fare']] = rscaler.fit_transform(combine[['Fare']] )<feature_engineering>
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 )
combine['Pclass'] =(combine['Pclass'] - 1)/ 2<feature_engineering>
all_data = all_data.drop(['Utilities', 'Street', 'PoolQC',], axis=1 )
combine['FamilySize'] = combine['SibSp'] + combine['Parch']<normalization>
final_features = pd.get_dummies(all_data ).reset_index(drop=True)
 final_features.shape
standard = StandardScaler()
combine[['FamilySize']] = standard.fit_transform(combine[['FamilySize']] )<correct_missing_values>
X = final_features.iloc[:len(y), :]
X_sub = final_features.iloc[len(y):, :]
combine['Embarked'].fillna(combine['Embarked'].mode() [0], inplace=True )<categorify>
outliers = [30, 88, 462, 631, 1322]
X = X.drop(X.index[outliers])
 y = y.drop(y.index[outliers] )
combine = pd.get_dummies(combine, columns=['Embarked'], drop_first=True )<categorify>
counts = X.BsmtUnfSF.value_counts()
combine = pd.get_dummies(combine, columns=['Title'], drop_first=True )<feature_engineering>
counts.iloc[0]
tmp = pd.DataFrame()
tmp['Cabin'] = combine['Cabin']
tmp['Cabin'].loc[tmp['Cabin'].notnull() ] = 1
combine['Cabin'] = tmp['Cabin']
combine['Cabin'].replace(np.nan, 0, inplace=True )<drop_column>
for i in X.columns:
counts = X[i].value_counts()
print(counts )
combine_cleaned = combine.drop(['PassengerId', 'Name', 'SibSp', 'Parch', 'Ticket'], axis=1)
 combine_cleaned.head()<train_on_grid>
def overfit_reducer(df):

 overfit = []
for i in df.columns:
counts = df[i].value_counts()
zeros = counts.iloc[0]
if zeros / len(df)* 100 > 99.94:
overfit.append(i)
 overfit = list(overfit)
 return overfit
overfitted_features = overfit_reducer(X)
 X = X.drop(overfitted_features, axis=1)
 X_sub = X_sub.drop(overfitted_features, axis=1 )
def hyperopt_and_pred(models, max_evals=100):
preds = []
for model in models:
clf = model['classifier']
space = model['space']
def objective(space):
classifier = clf(**space)
 classifier.fit(X_train, y_train)
 skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=0)
 acc = cross_validate(estimator=classifier, X=X_train, y=y_train, cv=skf)
 mean_acc = np.mean(acc['test_score'])
 return{'loss': 1 - mean_acc, 'status': STATUS_OK}
print('='*10, str(clf ).split('.')[-1].replace(''>', ''), '='*10)
 trials = Trials()
best = fmin(objective, space, algo=tpe.suggest, max_evals=max_evals, trials=trials, verbose=1)
 best_params = space_eval(space, best)
 predict = clf(**best_params ).fit(X_train, y_train ).predict(X_test)
 acc = 1 - trials.best_trial['result']['loss']
preds.append(predict)
 print('
', 'best parameters:', best_params)
 print('accuracy:', f'{acc:.04f}', '
')
 return preds<init_hyperparams>
X_train, X_test, y_train, y_test = train_test_split(X, y,test_size =.33, random_state = 0 )
models = [
{
'classifier': LogisticRegression,
'space': {
'C': hp.uniform('C', 0, 100),
'max_iter': hp.choice('max_iter', [2000]),
'solver': hp.choice('solver', ['lbfgs', 'liblinear', 'sag', 'saga'])
 }
},
{
'classifier': RandomForestClassifier,
'space': {
'n_estimators': hp.choice('n_estimators', np.arange(10, 401, 10)) ,
'max_depth': hp.uniform('max_depth', 1, 5),
'criterion': hp.choice('criterion', ['gini', 'entropy'])
 }
},
{
'classifier': KNeighborsClassifier,
'space': {
'n_neighbors': hp.choice('n_neighbors', np.arange(1, 15))
}
},
{
'classifier': AdaBoostClassifier,
'space': {
'n_estimators': hp.choice('n_estimators', [30,50,100,200,300]),
'learning_rate': hp.uniform('learning_rate', 0.8, 1.4)
 }
},
{
'classifier': SVC,
'space': {
'C': hp.uniform('C', 0, 2),
'gamma': hp.loguniform('gamma', -8, 2),
'kernel': hp.choice('kernel', ['rbf', 'poly', 'sigmoid'])
 }
},
{
'classifier': LGBMClassifier,
'space': {
'objective': hp.choice('objective', ['binary']),
'max_bin': hp.choice('max_bin', np.arange(64, 513, 1)) ,
'num_leaves': hp.choice('num_leaves', np.arange(30, 201, 10)) ,
'max_depth': hp.choice('max_depth', np.arange(3, 10, 1)) ,
'learning_rate': hp.uniform('learning_rate', 0.03, 0.2)
 }
},
{
'classifier': MLPClassifier,
'space': {
'hidden_layer_sizes': hp.choice('hidden_layer_sizes', [8, 16, 32,(8,8),(16,16)]),
'activation': hp.choice('activation', ['relu', 'tanh']),
'max_iter': hp.choice('max_iter', [3000])
 }
}
]<predict_on_test>
sample_train['mean_sale_price'] = sample_train.SalePrice.mean()
sample_train['mse'] = np.square(sample_train.mean_sale_price - sample_train.SalePrice)
 sample_train.mse.mean()
print("Mean Squared Error(MSE)for average line is : {}".format(sample_train.mse.mean()))
predictions = hyperopt_and_pred(models )<categorify>
sample_train['Linear_Yhat'] = beta_0 + beta_1*sample_train['GrLivArea']
ensembled_pred = np.round(sum(predictions)/ len(predictions)).astype('int' )<save_to_csv>
print("Mean Squared Error(MSE)for regression line is : {}".format(np.square(sample_train['SalePrice'] - sample_train['Linear_Yhat'] ).mean()))
results = pd.Series(ensembled_pred, name='Survived')
 submission = pd.concat([submission['PassengerId'], results], axis=1)
 submission.to_csv('submission.csv', index=False )<set_options>
mean_squared_error(sample_train['SalePrice'], sample_train.Linear_Yhat )
%matplotlib inline
pd.options.display.float_format = '{:.2f}'.format<load_from_csv>
lin_reg = LinearRegression(normalize=True, n_jobs=-1)
 lin_reg.fit(X_train, y_train)
 y_pred = lin_reg.predict(X_test )
train = pd.read_csv('.. /input/titanic/train.csv')
 train.head()<count_missing_values>
print('%.2f'%mean_squared_error(y_test, y_pred))
train.isnull().sum()<count_missing_values>
lin_reg = LinearRegression()
cv = KFold(shuffle=True, random_state=2, n_splits=10)
 scores = cross_val_score(lin_reg, X,y,cv = cv, scoring = 'neg_mean_absolute_error' )
train.isnull().sum()<count_values>
alpha_ridge = [-3,-2,-1,1e-15, 1e-10, 1e-8,1e-5,1e-4, 1e-3,1e-2,0.5,1,1.5, 2,3,4, 5, 10, 20, 30, 40]
temp_rss = {}
temp_mse = {}
for i in alpha_ridge:
ridge = Ridge(alpha= i, normalize=True)
 ridge.fit(X_train, y_train)
 y_pred = ridge.predict(X_test)
 mse = mean_squared_error(y_test, y_pred)
 rss = sum(( y_pred-y_test)**2)
 temp_mse[i] = mse
temp_rss[i] = rss
sur = train['Survived'].value_counts()
survival_rate = [sur[0]/len(train)*100,sur[1]/len(train)*100]<feature_engineering>
for key, value in sorted(temp_mse.items() , key=lambda item: item[1]):
print("%s: %s" %(key, value))
survived_male = len(train[(train['Sex'] == 'male')&(train['Survived'] == 1)])/len(train[train['Sex'] == 'male'])*100
survived_female = len(train[(train['Sex'] == 'female')&(train['Survived'] == 1)])/len(train[train['Sex'] == 'female'])*100
male = [survived_male,100-survived_male]
female = [survived_female,100-survived_female]<feature_engineering>
for key, value in sorted(temp_rss.items() , key=lambda item: item[1]):
print("%s: %s" %(key, value))
survived_class_1 = len(train[(train['Pclass'] == 1)& train['Survived'] == 1])/len(train[(train['Pclass'] == 1)])*100
survived_class_2 = len(train[(train['Pclass'] == 2)& train['Survived'] == 1])/len(train[(train['Pclass'] == 2)])*100
survived_class_3 = len(train[(train['Pclass'] == 3)& train['Survived'] == 1])/len(train[(train['Pclass'] == 3)])*100
class_1 = [survived_class_1,100-survived_class_1]
class_2 = [survived_class_2,100-survived_class_2]
class_3 = [survived_class_3,100-survived_class_3]<feature_engineering>
temp_rss = {}
temp_mse = {}
for i in alpha_ridge:
lasso_reg = Lasso(alpha= i, normalize=True)
 lasso_reg.fit(X_train, y_train)
 y_pred = lasso_reg.predict(X_test)
 mse = mean_squared_error(y_test, y_pred)
 rss = sum(( y_pred-y_test)**2)
 temp_mse[i] = mse
temp_rss[i] = rss
survived_S = len(train[(train['Embarked'] == 'S')&(train['Survived'] == 1)])/len(train[train['Embarked'] == 'S'])*100
survived_C = len(train[(train['Embarked'] == 'C')&(train['Survived'] == 1)])/len(train[train['Embarked'] == 'C'])*100
survived_Q = len(train[(train['Embarked'] == 'Q')&(train['Survived'] == 1)])/len(train[train['Embarked'] == 'Q'])*100
S = [survived_S,100-survived_S]
C = [survived_C,100-survived_C]
Q = [survived_Q,100-survived_Q]<feature_engineering>
for key, value in sorted(temp_mse.items() , key=lambda item: item[1]):
print("%s: %s" %(key, value))
train['Age_Mode'] = train['Age'].fillna(value = 24)
 train['Age_Med'] = train['Age'].fillna(train['Age'].median())
 train['Age_Mean'] = train['Age'].fillna(train['Age'].mean() )<feature_engineering>
for key, value in sorted(temp_rss.items() , key=lambda item: item[1]):
print("%s: %s" %(key, value))
train['Age'] = train['Age'].fillna(train['Age'].median())
 train = train.drop(columns= ['Age_Mode','Age_Med','Age_Mean'])
 train.head()<create_dataframe>
temp_rss = {}
temp_mse = {}
for i in alpha_ridge:
lasso_reg = ElasticNet(alpha= i, normalize=True)
 lasso_reg.fit(X_train, y_train)
 y_pred = lasso_reg.predict(X_test)
 mse = mean_squared_error(y_test, y_pred)
 rss = sum(( y_pred-y_test)**2)
 temp_mse[i] = mse
temp_rss[i] = rss
def get_age_group(dataframe,column_name):
dataframe[column_name] = dataframe[column_name].apply(np.ceil)
 age_group = {0:list(range(0,21)) ,1:list(range(21,41)) ,2:list(range(41,61)) ,3:list(range(61,81)) }
col = list(dataframe.columns)
 index = col.index(column_name)
 age = []
for j in range(len(dataframe)) :
for k in age_group.keys() :
for i in range(len(age_group[k])) :
if(age_group[k][i] == dataframe.iloc[j,index]):
age.append(k)
 dataframe['Age_Group'] = age<feature_engineering>
for key, value in sorted(temp_mse.items() , key=lambda item: item[1]):
print("%s: %s" %(key, value))
def get_initials(dataframe,column_name):
sub = []
initials = ['Mrs.','Ms.','Mr.','Miss.','Master.','Lady.','Don.','Rev.','Dr.','Mme.','Major.','Sir.','Mlle.','Col.','Capt.','Countess.','Jonkheer.','Dona.']
name = dataframe[column_name]
for i in range(len(name)) :
split_names = name[i].split()
for j in range(len(split_names)) :
if(split_names[j] in initials):
sub.append(split_names[j])
 dataframe[column_name] = sub<count_values>
for key, value in sorted(temp_rss.items() , key=lambda item: item[1]):
print("%s: %s" %(key, value))
get_initials(train,'Name')
 train['Name'].value_counts()<categorify>
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 )
le = LabelEncoder()
train['Name'] = le.fit_transform(train['Name'])
 encoded_values = train['Name'].unique()
decoded_values = le.inverse_transform(encoded_values)
 initials = {}
for i in range(len(encoded_values)) :
initials.setdefault(decoded_values[i],encoded_values[i] )<categorify>
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]
train['Name'] = le.fit_transform(train['Name'])
 train['Sex'] = le.fit_transform(train['Sex'])
 embarked = {0:'S',1:'C',2:'Q'}
train['Embarked'] = train['Embarked'].fillna('Q')
 train['Embarked'] = le.fit_transform(train['Embarked'] )<drop_column>
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,))
train = train.drop(columns = ['PassengerId','Ticket','Cabin','Age_Group'])
 train.head()<import_modules>
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 )
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2<prepare_x_and_y>
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,
)
features = train.iloc[:,1:]
target = train.iloc[:,0]<train_model>
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 )
best_features = SelectKBest(score_func = chi2,k = 8)
 fit = best_features.fit(features,target)
 dfscores = pd.DataFrame(fit.scores_)
 dfcolumns = pd.DataFrame(features.columns)
 featureScores = pd.concat([dfcolumns,dfscores],axis=1)
 featureScores.columns = ['Column','Score']
print(featureScores.nlargest(8,'Score'))<drop_column>
stack_gen = StackingCVRegressor(regressors=(ridge, lasso, elasticnet, xgboost, lightgbm),
meta_regressor=xgboost,
use_features_in_secondary=True )
train = train.drop(columns = ['Embarked','SibSp','Parch'])
 train.head()<normalization>
score = cv_rmse(ridge)
 print("Ridge: {:.4f}({:.4f})
 ".format(score.mean() , score.std()), datetime.now() ,)
 score = cv_rmse(lasso)
 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() ,)
 ".format(score.mean() , score.std()), datetime.now() ,)
 score = cv_rmse(xgboost)
 print("xgboost: {:.4f}({:.4f})
 ".format(score.mean() , score.std()), datetime.now() , )
sc = StandardScaler()
features = train[['Pclass','Sex','Name','Fare']].values
features = sc.fit_transform(features)
 target = train['Survived'].values<split>
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('xgboost')
 xgb_model_full_data = xgboost.fit(X, y)
 print('lightgbm')
 lgb_model_full_data = lightgbm.fit(X, y )
x_train, x_test, y_train, y_test = train_test_split(features, target, test_size = 0.25, random_state = 2 )<train_on_grid>
1.0 * elastic_model_full_data.predict(X )
def grid_search_cv(classifier,hyperparameters):
GSCV = GridSearchCV(classifier,hyperparameters,cv = 10)
 model = GSCV.fit(x_test,y_test)
 print(model)
 print('HIGHEST ACCURACY : ','{0:.2%}'.format(model.best_score_))
print('BEST PARAMETERS : ',model.best_params_ )<import_modules>
def blend_models_predict(X):
return(( 0.1 * elastic_model_full_data.predict(X)) + \ 
(0.05 * lasso_model_full_data.predict(X)) + \ 
(0.2 * ridge_model_full_data.predict(X)) + \ 
(0.1 * svr_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))))
from sklearn.linear_model import LogisticRegression<choose_model_class>
print('RMSLE score on train data:')
 print(rmsle(y, blend_models_predict(X)) )
classifier_lr = LogisticRegression(random_state = 0,C=10,penalty= 'l2' )<compute_test_metric>
print('Predict submission')
 submission = pd.read_csv(".. /input/house-prices-advanced-regression-techniques/sample_submission.csv")
 submission.iloc[:,1] = np.floor(np.expm1(blend_models_predict(X_sub)) )
model_evaluation(classifier_lr )<import_modules>
print('Blend with Top Kernels submissions
')
 sub_1 = pd.read_csv('.. /input/top-house-price-kernel-predictions/blending_high_scores_top_1_8th_place.csv')
 sub_2 = pd.read_csv('.. /input/top-house-price-kernel-predictions/house_prices_ensemble_7models.csv')
 sub_3 = pd.read_csv('.. /input/top-house-price-kernel-predictions/blend_and_stack_LR.csv')
 submission.iloc[:,1] = np.floor(( 0.25 * np.floor(np.expm1(blend_models_predict(X_sub)))) + 
(0.25 * sub_1.iloc[:,1])+ 
(0.25 * sub_2.iloc[:,1])+ 
(0.25 * sub_3.iloc[:,1]))
<choose_model_class><EOS> 
q1 = submission['SalePrice'].quantile(0.005)
 q2 = submission['SalePrice'].quantile(0.995)
 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 )