Kaggle competition: Mercari Price Suggestion Challenge
<SOS> metric: RMSLE Kaggle data source: mercari-price-suggestion-challenge<define_variables>
import os ; os.environ['OMP_NUM_THREADS'] = '4'
test_dir = "/kaggle/input/deepfake-detection-challenge/test_videos/"
test_videos = sorted([x for x in os.listdir(test_dir)if x[-4:] == ".mp4"])
 len(test_videos )<import_modules>
import pandas as pd
import operator
import matplotlib.pyplot as plt
import numpy as np
import nltk
import time
print("PyTorch version:", torch.__version__)
 print("CUDA version:", torch.version.cuda)
 print("cuDNN version:", torch.backends.cudnn.version() )<set_options>
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 )
gpu = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 gpu<load_pretrained>
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)
facedet = BlazeFace().to(gpu)
 facedet.load_weights("/kaggle/input/blazeface-pytorch/blazeface.pth")
 facedet.load_anchors("/kaggle/input/blazeface-pytorch/anchors.npy")
 _ = facedet.train(False )<load_pretrained>
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 )
frames_per_video = 17
video_reader = VideoReader()
video_read_fn = lambda x: video_reader.read_frames(x, num_frames=frames_per_video)
 face_extractor = FaceExtractor(video_read_fn, facedet )<define_variables>
print('Training Data')
 check_null(df, df.columns)
 print('Submission Data')
 check_null(df_sub, df_sub.columns )
input_size = 224<normalization>
df.shipping.value_counts()
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize_transform = Normalize(mean, std )<choose_model_class>
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()))
class MyResNeXt(models.resnet.ResNet):
def __init__(self, training=True):
super(MyResNeXt, self ).__init__(block=models.resnet.Bottleneck,
layers=[3, 4, 6, 3],
groups=32,
width_per_group=4)
 self.fc = nn.Linear(2048, 1 )<load_pretrained>
print('Free items: %d, representing %.5f%% of all items.' % 
(df.price[df.price == 0].count() , 
(df.price[df.price == 0].count() / df.shape[0])) )
checkpoint = torch.load("/kaggle/input/deepfakes-inference-demo/resnext.pth", map_location=gpu)
 model = MyResNeXt().to(gpu)
 model.load_state_dict(checkpoint)
 _ = model.eval()
del checkpoint<predict_on_test>
print('Free items where seller pays shipping: %d.' %
df.price[operator.and_(df.price == 0, df.shipping == 1)].count() )
def predict_on_video(video_path, batch_size):
try:
faces = face_extractor.process_video(video_path)
 face_extractor.keep_only_best_face(faces)
 if len(faces)> 0:
x = np.zeros(( batch_size, input_size, input_size, 3), dtype=np.uint8)
 n = 0
for frame_data in faces:
for face in frame_data["faces"]:
resized_face = isotropically_resize_image(face, input_size)
 resized_face = make_square_image(resized_face)
 if n < batch_size:
x[n] = resized_face
n += 1
else:
print("WARNING: have %d faces but batch size is %d" %(n, batch_size))
if n > 0:
x = torch.tensor(x, device=gpu ).float()
x = x.permute(( 0, 3, 1, 2))
for i in range(len(x)) :
x[i] = normalize_transform(x[i] / 255.)
with torch.no_grad() :
y_pred = model(x)
 y_pred = torch.sigmoid(y_pred.squeeze())
 return y_pred[:n].mean().item()
except Exception as e:
print("Prediction error on video %s: %s" %(video_path, str(e)))
 return 0.5<predict_on_test>
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()))
def predict_on_video_set(videos, num_workers):
def process_file(i):
filename = videos[i]
y_pred = predict_on_video(os.path.join(test_dir, filename), batch_size=frames_per_video)
 return y_pred
with ThreadPoolExecutor(max_workers=num_workers)as ex:
predictions = ex.map(process_file, range(len(videos)))
 return list(predictions )<define_variables>
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))
speed_test = False<predict_on_test>
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()
if speed_test:
start_time = time.time()
speedtest_videos = test_videos[:5]
predictions = predict_on_video_set(speedtest_videos, num_workers=4)
 elapsed = time.time() - start_time
