Kaggle competition: Mercari Price Suggestion Challenge
<SOS> metric: RMSLE Kaggle data source: mercari-price-suggestion-challenge<find_best_model_class>
import os ; os.environ['OMP_NUM_THREADS'] = '4'
test = h2o.import_file(".. /input/ashrae-stack-set-test-fin/test.csv", header=1)
 preds = glm_model.predict(test ).as_data_frame()
h2o.remove(test)
 del test
gc.collect()
h2o.cluster().shutdown()<load_from_csv>
import pandas as pd
import operator
import matplotlib.pyplot as plt
import numpy as np
import nltk
import time
%%time
test_df = pd.read_csv('.. /input/ashrae-energy-prediction/test.csv', parse_dates=["timestamp"])
 building_meta_df = pd.read_csv('.. /input/ashrae-energy-prediction/building_metadata.csv' )<feature_engineering>
df = pd.read_csv('.. /input/train.tsv', sep='\t')
 df_sub = pd.read_csv('.. /input/test.tsv', sep='\t')
 submission = pd.DataFrame()
submission['test_id'] = df_sub.test_id.copy()
y_target = list(df.price )
leak_df = pd.read_feather('.. /input/ashrae-leak-data-station/leak.feather')
 leak_df.fillna(0, inplace=True)
 leak_df = leak_df[(leak_df.timestamp.dt.year > 2016)&(leak_df.timestamp.dt.year < 2019)]
leak_df.loc[leak_df.meter_reading < 0, 'meter_reading'] = 0
leak_df = leak_df[leak_df.building_id!=245]<feature_engineering>
def null_percentage(column):
df_name = column.name
nans = np.count_nonzero(column.isnull().values)
 total = column.size
frac = nans / total
perc = int(frac * 100)
 print('%d%% or %d missing from %s column.' % 
(perc, nans, df_name))
def check_null(df, columns):
for col in columns:
null_percentage(df[col])
 check_null(df, df.columns)
test_df['pred'] = np.expm1(preds['predict'])
 test_df.loc[test_df.pred<0, 'pred'] = 0
test_df = reduce_mem_usage(test_df)
 leak_df = reduce_mem_usage(leak_df )<merge>
def merc_imputer(df_temp):
df_temp.brand_name = df_temp.brand_name.replace(np.nan, 'no_brand')
 df_temp.category_name = df_temp.category_name.replace(np.nan, 'uncategorized/uncategorized')
 df_temp.item_description = df_temp.item_description.replace(np.nan, 'No description yet')
 df_temp.item_description = df_temp.item_description.replace('No description yet', 'no_description')
 return df_temp
df = merc_imputer(df)
 df_sub = merc_imputer(df_sub )
leak_df = leak_df.merge(test_df[['building_id', 'meter', 'timestamp', 'pred','row_id']], left_on = ['building_id', 'meter', 'timestamp'], right_on = ['building_id', 'meter', 'timestamp'], how = "left")
 leak_df = leak_df.merge(building_meta_df[['building_id', 'site_id']], on='building_id', how='left' )<load_from_csv>
print('Training Data')
 check_null(df, df.columns)
 print('Submission Data')
 check_null(df_sub, df_sub.columns )
sample_submission = pd.read_csv('.. /input/ashrae-energy-prediction/sample_submission.csv')
 sample_submission['meter_reading'] = test_df.pred
sample_submission.loc[sample_submission.meter_reading < 0, 'meter_reading'] = 0<feature_engineering>
df.shipping.value_counts()
leak_df = leak_df[['meter_reading', 'row_id']].set_index('row_id' ).dropna()
sample_submission.loc[leak_df.index, 'meter_reading'] = leak_df['meter_reading']<save_to_csv>
print('$1 items: ' + str(df.price[df.price == 1].count()))
print('$2 items: ' + str(df.price[df.price == 2].count()))
print('$3 items: ' + str(df.price[df.price == 3].count()))
sample_submission.to_csv('submission.csv', index=False, float_format='%.4f' )<define_variables>
print('Free items: %d, representing %.5f%% of all items.' % 
(df.price[df.price == 0].count() , 
(df.price[df.price == 0].count() / df.shape[0])) )
nround = 1000
debug = False
black_day =10<define_variables>
print('Free items where seller pays shipping: %d.' %
df.price[operator.and_(df.price == 0, df.shipping == 1)].count() )
DATA_PATH = ".. /input/ashrae-energy-prediction/"<load_from_csv>
print('No description:', str(df.item_description[df.item_description == 'no_description'].count()))
print('Uncategorized:',str(df.category_name[df.category_name == 'uncategorized/uncategorized'].count()))
train_df = pd.read_csv(DATA_PATH + 'train.csv')
 train_df = train_df [ train_df['building_id'] != 1099 ]
