Kaggle competition: Tabular Playground Series - Jan 2021
<SOS> metric: RMSE Kaggle data source: tabular-playground-series-jan-2021<split>
import numpy as np
import pandas as pd
apps_train = apps[~apps['TARGET'].isnull() ]
apps_test = apps[apps['TARGET'].isnull() ]
apps.shape, apps_train.shape, apps_test.shape<split>
df_train = pd.read_csv(".. /input/tabular-playground-series-jan-2021/train.csv")
 df_test = pd.read_csv(".. /input/tabular-playground-series-jan-2021/test.csv" )
ftr_app = apps_train.drop(['SK_ID_CURR', 'TARGET'], axis=1)
 target_app = apps_train['TARGET']
train_x, valid_x, train_y, valid_y = train_test_split(ftr_app, target_app, test_size=0.3, random_state=2020)
 train_x.shape, valid_x.shape<train_model>
x = df_train.iloc[:, 1:15].values
print(x)
 y = df_train.iloc[:, -1].values
clf = LGBMClassifier(
 n_jobs=-1,
n_estimators=1000,
learning_rate=0.02,
num_leaves=32,
subsample=0.8,
max_depth=12,
silent=-1,
verbose=-1 
)
 clf.fit(train_x, train_y, eval_set=[(train_x, train_y),(valid_x, valid_y)], eval_metric= 'auc', verbose= 100,
early_stopping_rounds= 100 )<predict_on_test>
X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.1, random_state=0 )
preds = clf.predict_proba(apps_test.drop(['SK_ID_CURR', 'TARGET'], axis=1)) [:, 1 ]<save_to_csv>
from xgboost import XGBRegressor
import lightgbm as ltb
from sklearn.model_selection import GridSearchCV
from sklearn import metrics
from sklearn import model_selection
apps_test['TARGET'] = preds
apps_test[['SK_ID_CURR', 'TARGET']].to_csv('apps_baseline05.csv', index=False )<set_options>
XGB = XGBRegressor(max_depth=3,learning_rate=0.1,n_estimators=1000,reg_alpha=0.001,reg_lambda=0.000001,n_jobs=-1,min_child_weight=3)
 XGB.fit(X_train,y_train )
%matplotlib inline
warnings.filterwarnings('ignore')
 pd.set_option('display.max_rows', 100)
 pd.set_option('display.max_columns', 200 )<load_from_csv>
y_pred_xgb = XGB.predict(X_test )
app_train = pd.read_csv('.. /input/home-credit-default-risk/application_train.csv')
 app_test = pd.read_csv('.. /input/home-credit-default-risk/application_test.csv' )<count_missing_values>
print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, y_pred_xgb))
print('Mean Squared Error:', metrics.mean_squared_error(y_test, y_pred_xgb))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, y_pred_xgb)) )
print(app_train.isnull().sum())
 print("결측치 있는 컴럼 개수: ",sum(app_train.isnull().sum() !=0))<count_values>
lgbm = ltb.LGBMRegressor()
app_train['TARGET'].value_counts()<define_variables>
param_grid = {
"boosting_type": ['gbdt'],
"num_leaves": [9, 19],
"max_depth": [29],
"learning_rate": [0.1, 0.15],
"n_estimators": [1000],
"subsample_for_bin": [200000],
"objective": ["regression"],
"min_child_weight": [0.01],
"min_child_samples":[100, 200],
"subsample":[1.0],
"subsample_freq":[0],
"colsample_bytree":[1.0],
"reg_alpha":[0.0],
"reg_lambda":[0.0]
}
columns = ['AMT_INCOME_TOTAL','AMT_CREDIT', 'AMT_ANNUITY', 'AMT_GOODS_PRICE', 'DAYS_BIRTH', 'DAYS_EMPLOYED', 'DAYS_ID_PUBLISH',
'DAYS_REGISTRATION', 'DAYS_LAST_PHONE_CHANGE', 'CNT_FAM_MEMBERS', 'REGION_RATING_CLIENT', 'EXT_SOURCE_1',
'EXT_SOURCE_2', 'EXT_SOURCE_3', 'AMT_REQ_CREDIT_BUREAU_HOUR', 'AMT_REQ_CREDIT_BUREAU_DAY', 'AMT_REQ_CREDIT_BUREAU_WEEK',
'AMT_REQ_CREDIT_BUREAU_MON', 'AMT_REQ_CREDIT_BUREAU_QRT', 'AMT_REQ_CREDIT_BUREAU_YEAR']
show_hist_by_target(app_train, columns )<count_values>
model = model_selection.RandomizedSearchCV(
 estimator=lgbm,
param_distributions=param_grid,
n_iter=100,
scoring="neg_root_mean_squared_error",
verbose=10,
n_jobs=-1,
cv=5
)
cond_1 =(app_train['TARGET'] == 1)
 cond_0 =(app_train['TARGET'] == 0)
 print('CODE_GENDER
')
 print(app_train['CODE_GENDER'].value_counts() /app_train.shape[0])
 print('
연체인 경우
',app_train[cond_1]['CODE_GENDER'].value_counts() /app_train[cond_1].shape[0])
 print('
연체가 아닌 경우
',app_train[cond_0]['CODE_GENDER'].value_counts() /app_train[cond_0].shape[0] )<count_values>
model.fit(X_train, y_train )
app_train['DAYS_EMPLOYED'].value_counts()<count_values>
print(f"Best score: {model.best_score_}")
 print("Best parameters from the RandomSearchCV:")
 best_parameters = model.best_estimator_.get_params()
for param_name in sorted(param_grid.keys()):
print(f"\t{param_name}: {best_parameters[param_name]}" )
app_train['DAYS_EMPLOYED'] = app_train['DAYS_EMPLOYED'].replace(365243, np.nan)
 app_train['DAYS_EMPLOYED'].value_counts(dropna=False )<count_values>
best_model = model.best_estimator_
app_train['CODE_GENDER'].value_counts()<concatenate>
y_pred_lgb = best_model.predict(X_test )
apps = pd.concat([app_train, app_test])
 print(apps.shape )<feature_engineering>
print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, y_pred_lgb))
print('Mean Squared Error:', metrics.mean_squared_error(y_test, y_pred_lgb))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, y_pred_lgb)) )
apps['APPS_EXT_SOURCE_MEAN'] = apps[['EXT_SOURCE_1', 'EXT_SOURCE_2', 'EXT_SOURCE_3']].mean(axis=1)
 apps['APPS_EXT_SOURCE_STD'] = apps[['EXT_SOURCE_1', 'EXT_SOURCE_2', 'EXT_SOURCE_3']].std(axis=1 )<feature_engineering>
