Kaggle competition: Santander Customer Transaction Prediction
<set_options><EOS> 
%matplotlib inline
<SOS> metric: AUC Kaggle data source: santander-customer-transaction-prediction<load_from_csv>
%matplotlib inline
df_train = pd.read_csv('.. /input/aptos2019-blindness-detection/train.csv')
 df_test = pd.read_csv('.. /input/aptos2019-blindness-detection/test.csv')
 x = df_train['id_code']
y = df_train['diagnosis']
x, y = shuffle(x, y, random_state=8 )<split>
plt.style.use('bmh')
 plt.rcParams['figure.figsize'] =(10, 10)
 title_config = {'fontsize': 20, 'y': 1.05}
y = to_categorical(y, num_classes=NUM_CLASSES)
 train_x, valid_x, train_y, valid_y = train_test_split(x, y, test_size=0.15,
stratify=y, random_state=8)
 print(train_x.shape)
 print(train_y.shape)
 print(valid_x.shape)
 print(valid_y.shape)
<prepare_x_and_y>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
class My_Generator(Sequence):
def __init__(self, image_filenames, labels, batch_size, is_train=True, mix=False):
self.image_filenames, self.labels = image_filenames, labels
self.batch_size = batch_size
self.is_train = is_train
self.on_epoch_end()
self.is_mix = mix
def __len__(self):
return int(np.ceil(len(self.image_filenames)/ float(self.batch_size)))
 def __getitem__(self, idx):
batch_x = self.image_filenames[idx * self.batch_size:(idx + 1)* self.batch_size]
batch_y = self.labels[idx * self.batch_size:(idx + 1)* self.batch_size]
if(self.is_train):
return self.train_generate(batch_x, batch_y)
 return self.valid_generate(batch_x, batch_y)
 def on_epoch_end(self):
self.image_filenames, self.labels = shuffle(self.image_filenames, self.labels)
 def mix_up(self, x, y):
lam = np.random.beta(0.2, 0.4)
 ori_index = np.arange(int(len(x)))
 index_array = np.arange(int(len(x)))
 np.random.shuffle(index_array)
 mixed_x = lam * x[ori_index] +(1 - lam)* x[index_array]
mixed_y = lam * y[ori_index] +(1 - lam)* y[index_array]
return mixed_x, mixed_y
def train_generate(self, batch_x, batch_y):
batch_images = []
for(sample, label)in zip(batch_x, batch_y):
img = cv2.imread('.. /input/aptos2019-blindness-detection/train_images/'+sample+'.png')
 img = cv2.resize(img,(SIZE, SIZE))
img = seq.augment_image(img)
 batch_images.append(img)
 batch_images = np.array(batch_images, np.float32)/ 255
batch_y = np.array(batch_y, np.float32)
 if(self.is_mix):
batch_images, batch_y = self.mix_up(batch_images, batch_y)
 return batch_images, batch_y
def valid_generate(self, batch_x, batch_y):
batch_images = []
for(sample, label)in zip(batch_x, batch_y):
img = cv2.imread('.. /input/aptos2019-blindness-detection/train_images/'+sample+'.png')
 img = cv2.resize(img,(SIZE, SIZE))
batch_images.append(img)
 batch_images = np.array(batch_images, np.float32)/ 255
batch_y = np.array(batch_y, np.float32)
 return batch_images, batch_y<import_modules>
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )
BatchNormalization, Input, Conv2D, GlobalAveragePooling2D)
<choose_model_class>
X_train = train.iloc[:, 2:].values.astype('float64')
 y_train = train['target'].values
X_test = test.iloc[:, 1:].values.astype('float64' )
def create_model(input_shape, n_out):
input_tensor = Input(shape=input_shape)
 base_model_2 = InceptionResNetV2(include_top=False,
weights=None,
input_tensor=input_tensor)
 base_model_2.load_weights('.. /input/inceptionresnetv2/inception_resnet_v2_weights_tf_dim_ordering_tf_kernels_notop.h5')
 x = GlobalAveragePooling2D()(base_model_2.output)
 final_output = Dense(n_out, activation='softmax', name='final_output' )(x)
 model = Model(input_tensor, final_output)
 return model<choose_model_class>
class RadiusNeighborsNB(BaseEstimator, ClassifierMixin):
def __init__(self, radius=1.0, steps=100, threshold=500):
self.radius = radius
self.steps = steps
self.threshold = threshold
def fit(self, X, y):
X =(X - X.mean(axis=0)) / X.std(axis=0)
 self.log_prior_ = np.log(np.bincount(y)) - np.log(len(y))