train_df = train_df.query('not(building_id <= 104 & meter == 0 & timestamp <= "2016-05-20")')
 building_df = pd.read_csv(DATA_PATH + 'building_metadata.csv')
 weather_df = pd.read_csv(DATA_PATH + 'weather_train.csv' )<load_from_disk>
print('The top 75%% of categories represent %.1f%% of the dataset, and the top 50%% represent %.1f%%.' % 
(( sum([count for count in cat_counts if count > 10])/ len(df)) *100, 
(sum([count for count in cat_counts if count > 76])/ len(df)) *100))
root_black = '.. /input/ashrae-local-datatime-and-black-count/'
train_df_black = pd.read_feather(root_black + 'train_black.feather')
 train_df = train_df[train_df_black.black_count < 24*black_day]
del train_df_black
gc.collect()<feature_engineering>
brand_counts = np.sort(df.brand_name.value_counts())
 print(str(len(brand_counts)) + ' brands total.')
 print(str(df.shape[0])+ ' records total.')
 print('Category frequency percentiles, marked by lines:
25%%: %d, 50%%: %d, 75%%: %d, 95%%: %d, 97.5%%: %d.' % 
(brand_counts[int(len(brand_counts)*0.25)],
brand_counts[int(len(brand_counts)*0.5)],
brand_counts[int(len(brand_counts)*0.75)],
brand_counts[int(len(brand_counts)*0.9)],
brand_counts[int(len(brand_counts)*0.95)]))
title = 'Brand Quantity ECDF Without Top 25 Outliers'
plt.figure(title, figsize=(30,10))
plt.title(title, fontsize=32)
 x = np.sort(df.brand_name.value_counts())
 x = x[0:-25]
y = np.arange(1, len(x)+ 1)/ len(x)
 plt.plot(x, y, marker='.', linestyle='none')
 plt.xticks(fontsize=24)
 plt.yticks(fontsize=26)
 plt.axvline(x=x[int(len(x)*0.25)], linewidth=1, color='b')
 plt.axvline(x=x[int(len(x)*0.5)], linewidth=1, color='b')
 plt.axvline(x=x[int(len(x)*0.75)], linewidth=1, color='b')
 plt.axvline(x=x[int(len(x)*0.95)], linewidth=1, color='b')
 plt.axvline(x=x[int(len(x)*0.975)], linewidth=1, color='b')
 plt.show()
site_0_bids = building_df[building_df.site_id == 0].building_id.unique()
train_df.loc[(train_df.building_id.isin(site_0_bids)) &(train_df.meter==0), 'meter_reading'] = train_df[(train_df.building_id.isin(site_0_bids)) &(train_df.meter==0)]['meter_reading'] * 0.2931<data_type_conversions>
print('The top 75%% of categories represent %.1f%% of the dataset, and the top 50%% represent %.1f%%.' % 
(( sum([count for count in brand_counts if count > 1])/ len(df)) *100, 
(sum([count for count in brand_counts if count > 4])/ len(df)) *100))
def fill_weather_dataset(weather_df):
time_format = "%Y-%m-%d %H:%M:%S"
start_date = datetime.datetime.strptime(weather_df['timestamp'].min() ,time_format)
 end_date = datetime.datetime.strptime(weather_df['timestamp'].max() ,time_format)
 total_hours = int(((end_date - start_date ).total_seconds() + 3600)/ 3600)
 hours_list = [(end_date - datetime.timedelta(hours=x)).strftime(time_format)for x in range(total_hours)]
