Kaggle competition: House Prices - Advanced Regression Techniques
cat=train_data.drop(['RefId','IsBadBuy'],axis=1 ).columns[train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes=='object']
cat<filter>
sns.set_style('darkgrid')
 color = sns.color_palette()
pd.set_option('display.max_columns', None )
train_data[cat[0]]<categorify>
train = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/train.csv')
 test = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/test.csv' )
pd.get_dummies(train_data[cat[0]] )<categorify>
train_ID = train['Id']
test_ID = test['Id']
train.drop('Id', axis = 1, inplace = True)
 test.drop('Id', axis = 1, inplace = True )
for i in cat:
dummies=pd.get_dummies(train_data[i])
 dummies.columns=str(i)+'_'+dummies.columns
train_data=pd.concat([train_data,dummies],axis=1)
 train_data.drop(i,inplace=True,axis=1)
 dummies=pd.get_dummies(test_data[i])
 dummies.columns=str(i)+'_'+dummies.columns
test_data=pd.concat([test_data,dummies],axis=1)
 test_data.drop(i,inplace=True,axis=1 )<feature_engineering>
outs = train[(train['GrLivArea']>4000)&(train['SalePrice']<200000)]
train.drop(outs.index, inplace = True )
for i in train_data.drop('IsBadBuy',axis=1 ).columns:
if i not in test_data.columns:
test_data[i]=np.zeros(len(test_data))
for i in test_data.columns:
if i not in train_data.columns:
train_data[i]=np.zeros(len(train_data))<drop_column>
train['SalePrice'] = np.log1p(train['SalePrice'] )
test_data=test_data[train_data.drop("IsBadBuy",axis=1 ).columns]<prepare_x_and_y>
n_train = train.shape[0]
n_test = test.shape[0]
target = train.SalePrice.values
all_data = pd.concat(( train, test)).reset_index(drop = True)
 all_data.drop('SalePrice', axis = 1, inplace = True)
 print('Shape of complete data ', all_data.shape )
X=train_data.drop(['RefId','IsBadBuy'],axis=1)
 y=train_data['IsBadBuy']<split>
train.isna().sum()
X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=42 )<train_model>
all_data['PoolQC'].value_counts()
KNN=KNeighborsClassifier(n_neighbors=9)
 KNN.fit(X_train,y_train )<compute_test_metric>
all_data['PoolQC'].fillna('None', inplace = True )
KNN.score(X_test,y_test )<drop_column>
all_data['MiscFeature'].value_counts()
td=test_data.drop('RefId',axis=1 )<predict_on_test>
all_data['MiscFeature'].fillna('None', inplace = True )
predict=KNN.predict(td )<prepare_output>
all_data['Alley'].value_counts()
Subm=pd.DataFrame(data=predict,columns=['IsBadBuy'])
 Subm.head()<prepare_output>
all_data['Alley'].fillna('None', inplace = True )
Subm['RefId']=test_data['RefId']
Subm.set_index('RefId',inplace=True )<save_to_csv>
all_data['Fence'].fillna('None', inplace = True )
Subm.to_csv('DGK_KNN' )<load_from_csv>
all_data['FireplaceQu'].fillna('None', inplace = True )
train_data=pd.read_csv('/kaggle/input/DontGetKicked/training.csv')
 train_data.head()<load_from_csv>
fillers = all_data.groupby('Neighborhood')['LotFrontage'].median()
test_data=pd.read_csv('/kaggle/input/DontGetKicked/test.csv')
 test_data.head()<count_missing_values>