print("Elapsed %f sec.Average per video: %f sec." %(elapsed, elapsed / len(speedtest_videos)) )<predict_on_test>
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))
predictions = predict_on_video_set(test_videos, num_workers=4 )<save_to_csv>
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)) )
submission_df = pd.DataFrame({"filename": test_videos, "label": predictions})
 submission_df.to_csv("submission.csv", index=False )<import_modules>
nltk.data.path.append(r'D:\Python\Data Sets
ltk_data')
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename)) 
<set_options>
cat_vec = CountVectorizer(stop_words=[stopwords, string.punctuation], max_features=int(len(cat_counts)*0.9))
cat_matrix = cat_vec.fit_transform(df.category_name)
 cat_matrix_sub = cat_vec.transform(df_sub.category_name )
SEED = 42
FOLDS = 3
def seed_env(seed=SEED):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 pd.set_option('display.max_columns', 380)
 pd.set_option('display.max_rows', 500)
 seed_env()
sns.set()
%matplotlib inline<data_type_conversions>
cat_tokens = list(zip(cat_vec.get_feature_names() , np.array(cat_matrix.sum(axis=0)).ravel()))
def air_temp_fe(df):
df['min_day_temp'] = df.groupby(['building_id','month','day'])['air_temperature'].transform('min' ).astype(np.float16)
 df['max_day_temp'] = df.groupby(['building_id','month','day'])['air_temperature'].transform('max' ).astype(np.float16)
 df['mean_day_temp'] = df.groupby(['building_id','month','day'])['air_temperature'].transform('mean' ).astype(np.float16)
 df['site_id_mean_temp'] = df.groupby(['site_id','month'])['air_temperature'].transform('mean' ).astype(np.float16)
 nominal_degree = 18
df['degree_day'] =(np.round(( df['max_day_temp'] + df['min_day_temp'])/2)- nominal_degree ).astype(np.float16)
 df.drop(['min_day_temp','max_day_temp'], axis = 1, inplace = True)
 def sea_level_pressure_fe(df):
df['sea_level_pressure_month_mean'] = df.groupby(['building_id','month'])['sea_level_pressure'].transform('mean' ).astype(np.float16)
 def add_lag_feature(df, window=3):
group_df = df.groupby('site_id')
 cols = ['air_temperature']
rolled = group_df[cols].rolling(window=window, min_periods=0)
 lag_mean = rolled.mean().reset_index().astype(np.float16)
 for col in cols:
df[f'{col}_mean_lag{window}'] = lag_mean[col].astype(np.float32)
 del lag_mean,rolled,group_df
gc.collect() 
<data_type_conversions>
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 )
def set_localtime(df):
zone_dict={0:4,1:0,2:7,3:4,4:7,5:0,6:4,7:4,8:4,9:5,10:7,11:4,12:0,13:5,14:4,15:4}
for sid, zone in zone_dict.items() :
sids = df.site_id == sid
df.loc[sids, 'timestamp'] = df[sids].timestamp - pd.offsets.Hour(zone)
 def time_features(df):
df['month'] = df['timestamp'].dt.month.astype(np.int8)
 df['day'] = df['timestamp'].dt.day.astype(np.int8)
 df['hour'] = df['timestamp'].dt.hour.astype(np.int8)
 df['weekday'] = df['timestamp'].dt.weekday.astype(np.int8 )<data_type_conversions>
brand_tokens = list(zip(brand_vec.get_feature_names() , np.array(brand_matrix.sum(axis=0)).ravel()))
def building_features(df,df_group):
building_mean = df_group.mean().astype(np.float16)
 building_median = df_group.median().astype(np.float16)
 building_std = df_group.std().astype(np.float16)
 df['building_median'] = df['building_id'].map(building_median ).astype(np.float16)
 df['building_mean'] = df['building_id'].map(building_mean ).astype(np.float16)
 df['building_std'] = df['building_id'].map(building_std ).astype(np.float16)
 del building_mean,building_median,building_std