missing_hours = []
for site_id in range(16):
site_hours = np.array(weather_df[weather_df['site_id'] == site_id]['timestamp'])
 new_rows = pd.DataFrame(np.setdiff1d(hours_list,site_hours),columns=['timestamp'])
 new_rows['site_id'] = site_id
weather_df = pd.concat([weather_df,new_rows])
 weather_df = weather_df.reset_index(drop=True)
 weather_df["datetime"] = pd.to_datetime(weather_df["timestamp"])
 weather_df["day"] = weather_df["datetime"].dt.day
weather_df["week"] = weather_df["datetime"].dt.week
weather_df["month"] = weather_df["datetime"].dt.month
weather_df = weather_df.set_index(['site_id','day','month'])
 air_temperature_filler = pd.DataFrame(weather_df.groupby(['site_id','day','month'])['air_temperature'].mean() ,columns=["air_temperature"])
 weather_df.update(air_temperature_filler,overwrite=False)
 cloud_coverage_filler = weather_df.groupby(['site_id','day','month'])['cloud_coverage'].mean()
cloud_coverage_filler = pd.DataFrame(cloud_coverage_filler.fillna(method='ffill'),columns=["cloud_coverage"])
 weather_df.update(cloud_coverage_filler,overwrite=False)
 due_temperature_filler = pd.DataFrame(weather_df.groupby(['site_id','day','month'])['dew_temperature'].mean() ,columns=["dew_temperature"])
 weather_df.update(due_temperature_filler,overwrite=False)
 sea_level_filler = weather_df.groupby(['site_id','day','month'])['sea_level_pressure'].mean()
sea_level_filler = pd.DataFrame(sea_level_filler.fillna(method='ffill'),columns=['sea_level_pressure'])
 weather_df.update(sea_level_filler,overwrite=False)
 wind_direction_filler = pd.DataFrame(weather_df.groupby(['site_id','day','month'])['wind_direction'].mean() ,columns=['wind_direction'])
 weather_df.update(wind_direction_filler,overwrite=False)
 wind_speed_filler = pd.DataFrame(weather_df.groupby(['site_id','day','month'])['wind_speed'].mean() ,columns=['wind_speed'])
 weather_df.update(wind_speed_filler,overwrite=False)
 precip_depth_filler = weather_df.groupby(['site_id','day','month'])['precip_depth_1_hr'].mean()
precip_depth_filler = pd.DataFrame(precip_depth_filler.fillna(method='ffill'),columns=['precip_depth_1_hr'])
 weather_df.update(precip_depth_filler,overwrite=False)
 weather_df = weather_df.reset_index()
weather_df = weather_df.drop(['datetime','day','week','month'],axis=1)
 return weather_df
def reduce_mem_usage(df, use_float16=False):

 start_mem = df.memory_usage().sum() / 1024**2
print("Memory usage of dataframe is {:.2f} MB".format(start_mem))
for col in df.columns:
if is_datetime(df[col])or is_categorical_dtype(df[col]):
continue
col_type = df[col].dtype
if col_type != object:
c_min = df[col].min()
c_max = df[col].max()
if str(col_type)[:3] == "int":
if c_min > np.iinfo(np.int8 ).min and c_max < np.iinfo(np.int8 ).max:
df[col] = df[col].astype(np.int8)
 elif c_min > np.iinfo(np.int16 ).min and c_max < np.iinfo(np.int16 ).max:
df[col] = df[col].astype(np.int16)
 elif c_min > np.iinfo(np.int32 ).min and c_max < np.iinfo(np.int32 ).max:
df[col] = df[col].astype(np.int32)
 elif c_min > np.iinfo(np.int64 ).min and c_max < np.iinfo(np.int64 ).max:
df[col] = df[col].astype(np.int64)
 else:
if use_float16 and c_min > np.finfo(np.float16 ).min and c_max < np.finfo(np.float16 ).max:
df[col] = df[col].astype(np.float16)
 elif c_min > np.finfo(np.float32 ).min and c_max < np.finfo(np.float32 ).max:
df[col] = df[col].astype(np.float32)
 else:
df[col] = df[col].astype(np.float64)
 else:
