Kaggle competition: Santander Customer Transaction Prediction
<SOS> metric: AUC Kaggle data source: santander-customer-transaction-prediction<import_modules>
import os
import numpy as np
import keras
import sklearn
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import confusion_matrix, precision_score, recall_score, accuracy_score
import random<load_from_csv>
data = pd.read_csv('.. /input/train.csv' )
train_data = pd.read_csv('.. /input/train.csv' )<count_values>
train_data = data.iloc[:,2:203]
train_data.has_cactus.value_counts()<define_variables>
y = data.iloc[:,1]
positive_examples = train_data[train_data.has_cactus==1]
negative_examples = train_data[train_data.has_cactus==0]<define_variables>
X_train, X_test, y_train, y_test = train_test_split(train_data, y, test_size=0.2, random_state=142 )
def augment_img(img):
batch_input = []
batch_input += [img]
batch_input += [img[::-1, :, :]]
batch_input += [img[:, ::-1, :]]
batch_input += [np.rot90(img)]
temp_img = np.zeros_like(img)
 temp_img[:28, :, :] = img[4:, :, :]
batch_input += [temp_img]
temp_img = np.zeros_like(img)
 temp_img[:, :28, :] = img[:, 4:, :]
batch_input += [temp_img]
temp_img = np.zeros_like(img)
 temp_img[4:, :, :] = img[:28, :, :]
batch_input += [temp_img]
temp_img = np.zeros_like(img)
 temp_img[:, 4:, :] = img[:, :28, :]
batch_input += [temp_img]
batch_input += [cv2.resize(img[2:30, 2:30, :],(32, 32)) ]
batch_input += [scipy.ndimage.interpolation.rotate(img, 10, reshape=False)]
batch_input += [scipy.ndimage.interpolation.rotate(img, 5, reshape=False)]
return batch_input<define_search_model>
from xgboost import XGBClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC, LinearSVC
from sklearn.ensemble import RandomForestClassifier,GradientBoostingClassifier, VotingClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
def image_generator(batch_size = 8, all_data=True, shuffle=True, train=True, indexes=None):
while True:
if indexes is None:
if train:
indexes = positive_examples.index.tolist()
neg_indexes = negative_examples.index.tolist()
if shuffle:
np.random.shuffle(indexes)
 np.random.shuffle(neg_indexes)
 N = int(len(indexes)/(batch_size/2))
