Kaggle competition: Predict Future Sales
preds = xg_reg.predict(df_test)
 preds<save_to_csv>
%matplotlib inline
sales_train = '.. /input/competitive-data-science-predict-future-sales/sales_train.csv'
test = '.. /input/competitive-data-science-predict-future-sales/test.csv'
sample_submission = '.. /input/competitive-data-science-predict-future-sales/sample_submission.csv'
item_categories = '.. /input/competitive-data-science-predict-future-sales/item_categories.csv'
items = '.. /input/competitive-data-science-predict-future-sales/items.csv'
shops = '.. /input/competitive-data-science-predict-future-sales/shops.csv'
train_data = pd.read_csv(sales_train)
 test_data = pd.read_csv(test)
 items_data = pd.read_csv(items)
 shops_data = pd.read_csv(shops)
 item_cat_data = pd.read_csv(item_categories)
 submission_file = pd.read_csv(sample_submission)
 test_data = test_data.drop('ID', axis = 1)
 print(train_data.shape )
pred=pd.DataFrame(preds)
 subdf = pd.read_csv('/kaggle/input/house-prices-advanced-regression-techniques/sample_submission.csv')
 datasets=pd.concat([subdf['Id'],pred],axis=1)
 datasets.columns=['Id','SalePrice']
datasets.to_csv('sample_submission2.csv',index=False )<load_from_csv>
train_data = train_data [train_data.duplicated() == False]
train_data.shape
foo = pd.read_csv('sample_submission2.csv')
 foo<load_from_csv>
def downcast_dtypes(df):
float_cols = [c for c in df if df[c].dtype == "float64"]
int_cols = [c for c in df if df[c].dtype in ["int64", "int32"]]
df[float_cols] = df[float_cols].astype(np.float32)
 df[int_cols] = df[int_cols].astype(np.int16)
 return df
train_data = downcast_dtypes(train_data)
 test_data = downcast_dtypes(test_data)
 items_data = downcast_dtypes(items_data)
 shops_data = downcast_dtypes(shops_data)
 item_cat_data = downcast_dtypes(item_cat_data )
train = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv')
 print("setup completed" )<drop_column>
col_dsi = ['date_block_num', 'shop_id', 'item_id']
col_si = ['shop_id', 'item_id']
col_di = ['date_block_num', 'item_id']
col_ds = ['date_block_num', 'shop_id']
col_d = ['date_block_num']
col_s = ['shop_id']
col_i = ['item_id']
col_dsicat = ['date_block_num', 'shop_id', 'item_category_id']
col_dicati = ['date_block_num', 'item_category_id', 'item_id']
col_dp = ['date_block_num', 'price_bins']
train_ID = train['Id']
test_ID = test['Id']
train.drop("Id", axis = 1 , inplace = True)
 test.drop("Id", axis = 1 , inplace = True)
 print(f"The train and test data size before dropping Id feature is: {train.shape}, {test.shape}" )<prepare_x_and_y>
top_itemscat = list(items_data.groupby('item_category_id')['item_id'].count().nlargest(15 ).index )
ntrain = train.shape[0]
ntest = test.shape[0]
y_train = train.SalePrice.values
all_data = pd.concat(( train, test)).reset_index(drop=True)
 all_data.drop(['SalePrice'], axis=1, inplace=True)
 print(f"all_data size is : {all_data.shape}" )<create_dataframe>
l_cat = list(item_cat_data.item_category_name)
 for ind in range(0,1):
