Kaggle competition: Forest Cover Type Prediction
features = [c for c in train_df.columns if c not in ['ID_code', 'target']]
target = train_df['target']<data_type_conversions>
train_data=pd.read_csv('/kaggle/input/forest-cover-type-prediction/train.csv')
 train_data
def add_freq(df):
df_res = pd.DataFrame(index=df.index)
 for c in tqdm(var_cols):
df_res[f'{c}_freq_mean'] = df.groupby(f'{c}_freq_1')[c].transform(np.mean ).astype('float16')
 df_res[f'{c}_freq_std'] = df.groupby(f'{c}_freq_1')[c].transform(np.std ).astype('float16')
 df_res[f'{c}_freq_var1'] =(df_res[f'{c}_freq_mean'] - 2*df_res[f'{c}_freq_std'] ).astype('float16')
 df_res[f'{c}_freq_var2'] =(df_res[f'{c}_freq_mean'] + 2*df_res[f'{c}_freq_std'] ).astype('float16')
 df_res[f'{c}_freq_mean_unq'] = df[c]
idx = df[df[f'{c}_freq_1']==False].index
df_res.loc[idx, [f'{c}_freq_mean_unq']] = df_res.loc[idx][f'{c}_freq_mean_unq'].mean()
return df_res
def add_features(df):
df_count = add_freq(df)
 colums = [f'{x}_freq_N' for x in var_cols] + \
[f'{x}_freq_1' for x in var_cols] + \
[f'{x}_mul_freq' for x in var_cols] + \
[f'{x}_div_freq' for x in var_cols] + \
['target']
res = pd.concat([df[colums], df_count], axis=1, copy=False, sort=False)
 return res<init_hyperparams>
test_data=pd.read_csv('/kaggle/input/forest-cover-type-prediction/test.csv')
 test_data.head()
param = {
'random_seed': SEED,
'gpu_ram_part': 0.95,
'iterations': 200000,
'learning_rate': 0.04,
'l2_leaf_reg': 5,
'depth': 1,
'thread_count': 4,
'custom_metric': ['Logloss', 'AUC:hints=skip_train~false'],
'od_type': 'Iter',
'od_wait': 500,
'task_type': 'GPU',
'eval_metric': 'AUC',
'use_best_model': True
} 
<choose_model_class>
train_data['Cover_Type'].value_counts()
%%time
num_folds = 5
features = [c for c in train_df.columns if c not in ['ID_code', 'target']]
print('Training the Model:')
 val_list = []
predictions = []
for i in range(4):
param['depth'] = i%2 + 1
clf = ctb.CatBoostClassifier(**param)
 folds = StratifiedKFold(n_splits=num_folds, shuffle=True, random_state=SEED*i)
 oof = np.zeros(len(train_df))
for fold_,(trn_idx, val_idx)in enumerate(folds.split(train_df.values, target.values)) :
gc.collect()
print("Fold idx:{}".format(fold_ + 1))
trn_data = add_features(train_df.iloc[trn_idx])
 y_trn = trn_data['target']
features_ext = [c for c in trn_data.columns if c not in ['ID_code', 'target']]
trn_data = trn_data[features_ext]
val_data = add_features(train_df.iloc[val_idx])
 y_val = val_data['target']
val_data = val_data[features_ext]
clf.fit(X=trn_data, y=y_trn, eval_set=[(val_data, y_val)], verbose=1000, early_stopping_rounds = 1001)
 del trn_data, val_data
gc.collect()
train_data = add_features(train_df.iloc[val_idx])[features_ext]
oof[val_idx] = clf.predict_proba(train_data)[:,1]
del train_data
gc.collect()
test_data = add_features(test_df)[features_ext]
predictions.append(clf.predict_proba(test_data)[:,1])
 del test_data
del clf
val_list.append(oof)
 oof = gmean(val_list, 0)
 predictions_gmean = gmean(predictions, 0 )<save_to_csv>
X=train_data.drop(labels=['Id','Cover_Type'],axis=1)
 y=train_data['Cover_Type']
X_train,X_value,y_train,y_value=train_test_split(X,y,random_state=40)
 print(X_train.shape,y_train.shape)
 print(X_value.shape,y_value.shape)
sub = pd.DataFrame({"ID_code": test_df.index.values})
 sub["target"] = predictions_gmean
sub.to_csv('submission_cb_{}_seed_v{}.csv'.format(SEED, VERSION), index=False )<compute_test_metric>
RFC=RandomForestClassifier(n_estimators=70)
 RFC.fit(X_train,y_train)
roc = roc_auc_score(target, oof)
 print("CV score: {:<8.5f}".format(roc))<save_to_csv>
RFC.score(X_value,y_value)
oof_train = pd.DataFrame()
oof_train[f'oof_cb_{num_folds}_{SEED}_{VERSION}'] = oof
oof_test = pd.DataFrame()
oof_test[f'oof_cb_{num_folds}_{SEED}_{VERSION}'] = predictions_gmean
oof_train.to_csv('train_cb_roc_{}_seed_{}_v{}.csv'.format(
 roc, SEED, VERSION), index=False)
 oof_test.to_csv('test_cb_roc_{}_seed_{}_v{}.csv'.format(
 roc, SEED, VERSION), index=False )<install_modules>
predict=RFC.predict(test_data.drop(labels=['Id'],axis=1))
Submission=pd.DataFrame(data=predict,columns=['Cover_Type'])
 Submission.head()
! pip install gpyopt<import_modules>
Submission['Id']=test_data['Id']
Submission.set_index('Id',inplace=True)
 Submission.head()
<load_from_csv><EOS> 
Submission.to_csv('Submission.csv')