Kaggle competition: Porto Seguro’s Safe Driver Prediction
<SOS> metric: NormalizedGini Kaggle data source: porto-seguro’s-safe-driver-prediction<normalization>
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split, StratifiedKFold, cross_val_score
from xgboost import XGBClassifier
from lightgbm import LGBMClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier, ExtraTreesClassifier, GradientBoostingClassifier, AdaBoostClassifier
xception_preprocess = preprocess_input
params = dict(model_name = Xception, preprocess_input = xception_preprocess, images = images, pooling = 'avg')
 xception_feature = run_with_timer(get_feature, params )<feature_engineering>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
resnet_preprocess = preprocess_input
params = dict(model_name = InceptionResNetV2, preprocess_input = resnet_preprocess, images = images, pooling = 'avg')
 resnet_feature = run_with_timer(get_feature, params )<concatenate>
id_test = test['id'].values
train_target = train['target'].values
train = train.drop(['target', 'id'], axis = 1)
 test = test.drop(['id'], axis = 1 )
final_features = np.concatenate([inception_feature, nasnet_features, xception_feature, resnet_feature], axis = 1)
 print('final features shape: {}'.format(final_features.shape))
del images, inception_feature, nasnet_features, xception_feature, resnet_feature<choose_model_class>
col_to_drop = train.columns[train.columns.str.startswith('ps_calc_')]
print(col_to_drop )
def create_model(features_shape = 1024):
model = Sequential()
model.add(InputLayer(input_shape =(features_shape,)))
 model.add(Dropout(0.6))
model.add(Dense(4096, activation = 'relu'))
model.add(Dropout(0.6))
model.add(Dense(len(class_to_index), activation = 'softmax'))
model.compile(loss = 'sparse_categorical_crossentropy', optimizer ='Adam', metrics = ['accuracy'])
 return model<train_model>
train = train.drop(col_to_drop, axis=1)
 test = test.drop(col_to_drop, axis=1 )
model.fit(final_features, labels, batch_size = 128, epochs = 6, validation_split = 0.2 )<create_dataframe>
train = train.replace(-1, np.nan)
 test = test.replace(-1, np.nan )
params = dict(dataframe = sample, image_shape = image_shape, img_dir = '/kaggle/input/dog-breed-identification/test', x_col = 'id')
 images = run_with_timer(load_from_dataframe, params )<feature_engineering>
cat_features = [a for a in train.columns if a.endswith('cat')]
print(cat_features )
inception_feature = run_with_timer(get_feature, dict(model_name = InceptionV3, preprocess_input = inception_preprocess, images = images, pooling = 'avg'))
nasnet_features = run_with_timer(get_feature, dict(model_name = NASNetLarge, preprocess_input = nasnet_preprocessor, images = images, pooling = 'avg'))
xception_feature = run_with_timer(get_feature, dict(model_name = Xception, preprocess_input = xception_preprocess, images = images, pooling = 'avg'))
resnet_feature = run_with_timer(get_feature, dict(model_name = InceptionResNetV2, preprocess_input = resnet_preprocess, images = images, pooling = 'avg'))<concatenate>
for column in cat_features:
temp = pd.get_dummies(pd.Series(train[column]))
train = pd.concat([train, temp], axis=1)
 train = train.drop([column], axis=1 )
final_features = np.concatenate([inception_feature, nasnet_features, xception_feature, resnet_feature], axis = 1)
 print('final features shape: {}'.format(final_features.shape))
del images, inception_feature, nasnet_features, xception_feature, resnet_feature<prepare_output>
for column in cat_features:
temp = pd.get_dummies(pd.Series(test[column]))
test = pd.concat([test, temp], axis = 1)
 test = test.drop([column], axis=1 )
prediction = model.predict(final_features)
 submission = pd.DataFrame({'id':sample.id.values})
 submission['id'] = submission['id'].apply(lambda x : x.split('.')[0])
 prediction = pd.DataFrame(prediction)
 prediction.columns = class_to_index.keys()<save_to_csv>