l_cat[ind] = 'PC Headsets / Headphones'
for ind in range(1,8):
l_cat[ind] = 'Access'
l_cat[8] = 'Tickets(figure)'
l_cat[9] = 'Delivery of goods'
for ind in range(10,18):
l_cat[ind] = 'Consoles'
for ind in range(18,25):
l_cat[ind] = 'Consoles Games'
l_cat[25] = 'Accessories for games'
for ind in range(26,28):
l_cat[ind] = 'phone games'
for ind in range(28,32):
l_cat[ind] = 'CD games'
for ind in range(32,37):
l_cat[ind] = 'Card'
for ind in range(37,43):
l_cat[ind] = 'Movie'
for ind in range(43,55):
l_cat[ind] = 'Books'
for ind in range(55,61):
l_cat[ind] = 'Music'
for ind in range(61,73):
l_cat[ind] = 'Gifts'
for ind in range(73,79):
l_cat[ind] = 'Soft'
for ind in range(79,81):
l_cat[ind] = 'Office'
for ind in range(81,83):
l_cat[ind] = 'Clean'
l_cat[83] = 'Elements of a food'
item_cat_data ['item_category_name'] = pd.DataFrame(l_cat)
 items_data = items_data.merge(item_cat_data, on = 'item_category_id', how = 'left' ).drop(['item_name'], axis =1 )
all_data_na =(all_data.isnull().sum() / len(all_data)) * 100
all_data_na = all_data_na.drop(all_data_na[all_data_na == 0].index ).sort_values(ascending=False)[:30]
missing_data = pd.DataFrame({'Missing Ratio' :all_data_na})
 missing_data.head(30 )<data_type_conversions>
shops_data ['city'] = shops_data ['shop_name'].apply(lambda x : x.split(' ')[0])
 shops_data ['center'] = shops_data ['shop_name'].apply(lambda x : x.split(' ')[1])
 shops_data = shops_data.drop('shop_name', axis = 1 )
all_data["PoolQC"] = all_data["PoolQC"].fillna("None")
 all_data["MiscFeature"] = all_data["MiscFeature"].fillna("None")
 all_data["Alley"] = all_data["Alley"].fillna("None")
 all_data["Fence"] = all_data["Fence"].fillna("None")
 all_data["FireplaceQu"] = all_data["FireplaceQu"].fillna("None" )<categorify>
items_data ['item_category_name'] = LabelEncoder().fit_transform(items_data ['item_category_name'] ).astype(np.int8 )
all_data["LotFrontage"] = all_data.groupby("Neighborhood")["LotFrontage"].transform(
 lambda x: x.fillna(x.median()))<data_type_conversions>
shops_data ['city'] = LabelEncoder().fit_transform(shops_data ['city'] ).astype(np.int8)
 shops_data ['center'] = LabelEncoder().fit_transform(shops_data ['center'] ).astype(np.int8 )
for col in('GarageType', 'GarageFinish', 'GarageQual', 'GarageCond'):
all_data[col] = all_data[col].fillna('None' )<data_type_conversions>
train_data ['date'] = pd.to_datetime(train_data ['date'], format = '%d.%m.%Y')
 train_data ['item_price'] = round(train_data ['item_price'],1 )
for col in('GarageYrBlt', 'GarageArea', 'GarageCars'):
all_data[col] = all_data[col].fillna(0 )<data_type_conversions>
rus_holidays = holidays.Russia()
train_data ['is_holiday'] = train_data ['date'].apply(lambda x : 1 if x in rus_holidays else 0)
 holiday_month = train_data [train_data.is_holiday == 1].groupby('date_block_num')['date'].nunique()
holidays_df = pd.DataFrame(holiday_month ).reset_index()
holidays_df.columns = ['date_block_num', 'holiday_count']
master = pd.DataFrame(index = list(range(0,35)) , columns = ['holidays'] ).reset_index()
master.columns = ['date_block_num', 'holidays']
master ['holidays'] = 0
holidays = master.merge(holidays_df, on = 'date_block_num', how = 'left' ).fillna(0 ).drop('holidays', axis = 1)
 holidays ['date_block_num'] = holidays ['date_block_num'].astype(np.int8)
 holidays ['holiday_count'] = holidays ['holiday_count'].astype(np.int8 )
for col in('BsmtFinSF1', 'BsmtFinSF2', 'BsmtUnfSF','TotalBsmtSF', 'BsmtFullBath', 'BsmtHalfBath'):
all_data[col] = all_data[col].fillna(0 )<data_type_conversions>
train_data = train_data.loc [(train_data ['item_price'] >= 0)&(train_data ['item_price'] < 300000)&(train_data ['item_cnt_day'] >=0)&(train_data ['item_cnt_day'] < 2000), :]
train_data = train_data [['date_block_num', 'shop_id', 'item_id', 'item_price', 'item_cnt_day']]
for col in('BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2'):
all_data[col] = all_data[col].fillna('None' )<data_type_conversions>
td_piv = train_data.pivot_table(index = 'item_id', columns = ['date_block_num'], values = 'item_cnt_day', aggfunc = 'sum' ).fillna(0)
 last6 = td_piv.loc [:,td_piv.columns [27:33]]
