Kaggle competition: Santander Customer Transaction Prediction
images_train = []
labels_train = []
images = train_data['id'].values
for image_id in tqdm_notebook(images):
image = np.array(cv.imread(training_path + image_id))
label = train_data[train_data['id'] == image_id]['has_cactus'].values[0]
images_train.append(image)
 labels_train.append(label)
 images_train.append(np.flip(image))
labels_train.append(label)
 images_train.append(np.flipud(image))
labels_train.append(label)
 images_train.append(np.fliplr(image))
labels_train.append(label)
 images_train = np.asarray(images_train)
 images_train = images_train.astype('float32')
 images_train /= 255.
 labels_train = np.asarray(labels_train )<prepare_x_and_y>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv')
 features = [c for c in train.columns if c not in ['ID_code', 'target']]
target = train['target']
print("Data is ready!" )
x_tr = images_train
y_tr = labels_train
images_train = x_tr
labels_train = y_tr<data_type_conversions>
print("Missing data at training")
 train.isnull().values.any()
test_images_names = []
for filename in os.listdir(test_path):
test_images_names.append(filename)
 test_images_names.sort()
images_test = []
for image_id in tqdm_notebook(test_images_names):
images_test.append(np.array(cv.imread(test_path + image_id)))
 images_test = np.asarray(images_test)
 images_test = images_test.astype('float32')
 images_test /= 255<split>
print("Missing data at test")
 test.isnull().values.any()
x_train, x_test, y_train, y_test = train_test_split(images_train, labels_train, test_size = 0.2, stratify = labels_train )<choose_model_class>
train = train.drop(["ID_code", "target"], axis=1 )
model = create_model()<train_model>
sns.set_style('whitegrid')
 sns.countplot(target)
 sns.set_style('whitegrid' )
history = model.fit(x_train,
y_train,
batch_size = 128,
epochs = 80,
validation_data =(x_test, y_test),
verbose = 1,
callbacks = callbacks )<load_pretrained>
@jit
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
model.load_weights(file_path )<predict_on_test>
param = {
'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
}
predictions = model.predict(images_test, verbose = 1 )<save_to_csv>
num_folds = 11
features = [c for c in train.columns if c not in ['ID_code', 'target']]
folds = KFold(n_splits=num_folds, random_state=2319)
 oof = np.zeros(len(train))
getVal = np.zeros(len(train))
predictions = np.zeros(len(target))
feature_importance_df = pd.DataFrame()
test_df = pd.read_csv('.. /input/sample_submission.csv')
 X_test = []
images_test = test_df['id'].values
for img_id in tqdm_notebook(images_test):
X_test.append(cv.imread(test_path + img_id))
X_test = np.asarray(X_test)
 X_test = X_test.astype('float32')
 X_test /= 255
y_test_pred = model.predict_proba(X_test)
 test_df['has_cactus'] = y_test_pred
test_df.to_csv('aerial-cactus-submission.csv', index = False )<import_modules>
for fold_,(trn_idx, val_idx)in enumerate(folds.split(train.values, target.values)) :
X_train, y_train = train.iloc[trn_idx][features], target.iloc[trn_idx]
X_valid, y_valid = train.iloc[val_idx][features], target.iloc[val_idx]
X_tr, y_tr = augment(X_train.values, y_train.values)
 X_tr = pd.DataFrame(X_tr)
 print("Fold idx:{}".format(fold_ + 1))
trn_data = lgb.Dataset(X_tr, label=y_tr)
 val_data = lgb.Dataset(X_valid, label=y_valid)
 clf = lgb.train(param, trn_data, 1000000, valid_sets = [trn_data, val_data], verbose_eval=5000, early_stopping_rounds = 4000)
 oof[val_idx] = clf.predict(train.iloc[val_idx][features], num_iteration=clf.best_iteration)
 getVal[val_idx]+= clf.predict(train.iloc[val_idx][features], num_iteration=clf.best_iteration)/ folds.n_splits
fold_importance_df = pd.DataFrame()
fold_importance_df["feature"] = features
fold_importance_df["importance"] = clf.feature_importance()
fold_importance_df["fold"] = fold_ + 1
feature_importance_df = pd.concat([feature_importance_df, fold_importance_df], axis=0)
 predictions += clf.predict(test[features], num_iteration=clf.best_iteration)/ folds.n_splits
import numpy as np
import pandas as pd
from pathlib import Path
from fastai import *
from fastai.vision import *
import torch 
<define_variables>
print("
>> CV score: {:<8.5f}".format(roc_auc_score(target, oof)) )
data_folder = Path(".. /input")
<load_from_csv>
submission = pd.DataFrame({"ID_code": test.ID_code.values})
 submission["target"] = predictions
submission.to_csv("submission.csv", index=False )