Kaggle competition: ASHRAE - Great Energy Predictor III
<SOS> metric: RMSLE Kaggle data source: ashrae-great-energy-predictor-iii<feature_engineering>
py.init_notebook_mode(connected=True)
numeric_feats = Train.dtypes[Train.dtypes ==("float" or "int")].index
skewed_feats = Train[numeric_feats].apply(lambda x: skew(x.dropna()))
skewed_feats = skewed_feats[skewed_feats > 0.75]
skewed_feats = skewed_feats.index
print(skewed_feats)
 Train[skewed_feats] = np.log1p(Train[skewed_feats])
 Test[skewed_feats] = np.log1p(Test[skewed_feats] )<data_type_conversions>
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
Train=Train.fillna(0)
 Test=Test.fillna(0 )<prepare_x_and_y>
%%time
root = Path('.. /input/ashrae-feather-format-for-fast-loading')
 train_df = pd.read_feather(root/'train.feather')
 test_df = pd.read_feather(root/'test.feather')
 building_meta_df = pd.read_feather(root/'building_metadata.feather' )
X_t=Train[Train.columns[Train.columns!='SalePrice']]
y_t=Train['SalePrice']
X_test=Test<split>
leak_df = pd.read_feather('.. /input/ashare-leak-data-station-2/leak2.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]
X_train,X_valid,y_train,y_valid=train_test_split(X_t,y_t,test_size=0.2,shuffle=True )<train_on_grid>
leak_df.meter.value_counts()
categorical_features_indices= np.where(X_test.dtypes == np.object)[0]
Train_set=Pool(X_train, y_train,cat_features=categorical_features_indices)
 Eval_set=Pool(X_valid, y_valid,cat_features=categorical_features_indices)
 def objective(trial):
param = {
'iterations':500,
'learning_rate':0.05,
'use_best_model':True,
'od_type' : "Iter",
'od_wait' : 100,
"depth": trial.suggest_int("max_depth", 2, 10),
"l2_leaf_reg": trial.suggest_loguniform("lambda", 1e-8, 100),
'eval_metric':trial.suggest_categorical("loss_function",['MAE','RMSE']),
'one_hot_max_size':1024
}
model=CatBoostRegressor(**param)
 print(param)
 model.fit(Train_set,eval_set=Eval_set,plot=False,verbose=False)
 pred=model.predict(Pool(X_valid,cat_features= np.where(X_valid.dtypes == np.object)[0]))
mae=sklearn.metrics.mean_absolute_error(pred,y_valid)
 return mae<import_modules>
print(leak_df.duplicated().sum() )
import sklearn 
<choose_model_class>
print(len(leak_df)/ len(train_df))
param={'one_hot_max_size':1024,'iterations': 2000, 'learning_rate': 0.05, 'use_best_model': True, 'od_type': 'Iter', 'od_wait': 100, 'depth': 7, 'l2_leaf_reg': 0.13679400187948992,'loss_function':'MAE'}
model1=CatBoostRegressor(**param)
 model1.fit(Train_set,eval_set=Eval_set,plot=True,verbose=False)
 pred=model1.predict(Pool(X_test,cat_features= np.where(X_test.dtypes == np.object)[0]))<feature_engineering>
del train_df
gc.collect()
X_test['SalePrice']=pred<save_to_csv>
sample_submission1 = pd.read_csv('.. /input/ashrae-kfold-lightgbm-without-leak-1-08/submission.csv', index_col=0)
 sample_submission2 = pd.read_csv('.. /input/ashrae-half-and-half/submission.csv', index_col=0)
 sample_submission3 = pd.read_csv('.. /input/ashrae-highway-kernel-route4/submission.csv', index_col=0 )
X_test[['Id','SalePrice']].to_csv('submission.csv',index=False )<load_from_csv>
test_df['pred1'] = sample_submission1.meter_reading
test_df['pred2'] = sample_submission2.meter_reading
test_df['pred3'] = sample_submission3.meter_reading
test_df.loc[test_df.pred3<0, 'pred3'] = 0
del sample_submission1, sample_submission2, sample_submission3
gc.collect()
test_df = reduce_mem_usage(test_df)
 leak_df = reduce_mem_usage(leak_df )
train = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/train.csv')
 test = pd.read_csv('.. /input/house-prices-advanced-regression-techniques/test.csv' )<define_variables>
leak_df = leak_df.merge(test_df[['building_id', 'meter', 'timestamp', 'pred1', 'pred2', 'pred3', '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' )
train_ID = train['Id']
test_ID = test['Id']<drop_column>
leak_df['pred1_l1p'] = np.log1p(leak_df.pred1)
 leak_df['pred2_l1p'] = np.log1p(leak_df.pred2)
 leak_df['pred3_l1p'] = np.log1p(leak_df.pred3)
 leak_df['meter_reading_l1p'] = np.log1p(leak_df.meter_reading )
train.drop("Id", axis = 1, inplace = True)
 test.drop("Id", axis = 1, inplace = True )<drop_column>
leak_df[leak_df.pred1_l1p.isnull() ]
train = train.drop(train[(train['GrLivArea']>4000)&(train['SalePrice']<300000)].index )<feature_engineering>
leak_df['mean_pred'] = np.mean(leak_df[['pred1', 'pred2', 'pred3']].values, axis=1)
 leak_df['mean_pred_l1p'] = np.log1p(leak_df.mean_pred)
 leak_score = np.sqrt(mean_squared_error(leak_df.mean_pred_l1p, leak_df.meter_reading_l1p))
sns.distplot(leak_df.mean_pred_l1p)
 sns.distplot(leak_df.meter_reading_l1p)
 print('mean score=', leak_score )
train["SalePrice"] = np.log1p(train["SalePrice"] )<prepare_x_and_y>
N = 10
scores = np.zeros(N,)
 for i in range(N):
p = i * 1./N
v = p * leak_df['pred1'].values +(1.-p)* leak_df ['pred3'].values
vl1p = np.log1p(v)
 scores[i] = np.sqrt(mean_squared_error(vl1p, leak_df.meter_reading_l1p))
ntrain = train.shape[0]
y_train = train.SalePrice.values
concat_data = pd.concat(( train, test)).reset_index(drop=True)
 concat_data.drop(['SalePrice'], axis=1, inplace=True )<sort_values>
best_weight = np.argmin(scores)* 1./N
print(scores.min() , best_weight )
concat_data.isnull().sum().sort_values(ascending=False )<data_type_conversions>
scores = np.zeros(N,)
 for i in range(N):
p = i * 1./N
v = p *(best_weight * leak_df['pred1'].values +(1.-best_weight)* leak_df ['pred3'].values)+(1.-p)* leak_df ['pred2'].values
vl1p = np.log1p(v)
 scores[i] = np.sqrt(mean_squared_error(vl1p, leak_df.meter_reading_l1p))
concat_data["PoolQC"] = concat_data["PoolQC"].fillna("None")
 concat_data["MiscFeature"] = concat_data["MiscFeature"].fillna("None")
 concat_data["Alley"] = concat_data["Alley"].fillna("None")
 concat_data["Fence"] = concat_data["Fence"].fillna("None")
 concat_data["FireplaceQu"] = concat_data["FireplaceQu"].fillna("None" )<categorify>
best_weight2 = np.argmin(scores)* 1./N
print(scores.min() , best_weight2)
concat_data["LotFrontage"] = concat_data.groupby("Neighborhood")["LotFrontage"].transform(
 lambda x: x.fillna(x.median()))<data_type_conversions>
all_combinations = list(np.linspace(0.1,0.5,401))
all_combinations1 = [i for i in all_combinations if i > 0.37 and i < 0.4]
all_combinations2 = [i for i in all_combinations if i > 0.34 and i < 0.385]
all_combinations3 = [i for i in all_combinations if i > 0.19 and i < 0.22]
all_combinations
for col in('GarageType', 'GarageFinish', 'GarageQual', 'GarageCond'):
concat_data[col] = concat_data[col].fillna('None' )<data_type_conversions>
import itertools
for col in('GarageYrBlt', 'GarageArea', 'GarageCars'):
concat_data[col] = concat_data[col].fillna(0 )<data_type_conversions>
l = [all_combinations1, all_combinations2, all_combinations3]
all_l = list(itertools.product(*l)) + list(itertools.product(*reversed(l)) )
for col in('BsmtFinSF1', 'BsmtFinSF2', 'BsmtUnfSF','TotalBsmtSF', 'BsmtFullBath', 'BsmtHalfBath'):
concat_data[col] = concat_data[col].fillna(0 )<categorify>
filtered_combis = [l for l in all_l if l[0] + l[1] + l[2] > 0.95 and l[0] + l[1] + l[2] < 1]
for col in('BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2'):
concat_data[col] = concat_data[col].fillna('None' )<data_type_conversions>
best_combi = []
for i, combi in enumerate(filtered_combis):
print("Now at: " + str(i)+ " out of " + str(len(filtered_combis)))
 score1 = combi[0]
score2 = combi[1]
score3 = combi[2]
v = score1 * leak_df['pred1'].values + score2 * leak_df['pred3'].values + score3 * leak_df['pred2'].values
vl1p = np.log1p(v)
 curr_score = np.sqrt(mean_squared_error(vl1p, leak_df.meter_reading_l1p))
if best_combi:
prev_score = best_combi[0][1]
if curr_score < prev_score:
best_combi[:] = []
best_combi += [(i, curr_score)]
else:
best_combi += [(i, curr_score)]
score = best_combi[0][1]
print(score )
concat_data["MasVnrType"] = concat_data["MasVnrType"].fillna("None")
 concat_data["MasVnrArea"] = concat_data["MasVnrArea"].fillna(0 )<data_type_conversions>
sample_submission = pd.read_feather(os.path.join(root, 'sample_submission.feather'))
final_combi = filtered_combis[best_combi[0][0]]
w1 = final_combi[0]
w2 = final_combi[1]
w3 = final_combi[2]
print("The weights are: w1=" + str(w1)+ ", w2=" + str(w2)+ ", w3=" + str(w3))
sample_submission['meter_reading'] = w1 * test_df.pred1 + w2 * test_df.pred3 + w3 * test_df.pred2
sample_submission.loc[sample_submission.meter_reading < 0, 'meter_reading'] = 0
concat_data['MSZoning'] = concat_data['MSZoning'].fillna(concat_data['MSZoning'].mode() [0] )<concatenate>
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']
<data_type_conversions><EOS> 
sample_submission.to_csv('submission.csv', index=False, float_format='%.4f' )