gc.collect()<define_variables>
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 = ["primary_use","building_id","month","day","hour","weekday","site_id"]<train_model>
print('Kept %d words.' % len(name_vec.get_feature_names()))
def train(train_df, target, splits):
params = {
"objective": "regression",
"boosting": "gbdt",
"num_leaves": 41,
"max_depth": 6,
"learning_rate": 0.05,
"feature_fraction": 0.8,
"bagging_fraction": 0.33,
"bagging_freq": 5,
"metric": "rmse",
"reg_lambda": 2
}
lgb_models = list()
for split in splits:
train_index = split[0]
val_index = split[1]
train_X = train_df.iloc[train_index]
val_X = train_df.iloc[val_index]
train_y = target.iloc[train_index]
val_y = target.iloc[val_index]
lgb_train = lgb.Dataset(train_X, train_y, categorical_feature=categorical_features)
 lgb_eval = lgb.Dataset(val_X, val_y, categorical_feature=categorical_features)
 lgb_model = lgb.train(params,
lgb_train,
valid_sets=(lgb_train, lgb_eval),
valid_names=["train","valid"],
num_boost_round=1600,
early_stopping_rounds=200,
verbose_eval = 200)
 lgb_models.append(lgb_model)
 del train_X,val_X,train_y,val_y,lgb_train,lgb_eval
gc.collect()
return lgb_models<data_type_conversions>
desc_vec = TfidfVectorizer(max_features=200000,
stop_words=[stopwords, string.punctuation])
 desc_matrix = desc_vec.fit_transform(df.item_description)
 desc_matrix_sub= desc_vec.transform(df_sub.item_description )
def df_transformation(df, group):
df['timestamp'] = pd.to_datetime(df['timestamp'], format="%Y-%m-%d %H:%M:%S")
 df["meter"] = df["meter"].astype(np.int8)
 df["building_id"] = df["building_id"].astype(np.int16)
 time_features(df)
 building_features(df,group)
 def weather_transformation(weather):
weather['timestamp'] = pd.to_datetime(weather['timestamp'], format="%Y-%m-%d %H:%M:%S")
 set_localtime(weather)
 def building_transformation(building):
building["primary_use"] = building["primary_use"].astype(np.int8)
 building["floor_count"] = building["floor_count"].astype(np.int8)
 building["year_built"] = building["year_built"].astype(np.int16)
 def memory_adj(df):
df['site_id'] = df['site_id'].astype(np.int8)
 df['square_feet'] = df['square_feet'].astype(np.int32 )<feature_engineering>
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))
train_df = pd.read_feather('.. /input/ashre-data-clean-leak-for-train/train_no_nan_s.feather')
 train_df['meter_reading'] = np.log1p(train_df['meter_reading'])
 df_group = train_df.groupby('building_id')['meter_reading']
df_transformation(train_df,df_group)
 weather_df = pd.read_feather('.. /input/ashre-data-clean-leak-for-train/weather_intr_no_nan_s.feather')
 building_metadata_df = pd.read_feather('.. /input/ashre-data-clean-leak-for-train/building_no_nan_s.feather')
 weather_transformation(weather_df)
 building_transformation(building_metadata_df )<merge>
ship_matrix = sparse.csr_matrix(df.shipping ).transpose()
ship_matrix_sub = sparse.csr_matrix(df_sub.shipping ).transpose()
def df_for_meter_type(meter):
target_train_df = train_df[train_df['meter'] == meter]
target_train_df = target_train_df.merge(building_metadata_df, on='building_id', how='left')
 target_train_df = target_train_df.merge(weather_df, on=['site_id', 'timestamp'], how='left')
 air_temp_fe(target_train_df)
 sea_level_pressure_fe(target_train_df)
 target_train_df.drop(['air_temperature','sea_level_pressure','wind_direction','wind_speed'], axis = 1, inplace = True)
 train_idx_f1 = target_train_df[(target_train_df.timestamp.dt.year == 2016)].index
test_idx_f1 = target_train_df[(target_train_df.timestamp.dt.year == 2017)|(target_train_df.timestamp.dt.year == 2018)].index
train_idx_f2 = target_train_df[(target_train_df.timestamp.dt.year == 2016)|(target_train_df.timestamp.dt.year == 2017)].index
test_idx_f2 = target_train_df[(target_train_df.timestamp.dt.year == 2018)].index
splits = [(train_idx_f1,test_idx_f1),(train_idx_f2,test_idx_f2)]
target = target_train_df['meter_reading']
meter_df = target_train_df.drop(['meter_reading','timestamp','meter'],axis = 1,inplace = False)
 memory_adj(meter_df)
 del target_train_df,train_idx_f1,test_idx_f1,train_idx_f2,test_idx_f2