self.grid_ = np.linspace(-5, 5, self.steps)
 shape =(self.steps, X.shape[1], len(self.log_prior_))
self.log_prob_grid_ = np.full(shape, self.log_prior_)
 for i in range(shape[0]):
for j in range(shape[1]):
mask = np.abs(X[:, j] - self.grid_[i])< self.radius
total = mask.sum()
if total >= self.threshold:
self.log_prob_grid_[i, j] =(np.log(np.bincount(y[mask]))
- np.log(total))
return self
def predict_proba(self, X):
X =(X - X.mean(axis=0)) / X.std(axis=0)
 shape =(*X.shape, len(self.log_prior_))
log_prob = np.empty(shape)
 for j in range(shape[1]):
lookup = np.searchsorted(self.grid_, X[:, j])
 lookup[lookup == len(self.grid_)] -= 1
log_prob[:, j] = self.log_prob_grid_[lookup, j]
log_posterior = log_prob.sum(axis=1)-(X.shape[1] - 1)* self.log_prior_
return np.exp(log_posterior - logsumexp(log_posterior))
def predict(self, X):
return self.predict_proba(X ).argmax(axis=1 )
EarlyStopping, ReduceLROnPlateau,CSVLogger)
 epochs = 15; batch_size = 16
checkpoint = ModelCheckpoint('.. /working/combo.h5', monitor='val_loss', verbose=1,
save_best_only=True, mode='min', save_weights_only = True)
 reduceLROnPlat = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=3,
verbose=1, mode='auto', epsilon=0.0001)
 early = EarlyStopping(monitor="val_loss",
mode="min",
patience=7)
 csv_logger = CSVLogger(filename='.. /working/training_log.csv',
separator=',',
append=True)
 callbacks_list = [checkpoint, csv_logger, reduceLROnPlat, early]
train_generator = My_Generator(train_x, train_y, batch_size, is_train=True)
 train_mixup = My_Generator(train_x, train_y, batch_size, is_train=True, mix=True)
 valid_generator = My_Generator(valid_x, valid_y, batch_size, is_train=False)
 model = create_model(
 input_shape=(SIZE,SIZE,3),
n_out=NUM_CLASSES )<train_model>
i_train, i_valid = next(StratifiedShuffleSplit(n_splits=1 ).split(X_train, y_train))
for layer in model.layers:
layer.trainable = False
for i in range(-1,0):
model.layers[i].trainable = True
model.compile(
 loss='categorical_crossentropy',
optimizer='sgd')
 model.fit_generator(
 train_mixup,
steps_per_epoch=np.ceil(float(len(train_y)) / float(128)) ,
epochs=7,
class_weight=class_weight,
max_queue_size=16, workers=WORKERS, use_multiprocessing=True,
verbose=1 )<train_model>
model = RadiusNeighborsNB(radius=0.35, threshold=30)
 model.fit(X_train[i_train], y_train[i_train])
 print(f'Training AUC is {roc_auc_score(y_train[i_train], model.predict_proba(X_train[i_train])[:, 1])}.')
 print(f'Validation AUC is {roc_auc_score(y_train[i_valid], model.predict_proba(X_train[i_valid])[:, 1])}.' )
for layer in model.layers:
layer.trainable = True
callbacks_list = [checkpoint, csv_logger, reduceLROnPlat]
model.compile(loss='categorical_crossentropy',
optimizer=Adam(lr=1e-4),
metrics=['accuracy'])
 model.fit_generator(
 train_mixup,
steps_per_epoch=np.ceil(float(len(train_x)) / float(batch_size)) ,
validation_data=valid_generator,
validation_steps=np.ceil(float(len(valid_x)) / float(batch_size)) ,
class_weight=class_weight,
epochs=epochs,
verbose=1,
max_queue_size=16, workers=2, use_multiprocessing=True,
callbacks=callbacks_list )<load_from_csv>
model.fit(X_train, y_train )
submit = pd.read_csv('.. /input/aptos2019-blindness-detection/sample_submission.csv')
 model.load_weights('.. /working/combo.h5')
 predicted = []<predict_on_test>
model.fit(X_train, y_train )
<save_to_csv><EOS> 
submission = pd.read_csv('.. /input/sample_submission.csv')
 submission['target'] = model.predict_proba(X_test)[:, 1]
submission.to_csv('submission.csv', index=False )