preds_ensemble_avg =(y_pred_xgb + y_pred_lgb)/2
apps['APPS_EXT_SOURCE_STD'] = apps['APPS_EXT_SOURCE_STD'].fillna(apps['APPS_EXT_SOURCE_STD'].mean() )<feature_engineering>
print("Averaging Ensemble predictions KPI here:")
 print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, preds_ensemble_avg))
print('Mean Squared Error:', metrics.mean_squared_error(y_test, preds_ensemble_avg))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, preds_ensemble_avg)) )
apps['APPS_ANNUITY_CREDIT_RATIO'] = apps['AMT_ANNUITY']/apps['AMT_CREDIT']
apps['APPS_GOODS_CREDIT_RATIO'] = apps['AMT_GOODS_PRICE']/apps['AMT_CREDIT']
apps['APPS_CREDIT_GOODS_DIFF'] = apps['AMT_CREDIT'] - apps['AMT_GOODS_PRICE']<feature_engineering>
preds_ensemble_avg =(y_pred_xgb*0.1 + y_pred_lgb *0.9 )
apps['APPS_ANNUITY_INCOME_RATIO'] = apps['AMT_ANNUITY']/apps['AMT_INCOME_TOTAL']
apps['APPS_CREDIT_INCOME_RATIO'] = apps['AMT_CREDIT']/apps['AMT_INCOME_TOTAL']
apps['APPS_GOODS_INCOME_RATIO'] = apps['AMT_GOODS_PRICE']/apps['AMT_INCOME_TOTAL']
apps['APPS_CNT_FAM_INCOME_RATIO'] = apps['AMT_INCOME_TOTAL']/apps['CNT_FAM_MEMBERS'] 
<feature_engineering>
print("Weighted average Ensemble predictions KPI here:")
 print('Mean Absolute Error:', metrics.mean_absolute_error(y_test, preds_ensemble_avg))
print('Mean Squared Error:', metrics.mean_squared_error(y_test, preds_ensemble_avg))
print('Root Mean Squared Error:', np.sqrt(metrics.mean_squared_error(y_test, preds_ensemble_avg)) )
apps['APPS_EMPLOYED_BIRTH_RATIO'] = apps['DAYS_EMPLOYED']/apps['DAYS_BIRTH']
apps['APPS_INCOME_EMPLOYED_RATIO'] = apps['AMT_INCOME_TOTAL']/apps['DAYS_EMPLOYED']
apps['APPS_INCOME_BIRTH_RATIO'] = apps['AMT_INCOME_TOTAL']/apps['DAYS_BIRTH']
apps['APPS_CAR_BIRTH_RATIO'] = apps['OWN_CAR_AGE'] / apps['DAYS_BIRTH']
apps['APPS_CAR_EMPLOYED_RATIO'] = apps['OWN_CAR_AGE'] / apps['DAYS_EMPLOYED']<feature_engineering>
preds_xgb = XGB.predict(df_test.iloc[:,1:].values )
object_columns = apps.dtypes[apps.dtypes=='object'].index.tolist()
for column in object_columns:
apps[column] = pd.factorize(apps[column])[0]<split>
preds_lgb = best_model.predict(df_test.iloc[:,1:].values )
ftr_app = apps_train.drop(['SK_ID_CURR', 'TARGET'], axis=1)
 target_app = app_train['TARGET']
train_x, valid_x, train_y, valid_y = train_test_split(ftr_app, target_app, test_size=0.3, random_state=2020)
 train_x.shape, valid_x.shape 
<choose_model_class>
sub=pd.read_csv(".. /input/tabular-playground-series-jan-2021/sample_submission.csv" )
<train_model><EOS> 
sub.target =(preds_xgb*0.1+ preds_lgb*0.9)
 sub.to_csv("submission.csv", index=False )