Kaggle competition: Forest Cover Type Prediction
for fold,(trn_idx, val_idx)in enumerate(skf.split(train, train['target'])) :
X_train, y_train = train.iloc[trn_idx][features], train.iloc[trn_idx]['target']
X_valid, y_valid = train.iloc[val_idx][features], train.iloc[val_idx]['target']
N = 3
p_valid,yp = 0,0
for i in range(N):
X_t, y_t = augment(X_train.values, y_train.values,2)
 X_t = pd.DataFrame(X_t)
 X_t.columns = features
trn_data = Pool(X_t, label=y_t)
 val_data = Pool(X_valid, label=y_valid)
 evals_result = {}
model = CatBoostClassifier(iterations=10000000,
learning_rate = 0.03,
od_type='Iter',
od_wait=3500,
loss_function="Logloss",
eval_metric='AUC',
bagging_temperature=0.7,
random_seed = 2019,
task_type='GPU' 
)
 model.fit(trn_data,eval_set=val_data,
use_best_model=True,
verbose=3000 
)
 p_valid += model.predict_proba(X_valid)[:,1]
yp += model.predict_proba(test[features])[:,1]
gc.collect()
gc.collect()
oof['target'][val_idx] = p_valid/N
val_score = roc_auc_score(y_valid, p_valid)
 print("fold {}|5".format(fold+1),"auc score : ",val_score)
 val_aucs.append(val_score)
 predictions['fold{}'.format(fold+1)] = yp/N<compute_test_metric>
train_data =pd.read_csv('/kaggle/input/forest-cover-type-prediction/train.csv')
 train_data.head()
print("oof auc")
 roc_auc_score(train['target'], oof['target'] )<compute_test_metric>
test_data = pd.read_csv('/kaggle/input/forest-cover-type-prediction/test.csv')
 test_data.head()
"auc mean:",np.mean(val_aucs )<save_to_csv>
train_data['Cover_Type'].value_counts()
predictions['target'] = np.mean(predictions[[col for col in predictions.columns if col not in ['ID_code', 'target']]].values, axis=1)
 predictions.to_csv('lgb_all_predictions.csv', index=None)
 sub_df = pd.DataFrame({"ID_code":test["ID_code"].values})
 sub_df["target"] = predictions['target']
sub_df.to_csv("lgb_submission.csv", index=False)
 oof_all=pd.concat([oof,sub_df])
 oof_all.to_csv('augment_cat_oof.csv', index=False )<set_options>
X = train_data.drop(labels =['Id','Cover_Type'],axis=1)
 Y = train_data['Cover_Type']
Y
warnings.filterwarnings('ignore')
 pd.set_option('display.max_columns', 200)
 pd.set_option('display.max_rows', 200 )<define_variables>
from sklearn.model_selection import train_test_split
VERSION = 6
SEED = 42<load_from_csv>
from sklearn.model_selection import train_test_split
test_df_src = pd.read_csv('.. /input/santander-customer-transaction-prediction/test.csv')
 train_df_src = pd.read_csv('.. /input/santander-customer-transaction-prediction/train.csv')
 with open('.. /input/data-separation/synthetic_samples_indexes.npy', 'rb')as f:
test_synth = np.load(f)
 test_synth = list(test_synth)
 with open('.. /input/data-separation/public_LB.npy', 'rb')as f:
test_pub = np.load(f)
 test_pub = list(test_pub.reshape(-1)[0])
 with open('.. /input/data-separation/private_LB.npy', 'rb')as f:
test_priv = np.load(f)
 test_priv = list(test_priv.reshape(-1)[0])
<define_variables>
x_train,x_valid,y_train,y_valid = train_test_split(X,Y,random_state = 21 )
var_cols = [c for c in list(train_df_src.columns)if 'var_' in c]<data_type_conversions>
from sklearn.ensemble import RandomForestClassifier
all_df_real = pd.concat([train_df_src, test_df_src.loc[test_pub + test_priv]],
axis=0, copy=False, sort=False ).set_index('ID_code' ).astype('float32')
 all_df_synth = pd.concat([train_df_src, test_df_src.loc[test_synth]],
axis=0, copy=False, sort=False ).set_index('ID_code' ).astype('float32' )<data_type_conversions>
rfc = RandomForestClassifier(n_estimators = 80)
 rfc.fit(x_train,y_train )
def get_all_freq(df, columns, frqs_series=None):
freq_df = pd.DataFrame(index=df.index)
 f_s = frqs_series if frqs_series else {}
for col in tqdm(columns):
if not frqs_series:
f_s[col] = df[f'{col}'].value_counts()
freq_df[f'{col}_freq_N'] = df[f'{col}'].map(f_s[col])
 freq_df[f'{col}_freq_1'] =(freq_df[f'{col}_freq_N'] > 1 ).astype('category')
 freq_df[f'{col}_mul_freq'] = df[col]*freq_df[f'{col}_freq_N']
freq_df[f'{col}_div_freq'] = df[col]/freq_df[f'{col}_freq_N']
return freq_df, f_s<concatenate>
rfc.score(x_valid,y_valid )
all_df_real_uflag, f_s = get_all_freq(all_df_real, var_cols)
 all_df_synth_uflag, f_s = get_all_freq(all_df_synth, var_cols, f_s)
 all_df_real = pd.concat([all_df_real, all_df_real_uflag],
axis=1, copy=False, sort=False)
 all_df_synth = pd.concat([all_df_synth, all_df_synth_uflag],
axis=1, copy=False, sort=False)
 train_df = all_df_real[all_df_real.index.str.contains('train')| all_df_real.index.str.contains('pred')].copy()
train_df = train_df.loc[train_df_src['ID_code']] 
<drop_column>
predict = rfc.predict(new_test )
del all_df_real_uflag, all_df_synth_uflag, train_df_src
gc.collect()<concatenate>
Submission = pd.DataFrame(data = predict,columns=['Cover_Type'])
 Submission.head()
test_df_real = all_df_real[all_df_real.index.str.contains('test')]
test_df_synth = all_df_synth[all_df_synth.index.str.contains('test')]
test_df = pd.concat([test_df_real, test_df_synth], axis=0, copy=True, sort=False)
 test_df = test_df.loc[test_df_src['ID_code']]<drop_column>
Submission['Id'] = test_data['Id']
Submission.set_index('Id',inplace = True )
del all_df_real, all_df_synth, test_df_src
gc.collect()<define_variables>
Submission.to_csv('Submission.csv' )