Kaggle competition: House Prices - Advanced Regression Techniques
<SOS> metric: rmsle Kaggle data source: house-prices-advanced-regression-techniques<categorify>
print('Python: {}'.format(sys.version))
print('scipy: {}'.format(scipy.__version__))
print('numpy: {}'.format(np.__version__))
print('matplotlib: {}'.format(matplotlib.__version__))
print('pandas: {}'.format(pd.__version__))
print('sklearn: {}'.format(sklearn.__version__))
print('seaborn: {}'.format(sns.__version__))
def rep(x):
if x<0:
y=0
else:
y=1
return y<feature_engineering>
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" )
bureau_new['CREDIT_ENDDATE_BINARY'] = bureau_new['DAYS_CREDIT_ENDDATE'].apply(lambda x: rep(x))<groupby>
total = train.isnull().sum().sort_values(ascending=False)
 percent =(train.isnull().sum() /train.isnull().count() ).sort_values(ascending=False)
 missing_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing_data.head(20 )
grp = bureau_new.groupby('SK_ID_CURR')['CREDIT_ENDDATE_BINARY'].mean().reset_index().rename(index=str, columns={'CREDIT_ENDDATE_BINARY': 'CREDIT_ENDDATE_PERCENTAGE'} )<merge>
total = test.isnull().sum().sort_values(ascending=False)
 percent =(test.isnull().sum() /train.isnull().count() ).sort_values(ascending=False)
 missing_data = pd.concat([total, percent], axis=1, keys=['Total', 'Percent'])
 missing_data.head(20 )
bureau_new = bureau_new.merge(grp, on = 'SK_ID_CURR', how = 'left')
 del grp<define_variables>
train.fillna(-999,inplace=True)
 test.fillna(-999,inplace=True )
num_aggregations = {
'DAYS_CREDIT': ['min', 'max', 'mean', 'var'],
'DAYS_CREDIT_ENDDATE': ['min', 'max', 'mean'],
'DAYS_CREDIT_UPDATE': ['mean'],
'CREDIT_DAY_OVERDUE': ['max', 'mean'],
'AMT_CREDIT_MAX_OVERDUE': ['mean'],
'AMT_CREDIT_SUM': ['max', 'mean', 'sum'],
'AMT_CREDIT_SUM_DEBT': ['max', 'mean', 'sum'],
'AMT_CREDIT_SUM_OVERDUE': ['mean'],
'AMT_CREDIT_SUM_LIMIT': ['mean', 'sum'],
'AMT_ANNUITY': ['max', 'mean'],
'CNT_CREDIT_PROLONG': ['sum'],
}<groupby>
train.isnull().sum().sum() , test.isnull().sum().sum()
bureau_agg = bureau_new.groupby('SK_ID_CURR' ).agg({**num_aggregations} )<feature_engineering>
y = train.SalePrice.values
X = train.drop(['SalePrice', 'Id'], axis=1)
 X_test = test.drop(['Id'], axis = 1 )
bureau_agg.columns = pd.Index(['BURO_' + e[0] + "_" + e[1].upper() for e in bureau_agg.columns.tolist() ])
 bureau_agg.reset_index(inplace=True )<merge>
categorical_features_indices = np.where(X.dtypes != np.float)[0]
categorical_features_indices = np.where(X_test.dtypes != np.float)[0]
bureau_merge = bureau_new.merge(bureau_agg, on = 'SK_ID_CURR', how = 'left')
 del bureau_agg<define_variables>
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.1, random_state=1, shuffle=True)
 X_train.shape, X_val.shape, y_train.shape,y_val.shape, X_test.shape
buro_cat_features = [bcol for bcol in bureau_merge.columns if bureau_merge[bcol].dtype == 'object']<categorify>
model = CatBoostRegressor(random_state=1, iterations=1500, depth=5, learning_rate=.1, loss_function='RMSE')
 model.fit(X_train, y_train,cat_features=categorical_features_indices,eval_set=(X_val, y_val),plot=True )
buro = pd.get_dummies(bureau_merge, columns=buro_cat_features )<define_variables>
print(model.score(X_train, y_train))
cat_columns = [col for col in bureau_balance.columns if bureau_balance[col].dtype == 'object']<categorify>
y_pred = model.predict(X_val)
 y_pred = y_pred.astype(int)
 print(model.score(X_val, y_val)) , print(r2_score(y_pred, model.predict(X_val)) )
bureau_balance = pd.get_dummies(bureau_balance,cat_columns, dummy_na = True )<groupby>
df=pd.DataFrame({'Actual': y_val, 'Predicted':y_pred})
 df
bb_group = bureau_balance.groupby('SK_ID_BUREAU' ).agg(['min', 'max', 'mean'] )<rename_columns>
final_labels = model.predict(X_test)
 final_labels = final_labels.astype(int)
 final_labels
bb_group.columns = pd.Index([e[0] + "_" + e[1].upper() for e in bb_group.columns.tolist() ])
 bb_group.reset_index(inplace=True)
<merge>
final_result = pd.DataFrame({'Id': test['Id'], 'SalePrice': final_labels} )
buro = buro.merge(bb_group, on = 'SK_ID_BUREAU', how = 'left' )<groupby>
final_result.to_csv('submission.csv', index=False)
 print("Your submission was successfully saved!" )
<groupby><EOS> 
submission = pd.read_csv("submission.csv")
 submission