class Ensemble(object):
def __init__(self, n_splits, stacker, base_models):
self.n_splits = n_splits
self.stacker = stacker
self.base_models = base_models
def fit_predict(self, X, y, T):
increase = True
print(X.shape)
 if increase:
pos = pd.Series(y == 1)
 y = pd.Series(y)
 X = pd.concat([X, X.loc[pos]], axis = 0)
 y = pd.concat([y, y.loc[pos]], axis = 0)
 idx = np.arange(len(X))
np.random.shuffle(idx)
 X = X.iloc[idx]
y = y.iloc[idx]
print(X.shape)
 print(T.shape)
 X = np.array(X)
 y = np.array(y)
 T = np.array(T)
 folds = list(StratifiedKFold(n_splits=self.n_splits, shuffle = True, random_state=17 ).split(X, y))
S_train = np.zeros(( X.shape[0], len(self.base_models)))
 S_test = np.zeros(( T.shape[0], len(self.base_models)))
 for i, clf in enumerate(self.base_models):
S_test_i = np.zeros(( T.shape[0], self.n_splits))
for j,(train_idx, test_idx)in enumerate(folds):
X_train = X[train_idx]
y_train = y[train_idx]
X_holdout = X[test_idx]
print("fit %s fold %d " %(str(clf ).split('(')[0], j+1))
clf.fit(X_train, y_train)
 y_pred = clf.predict_proba(X_holdout)[:, 1]
S_train[test_idx, i] = y_pred
S_test_i[:, j] = clf.predict_proba(T)[:, 1]
S_test[:, i] = S_test_i.mean(axis=1)
 result = cross_val_score(self.stacker, S_train, y, cv=3)
 print("Stacker score : %.5f "%(result.mean()))
self.stacker.fit(S_train, y)
 res = self.stacker.predict_proba(S_test)[:, 1]
return res
submission = pd.concat([submission, prediction], axis = 1)
 submission.to_csv('submission.csv', index = False )<load_from_csv>
lgb_params = {
'learning_rate' : 0.02,
'n_estimators' : 650,
'max_bin' : 10,
'subsample' : 0.8,
'subsample_freq' : 10,
'colsample_bytree' : 0.8,
'min_child_samples' : 500,
'seed' : 99
}
lgb_params2 = {
'n_estimators' : 1090,
'learning_rate' : 0.02,
'colsample_bytree' : 0.3,
'subsample' : 0.7,
'subsample_freq' : 2,
'num_leaves' : 16,
'seed' : 99
}
lgb_params3 = {
'n_estimators' : 110,
'max_depth' : 4,
'learning_rate' : 0.02,
'seed' : 99
}
lgb_model = LGBMClassifier(**lgb_params)
 lgb_model2 = LGBMClassifier(**lgb_params2)
 lgb_model3 = LGBMClassifier(**lgb_params3)
 log_model = LogisticRegression()
train_df_X = pd.read_csv('.. /input/X_train.csv',sep=',')
 train_df_Y = pd.read_csv('.. /input/y_train.csv')
 test_df_X = pd.read_csv('.. /input/X_test.csv')
 train_df_X.shape, train_df_Y.shape, test_df_X.shape<count_missing_values>
stack = Ensemble(n_splits = 3, stacker = log_model, base_models =(lgb_model, lgb_model2))
train_df_X.isnull().sum()<count_values>
y_pred = stack.fit_predict(train, train_target, test )
train_df_Y.surface.value_counts()<count_missing_values>
sub = pd.DataFrame()
sub['id'] = id_test
sub['target'] = y_pred
train_df_X.isnull().sum()<compute_test_metric>
sub.to_csv('submission.csv', index=False )
<feature_engineering><EOS> 
sub.to_csv('submission.csv', index=False )