Kaggle competition: Porto Seguro’s Safe Driver Prediction
<SOS> metric: NormalizedGini Kaggle data source: porto-seguro’s-safe-driver-prediction<categorify>
MAX_ROUNDS = 650
OPTIMIZE_ROUNDS = False
LEARNING_RATE = 0.05
submission1.surface=le.inverse_transform(submission1.surface )<save_to_csv>
import numpy as np
import pandas as pd
from catboost import CatBoostClassifier
from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold
from numba import jit
submission1.to_csv('submissionlgb.csv', index=False )<load_from_csv>
@jit
def eval_gini(y_true, y_prob):
y_true = np.asarray(y_true)
 y_true = y_true[np.argsort(y_prob)]
ntrue = 0
gini = 0
delta = 0
n = len(y_true)
 for i in range(n-1, -1, -1):
y_i = y_true[i]
ntrue += y_i
gini += y_i * delta
delta += 1 - y_i
gini = 1 - 2 * gini /(ntrue *(n - ntrue))
return gini
%%time
train_df = pd.read_csv('.. /input/X_train.csv')
 test_df = pd.read_csv('.. /input/X_test.csv')
 target_df = pd.read_csv('.. /input/y_train.csv' )<count_values>
train_df = pd.read_csv('.. /input/train.csv', na_values="-1")
 test_df = pd.read_csv('.. /input/test.csv', na_values="-1" )
len(train_df.measurement_number.value_counts() )<count_values>
id_test = test_df['id'].values
id_train = train_df['id'].values
train_df = train_df.fillna(999)
 test_df = test_df.fillna(999)
 col_to_drop = train_df.columns[train_df.columns.str.startswith('ps_calc_')]
train_df = train_df.drop(col_to_drop, axis=1)
 test_df = test_df.drop(col_to_drop, axis=1)
 for c in train_df.select_dtypes(include=['float64'] ).columns:
train_df[c]=train_df[c].astype(np.float32)
 test_df[c]=test_df[c].astype(np.float32)
 for c in train_df.select_dtypes(include=['int64'] ).columns[2:]:
train_df[c]=train_df[c].astype(np.int8)
 test_df[c]=test_df[c].astype(np.int8)
 y = train_df['target']
X = train_df.drop(['target', 'id'], axis=1)
 y_valid_pred = 0*y
X_test = test_df.drop(['id'], axis=1)
 y_test_pred = 0
target_df['surface'].value_counts().reset_index().rename(columns={'index': 'target'} )<count_missing_values>
K = 5
kf = KFold(n_splits = K, random_state = 1, shuffle = True )
print('Are there NaNs in {}?: {}
'.format('train_df',train_df.isnull().values.any())+
'Are there NaNs in {}?: {}
'.format('test_df',test_df.isnull().values.any())+
'Are there NaNs in {}?: {}
'.format('target_df',target_df.isnull().values.any()))<categorify>
model = CatBoostClassifier(
 learning_rate=LEARNING_RATE,
depth=6,
l2_leaf_reg = 14,
iterations = MAX_ROUNDS,
loss_function='Logloss'
)
le = LabelEncoder()
le.fit(target_df['surface'])
 target_df['surface'] = le.transform(target_df['surface'] )<count_values>
for i,(train_index, test_index)in enumerate(kf.split(train_df)) :
y_train, y_valid = y.iloc[train_index], y.iloc[test_index]
X_train, X_valid = X.iloc[train_index,:], X.iloc[test_index,:]
print("
Fold ", i)
 if OPTIMIZE_ROUNDS:
fit_model = model.fit(X_train, y_train,
eval_set=[X_valid, y_valid],
use_best_model=True 
)
 print(" N trees = ", model.tree_count_)
 else:
fit_model = model.fit(X_train, y_train)
 pred = fit_model.predict_proba(X_valid)[:,1]
print(" Gini = ", eval_gini(y_valid, pred))
y_valid_pred.iloc[test_index] = pred
y_test_pred += fit_model.predict_proba(X_test)[:,1]
y_test_pred /= K
print("
Gini for full training set:")
 eval_gini(y, y_valid_pred )
target_df['surface'].value_counts()<create_dataframe>
val = pd.DataFrame()
val['id'] = id_train
val['target'] = y_valid_pred.values
val.to_csv('cat_valid.csv', float_format='%.6f', index=False )
<feature_engineering><EOS> 
sub = pd.DataFrame()
sub['id'] = id_test
sub['target'] = y_test_pred
sub.to_csv('cat_submit.csv', float_format='%.6f', index=False )