Kaggle competition: Porto Seguro’s Safe Driver Prediction
test_path = "/kaggle/input/dog-breed-identification/test/"
preds_df["id"] = [os.path.splitext(path)[0] for path in os.listdir(test_path)]
preds_df.head()<save_to_csv>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
preds_df.loc[:,list(classes)]= pred
preds_df.to_csv('submission.csv',index=None)
 preds_df.head()<define_variables>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
Image('.. /input/goldend/DBS_GoldRetriever_1280.jpg' )<train_model>
for col in train.columns:
train.loc[train[col] == -1, col] = np.nan
for col in test.columns:
test.loc[test[col] == -1, col] = np.nan
img_g = load_img('.. /input/goldend/DBS_GoldRetriever_1280.jpg',target_size = img_size)
 img_g = np.expand_dims(img_g, axis=0)
<predict_on_test>
seed = 42
param = {'num_leaves': 31,
'boosting_type': 'gbdt',
'learning_rate': 0.01,
'max_bin': 255,
'min_data_in_leaf': 1500,
'feature_fraction': 0.7,
'bagging_freq': 1,
'bagging_fraction': 0.7,
'lambda_l1': 1,
'lambda_l2': 1,
'save_binary': True,
'seed': seed,
'feature_fraction_seed': seed,
'bagging_seed': seed,
'drop_seed': seed,
'data_random_seed': seed,
'objective': 'binary',
'verbose': 1,
'metric': 'binary_logloss',
'is_unbalance': False,
'boost_from_average': True
}
test_features = extact_features(img_g)
 predg = model.predict(test_features)
 print(f"Predicted label: {classes[np.argmax(predg[0])]}")
 print(f"Probability of prediction): {round(np.max(predg[0])) * 100} %" )<import_modules>
cat_names = ['ps_ind_02_cat', 'ps_ind_04_cat', 'ps_ind_05_cat', 'ps_car_01_cat',
'ps_car_02_cat', 'ps_car_03_cat', 'ps_car_04_cat', 'ps_car_05_cat',
'ps_car_06_cat', 'ps_car_07_cat', 'ps_car_08_cat', 'ps_car_09_cat',
'ps_car_10_cat', 'ps_car_11_cat']
train = pd.get_dummies(train, columns = cat_names)
 test = pd.get_dummies(test, columns = cat_names )
import numpy as np
import pandas as pd
from keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array
from keras.utils import to_categorical
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import random
import time
import os<load_from_csv>
calc_names = ['ps_calc_01', 'ps_calc_02', 'ps_calc_03', 'ps_calc_04',
'ps_calc_05', 'ps_calc_06', 'ps_calc_07', 'ps_calc_08',
'ps_calc_09', 'ps_calc_10', 'ps_calc_11', 'ps_calc_12',
'ps_calc_13', 'ps_calc_14', 'ps_calc_15_bin','ps_calc_16_bin',
'ps_calc_17_bin','ps_calc_18_bin','ps_calc_19_bin',
'ps_calc_20_bin']
train.drop(columns = calc_names, inplace=True)
labels_dataframe = pd.read_csv('/kaggle/input/dog-breed-identification/labels.csv')
 sample = pd.read_csv('/kaggle/input/dog-breed-identification/sample_submission.csv' )<create_dataframe>
predictors = list(train.columns)
 predictors.remove('id')
 predictors.remove('target' )
ix = np.random.permutation(len(labels_dataframe))
labels_dataframe = labels_dataframe.iloc[ix]<create_dataframe>
nfold = 5
target = 'target'
skf = KFold(n_splits=nfold, shuffle=True, random_state=2019)
 oof = np.zeros(len(train))
predictions = np.zeros(len(test))
def load_from_dataframe(dataframe, image_shape, img_dir, x_col = None, y_col = None,):
no_of_images = len(dataframe)
 images = np.zeros(( no_of_images, image_shape[0], image_shape[1], image_shape[2]), dtype = np.uint8)
 if y_col:
labels = np.zeros(( no_of_images, 1), dtype = np.uint8)
 for ix in range(no_of_images):
filename = dataframe.loc[ix, x_col]
path = os.path.join(img_dir, filename + '.jpg')
 image = load_img(path, target_size =(image_shape[0], image_shape[1]))
image = img_to_array(image)
 images[ix] = image
labels[ix] = dataframe.loc[ix, y_col]
print('Found {} validated image filenames belonging to {} classes.'.format(no_of_images, np.unique(labels ).size))
return images, labels
else:
for ix in range(no_of_images):
filename = dataframe.loc[ix, x_col]
path = os.path.join(img_dir, filename + '.jpg')
 image = load_img(path, target_size =(image_shape[0], image_shape[1]))
image = img_to_array(image)
 images[ix] = image
print('Found {} validated image filenames'.format(no_of_images))
return images<prepare_x_and_y>
i = 1
for train_index, valid_index in skf.split(train, train.target.values):
print("
fold {}".format(i))
t=train.iloc[train_index]
xg_train = lgb.Dataset(t[predictors].values,
label=t[target].values,
feature_name=predictors,
free_raw_data = False 
)
 xg_valid = lgb.Dataset(train.iloc[valid_index][predictors].values,
label=train.iloc[valid_index][target].values,
feature_name=predictors,
free_raw_data = False 
)
 num_rounds = 1400
clf = lgb.train(param, xg_train, num_rounds, valid_sets = [xg_train, xg_valid],
verbose_eval=2000, early_stopping_rounds = 1000)
 oof[valid_index] = clf.predict(train.iloc[valid_index][predictors].values, num_iteration=clf.best_iteration)
 predictions += clf.predict(test[predictors], num_iteration=clf.best_iteration)/ nfold
i = i + 1
params = dict(dataframe = labels_dataframe, image_shape = image_shape, img_dir = '/kaggle/input/dog-breed-identification/train', x_col = 'id', y_col = 'breed')
 images, labels = run_with_timer(load_from_dataframe, params )<choose_model_class>
print("
CV AUC: {:<0.5f}".format(metrics.roc_auc_score(train.target.values, oof)))
 print("
CV log loss: {:<0.5f}".format(metrics.log_loss(train.target.values, oof)))
 print("
CV Gini: {:<0.5f}".format(2 * metrics.roc_auc_score(train.target.values, oof)-1))
def get_feature(model_name, preprocess_input, images, pooling = 'avg', target_size =(331,331,3)) :
base_model = model_name(input_shape = target_size, include_top=False, pooling = pooling)
 model = Sequential()
model.add(InputLayer(input_shape = target_size))
model.add(Lambda(preprocess_input))
model.add(base_model)
 feature = model.predict(images)
 print('feature-map shape: {}'.format(feature.shape))
return feature<feature_engineering>
sub_df = pd.read_csv('.. /input/sample_submission.csv')
 sub_df["target"] = predictions
sub_df[:10]
<feature_engineering><EOS> 
sub_df.to_csv("lightgbm.csv", index=False )