a = all_data[all_data['LotFrontage'].isna() ]
train_data.isnull().sum()<count_missing_values>
all_data['LotFrontage'] = all_data['LotFrontage'].fillna(all_data['Neighborhood'].map(fillers))
test_data.isnull().sum()<count_values>
for col in 'GarageFinish', 'GarageQual', 'GarageCond', 'GarageType':
all_data[col].fillna('None', inplace = True )
train_data['IsBadBuy'].value_counts()<count_values>
for col in 'GarageYrBlt', 'GarageArea','GarageCars':
all_data[col].fillna(0, inplace = True )
train_data['Model'].value_counts()<drop_column>
for col in 'BsmtFinSF1', 'BsmtFinSF2', 'BsmtUnfSF','TotalBsmtSF', 'BsmtFullBath', 'BsmtHalfBath':
all_data[col].fillna(0, inplace = True )
train_data.drop('Model',axis=1,inplace=True)
 test_data.drop('Model',axis=1,inplace=True )<count_values>
for col in 'BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1' ,'BsmtFinType2':
all_data[col].fillna('None', inplace = True)
train_data['Trim'].value_counts()<drop_column>
all_data["MasVnrType"] = all_data["MasVnrType"].fillna("None")
 all_data["MasVnrArea"] = all_data["MasVnrArea"].fillna(0 )
train_data.drop('Trim',axis=1,inplace=True)
 test_data.drop('Trim',axis=1,inplace=True )<count_values>
all_data['MSZoning'] = all_data['MSZoning'].fillna(all_data['MSZoning'].mode() [0] )
train_data['SubModel'].value_counts()<drop_column>
all_data = all_data.drop(['Utilities'], axis=1 )
train_data.drop('SubModel',axis=1,inplace=True)
 test_data.drop('SubModel',axis=1,inplace=True )<count_values>
all_data["Functional"] = all_data["Functional"].fillna("Typ" )
train_data['Color'].value_counts()<count_values>
all_data['Electrical'] = all_data['Electrical'].fillna(all_data['Electrical'].mode() [0] )
test_data['Color'].value_counts()<data_type_conversions>
all_data['KitchenQual'] = all_data['KitchenQual'].fillna(all_data['KitchenQual'].mode() [0] )
train_data['Color'].fillna(value='Color_Unknown',inplace=True)
 test_data['Color'].fillna(value='Color_Unknown',inplace=True )<count_missing_values>
all_data['Exterior1st'] = all_data['Exterior1st'].fillna(all_data['Exterior1st'].mode() [0])
 all_data['Exterior2nd'] = all_data['Exterior2nd'].fillna(all_data['Exterior2nd'].mode() [0] )
print("Number of null values in color column"+str(train_data['Color'].isnull().sum()))
print("Number of null values in color column"+str(test_data['Color'].isnull().sum()))<count_values>
all_data['SaleType'] = all_data['SaleType'].fillna(all_data['SaleType'].mode() [0] )
train_data['Transmission'].value_counts()<count_values>
all_data['MSSubClass'] = all_data['MSSubClass'].fillna("None" )
test_data['Transmission'].value_counts()<filter>
all_data.isna().sum()
train_data[train_data['Transmission']=='Manual']<rename_columns>
all_data["AllSF"] = all_data["GrLivArea"] + all_data["TotalBsmtSF"]
all_data['Area'] = all_data['LotArea']*all_data['LotFrontage']
all_data['Area_log'] = np.log1p(all_data['Area'] )
train_data['Transmission'].replace("Manual","MANUAL",inplace=True )<count_values>
num_cols = all_data.columns[all_data.dtypes != 'object']
train_data['Transmission'].value_counts()<data_type_conversions>
c = []
for col in num_cols:
if(all_data[col].nunique() < 15):
c.append(col)
train_data['Transmission'].fillna(value='Transmission_Unk',inplace=True)
 test_data['Transmission'].fillna(value='Transmission_Unk',inplace=True )<count_values>
c.remove('PoolArea' )
train_data['WheelTypeID'].value_counts()<drop_column>
for col in c:
all_data[col] = all_data[col].astype(str)
train_data.drop('WheelTypeID',axis=1,inplace=True)
 test_data.drop('WheelTypeID',axis=1,inplace=True )<count_values>