inactive_items = list(last6[last6.iloc [:, :].sum(axis = 1)== 0].index)
 active_items = list(td_piv [td_piv.index.isin(inactive_items)== False].index)
 print('active items {}'.format(len(active_items)))
 print('Inactive items {}'.format(len(inactive_items)))
 inactive_test_items = list(set(test_data.loc [test_data ['item_id'].isin(inactive_items), 'item_id'].values))
print('inactive_test_items {}'.format(len(inactive_test_items)))
 active_test_items = list(set(test_data.loc [test_data ['item_id'].isin(inactive_test_items)== False, 'item_id'].values))
print('active_test_items {}'.format(len(active_test_items)))
 new_items = list(set(test_data.item_id)-set(train_data.item_id))
no_forecast_items = inactive_test_items + new_items
print('no_forecast_items : {}'.format(len(no_forecast_items)) )
all_data["MasVnrType"] = all_data["MasVnrType"].fillna("None")
 all_data["MasVnrArea"] = all_data["MasVnrArea"].fillna(0)
 all_data['MSZoning'] = all_data['MSZoning'].fillna(all_data['MSZoning'].mode() [0] )<drop_column>
train_data ['revenue'] = train_data ['item_price'] * train_data ['item_cnt_day']
all_data = all_data.drop(['Utilities'], axis=1 )<data_type_conversions>
revenue = train_data.groupby(col_dsi)['revenue'].sum().reset_index()
revenue.columns = col_dsi + ['revenue']
target = train_data.groupby(col_dsi)['item_cnt_day'].sum().reset_index()
target.columns = col_dsi + ['target']
price = train_data.groupby(col_dsi ).agg({'item_price' : ['mean']} ).reset_index().fillna(0)
 price.columns = col_dsi + ['price']
train_data = train_data [col_dsi].merge(target, on = col_dsi, how = 'left' ).fillna(0)
 train_data ['target'] = train_data ['target'].astype(np.float16)
 train_data = train_data.merge(price, on = col_dsi, how = 'left' ).fillna(0)
 train_data = train_data.merge(revenue, on = col_dsi, how = 'left' ).fillna(0)
 train_data = downcast_dtypes(train_data )
all_data["Functional"] = all_data["Functional"].fillna("Typ" )<data_type_conversions>
train_data ['date_block_num'] = train_data ['date_block_num'].astype(np.int8)
 train_data ['shop_id'] = train_data ['shop_id'].astype(np.int8)
 train_data ['price'] = train_data ['price'].astype(np.float16 )
all_data['Electrical'] = all_data['Electrical'].fillna(all_data['Electrical'].mode() [0] )<data_type_conversions>
grid = []
cols = ['date_block_num','shop_id','item_id']
for i in range(34):
sales = train_data[train_data.date_block_num==i]
grid.append(np.array(list(product([i], sales.shop_id.unique() , sales.item_id.unique())) , dtype='int16'))
grid = pd.DataFrame(np.vstack(grid), columns=cols)
 grid ['date_block_num'] = grid ['date_block_num'].astype(np.int8)
 grid ['shop_id'] = grid ['shop_id'].astype(np.int8)
 grid ['item_id'] = grid ['item_id'].astype(np.int16)
 grid.sort_values(cols,inplace=True)
all_data['KitchenQual'] = all_data['KitchenQual'].fillna(all_data['KitchenQual'].mode() [0] )<categorify>
train_data = grid.merge(train_data, on = col_dsi, how = 'left' ).drop_duplicates(subset = col_dsi, keep = 'first' ).fillna(0)
 train_data = train_data.sort_values(by = col_dsi, ascending = True )