neg_N = int(len(neg_indexes)/(batch_size/2))
j = 0
for i in range(N):
current_indexes = indexes[i*(batch_size//2):(i+1)*(batch_size//2)]
current_neg_indexes = neg_indexes[j*(batch_size//2):(j+1)*(batch_size//2)]
j =(j + 1)% neg_N
batch_input = []
batch_output = []
for ind in range(len(current_indexes)) :
index = current_indexes[ind]
neg_index = current_neg_indexes[ind]
img = mpimg.imread('.. /input/train/train/' + train_data.id[index])
 batch_input.extend(augment_img(img))
for _ in range(11):
batch_output += [train_data.has_cactus[index]]
neg_img = mpimg.imread('.. /input/train/train/' + train_data.id[neg_index])
 batch_input.extend(augment_img(neg_img))
for _ in range(11):
batch_output += [train_data.has_cactus[neg_index]]
batch_input = np.array(batch_input)
 batch_output = np.array(batch_output)
 yield(batch_input, batch_output.reshape(-1, 1))<choose_model_class>
gnb = GaussianNB()
gnb.fit(X_train, y_train )
model = keras.models.Sequential()
model.add(keras.layers.Conv2D(64,(5, 5), input_shape=(32, 32, 3)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Conv2D(64,(5, 5)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Conv2D(128,(5, 5)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Conv2D(128,(5, 5)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Conv2D(256,(3, 3)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Conv2D(256,(3, 3)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Conv2D(512,(3, 3)))
 model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Flatten())
 model.add(keras.layers.Dense(100))
model.add(keras.layers.BatchNormalization())
 model.add(keras.layers.LeakyReLU(alpha=0.3))
model.add(keras.layers.Dense(1, activation='sigmoid'))<choose_model_class>
y_preds_gnb = gnb.predict(X_test)
 print(accuracy_score(y_test, y_preds_gnb))
opt = keras.optimizers.SGD(lr=0.0001, momentum=0.9, nesterov=True)
 model.compile(optimizer=opt, loss='binary_crossentropy', metrics=['accuracy'] )<load_pretrained>
test_data = pd.read_csv('.. /input/test.csv' )
indexes = np.arange(train_data.shape[0])
 N = int(len(indexes)/ 64)
 batch_size = 64
wrong_ind = []
for i in range(N):
current_indexes = indexes[i*64:(i+1)*64]
batch_input = []
batch_output = []
for index in current_indexes:
img = mpimg.imread('.. /input/train/train/' + train_data.id[index])
 batch_input += [img]
batch_output.append(train_data.has_cactus[index])
 batch_input = np.array(batch_input)
 model_pred = model.predict_classes(batch_input)
 for j in range(len(batch_output)) :
if model_pred[j] != batch_output[j]:
wrong_ind.append(i*batch_size+j )<load_pretrained>
X_test_data = test_data.iloc[:,1:202]
indexes = np.arange(train_data.shape[0])
 N = int(len(indexes)/ 64)
 batch_size = 64
wrong_ind = []
for i in range(N):
current_indexes = indexes[i*64:(i+1)*64]
batch_input = []
batch_output = []
for index in current_indexes:
img = mpimg.imread('.. /input/train/train/' + train_data.id[index])
 batch_input += [img[::-1, :, :]]
batch_output.append(train_data.has_cactus[index])
 batch_input = np.array(batch_input)
 model_pred = model.predict_classes(batch_input)
 for j in range(len(batch_output)) :
if model_pred[j] != batch_output[j]:
wrong_ind.append(i*batch_size+j )<load_pretrained>
y_preds_test_data_gnb = gnb.predict(X_test_data )
indexes = np.arange(train_data.shape[0])
 N = int(len(indexes)/ 64)
 batch_size = 64
wrong_ind = []
for i in range(N):
current_indexes = indexes[i*64:(i+1)*64]
batch_input = []
batch_output = []
for index in current_indexes:
img = mpimg.imread('.. /input/train/train/' + train_data.id[index])
 batch_input += [img[:, ::-1, :]]
batch_output.append(train_data.has_cactus[index])
 batch_input = np.array(batch_input)
 model_pred = model.predict_classes(batch_input)
 for j in range(len(batch_output)) :
if model_pred[j] != batch_output[j]:
wrong_ind.append(i*batch_size+j )<define_variables>
my_submission_gnb = pd.DataFrame({'ID_code': test_data.ID_code, 'target': y_preds_test_data_gnb})
 my_submission_gnb.to_csv('submission_gnb.csv', index=False )
test_files = os.listdir('.. /input/test/test/' )<predict_on_test>
from catboost import CatBoostClassifier
batch = 40
all_out = []
for i in range(int(4000/batch)) :
images = []
for j in range(batch):
img = mpimg.imread('.. /input/test/test/'+test_files[i*batch + j])
 images += [img]
out = model.predict(np.array(images))
all_out += [out]<create_dataframe>
cat = CatBoostClassifier(iterations=3000, learning_rate=0.03, objective="Logloss", eval_metric='AUC')
 cat.fit(X_train, y_train )
sub_file = pd.DataFrame(data = {'id': test_files, 'has_cactus': all_out.reshape(-1 ).tolist() } )<save_to_csv>
y_preds_cat = cat.predict(X_test)
 print(accuracy_score(y_test, y_preds_cat))
sub_file.to_csv('sample_submission.csv', index=False )<load_from_csv>
y_preds_test_data_cat = cat.predict(X_test_data )
train_dir = ".. /input/aerial-cactus-identification/train/train/"
test_dir = ".. /input/aerial-cactus-identification/test/test/"
train_df = pd.read_csv('.. /input/aerial-cactus-identification/train.csv')
 train_df.head()<load_pretrained>
my_submission_cat = pd.DataFrame({'ID_code': test_data.ID_code, 'target': y_preds_test_data_cat})
 my_submission_cat.to_csv('submission_cat.csv', index=False )
im = cv2.imread(".. /input/aerial-cactus-identification/train/train/01e30c0ba6e91343a12d2126fcafc0dd.jpg")
 plt.imshow(im )<prepare_x_and_y>
import lightgbm as lgb
from sklearn.model_selection import StratifiedKFold
import time
X_tr = []
Y_tr = []
imges = train_df['id'].values
for img_id in tqdm_notebook(imges):
image = np.array(cv2.imread(train_dir + img_id))
X_tr.append(image)
 Y_tr.append(train_df[train_df['id'] == img_id]['has_cactus'].values[0])
 X_tr.append(np.flip(image))
Y_tr.append(train_df[train_df['id'] == img_id]['has_cactus'].values[0])
 X_tr.append(np.flipud(image))
Y_tr.append(train_df[train_df['id'] == img_id]['has_cactus'].values[0])
 X_tr.append(np.fliplr(image))
Y_tr.append(train_df[train_df['id'] == img_id]['has_cactus'].values[0])
 X_tr = np.asarray(X_tr)
 X_tr = X_tr.astype('float32')
 X_tr /= 255
Y_tr = np.asarray(Y_tr)
<prepare_x_and_y>
def augment(x,y,t=2):
xs,xn = [],[]
for i in range(t):
mask = y>0
x1 = x[mask].copy()
ids = np.arange(x1.shape[0])
 for c in range(x1.shape[1]):
np.random.shuffle(ids)
 x1[:,c] = x1[ids][:,c]
xs.append(x1)
 for i in range(t//2):
mask = y==0
x1 = x[mask].copy()
ids = np.arange(x1.shape[0])
 for c in range(x1.shape[1]):
np.random.shuffle(ids)
 x1[:,c] = x1[ids][:,c]
xn.append(x1)
 xs = np.vstack(xs)
 xn = np.vstack(xn)
 ys = np.ones(xs.shape[0])
 yn = np.zeros(xn.shape[0])
 x = np.vstack([x,xs,xn])
 y = np.concatenate([y,ys,yn])
 return x,y
X_tr_2 = X_tr
Y_tr_2 = Y_tr 
<prepare_x_and_y>
n_fold = 5
folds = StratifiedKFold(n_splits=n_fold, shuffle=False, random_state=42 )
X_tr = X_tr_2
Y_tr = Y_tr_2 
<data_type_conversions>
lgbm_params = {'bagging_freq': 5,
'bagging_fraction': 0.335,
'boost_from_average':'false',
'boost': 'gbdt',
'feature_fraction': 0.041,
'learning_rate': 0.0083,
'max_depth': -1,
'metric':'auc',
'min_data_in_leaf': 80,
'min_sum_hessian_in_leaf': 10.0,
'num_leaves': 13,
'num_threads': 8,
'tree_learner': 'serial',
'objective': 'binary',
'verbosity': -1
}
test_image_names = []
for filename in os.listdir(test_dir):
test_image_names.append(filename)
 test_image_names.sort()
X_ts = []
for img_id in tqdm_notebook(test_image_names):
X_ts.append(cv2.imread(test_dir + img_id))
X_ts = np.asarray(X_ts)
 X_ts = X_ts.astype('float32')
 X_ts /= 255<split>
prediction_lgb_new = np.zeros(len(X_test_data))
for fold_n,(train_index, valid_index)in enumerate(folds.split(train_data,y)) :
print('Fold', fold_n)
 X_training, X_validation = train_data.iloc[train_index], train_data.iloc[valid_index]
y_training, y_validation = y.iloc[train_index], y.iloc[valid_index]
X_training, y_training = augment(X_training.values, y_training.values)
 X_training = pd.DataFrame(X_training)
 training_data = lgb.Dataset(X_training, label=y_training)
 validation_data = lgb.Dataset(X_validation, label=y_validation)
 model = lgb.train(lgbm_params,training_data,num_boost_round=1000000,
valid_sets = [training_data, validation_data],verbose_eval=1000,early_stopping_rounds = 3000)
 prediction_lgb_new += model.predict(X_test_data, num_iteration=model.best_iteration)/folds.n_splits
x_train,x_test,y_train,y_test = train_test_split(X_tr, Y_tr, test_size = 0.2 , stratify = Y_tr )<choose_model_class>
my_submission_lgb_new = pd.DataFrame({'ID_code': test_data.ID_code, 'target': prediction_lgb_new})
 my_submission_lgb_new.to_csv('submission_lgb_new.csv', index=False )
base=keras.applications.vgg16.VGG16(include_top=False, weights='.. /input/weights/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5',input_shape=(32,32,3)) 
<choose_model_class>
my_submission_cat_lgb_gnb = pd.DataFrame({'ID_code': test_data.ID_code,
'target':(y_preds_test_data_cat + y_preds_test_data_gnb)/2})
 my_submission_cat_lgb_gnb.to_csv('my_submission_cat_gnb.csv', index=False )