cats = all_data.columns[all_data.dtypes == 'object']
train_data['WheelType'].value_counts()<count_values>
ordinals = ['MSSubClass','ExterQual','LotShape','BsmtQual','BsmtCond','BsmtExposure',
'BsmtFinType1', 'BsmtFinType2','HeatingQC','Functional','FireplaceQu','KitchenQual',
'GarageFinish','GarageQual','GarageCond','PoolQC','Fence']
orders=[ ['20','30','40','45','50','60','70','75','80','85', '90','120','150','160','180','190'],
['Fa','TA','Gd','Ex'], ['Reg','IR1' ,'IR2','IR3'],
['None','Fa','TA','Gd','Ex'], ['None','Po','Fa','TA','Gd','Ex'],
['None','No','Mn','Av','Gd'],
['None','Unf','LwQ', 'Rec','BLQ','ALQ' , 'GLQ' ],
['None','Unf','LwQ', 'Rec','BLQ','ALQ' , 'GLQ' ],
['Po','Fa','TA','Gd','Ex'], ['Sev','Maj2','Maj1','Mod','Min2','Min1','Typ'],
['None','Po','Fa','TA','Gd','Ex'], ['Fa','TA','Gd','Ex'],
['None','Unf','RFn','Fin'], ['None','Po','Fa','TA','Gd','Ex'],
['None','Po','Fa','TA','Gd','Ex'],
['None','Fa','Gd','Ex'], ['None','MnWw','GdWo','MnPrv','GdPrv'] ]
test_data['WheelType'].value_counts()<data_type_conversions>
from sklearn.preprocessing import OrdinalEncoder
train_data['WheelType'].fillna(value='WheelType_unk',inplace=True)
 test_data['WheelType'].fillna(value='WheelType_unk',inplace=True )<count_values>
for i in range(len(orders)) :
oe = OrdinalEncoder(categories={0:orders[i]})
 all_data.loc[:,ordinals[i]] = oe.fit_transform(all_data.loc[:,ordinals[i]].values.reshape(-1,1))
train_data['WheelType'].value_counts()<count_values>
all_data['TotalSF'] = all_data['TotalBsmtSF'] + all_data['1stFlrSF'] + all_data['2ndFlrSF']
train_data['Nationality'].value_counts()<count_values>
for i in ordinals:
all_data[i] = all_data[i].astype('str' )
test_data['Nationality'].value_counts()<data_type_conversions>
nums = all_data.columns[all_data.dtypes != 'object']
skews = all_data[nums].apply(lambda x : skew(x.dropna())).sort_values(ascending = False)
 skewness = pd.DataFrame({'Skewness':skews} )
train_data['Nationality'].fillna(value='Nationality_unk',inplace=True)
 test_data['Nationality'].fillna(value='Nationality_unk',inplace=True )<count_values>
skewness = skewness[abs(skewness)>0.75]
train_data['Size'].value_counts() 
<count_values>
from scipy.special import boxcox1p
test_data['Size'].value_counts()<data_type_conversions>
skewed_features = skewness.index
train_data['Size'].fillna(value='Size_unk',inplace=True)
 test_data['Size'].fillna(value='Size_unk',inplace=True )<count_values>
skewed_features
lam = 0.15
for feature in skewed_features:
all_data[feature] = boxcox1p(all_data[feature], lam )
train_data['TopThreeAmericanName'].value_counts()<count_values>
rest = set(all_data.columns ).difference(ordinals)
 len(rest )
test_data['TopThreeAmericanName'].value_counts()<data_type_conversions>
OHE_encoded = pd.get_dummies(all_data[rest],drop_first=True )
train_data['TopThreeAmericanName'].fillna(value='Top_unk',inplace=True)
 test_data['TopThreeAmericanName'].fillna(value='Top_unk',inplace=True )<count_values>
df = pd.concat(( OHE_encoded, all_data[ordinals]), axis = 1 )
train_data['PRIMEUNIT'].value_counts()<count_values>
for col in df:
df[col] = pd.to_numeric(df[col] )
test_data['PRIMEUNIT'].value_counts()<data_type_conversions>
train = df[:n_train]
test = df[n_train:]
train_data['PRIMEUNIT'].fillna(value='Prime_unk',inplace=True)
 test_data['PRIMEUNIT'].fillna(value='Prime_unk',inplace=True )<categorify>