df[col] = df[col].astype("category")
 end_mem = df.memory_usage().sum() / 1024**2
print("Memory usage after optimization is: {:.2f} MB".format(end_mem))
print("Decreased by {:.1f}%".format(100 *(start_mem - end_mem)/ start_mem))
return df
def features_engineering(df):
df.sort_values("timestamp")
 df.reset_index(drop=True)
 df["timestamp"] = pd.to_datetime(df["timestamp"],format="%Y-%m-%d %H:%M:%S")
 df["hour"] = df["timestamp"].dt.hour
df["weekend"] = df["timestamp"].dt.weekday
holidays = ["2016-01-01", "2016-01-18", "2016-02-15", "2016-05-30", "2016-07-04",
"2016-09-05", "2016-10-10", "2016-11-11", "2016-11-24", "2016-12-26",
"2017-01-02", "2017-01-16", "2017-02-20", "2017-05-29", "2017-07-04",
"2017-09-04", "2017-10-09", "2017-11-10", "2017-11-23", "2017-12-25",
"2018-01-01", "2018-01-15", "2018-02-19", "2018-05-28", "2018-07-04",
"2018-09-03", "2018-10-08", "2018-11-12", "2018-11-22", "2018-12-25",
"2019-01-01"]
df["is_holiday"] =(df.timestamp.isin(holidays)).astype(int)
 df['square_feet'] = np.log1p(df['square_feet'])
 drop = ["timestamp","sea_level_pressure", "wind_direction", "wind_speed","year_built","floor_count"]
df = df.drop(drop, axis=1)
 gc.collect()
le = LabelEncoder()
df["primary_use"] = le.fit_transform(df["primary_use"])
 return df<feature_engineering>
print('%d items, or %.2f%%, are missing a brand name.' % 
(len(df[df.brand_name == 'no_brand']),
len(df[df.brand_name == 'no_brand'])/ len(df)) )
def add_sg(df):
w = 11
p = 2
for si in df.site_id.unique() :
index = df.site_id == si
df.loc[index, 'air_smooth'] = sg(df[index].air_temperature, w, p)
 df.loc[index, 'dew_smooth'] = sg(df[index].dew_temperature, w, p)
 df.loc[index, 'air_diff'] = sg(df[index].air_temperature, w, p, 1)
 df.loc[index, 'dew_diff'] = sg(df[index].dew_temperature, w, p, 1)
 df.loc[index, 'air_diff2'] = sg(df[index].air_temperature, w, p, 2)
 df.loc[index, 'dew_diff2'] = sg(df[index].dew_temperature, w, p, 2 )<create_dataframe>