all_data['Exterior1st'] = all_data['Exterior1st'].fillna(all_data['Exterior1st'].mode() [0])
 all_data['Exterior2nd'] = all_data['Exterior2nd'].fillna(all_data['Exterior2nd'].mode() [0] )<data_type_conversions>
test_data ['date_block_num'] = 34
test_data ['date_block_num'] = test_data ['date_block_num'].astype(np.int8)
 test_data ['shop_id'] = test_data ['shop_id'].astype(np.int8)
 test_data ['item_id'] = test_data ['item_id'].astype(np.int16 )
all_data['SaleType'] = all_data['SaleType'].fillna(all_data['SaleType'].mode() [0] )<data_type_conversions>
data = pd.concat([train_data, test_data], axis = 0 ).fillna(0 )
all_data['MSSubClass'] = all_data['MSSubClass'].fillna("None" )<sort_values>
def lags(train, lags, col):
col_dsi = ['date_block_num', 'shop_id', 'item_id']
df = train
for i in lags:
prev = train [col_dsi + [col]].copy()
prev ['date_block_num'] = prev ['date_block_num'] + i
prev = prev.rename(columns = {col : 'prev_' + col + str(i)})
 df = df.merge(prev, on = col_dsi, how = 'left')
 return df.fillna(0 )
all_data_na =(all_data.isnull().sum() / len(all_data)) * 100
all_data_na = all_data_na.drop(all_data_na[all_data_na == 0].index ).sort_values(ascending=False)
 missing_data = pd.DataFrame({'Missing Ratio' :all_data_na})
 missing_data.head()<data_type_conversions>
lags_ = [1,2,3,12]
data = lags(data, lags_, 'target' )
all_data['MSSubClass'] = all_data['MSSubClass'].apply(str)
 all_data['OverallCond'] = all_data['OverallCond'].astype(str)
 all_data['YrSold'] = all_data['YrSold'].astype(str)
 all_data['MoSold'] = all_data['MoSold'].astype(str )<categorify>
lags_ = [1]
data = lags(data, lags_, 'price' )
cols =('FireplaceQu', 'BsmtQual', 'BsmtCond', 'GarageQual', 'GarageCond',
'ExterQual', 'ExterCond','HeatingQC', 'PoolQC', 'KitchenQual', 'BsmtFinType1',
'BsmtFinType2', 'Functional', 'Fence', 'BsmtExposure', 'GarageFinish', 'LandSlope',
'LotShape', 'PavedDrive', 'Street', 'Alley', 'CentralAir', 'MSSubClass', 'OverallCond',
'YrSold', 'MoSold')
 for c in cols:
lbl = LabelEncoder()
lbl.fit(list(all_data[c].values))
all_data[c] = lbl.transform(list(all_data[c].values))
print('Shape all_data: {}'.format(all_data.shape))<feature_engineering>
target_mean = data.groupby('item_id', as_index = False)['price'].max()
target_mean.columns = ['item_id'] + ['item_pricemax']
data = pd.merge(data, target_mean, on = ['item_id'], how = 'left')
 target_mean = data.groupby('item_id', as_index = False)['price'].min()
target_mean.columns = ['item_id'] + ['item_pricemin']
data = pd.merge(data, target_mean, on = ['item_id'], how = 'left')
 data ['discount'] = data ['item_pricemax'] - data ['item_pricemin']
lags_ = [1]
data = lags(data, lags_, 'discount')
 data = data.drop('discount', axis = 1)
 data = data.drop(['item_pricemax','item_pricemin'], axis = 1 )
all_data['TotalSF'] = all_data['TotalBsmtSF'] + all_data['1stFlrSF'] + all_data['2ndFlrSF']<feature_engineering>
lags_ = [1]
data = lags(data, lags_, 'revenue' )
skewness = skewness[abs(skewness)> 0.75]
print("There are {} skewed numerical features to Box Cox transform".format(skewness.shape[0]))
skewed_features = skewness.index
lam = 0.15
for feat in skewed_features:
all_data[feat] = boxcox1p(all_data[feat], lam)
<categorify>
data ['revenue'] = data ['revenue'].astype(np.float32)
 data ['prev_revenue1'] = data ['prev_revenue1'].astype(np.float32 )