from sklearn.linear_model import ElasticNet, Lasso, BayesianRidge, LassoLarsIC
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from sklearn.kernel_ridge import KernelRidge
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import RobustScaler
from sklearn.base import BaseEstimator, TransformerMixin, RegressorMixin, clone
from sklearn.model_selection import KFold, cross_val_score, train_test_split
from sklearn.metrics import mean_squared_error
import xgboost as xgb
import lightgbm as lgb
from sklearn.svm import SVR
train_data['AUCGUART'].replace("AGREEN","GREEN",inplace=True)
 test_data['AUCGUART'].replace("ARED","RED",inplace=True )<data_type_conversions>
!pip install pycaret[full]
train_data['AUCGUART'].fillna(value='AUC_unk',inplace=True)
 test_data['AUCGUART'].fillna(value='AUC_unk',inplace=True )<drop_column>
X_train, X_val, y_train, y_val = train_test_split(train, target, test_size =.2 )
train_data.drop(['MMRAcquisitionAuctionAveragePrice', 'MMRAcquisitionAuctionCleanPrice' ,
'MMRAcquisitionRetailAveragePrice' , 'MMRAcquisitonRetailCleanPrice' ,
'MMRCurrentAuctionAveragePrice' , 'MMRCurrentAuctionCleanPrice' ,
'MMRCurrentRetailAveragePrice' , 'MMRCurrentRetailCleanPrice' ],
axis=1,inplace=True)
 test_data.drop(['MMRAcquisitionAuctionAveragePrice', 'MMRAcquisitionAuctionCleanPrice' ,
'MMRAcquisitionRetailAveragePrice' , 'MMRAcquisitonRetailCleanPrice' ,
'MMRCurrentAuctionAveragePrice' , 'MMRCurrentAuctionCleanPrice' ,
'MMRCurrentRetailAveragePrice' , 'MMRCurrentRetailCleanPrice' ],
axis=1,inplace=True )<drop_column>
def rmse_cv(model):
kf = KFold(n_splits = 6, shuffle = True, random_state = 42 ).get_n_splits(train.values)
 rmse = np.sqrt(-cross_val_score(model, train.values, target, scoring ='neg_mean_squared_error', cv = kf))
rmse_val = np.sqrt(-cross_val_score(model, train.values, target, scoring = 'neg_mean_squared_error', cv = kf))
return "Train ",rmse, "Validation", rmse_val
train_data.drop(['PurchDate'],axis=1,inplace=True)
 test_data.drop(['PurchDate'],axis=1,inplace=True )<drop_column>
lasso = make_pipeline(RobustScaler() , Lasso(alpha = 0.0002144127223451149, max_iter=200000))
train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes!='object'<drop_column>
ENet = make_pipeline(RobustScaler() , ElasticNet(alpha=0.0010936129372972772, l1_ratio=0.21322431030386174, random_state=3, max_iter= 200000))
not_categorical=train_data.drop(['RefId','IsBadBuy'],axis=1 ).columns[train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes!='object']<feature_engineering>
KRR = KernelRidge(alpha=0.015, kernel='polynomial', degree=1, coef0=0.5)
for i in not_categorical:
maximum=np.max(train_data[i])
 train_data[i]=train_data[i]/maximum
maximum_test=np.max(test_data[i])
 test_data[i]=test_data[i]/maximum_test<drop_column>
