Kaggle competition: Tabular Playground Series - Jan 2021
<SOS> metric: RMSE Kaggle data source: tabular-playground-series-jan-2021<train_model>
%matplotlib inline
%time _ = gs.fit(X_train, y_train )<find_best_params>
!pip install optuna
gs.best_params_, gs.best_score_<train_model>
train = pd.read_csv(DATA / "train.csv")
 test = pd.read_csv(DATA / "test.csv")
 smpl_sub = pd.read_csv(DATA / "sample_submission.csv")
 print("train: {}, test: {}, sample sub: {}".format(
 train.shape, test.shape, smpl_sub.shape 
))
gbr1 = GradientBoostingRegressor(max_depth=5,n_estimators=94)
 gbr1.fit(X_train, y_train )<predict_on_test>
features = ['cont1', 'cont2', 'cont3', 'cont4', 'cont5', 'cont6', 'cont7',
'cont8', 'cont9', 'cont10', 'cont11', 'cont12', 'cont13', 'cont14']
datax = pd.concat([train[features],test[features]], axis=0)
 datax1 = datax.abs()
def featx(data, features):
cx=int(1000000)
 for f in features:
data[f] = data[f]*cx
dmm = data[f].max() -data[f].min()
data[f] =(data[f]-data[f].min())/dmm

 return data
data = featx(datax, features)
 data.head()
y_pred = gbr1.predict(X_test)
 for i in range(len(y_pred)) :
if y_pred[i] > 3:
y_pred[i] = 3<compute_train_metric>
X = data[:300000]
Xtest = data[300000:]
y = train['target']
def caculator_RMSE(X_train, y_train, model):
return np.sqrt(((y_train - model.predict(X_train)) ** 2 ).mean() )<compute_test_metric>
print('RMSE using GBRegression : ' , caculator_RMSE(X_train, y_train, gbr1))<save_to_csv>
params={'objective': 'regression',
'metric': 'rmse',
'verbosity': -1,
'boosting_type': 'gbdt',
'learning_rate': 0.04,
'feature_pre_filter': False,
'min_data_in_leaf': 100,
'lambda_l1': 9.486545871986378,
'lambda_l2': 6.887206686817597,
'num_leaves': 230,
'feature_fraction': 0.4,
'bagging_fraction': 1.0,
'bagging_freq': 0,
'min_child_samples': 20}
pd.DataFrame({"id": id_test, "relevance": y_pred} ).to_csv('submission.csv',index=False )<train_on_grid>
N_FOLDS = 12
oof = np.zeros(len(y))
preds = np.zeros(len(Xtest))
params['learning_rate'] = 0.004
params['num_iterations'] = 12000
i=0
for train_ind, test_ind in tqdm(KFold(n_splits = N_FOLDS,shuffle=True,random_state=21 ).split(X),
total=N_FOLDS, desc="k-fold"):
Xtrain = X.iloc[train_ind]
Xval = X.iloc[test_ind]
ytrain = y.iloc[train_ind]
yval = y.iloc[test_ind]
model = LGBMRegressor(**params)
 model.fit(Xtrain, ytrain,
eval_set =(( Xval,yval)) ,
early_stopping_rounds = 120,
verbose = 1000)
 p = model.predict(Xval)
 oof[test_ind] = p
preds += model.predict(Xtest)/N_FOLDS
i+=1
if i>5:
print(f'MSE::: {np.round(mean_squared_error(y, oof, squared=False),8)}')
 else:
pass
<save_to_csv>
submission = test[['id']]
submission['target'] = preds
submission.to_csv('submission.csv', index = False )