return meter_df, target, splits<split>
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]))
%%time
meter_0_df, target, splits = df_for_meter_type(0)
 meter_0_df<split>
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." )
%%time
meter_1_df, target, splits = df_for_meter_type(1)
 meter_1_df<split>
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()
%%time
meter_2_df, target, splits = df_for_meter_type(2)
 meter_2_df<split>
def rmsle(true, pred):
assert len(pred)== len(true)
 return np.sqrt(np.mean(np.power(np.log1p(pred)-np.log1p(true), 2)) )
%%time
meter_3_df, target, splits = df_for_meter_type(2)
 meter_3_df<set_options>
y_target = np.log10(np.array(y_target)+ 1 )
del train_df, weather_df
gc.collect()<load_from_disk>
X_train, X_test, y_train, y_test = train_test_split(sparse_matrix, y_target, test_size = 0.1 )
test_df = pd.read_feather('.. /input/ashre-simple-no-missing-for-test/test_no_nan_s.feather')
 df_transformation(test_df,df_group)
 weather_test_df = pd.read_feather('.. /input/ashre-simple-no-missing-for-test/weather_test_intr_no_nan_s.feather')
 building_metadata_df = pd.read_feather('.. /input/ashre-simple-no-missing-for-test/building_test_no_nan_s.feather')
 weather_transformation(weather_test_df)
 building_transformation(building_metadata_df )<merge>
import lightgbm as lgb
def df_for_meter_type_test(meter):
target_test_df = test_df[test_df['meter'] == meter]
target_test_df = target_test_df.merge(building_metadata_df, on='building_id', how='left')
 target_test_df = target_test_df.merge(weather_test_df, on=['site_id', 'timestamp'], how='left')
 air_temp_fe(target_test_df)
 sea_level_pressure_fe(target_test_df)
 target_test_df.drop(['air_temperature','sea_level_pressure','wind_direction','wind_speed'], axis = 1, inplace = True)
 meter_df = target_test_df.drop(['timestamp','meter','row_id'],axis = 1,inplace = False)
 memory_adj(meter_df)
 del target_test_df
return meter_df<predict_on_test>
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)) )
X_test = df_for_meter_type_test(0)
 y_test0 = sum([model.predict(X_test, num_iteration=model.best_iteration)for model in meter_0_models])/2
del X_test,meter_0_models
gc.collect()<predict_on_test>
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 )
X_test = df_for_meter_type_test(1)
 y_test1 = sum([model.predict(X_test, num_iteration=model.best_iteration)for model in meter_1_models])/2
del X_test,meter_1_models
gc.collect()<predict_on_test>
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
X_test = df_for_meter_type_test(2)
 y_test2 = sum([model.predict(X_test, num_iteration=model.best_iteration)for model in meter_2_models])/2
del X_test,meter_2_models
gc.collect()<predict_on_test>
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 )
X_test = df_for_meter_type_test(3)
 y_test3 = sum([model.predict(X_test, num_iteration=model.best_iteration)for model in meter_3_models])/2
del X_test,meter_3_models
gc.collect()<load_from_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 )
sample_submission = pd.read_csv('.. /input/ashrae-energy-prediction/sample_submission.csv' )<feature_engineering>
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)]))
sample_submission.loc[test_df[test_df['meter'] == 0]['row_id'], 'meter_reading'] = np.clip(np.expm1(y_test0),0, a_max=None)
 sample_submission.loc[test_df[test_df['meter'] == 1]['row_id'], 'meter_reading'] = np.clip(np.expm1(y_test1),0, a_max=None)
 sample_submission.loc[test_df[test_df['meter'] == 2]['row_id'], 'meter_reading'] = np.clip(np.expm1(y_test2),0, a_max=None)
 sample_submission.loc[test_df[test_df['meter'] == 3]['row_id'], 'meter_reading'] = np.clip(np.expm1(y_test3),0, a_max=None )<save_to_csv>
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 )
sample_submission.to_csv('submission_Dec8_1.csv', index=False, float_format='%.4f' )<set_options>
start = time.time()
params = {
'boosting_type': 'gbdt',
'objective': 'regression',
'metric': 'rmse',
'verbose': 0,
'num_leaves': 35,
'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 )
<load_from_csv><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 )