GBoost = GradientBoostingRegressor(n_estimators=1070, max_depth = 3, min_samples_split = 0.9900745088899041,
min_samples_leaf = 0.0024212450940811515, max_features = 'log2',
learning_rate = 0.23500000000000001, loss = 'huber',
criterion = 'mse')
categorical=train_data.drop(['RefId','IsBadBuy'],axis=1 ).columns[train_data.drop(['RefId','IsBadBuy'],axis=1 ).dtypes=='object']<filter>
model_xgb = xgb.XGBRegressor(base_score=0.5, booster='gbtree', colsample_bylevel=1,
colsample_bynode=1, colsample_bytree=0.9, gamma=0, gpu_id=-1,
importance_type='gain', interaction_constraints='',
learning_rate=0.191, max_delta_step=0, max_depth=2,
min_child_weight=3, monotone_constraints='() ',
n_estimators=180, n_jobs=-1, num_parallel_tree=1,
objective='reg:squarederror', random_state=123, reg_alpha=0.001,
reg_lambda=0.4, scale_pos_weight=13.8, subsample=0.7,
tree_method='auto', validate_parameters=1, verbosity=0 )
train_data[categorical[0]]<categorify>
model_lgb = lgb.LGBMRegressor(objective='regression',num_leaves=5,
learning_rate=0.05, n_estimators=720,
max_bin = 55, bagging_fraction = 0.8,
bagging_freq = 5, feature_fraction = 0.2319,
feature_fraction_seed=9, bagging_seed=9,
min_data_in_leaf =6, min_sum_hessian_in_leaf = 11 )
pd.get_dummies(train_data[categorical[0]] )<categorify>
model_svr = SVR()
model_svr.get_params()
for i in categorical:
dummies= pd.get_dummies(train_data[i])
 dummies.columns=str(i)+'_'+dummies.columns
train_data=pd.concat([train_data,dummies],axis=1)
 train_data.drop(i,inplace=True,axis=1)
 dummies=pd.get_dummies(test_data[i])
 dummies.columns=str(i)+'_'+dummies.columns
test_data=pd.concat([test_data,dummies],axis=1)
 test_data.drop(i,inplace=True,axis=1 )<feature_engineering>