nltk.data.path.append(r'D:\Python\Data Sets
ltk_data')
weather_df = fill_weather_dataset(weather_df)
 add_sg(weather_df )<categorify>
cat_vec = CountVectorizer(stop_words=[stopwords, string.punctuation], max_features=int(len(cat_counts)*0.5))
cat_matrix = cat_vec.fit_transform(df.category_name)
 cat_matrix_sub = cat_vec.transform(df_sub.category_name )
train_df = reduce_mem_usage(train_df,use_float16=True)
 building_df = reduce_mem_usage(building_df,use_float16=True)
 weather_df = reduce_mem_usage(weather_df,use_float16=True )<merge>
cat_tokens = list(zip(cat_vec.get_feature_names() , np.array(cat_matrix.sum(axis=0)).ravel()))
train_df = train_df.merge(building_df, left_on='building_id',right_on='building_id',how='left')
 train_df = train_df.merge(weather_df,how='left',left_on=['site_id','timestamp'],right_on=['site_id','timestamp'])
 del weather_df
gc.collect()<feature_engineering>
brand_vec = CountVectorizer(stop_words=[stopwords, string.punctuation], max_features=int(len(brand_counts)*0.5))
brand_matrix = brand_vec.fit_transform(df.brand_name)
 brand_matrix_sub = brand_vec.transform(df_sub.brand_name )
train_df = features_engineering(train_df )<prepare_x_and_y>
brand_tokens = list(zip(brand_vec.get_feature_names() , np.array(brand_matrix.sum(axis=0)).ravel()))
target = np.log1p(train_df["meter_reading"])
 features = train_df.drop('meter_reading', axis = 1)
 del train_df
gc.collect()<split>
name_vec = TfidfVectorizer(min_df=15, stop_words=[stopwords, string.punctuation])
 name_matrix = name_vec.fit_transform(df.name)
 name_matrix_sub = name_vec.transform(df_sub.name )
categorical_features = ["building_id", "site_id", "meter", "primary_use", "is_holiday", "weekend"]
params = {
"objective": "regression",
"boosting": "gbdt",
"num_leaves": 1280,
"learning_rate": 0.05,
"feature_fraction": 0.85,
"reg_lambda": 2,
"metric": "rmse",
}
kf = KFold(n_splits=3)
 models = []
for train_index,test_index in kf.split(features):
train_features = features.loc[train_index]
train_target = target.loc[train_index]
test_features = features.loc[test_index]
test_target = target.loc[test_index]
d_training = lgb.Dataset(train_features, label=train_target,categorical_feature=categorical_features, free_raw_data=False)
 d_test = lgb.Dataset(test_features, label=test_target,categorical_feature=categorical_features, free_raw_data=False)
 model = lgb.train(params, train_set=d_training, num_boost_round=nround, valid_sets=[d_training,d_test], verbose_eval=25, early_stopping_rounds=50)
 models.append(model)
 del train_features, train_target, test_features, test_target, d_training, d_test
gc.collect()<set_options>
print('Kept %d words.' % len(name_vec.get_feature_names()))
del features, target
gc.collect()<load_from_csv>
desc_vec = TfidfVectorizer(max_features=100000,
stop_words=[stopwords, string.punctuation])
 desc_matrix = desc_vec.fit_transform(df.item_description)
 desc_matrix_sub= desc_vec.transform(df_sub.item_description )
test_df = pd.read_csv(DATA_PATH + 'test.csv')
 row_ids = test_df["row_id"]
test_df.drop("row_id", axis=1, inplace=True)
 test_df = reduce_mem_usage(test_df )<merge>
cond_matrix = sparse.csr_matrix(pd.get_dummies(df.item_condition_id, sparse=True, drop_first=True))
cond_matrix_sub = sparse.csr_matrix(pd.get_dummies(df_sub.item_condition_id, sparse=True, drop_first=True))
test_df = test_df.merge(building_df,left_on='building_id',right_on='building_id',how='left')
 del building_df
gc.collect()<load_from_csv>
ship_matrix = sparse.csr_matrix(df.shipping ).transpose()
ship_matrix_sub = sparse.csr_matrix(df_sub.shipping ).transpose()
weather_df = pd.read_csv(DATA_PATH + 'weather_test.csv')
 weather_df = fill_weather_dataset(weather_df)
 add_sg(weather_df)
 weather_df = reduce_mem_usage(weather_df)
<merge>
sparse_matrix = sparse.csr_matrix(sparse.hstack([cat_matrix, brand_matrix, name_matrix, desc_matrix,
cond_matrix, ship_matrix]))
sparse_matrix_sub = sparse.csr_matrix(sparse.hstack([cat_matrix_sub, brand_matrix_sub, name_matrix_sub,
desc_matrix_sub, cond_matrix_sub, ship_matrix_sub]))
test_df = test_df.merge(weather_df,how='left',on=['timestamp','site_id'])
 del weather_df
gc.collect()<normalization>
if sparse_matrix.shape[1] == sparse_matrix_sub.shape[1]:
print('Features check out.')
 else:
print("The number of features in training and test set don't match." )
test_df = features_engineering(test_df)
 test_df = reduce_mem_usage(test_df )<predict_on_test>
del(cat_matrix, brand_matrix, name_matrix, desc_matrix, cond_matrix, ship_matrix)
 del(cat_matrix_sub, brand_matrix_sub, name_matrix_sub, desc_matrix_sub, cond_matrix_sub, ship_matrix_sub)
 del(df, df_sub)
 gc.collect()
results = []
for model in models:
if results == []:
results = np.expm1(model.predict(test_df, num_iteration=model.best_iteration)) / len(models)
 else:
results += np.expm1(model.predict(test_df, num_iteration=model.best_iteration)) / len(models)
 del model
gc.collect()<create_dataframe>
def rmsle(true, pred):
assert len(pred)== len(true)
 return np.sqrt(np.mean(np.power(np.log1p(pred)-np.log1p(true), 2)) )
sample_submission = pd.DataFrame({"row_id": row_ids, "meter_reading": np.clip(results, 0, a_max=None)})
 del row_ids,results
gc.collect()<feature_engineering>
y_target = np.log10(np.array(y_target)+ 1 )
sample_submission.loc[(test_df.building_id.isin(site_0_bids)) &(test_df.meter==0), 'meter_reading'] = sample_submission[(test_df.building_id.isin(site_0_bids)) &(test_df.meter==0)]['meter_reading'] * 3.4118<set_options>
X_train, X_test, y_train, y_test = train_test_split(sparse_matrix, y_target, test_size = 0.1 )
del test_df, models
gc.collect()<save_to_csv>
start = time.time()
reg_ridge = Ridge(solver='sag', alpha=5)
 reg_ridge.fit(X_train, y_train)
 y_pred = reg_ridge.predict(X_test)
 print(rmsle(10 ** y_test - 1, 10 ** y_pred - 1))
print_time(start )
if not debug:
sample_submission.to_csv("submission.csv", index=False )<feature_engineering>
import lightgbm as lgb
leak_df = pd.read_feather('.. /input/ashrae-leak-data-station/leak.feather')
 leak_df.fillna(0, inplace=True)
 leak_df = leak_df[(leak_df.timestamp.dt.year > 2016)&(leak_df.timestamp.dt.year < 2019)]
leak_df.loc[leak_df.meter_reading < 0, 'meter_reading'] = 0
leak_df = leak_df[leak_df.building_id!=245]
root = '.. /input/ashrae-feather-format-for-fast-loading/'
test_df = pd.read_feather(root + 'test.feather')
 building_meta_df = pd.read_feather(root + 'building_metadata.feather')
 test_df['timestamp'] = pd.to_datetime(test_df.timestamp )<merge>
def rmsle_lgb(y_true, y_pred):
assert len(y_true)== len(y_pred)
 return np.sqrt(np.mean(np.power(np.log1p(y_true + 1)- np.log1p(y_pred + 1), 2)) )
sample_submission.loc[sample_submission.meter_reading < 0, 'meter_reading'] = 0
test_df['pred'] = sample_submission.meter_reading
leak_df = leak_df.merge(test_df[['building_id', 'meter', 'timestamp', 'pred', 'row_id']], left_on = ['building_id', 'meter', 'timestamp'], right_on = ['building_id', 'meter', 'timestamp'], how = "left")
 leak_df = leak_df.merge(building_meta_df[['building_id', 'site_id']], on='building_id', how='left' )<import_modules>
print("Cell didn't crash...")
 start = time.time()
params = {
'boosting_type': 'gbdt',
'objective': 'regression',
'metric': 'rmse',
'verbose': 1,
'num_leaves': 31,
'n_estimators': 1000,
'learning_rate': 0.5,
'max_depth': 10
}
reg_lgbm = lgb.LGBMRegressor(**params, n_jobs=4)
 print('check')
 reg_lgbm.fit(X_train, y_train)
 y_pred = reg_lgbm.predict(X_test)
 print(rmsle(10 ** y_test - 1, 10 ** y_pred - 1))
print_time(start )
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import mean_squared_error<compute_test_metric>
def stack_predictions(X_train, y_train, X_test, submit, K, *models):
train_preds = pd.DataFrame(index=np.array(range(X_train.shape[0])))
 test_preds = pd.DataFrame(index=np.array(range(X_test.shape[0])))
 submit_preds = pd.DataFrame(index=np.array(range(submit.shape[0])))
 folds = KFold(n_splits=K, shuffle=True)
 fold_n = 0
train_folds = np.zeros(len(train_preds))
for train_index, test_index in folds.split(X_train):
train_folds[test_index] = fold_n
fold_n += 1
fold_n = 0
test_folds = np.zeros(len(test_preds))
for train_index, test_index in folds.split(X_test):
test_folds[test_index] = fold_n
fold_n += 1
fold_n = 0
submit_folds = np.zeros(len(submit_preds))
for train_index, test_index in folds.split(submit):
submit_folds[test_index] = fold_n
fold_n += 1
for m, model in enumerate(models):
print('Selecting model %d.' %(m+1))
col = 'pred_col_' + str(m)
 train_preds[col] = np.nan
test_preds[col] = np.nan
submit_preds[col] = np.nan
for fold in range(K):
print('Processing a fold...')
 current_model = clone(model)
 current_model.fit(X_train[np.where(train_folds!=fold)], y_train[np.where(train_folds!=fold)])
 train_preds[col].iloc[np.where(train_folds==fold)] = current_model.predict(
 X_train[np.where(train_folds==fold)])
 test_preds[col].iloc[np.where(test_folds==fold)] = current_model.predict(
 X_test[np.where(test_folds==fold)])
 submit_preds[col].iloc[np.where(submit_folds==fold)] = current_model.predict(
 submit[np.where(submit_folds==fold)])
 return train_preds, test_preds, submit_preds
leak_df['pred_l1p'] = np.log1p(leak_df.pred)
 leak_df['meter_reading_l1p'] = np.log1p(leak_df.meter_reading)
 sns.distplot(leak_df.pred_l1p)
 sns.distplot(leak_df.meter_reading_l1p)
 leak_score = np.sqrt(mean_squared_error(leak_df.pred_l1p, leak_df.meter_reading_l1p))<compute_test_metric>
reg_ridge = Ridge(solver='sag', alpha=5)
 params = {
'boosting_type': 'gbdt',
'objective': 'regression',
'metric': 'rmse',
'verbose': 0,
'num_leaves': 31
}
reg_lgbm = lgb.LGBMRegressor(**params )
print('total score=', leak_score )<save_to_csv>
start = time.time()
train_preds, test_preds, sub_preds = stack_predictions(X_train, y_train, X_test,
sparse_matrix_sub, 5,
reg_ridge, reg_lgbm)
 print_time(start )
leak_df = leak_df[['meter_reading', 'row_id']].set_index('row_id' ).dropna()
sample_submission.loc[leak_df.index, 'meter_reading'] = leak_df['meter_reading']
if not debug:
sample_submission.to_csv('submission_ucf_replaced.csv', index=False, float_format='%.4f' )<set_options>
X_train_stacked = sparse.csr_matrix(sparse.hstack([X_train, sparse.csr_matrix(train_preds)]))
X_test_stacked = sparse.csr_matrix(sparse.hstack([X_test, sparse.csr_matrix(test_preds)]))
sub_stacked = sparse.csr_matrix(sparse.hstack([sparse_matrix_sub, sparse.csr_matrix(sub_preds)]))
py.init_notebook_mode(connected=True )<compute_test_metric>
start = time.time()
reg_ridge = Ridge(solver='sag', alpha=5)
 reg_ridge.fit(X_train_stacked, y_train)
 y_pred = reg_ridge.predict(X_test_stacked)
 print(rmsle(10 ** y_test - 1, 10 ** y_pred - 1))
print_time(start )
def metric(y_true, y_pred):
return mean_squared_error(np.log1p(y_true), np.log1p(y_pred)) **(0.5 )<define_variables>
start = time.time()
params = {
'boosting_type': 'gbdt',
'objective': 'regression',
'metric': 'rmse',
'verbose': 0,
'num_leaves': 31,
'n_estimators': 1000,
'learning_rate': 0.5,
'max_depth': 10,
}
reg_lgbm = lgb.LGBMRegressor(**params, n_jobs=4)
 reg_lgbm.fit(X_train_stacked, y_train)
 r_pred = reg_lgbm.predict(X_test_stacked)
 print(rmsle(10 ** y_test - 1,10 ** r_pred - 1))
print_time(start )
<set_options><EOS> 
pred_sub = reg_lgbm.predict(sub_stacked)
 lightgbm_submission = submission.copy()
lightgbm_submission['price'] = pd.DataFrame(10 ** pred_sub - 1)
 lightgbm_submission.to_csv('stacked_submission_1.csv', index=False )