Kaggle competition: Tabular Playground Series - Feb 2021
%%time
idx = features = train_df.columns.values[2:202]
for df in [test_df, train_df]:
df['sum'] = df[idx].sum(axis=1)
 df['min'] = df[idx].min(axis=1)
 df['max'] = df[idx].max(axis=1)
 df['mean'] = df[idx].mean(axis=1)
 df['std'] = df[idx].std(axis=1)
 df['skew'] = df[idx].skew(axis=1)
 df['kurt'] = df[idx].kurtosis(axis=1)
 df['med'] = df[idx].median(axis=1 )<train_model>
warnings.filterwarnings("ignore" )
print('Train and test columns: {} {}'.format(len(train_df.columns), len(test_df.columns)) )<define_variables>
train_data = pd.read_csv('/kaggle/input/tabular-playground-series-feb-2021/train.csv')
 test_data = pd.read_csv('/kaggle/input/tabular-playground-series-feb-2021/test.csv')
 print(train_data.head() , "
")
 print(test_data.head() )
features = [c for c in train_df.columns if c not in ['ID_code', 'target']]
target = train_df['target']<init_hyperparams>
import matplotlib.pyplot as plt
import matplotlib as mpl
import seaborn as sns
param = {
'bagging_freq': 5,
'bagging_fraction': 0.4,
'boost_from_average':'false',
'boost': 'gbdt',
'feature_fraction': 0.05,
'learning_rate': 0.01,
'max_depth': -1,
'metric':'auc',
'min_data_in_leaf': 80,
'min_sum_hessian_in_leaf': 10.0,
'num_leaves': 13,
'num_threads': 8,
'tree_learner': 'serial',
'objective': 'binary',
'verbosity': 1
}<train_model>
cat_features = ['cat0', 'cat1', 'cat2', 'cat3', 'cat4', 'cat5', 'cat6', 'cat7', 'cat8', 'cat9']
numerical_features = ['cont0', 'cont1', 'cont2', 'cont3', 'cont4', 'cont5','cont6', 'cont7', 'cont8', 'cont9', 'cont10', 'cont11', 'cont12', 'cont13']
folds = StratifiedKFold(n_splits=10, shuffle=False, random_state=44000)
 oof = np.zeros(len(train_df))
predictions = np.zeros(len(test_df))
feature_importance_df = pd.DataFrame()
for fold_,(trn_idx, val_idx)in enumerate(folds.split(train_df.values, target.values)) :
print("Fold {}".format(fold_))
trn_data = lgb.Dataset(train_df.iloc[trn_idx][features], label=target.iloc[trn_idx])
 val_data = lgb.Dataset(train_df.iloc[val_idx][features], label=target.iloc[val_idx])
 num_round = 1000000
clf = lgb.train(param, trn_data, num_round, valid_sets = [trn_data, val_data], verbose_eval=1000, early_stopping_rounds = 3000)
 oof[val_idx] = clf.predict(train_df.iloc[val_idx][features], num_iteration=clf.best_iteration)
 fold_importance_df = pd.DataFrame()
fold_importance_df["Feature"] = features
fold_importance_df["importance"] = clf.feature_importance()
fold_importance_df["fold"] = fold_ + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 predictions += clf.predict(test_df[features], num_iteration=clf.best_iteration)/ folds.n_splits
print("CV score: {:<8.5f}".format(roc_auc_score(target, oof)) )<save_to_csv>
outlier = train_data.loc[train_data.target < 1.0]
print(outlier, "
")
 print(outlier.index )
sub_df = pd.DataFrame({"ID_code":test_df["ID_code"].values})
 sub_df["target"] = predictions
sub_df.to_csv("submission.csv", index=False )<load_from_csv>
train_data.drop([99682], inplace = True )
random_state = 42
np.random.seed(random_state)
 df_train = pd.read_csv('.. /input/train.csv')
 df_test = pd.read_csv('.. /input/test.csv' )<groupby>
categorical_features = cat_features
y_train = train_data["target"]
train_data.drop(columns = ['target'], inplace = True)
 test_data_backup = test_data.copy()
train_data.drop(columns = ["id"], inplace = True)
 test_data.drop(columns = ["id"], inplace = True )
print("Total values in the dataset : {}".format(df_train['target'].count()))
Ones = df_train.groupby('target')['target'].count()
print("% of 1s in total {}".format(Ones[1]*100.0/200000))<prepare_x_and_y>
model_ctb = CatBoostRegressor(iterations = 3000,
learning_rate = 0.02,
od_type = 'Iter',
loss_function = 'RMSE',
grow_policy = 'SymmetricTree',
subsample = 0.8,
verbose = 3,
random_seed = 17)
 model_ctb.fit(train_data, y_train, cat_features=categorical_features)
 y_pred = model_ctb.predict(test_data)
 print(y_pred )
<categorify><EOS> 
solution = pd.DataFrame({"id":test_data_backup.id, "target":y_pred})
 solution.to_csv("solution.csv", index = False)
 print("saved successful!" )