score = rmse_cv(lasso)
 print("
Lasso score on Training Set: {:.4f}({:.4f})
 ".format(score[1].mean() , score[1].std()))
print("
Lasso score on Test Set: {:.4f}({:.4f})
 ".format(score[3].mean() , score[3].std()))
for i in train_data.drop('IsBadBuy',axis=1 ).columns:
if i not in test_data.columns:
test_data[i]=np.zeros(len(test_data))<feature_engineering>
score = rmse_cv(ENet)
 print("
ElasticNet score on Training Set: {:.4f}({:.4f})
 ".format(score[1].mean() , score[1].std()))
print("
ElasticNet score on Test Set: {:.4f}({:.4f})
 ".format(score[3].mean() , score[3].std()))
for i in test_data.columns:
if i not in train_data.columns:
train_data[i]=np.zeros(len(train_data))<drop_column>
score = rmse_cv(KRR)
 print("
Kernel Ridge score on Training Set: {:.4f}({:.4f})
 ".format(score[1].mean() , score[1].std()))
print("
Kernel Ridge score Test Set: {:.4f}({:.4f})
 ".format(score[3].mean() , score[3].std()))
test_data=test_data[train_data.drop("IsBadBuy",axis=1 ).columns]<prepare_x_and_y>
score = rmse_cv(GBoost)
 print("
Gradient Boosting scoreon Training Set: {:.4f}({:.4f})
 ".format(score[1].mean() , score[1].std()))
print("
Gradient Boosting score Test aSet: {:.4f}({:.4f})
 ".format(score[3].mean() , score[3].std()))
X=train_data.drop(['RefId','IsBadBuy'],axis=1)
 y=train_data['IsBadBuy']<split>
score = rmse_cv(model_xgb)
 print("
Xgboost Training Set: {:.4f}({:.4f})
 ".format(score[1].mean() , score[1].std()))
print("
Xgboost Test Set: {:.4f}({:.4f})
 ".format(score[3].mean() , score[3].std()))
X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=42 )<import_modules>
score = rmse_cv(model_lgb)
 print("\LGM Training Set: {:.4f}({:.4f})
 ".format(score[1].mean() , score[1].std()))
print("\LGM Test Set: {:.4f}({:.4f})
 ".format(score[3].mean() , score[3].std()))
from sklearn.neighbors import KNeighborsClassifier<import_modules>
from sklearn.model_selection import GridSearchCV, RandomizedSearchCV, KFold
from sklearn import metrics
from sklearn.neighbors import KNeighborsClassifier<import_modules>
from skopt import space
from skopt import gp_minimize
from sklearn.metrics import mean_squared_error
from sklearn.neighbors import KNeighborsClassifier<train_model>
from hyperopt import hp, fmin,tpe, Trials
from hyperopt.pyll import scope
KNN=KNeighborsClassifier(n_neighbors=11)
 KNN.fit(X_train,y_train )<compute_test_metric>
restore_these_settings = np.geterr()
temp_settings = restore_these_settings.copy()
temp_settings["over"] = "ignore"
temp_settings["under"] = "ignore"
np.seterr(**temp_settings)
KNN.score(X_test,y_test )<predict_on_test>
param_space = {
'loss' : hp.choice('loss', ['ls', 'lad', 'huber', 'quantile']),
'learning_rate' : scope.float(hp.quniform('learning_rate',0.1,1, 0.005)) ,
'n_estimators' : scope.int(hp.quniform('n_estimators', 20,4000,10)) ,
'criterion' : hp.choice('criterion', ['friedman_mse','mse']),
'min_samples_split' : hp.uniform('min_samples_split', 0.0,1.0,),
'min_samples_leaf' : scope.float(hp.uniform('min_samples_leaf', 0.0,0.5)) ,
'max_depth' : scope.int(hp.quniform('max_depth', 2,20,1)) ,
'max_features' : hp.choice('max_features',['auto', 'sqrt', 'log2',])
 }
predict=KNN.predict(test_data.drop('RefId',axis=1))<prepare_output>
def optimize(params, x, y):
model = GradientBoostingRegressor(**params)
 kf = KFold(n_splits=5)
 accuracies = []
for train_idx, test_idx in kf.split(X=x):
xtrain = x[train_idx]
ytrain = y[train_idx]
x_test = x[test_idx]
y_test = y[test_idx]
model.fit(xtrain, ytrain)
 preds = model.predict(x_test)
 fold_acc = mean_squared_error(preds, y_test)
 accuracies.append(fold_acc)
 return np.mean(accuracies)
Submission=pd.DataFrame(data=predict,columns=['IsBadBuy'])
 Submission.head()<prepare_output>
!pip install scikit-opt
Submission['RefId']=test_data['RefId']
Submission.set_index('RefId',inplace=True )<save_to_csv>
optimization_function = partial(
 optimize,
x = train.values,
y = target)
 trials = Trials()
Submission.head()
Submission.to_csv('Submission.csv' )<import_modules>
results = fmin(fn = optimization_function,
space = param_space,
max_evals = 100,
trials=trials,
algo = tpe.suggest,
verbose = 10)
 print(results)
import numpy as np
import pandas as pd
from sklearn.pipeline import make_pipeline
from sklearn.compose import make_column_transformer
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import KBinsDiscretizer, OneHotEncoder, StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score<load_from_csv>
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 )
train = pd.read_csv('/kaggle/input/DontGetKicked/training.csv')
 test = pd.read_csv('/kaggle/input/DontGetKicked/test.csv' )<prepare_x_and_y>
averaged_models = AveragingModels(models =(ENet, GBoost, KRR, lasso))
score = rmse_cv(averaged_models)
 print("\Averaged base models score Training Set: {:.4f}({:.4f})
 ".format(score[1].mean() , score[1].std()))
print("\Averaged base models score Test Set: {:.4f}({:.4f})
 ".format(score[3].mean() , score[3].std()))
y = train['IsBadBuy']
X = train.drop(['IsBadBuy','RefId'], axis = 1 )<define_variables>
class StackingAveragedModels(BaseEstimator, RegressorMixin, TransformerMixin):
def __init__(self, base_models, meta_model, n_folds=5):
self.base_models = base_models
self.meta_model = meta_model
self.n_folds = n_folds
def fit(self, X, y):
self.base_models_ = [list() for x in self.base_models]
self.meta_model_ = clone(self.meta_model)
 kfold = KFold(n_splits=self.n_folds, shuffle=True, random_state=156)
 out_of_fold_predictions = np.zeros(( X.shape[0], len(self.base_models)))
 for i, model in enumerate(self.base_models):
for train_index, holdout_index in kfold.split(X, y):
instance = clone(model)
 self.base_models_[i].append(instance)
 instance.fit(X[train_index], y[train_index])
 y_pred = instance.predict(X[holdout_index])
 out_of_fold_predictions[holdout_index, i] = y_pred
self.meta_model_.fit(out_of_fold_predictions, y)
 return self
def predict(self, X):
meta_features = np.column_stack([
np.column_stack([model.predict(X)for model in base_models] ).mean(axis=1)
 for base_models in self.base_models_ ])
 return self.meta_model_.predict(meta_features )
numerical_features = [c for c, dtype in zip(X.columns, X.dtypes)
 if dtype.kind in ['i','f']]
categorical_features = [c for c, dtype in zip(X.columns, X.dtypes)
 if dtype.kind not in ['i','f']]<split>
stacked_averaged_models = StackingAveragedModels(base_models =(ENet, GBoost, KRR),
meta_model = lasso)
 score = rmse_cv(stacked_averaged_models)
 print("\Stacking base models score Training Set: {:.4f}({:.4f})
 ".format(score[1].mean() , score[1].std()))
print("\Stacking base models score Test Set: {:.4f}({:.4f})
 ".format(score[3].mean() , score[3].std()))
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0, stratify = y )<categorify>
def rmsle(y, y_pred):
return np.sqrt(mean_squared_error(y, y_pred))
preprocessor = make_column_transformer(
(make_pipeline(
 SimpleImputer(strategy = 'median'),
StandardScaler() ,
KBinsDiscretizer(n_bins=3)) , numerical_features), 
(make_pipeline(
 SimpleImputer(strategy = 'constant', fill_value = 'missing'),
OneHotEncoder(categories = 'auto', handle_unknown = 'ignore')) , categorical_features),
)<import_modules>
stacked_averaged_models.fit(train.values, target)
 stacked_train_pred = stacked_averaged_models.predict(train.values)
 stacked_pred = np.expm1(stacked_averaged_models.predict(test.values))
print(rmsle(target, stacked_train_pred))
from sklearn.ensemble import RandomForestClassifier<choose_model_class>
od = train.columns[train.dtypes == 'object']
for col in od:
train[col] = train[col].astype('float')
 test[col] = test[col].astype('float' )
RF_Model = make_pipeline(preprocessor, RandomForestClassifier(n_estimators = 100))<train_model>
model_xgb.fit(train, target)
 xgb_train_pred = model_xgb.predict(train)
 xgb_pred = np.expm1(model_xgb.predict(test))
print(rmsle(target, xgb_train_pred))
RF_Model.fit(X_train, y_train )<compute_test_metric>
model_lgb.fit(train, target)
 lgb_train_pred = model_lgb.predict(train)
 lgb_pred = np.expm1(model_lgb.predict(test.values))
print(rmsle(target, lgb_train_pred))
print(f'Train : {RF_Model.score(X_train, y_train):.3f}')
 print(f'Test : {RF_Model.score(X_test, y_test):.3f}' )<drop_column>

 print('RMSLE score on train data:')
 print(rmsle(target,stacked_train_pred*0.70 +
xgb_train_pred*0.15 + lgb_train_pred*0.15))
sub_test = test.drop(['RefId'], axis = 1 )<predict_on_test>
ensemble = stacked_pred*0.70 + xgb_pred*0.15 + lgb_pred*0.15
sub_test_pred = RF_Model.predict(sub_test ).astype(int )<save_to_csv>
sub = pd.DataFrame()
sub['Id'] = test_ID
sub['SalePrice'] = ensemble
sub.to_csv('ensemble.csv',index=False )