Kaggle competition: Tabular Playground Series - Feb 2021
<SOS> metric: RMSE Kaggle data source: tabular-playground-series-feb-2021<import_modules>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import optuna
from lightgbm import LGBMRegressor
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split,KFold
from sklearn.preprocessing import LabelEncoder
from fastai.utils import *
from fastai.vision import *
from fastai.callbacks import *
from pathlib import Path
import PIL
from torch.utils import model_zoo<load_from_csv>
train=pd.read_csv('.. /input/tabular-playground-series-feb-2021/train.csv')
 test=pd.read_csv('.. /input/tabular-playground-series-feb-2021/test.csv' )
PATH = ".. /input/aerial-cactus-identification/train/"
TMP_PATH = "/tmp/tmp"
MODEL_PATH = "/tmp/model/"
sz=224
train_df = pd.read_csv('.. /input/aerial-cactus-identification/train.csv')
 test_df = pd.read_csv('.. /input/aerial-cactus-identification/sample_submission.csv' )<set_options>
cat_var=[f'cat{i}' for i in range(10)]
cont_var=[f'cont{i}' for i in range(14)]
columns=[ col for col in train.columns.tolist() if col not in ['id','target']]
for cat in cat_var:
le = LabelEncoder()
train[cat]=le.fit_transform(train[cat])
 test[cat]=le.transform(test[cat] )
torch.cuda.is_available()<set_options>
X=train[columns]
y=train.target
torch.backends.cudnn.enabled<count_values>
lgb_params={'random_state': 2021,
'metric': 'rmse',
'n_estimators': 30000,
'n_jobs': -1,
'cat_feature': [x for x in range(len(cat_var)) ],
'bagging_seed': 2021,
'feature_fraction_seed': 2021,
'learning_rate': 0.003899156646724397,
'max_depth': 99,
'num_leaves': 63,
'reg_alpha': 9.562925363678952,
'reg_lambda': 9.355810045480153,
'colsample_bytree': 0.2256038826485174,
'min_child_samples': 290,
'subsample_freq': 1,
'subsample': 0.8805303688019942,
'max_bin': 882,
'min_data_per_group': 127,
'cat_smooth': 96,
'cat_l2': 19
}
target_count = train_df.has_cactus.value_counts()
print('Class 0:', target_count[0])
 print('Class 1:', target_count[1])
 print('Proportion:', round(target_count[0] / target_count[1], 2), ': 1')
<count_values>
f1= 0.6547870667136243
f2= 2.6711351556035487
f3= 20
f4= 49
f5= 2
count_class_0, count_class_1 = train_df.has_cactus.value_counts()
df_class_0 = train_df[train_df['has_cactus'] == 0]
df_class_1 = train_df[train_df['has_cactus'] == 1]<prepare_x_and_y>
%%time
kf=KFold(n_splits=5,random_state=48,shuffle=True)
 preds = np.zeros(test.shape[0])
 rmse=[]
i=0
for idx_train,idx_test in kf.split(X,y):
X_train,X_test=X.iloc[idx_train],X.iloc[idx_test]
y_train,y_test=y.iloc[idx_train],y.iloc[idx_test]
model=LGBMRegressor(**lgb_params)
 model.fit(X_train,y_train,eval_set=(X_test,y_test),early_stopping_rounds=300,verbose=False,eval_metric='rmse')
 predictions=model.predict(X_test,num_iteration=model.best_iteration_)
 rmse.append(mean_squared_error(y_test,predictions,squared=False))
print('First Round:')
 print(f'RMSE {rmse[i]}')
 rmse_tuned=[]
params = lgb_params.copy()
for t in range(1,17):
if t >2:
params['reg_lambda'] *= f1
params['reg_alpha'] += f2
params['num_leaves'] += f3
params['min_child_samples'] -= f4
params['cat_smooth'] -= f5
params['learning_rate']=0.003
if params['min_child_samples']<1:
params['min_child_samples']=1
if t>11:
params['learning_rate']=0.001
model=LGBMRegressor(**params ).fit(X_train,y_train,eval_set=(X_test,y_test),eval_metric='rmse',early_stopping_rounds=200,verbose=False,init_model=model)
 predictions=model.predict(X_test, num_iteration= model.best_iteration_)
 rmse_tuned.append(mean_squared_error(y_test,predictions,squared=False))
print(f'RMSE tuned {t}: {rmse_tuned[t-1]}')
 print(f'Improvement of {rmse[i]-rmse_tuned[t-1]}')
 preds+=model.predict(test[columns],num_iteration=model.best_iteration_)/kf.n_splits
i+=1
<define_variables><EOS> 
test['target']=preds
test=test[['id','target']]
test.to_csv('submission.csv',index=False )