all_data = pd.get_dummies(all_data)
 print(all_data.shape )<split>
target_mean = data.groupby(col_di, as_index = False)['target'].mean()
target_mean.columns = col_di + ['target_meandi']
data = pd.merge(data, target_mean, on = col_di, how = 'left')
 lags_ = [1]
data = lags(data, lags_, 'target_meandi')
 data = data.drop('target_meandi', axis = 1 )
train = all_data[:ntrain]
test = all_data[ntrain:]<import_modules>
target_mean = data.groupby(col_ds, as_index = False)['target'].mean()
target_mean.columns = col_ds + ['target_meands']
data = pd.merge(data, target_mean, on = col_ds, how = 'left')
 lags_ = [1]
data = lags(data, lags_, 'target_meands')
 data = data.drop('target_meands', axis = 1 )
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<compute_train_metric>
target_mean = data.groupby(col_ds, as_index = False)['revenue'].mean()
target_mean.columns = col_ds + ['revenue_means']
data = pd.merge(data, target_mean, on = col_ds, how = 'left')
 lags_ = [1]
data = lags(data, lags_, 'revenue_means')
 data = data.drop('revenue_means', axis = 1)
 target_mean = data.groupby(col_s, as_index = False)['revenue'].mean()
target_mean.columns = col_s + ['revenue_means_tot']
data = pd.merge(data, target_mean, on = col_s, how = 'left')
 lags_ = [1]
data = lags(data, lags_, 'revenue_means_tot')
 data = data.drop('revenue_means_tot', axis = 1)
n_folds = 5
def rmsle_cv(model):
kf = KFold(n_folds, shuffle=True, random_state=42 ).get_n_splits(train.values)
 rmse= np.sqrt(-cross_val_score(model, train.values, y_train, scoring="neg_mean_squared_error", cv = kf))
return(rmse )<choose_model_class>
data ['revenue_delta'] = data ['prev_revenue_means_tot1'] - data ['prev_revenue_means1']
data = data.drop(['prev_revenue_means_tot1','prev_revenue_means1'], axis = 1 )
lasso = make_pipeline(RobustScaler() , Lasso(alpha =0.0005, random_state=1))<choose_model_class>
target_mean = data.groupby(col_d, as_index = False)['target'].mean()
target_mean.columns = col_d + ['M_mean']
data = pd.merge(data, target_mean, on = col_d, how = 'left')
 lags_ = [1,2,3,12]
data = lags(data, lags_, 'M_mean')
 data = data.drop('M_mean', axis = 1 )
ENet = make_pipeline(RobustScaler() , ElasticNet(alpha=0.0005, l1_ratio=.9, random_state=3))<choose_model_class>
data = data.merge(shops_data, on = 'shop_id', how = 'left' ).fillna(0)
 data = data.merge(items_data, on = 'item_id', how = 'left' ).fillna(0 )
KRR = KernelRidge(alpha=0.6, kernel='polynomial', degree=2, coef0=2.5 )<choose_model_class>
target_mean = data.groupby(col_dsicat, as_index = False)['target'].mean()
target_mean.columns = col_dsicat + ['target_meansicat']
data = pd.merge(data, target_mean, on = col_dsicat, how = 'left')
 lags_ = [1]
data = lags(data, lags_, 'target_meansicat')
 data = data.drop('target_meansicat', axis = 1 )
GBoost = 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 =5 )<choose_model_class>
data ['inactive_items'] = data ['item_id'].isin(inactive_items ).replace({False:0, True:1} ).astype(np.int16)
 data = data.merge(holidays, on = 'date_block_num', how = 'left')
 bins = np.linspace(data.price.min() , data.price.max() ,6)
 labels = ['very_low', 'low', 'medium', 'high','very_high']
data ['price_bins'] = pd.cut(data ['price'],bins,labels = labels, include_lowest = True)
 data ['price_bins'] = data ['price_bins'].astype('O')
 data ['price_bins'] = LabelEncoder().fit_transform(data ['price_bins'] ).astype(np.int8 )
model_xgb = xgb.XGBRegressor(colsample_bytree=0.4603, gamma=0.0468,
learning_rate=0.05, max_depth=3,
min_child_weight=1.7817, n_estimators=2200,
reg_alpha=0.4640, reg_lambda=0.8571,
subsample=0.5213, silent=1,
random_state =7, nthread = -1 )<choose_model_class>
data ['month'] = data ['date_block_num'].apply(lambda x : x%12+1 ).astype(np.int8)
 data ['year'] = data ['date_block_num'].apply(lambda x : x//12 ).astype(np.int8 )
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 )<compute_test_metric>
days = pd.Series([31,28,31,30,31,30,31,31,30,31,30,31])
 data['days'] = data['month'].map(days)
 data['days'] = data['days'].astype(np.float16 )
score = rmsle_cv(lasso)
 print("
Lasso score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))<compute_test_metric>
req_data = data.copy()
score = rmsle_cv(ENet)
 print("ElasticNet score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))<compute_test_metric>
req_data = req_data.drop(['item_category_id','price', 'revenue', 'shop_id', 'item_id'], axis = 1 ).fillna(0)
 req_data = req_data.loc [req_data.date_block_num > 11, :]
score = rmsle_cv(KRR)
 print("Kernel Ridge score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))<compute_test_metric>
del data
gc.collect()
del grid
gc.collect()
del target_mean
gc.collect()
del shops_data
gc.collect()
del items_data
gc.collect()
del target
gc.collect()
del price
gc.collect()
del train_data
gc.collect()
del item_cat_data
gc.collect()
del holidays
gc.collect()
del revenue
gc.collect()
score = rmsle_cv(GBoost)
 print("Gradient Boosting score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))<compute_test_metric>
X = req_data.loc [(req_data.date_block_num<33),:].drop(['date_block_num', 'target'], axis = 1)
 y = req_data.loc [(req_data.date_block_num<33),:]['target'].clip(0,20)
 X_valid = req_data [req_data ['date_block_num']==33].drop(['date_block_num', 'target'], axis = 1)
 y_valid = req_data [req_data ['date_block_num']==33]['target'].clip(0,20)
 X_test = req_data [req_data ['date_block_num']==34].drop(['date_block_num', 'target'], axis = 1 )
score = rmsle_cv(model_xgb)
 print("Xgboost score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))<compute_test_metric>
del req_data
gc.collect() ;
score = rmsle_cv(model_lgb)
 print("LGBM score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))<predict_on_test>
model = XGBRegressor(
 max_depth=8,
n_estimators=150,
min_child_weight=0.5,
colsample_bytree=0.8,
subsample=0.8,
eta=0.1,
seed=42, reg_lambda=1, gamma = 0)
 model.fit(
 X,
y,
eval_metric="rmse",
eval_set=[(X, y),(X_valid, y_valid)],
verbose=True,
early_stopping_rounds = 6 )
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 )<compute_train_metric>
prediction1 = model.predict(X_valid ).clip(0,20)
 prediction2 = model.predict(X_test ).clip(0,20 )
averaged_models = AveragingModels(models =(ENet, GBoost, KRR, lasso))
score = rmsle_cv(averaged_models)
 print(" Averaged base models score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))<train_model>
Score = sqrt(mean_squared_error(prediction1, y_valid))
print(round(np.mean(Score),4))
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 )<compute_train_metric>
df = pd.DataFrame(model.get_booster().get_score(importance_type='weight'), index =[0] ).transpose()
df.columns = ['features']
stacked_averaged_models = StackingAveragedModels(base_models =(ENet, GBoost, KRR),
meta_model = lasso)
 score = rmsle_cv(stacked_averaged_models)
 print("Stacking Averaged models score: {:.4f}({:.4f})".format(score.mean() , score.std()))<compute_test_metric>
correlation = np.corrcoef(prediction1, y_valid)[0,1]
round(correlation,2 )
<predict_on_test><EOS> 
output = pd.DataFrame({'ID': submission_file.ID, 'item_cnt_month': prediction2})
 output.to_csv('submission.csv', index=False )