Kaggle competition: WSDM - KKBox's Churn Prediction Challenge
GCS_DS_PATH = KaggleDatasets().get_gcs_path("cifar-10-unzipped" )<define_variables>
df_train_file = ".. /input/kkbox-churn-scala-label/user_label_201703.csv"
df_train = pd.read_csv(df_train_file, dtype = {'is_churn': 'int8'})
 df_train.info()
IMAGE_SIZE = [32, 32]
EPOCHS = 100
BATCH_SIZE = 16 * strategy.num_replicas_in_sync
ROOT_PATH_MAP = {
32: GCS_DS_PATH + "/tfrecords-jpeg-32x32/"
}
ROOT_PATH = ROOT_PATH_MAP[IMAGE_SIZE[0]]
TRAINING_FILENAMES = tf.io.gfile.glob(ROOT_PATH + "train/*.tfrec")
 VALIDATION_FILENAMES = tf.io.gfile.glob(ROOT_PATH + "validation/*.tfrec")
 TEST_FILENAMES = tf.io.gfile.glob(ROOT_PATH + "test/*.tfrec")
 CLASSES = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']<categorify>
df_test_file = ".. /input/kkbox-churn-prediction-challenge/sample_submission_v2.csv"
df_test = pd.read_csv(df_test_file, dtype = {'is_churn': 'int8'})
 df_test.info()
np.set_printoptions(threshold=15, linewidth=80)
 def batch_to_numpy_images_and_labels(data):
images, labels = data
numpy_images = images.numpy()
numpy_labels = labels.numpy()
if numpy_labels.dtype == object:
numpy_labels = [None for _ in enumerate(numpy_images)]
return numpy_images, numpy_labels
def title_from_label_and_target(label, correct_label):
if correct_label is None:
return CLASSES[label], True
correct =(label == correct_label)
 return "{} [{}{}{}]".format(CLASSES[label], 'OK' if correct else 'NO', u"\u2192" if not correct else '',
CLASSES[correct_label] if not correct else ''), correct
def display_one_flower(image, title, subplot, red=False, titlesize=16):
plt.subplot(*subplot)
 plt.axis('off')
 plt.imshow(image)
 if len(title)> 0:
plt.title(title, fontsize=int(titlesize)if not red else int(titlesize/1.2), color='red' if red else 'black', fontdict={'verticalalignment':'center'}, pad=int(titlesize/1.5))
return(subplot[0], subplot[1], subplot[2]+1)
 def display_batch_of_images(databatch, predictions=None):

 images, labels = batch_to_numpy_images_and_labels(databatch)
 if labels is None:
labels = [None for _ in enumerate(images)]
rows = int(math.sqrt(len(images)))
 cols = len(images)//rows
FIGSIZE = 13.0
SPACING = 0.1
subplot=(rows,cols,1)
 if rows < cols:
plt.figure(figsize=(FIGSIZE,FIGSIZE/cols*rows))
else:
plt.figure(figsize=(FIGSIZE/rows*cols,FIGSIZE))
for i,(image, label)in enumerate(zip(images[:rows*cols], labels[:rows*cols])) :
title = '' if label is None else CLASSES[label]
correct = True
if predictions is not None:
title, correct = title_from_label_and_target(predictions[i], label)
 dynamic_titlesize = FIGSIZE*SPACING/max(rows,cols)*40+3
subplot = display_one_flower(image, title, subplot, not correct, titlesize=dynamic_titlesize)
 plt.tight_layout()
if label is None and predictions is None:
plt.subplots_adjust(wspace=0, hspace=0)
 else:
plt.subplots_adjust(wspace=SPACING, hspace=SPACING)
 plt.show()
def display_confusion_matrix(cmat, score, precision, recall):
plt.figure(figsize=(15,15))
ax = plt.gca()
ax.matshow(cmat, cmap='Reds')
 ax.set_xticks(range(len(CLASSES)))
 ax.set_xticklabels(CLASSES, fontdict={'fontsize': 7})
 plt.setp(ax.get_xticklabels() , rotation=45, ha="left", rotation_mode="anchor")
 ax.set_yticks(range(len(CLASSES)))
 ax.set_yticklabels(CLASSES, fontdict={'fontsize': 7})
 plt.setp(ax.get_yticklabels() , rotation=45, ha="right", rotation_mode="anchor")
 titlestring = ""
if score is not None:
titlestring += 'f1 = {:.3f} '.format(score)
 if precision is not None:
titlestring += '
precision = {:.3f} '.format(precision)
 if recall is not None:
titlestring += '
recall = {:.3f} '.format(recall)
 if len(titlestring)> 0:
ax.text(101, 1, titlestring, fontdict={'fontsize': 18, 'horizontalalignment':'right', 'verticalalignment':'top', 'color':'
plt.show()
def display_training_curves(training, validation, title, subplot):
if subplot%10==1:
plt.subplots(figsize=(10,10), facecolor='
plt.tight_layout()
ax = plt.subplot(subplot)
 ax.set_facecolor('
ax.plot(training)
 ax.plot(validation)
 ax.set_title('model '+ title)
 ax.set_ylabel(title)
 ax.set_xlabel('epoch')
 ax.legend(['train', 'valid.'] )<categorify>
df_userlogs_file = ".. /input/kkbox-churn-prediction-challenge/user_logs.csv"
df_userlogs_file_2 = ".. /input/kkbox-churn-prediction-challenge/user_logs_v2.csv"
df_userlogs = pd.read_csv(df_userlogs_file, nrows = 36000000)
 df_userlogs_2 = pd.read_csv(df_userlogs_file_2)
 df_userlogs = df_userlogs.append(df_userlogs_2, ignore_index = True)
 df_userlogs.info()
del df_userlogs_2
def to_float32(image, label):
return tf.cast(image, tf.float32), label
def decode_image(image_data):
image = tf.image.decode_jpeg(image_data, channels=3)
 image = tf.cast(image, tf.float32)/ 255.0
image = tf.reshape(image, [*IMAGE_SIZE, 3])
 return image
def read_labeled_tfrecord(example):
LABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"class": tf.io.FixedLenFeature([], tf.int64),
}
example = tf.io.parse_single_example(example, LABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 label = tf.cast(example['class'], tf.int32)
 return image, label
def read_unlabeled_tfrecord(example):
UNLABELED_TFREC_FORMAT = {
"image": tf.io.FixedLenFeature([], tf.string),
"id": tf.io.FixedLenFeature([], tf.string),
}
example = tf.io.parse_single_example(example, UNLABELED_TFREC_FORMAT)
 image = decode_image(example['image'])
 idnum = example['id']
return image, idnum
def load_dataset(filenames, labeled=True, ordered=False):
ignore_order = tf.data.Options()
if not ordered:
ignore_order.experimental_deterministic = False
dataset = tf.data.TFRecordDataset(filenames, num_parallel_reads=AUTO)
 dataset = dataset.with_options(ignore_order)
 dataset = dataset.map(read_labeled_tfrecord if labeled else read_unlabeled_tfrecord, num_parallel_calls=AUTO)
 return dataset
def data_augment(image, label):
image = tf.image.random_flip_left_right(image)
 image = tf.image.random_saturation(image, 0, 2)
 image = tf.clip_by_value(image, 0, 1)
 return image, label
def get_training_dataset() :
dataset = load_dataset(TRAINING_FILENAMES, labeled=True)
 dataset = dataset.map(data_augment, num_parallel_calls=AUTO)
 dataset = dataset.repeat()
dataset = dataset.shuffle(2048)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
def get_validation_dataset(ordered=False):
dataset = load_dataset(VALIDATION_FILENAMES, labeled=True, ordered=ordered)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.cache()
dataset = dataset.prefetch(AUTO)
 return dataset
def get_test_dataset(ordered=False):
dataset = load_dataset(TEST_FILENAMES, labeled=False, ordered=ordered)
 dataset = dataset.batch(BATCH_SIZE)
 dataset = dataset.prefetch(AUTO)
 return dataset
def count_data_items(filenames):
n = [int(re.compile(r"-([0-9]*)\." ).search(filename ).group(1)) for filename in filenames]
return np.sum(n)
 NUM_TRAINING_IMAGES = count_data_items(TRAINING_FILENAMES)
 NUM_VALIDATION_IMAGES = count_data_items(VALIDATION_FILENAMES)
 NUM_TEST_IMAGES = count_data_items(TEST_FILENAMES)
 STEPS_PER_EPOCH = NUM_TRAINING_IMAGES // BATCH_SIZE
print('Dataset: {} training images, {} validation images, {} unlabeled test images'.format(NUM_TRAINING_IMAGES, NUM_VALIDATION_IMAGES, NUM_TEST_IMAGES))<train_model>
del df_userlogs['date']
counts = df_userlogs.groupby('msno')['total_secs'].count().reset_index()
counts.columns = ['msno', 'days_listened']
sums = df_userlogs.groupby('msno' ).sum().reset_index()
df_userlogs = sums.merge(counts, how = 'inner', on = 'msno' )
print("Training data shapes:")
 for image, label in get_training_dataset().take(3):
print(image.numpy().shape, label.numpy().shape)
 print("Training data label examples:", label.numpy())
 print("Validation data shapes:")
 for image, label in get_validation_dataset().take(3):
print(image.numpy().shape, label.numpy().shape)
 print("Validation data label examples:", label.numpy())
 print("Test data shapes:")
 for image, idnum in get_test_dataset().take(3):
print(image.numpy().shape, idnum.numpy().shape)
 print("Test data IDs:", idnum.numpy().astype('U'))<train_model>
df_userlogs['secs_per_song'] = df_userlogs['total_secs'].div(df_userlogs['num_25'] + df_userlogs['num_50'] + df_userlogs['num_75'] + df_userlogs['num_985'] + df_userlogs['num_100'] )
training_dataset = get_training_dataset()
training_dataset = training_dataset.unbatch().batch(20)
 train_batch = iter(training_dataset )<define_variables>
df_members_file = ".. /input/kkbox-churn-prediction-challenge/members_v3.csv"
df_members = pd.read_csv(df_members_file)
 df_members.info()
display_batch_of_images(next(train_batch))<define_variables>
df_members['city'] = df_members.city.apply(lambda x: int(x)if pd.notnull(x)else "NAN")
 df_members['bd'] = df_members.bd.apply(lambda x: -99999 if float(x)<= 1 else x)
 df_members['bd'] = df_members.bd.apply(lambda x: -99999 if float(x)>= 100 else x)
 df_members['gender'] = df_members['gender'].fillna("others")
 df_members['registered_via'] = df_members.registered_via.apply(lambda x: int(x)if pd.notnull(x)else "NAN")
 current = datetime.strptime('20170331', "%Y%m%d" ).date()
df_members['num_days'] = df_members.registration_init_time.apply(lambda x:(current - datetime.strptime(str(int(x)) , "%Y%m%d" ).date() ).days if pd.notnull(x)else "NAN")
 del df_members['registration_init_time']
validation_dataset = get_validation_dataset()
validation_dataset = validation_dataset.unbatch().batch(20)
 validation_batch = iter(validation_dataset )<define_variables>
del df_members['city']
display_batch_of_images(next(validation_batch))<define_variables>
df_members['registered_via'].replace([-1, 1, 2, 5, 6, 8, 10, 11, 13, 14, 16, 17, 18, 19], 1, inplace = True )
test_dataset = get_test_dataset()
test_dataset = test_dataset.unbatch().batch(20)
 test_batch = iter(test_dataset )<define_variables>
del df_members['bd']
display_batch_of_images(next(test_batch))<choose_model_class>
df_transactions_file = ".. /input/kkbox-churn-prediction-challenge/transactions.csv"
df_transactions_file_2 = ".. /input/kkbox-churn-prediction-challenge/transactions_v2.csv"
df_transactions = pd.read_csv(df_transactions_file)
 df_transactions_2 = pd.read_csv(df_transactions_file_2)
 df_transactions = df_transactions.append(df_transactions_2, ignore_index = True)
 df_transactions.describe()
del df_transactions_2
with strategy.scope() :
model = Sequential()
model.add(Conv2D(32,(3, 3), activation='relu', kernel_initializer='he_uniform', padding='same', kernel_regularizer=l2(0.001), input_shape=(IMAGE_SIZE[0], IMAGE_SIZE[1], 3)))
 model.add(BatchNormalization())
 model.add(Conv2D(32,(3, 3), activation='relu', kernel_initializer='he_uniform', padding='same', kernel_regularizer=l2(0.001)))
 model.add(BatchNormalization())
 model.add(MaxPooling2D(( 2, 2)))
 model.add(Dropout(0.2))
model.add(Conv2D(64,(3, 3), activation='relu', kernel_initializer='he_uniform', padding='same', kernel_regularizer=l2(0.001)))
 model.add(BatchNormalization())
 model.add(Conv2D(64,(3, 3), activation='relu', kernel_initializer='he_uniform', padding='same', kernel_regularizer=l2(0.001)))
 model.add(BatchNormalization())
 model.add(MaxPooling2D(( 2, 2)))
 model.add(Dropout(0.3))
model.add(Conv2D(128,(3, 3), activation='relu', kernel_initializer='he_uniform', padding='same', kernel_regularizer=l2(0.001)))
 model.add(BatchNormalization())
 model.add(Conv2D(128,(3, 3), activation='relu', kernel_initializer='he_uniform', padding='same', kernel_regularizer=l2(0.001)))
 model.add(BatchNormalization())
 model.add(MaxPooling2D(( 2, 2)))
 model.add(Dropout(0.4))
model.add(Flatten())
 model.add(Dense(128, activation='relu', kernel_initializer='he_uniform', kernel_regularizer=l2(0.001)))
 model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
 model.summary()<train_model>
del df_transactions['payment_plan_days']
training_dataset = get_training_dataset().map(to_float32)
 validation_dataset = get_validation_dataset().map(to_float32)
 checkpoint = ModelCheckpoint("model_checkpoint.h5", monitor='val_loss', mode='min', save_best_only=True, verbose=1)
 early_stopping = EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=1, restore_best_weights=True)
 reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=3, verbose=1, min_delta=0.0001)
 callbacks = [early_stopping, reduce_lr]
history = model.fit(training_dataset, steps_per_epoch=STEPS_PER_EPOCH, epochs=EPOCHS, validation_data=validation_dataset, callbacks=callbacks )<predict_on_test>
del df_transactions['membership_expire_date']
del df_transactions['transaction_date']
df_transactions = df_transactions.drop_duplicates()
df_transactions = df_transactions.groupby('msno' ).mean().reset_index()
df_transactions.head()
cmdataset = get_validation_dataset(ordered=True)
 cmdataset = cmdataset.map(to_float32)
 images_ds = cmdataset.map(lambda image, label: image)
 labels_ds = cmdataset.map(lambda image, label: label ).unbatch()
cm_correct_labels = next(iter(labels_ds.batch(NUM_VALIDATION_IMAGES)) ).numpy()
cm_probabilities = model.predict(images_ds)
 cm_predictions = np.argmax(cm_probabilities, axis=-1)
 print("Correct labels: ", cm_correct_labels.shape, cm_correct_labels)
 print("Predicted labels: ", cm_predictions.shape, cm_predictions )<compute_train_metric>
gender = {'male': 0, 'female': 1, 'others' :2}
cmat = confusion_matrix(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)))
 score = f1_score(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)) , average='macro')
 precision = precision_score(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)) , average='macro')
 recall = recall_score(cm_correct_labels, cm_predictions, labels=range(len(CLASSES)) , average='macro')
 cmat =(cmat.T / cmat.sum(axis=1)).T
display_confusion_matrix(cmat, score, precision, recall)
 print('f1 score: {:.3f}, precision: {:.3f}, recall: {:.3f}'.format(score, precision, recall))<save_to_csv>
df_training = pd.merge(left = df_train, right = df_members, how = 'left', on = ['msno'])
 df_training = pd.merge(left = df_training, right = df_transactions , how = 'left', on = ['msno'])
 df_training = pd.merge(left = df_training, right = df_userlogs, how = 'left', on = ['msno'])
 df_training['gender'] = df_training['gender'].map(gender )
test_ds = get_test_dataset(ordered=True)
 test_ds = test_ds.map(to_float32)
 print('Computing predictions...')
 test_images_ds = test_ds.map(lambda image, idnum: image)
 probabilities = model.predict(test_images_ds)
 predictions = np.argmax(probabilities, axis=-1)
 print(predictions)
 label_map = np.vectorize(lambda index : CLASSES[int(index)])
 predictions_with_label = label_map(predictions)
 print(predictions_with_label)
 print('Generating submission.csv file...')
 test_ids_ds = test_ds.map(lambda image, idnum: idnum ).unbatch()
test_ids = next(iter(test_ids_ds.batch(NUM_TEST_IMAGES)) ).numpy().astype('U')
 np.savetxt('submission.csv', np.rec.fromarrays([test_ids, predictions_with_label]), fmt=['%s', '%s'], delimiter=',', header='id,label', comments='')
 !head submission.csv<define_variables>
df_testing = pd.merge(left = df_test, right = df_members, how = 'left', on = ['msno'])
 df_testing = pd.merge(left = df_testing, right = df_transactions , how = 'left', on = ['msno'])
 df_testing = pd.merge(left = df_testing, right = df_userlogs, how = 'left', on = ['msno'])
 df_testing['gender'] = df_testing['gender'].map(gender )
dataset = get_validation_dataset()
dataset = dataset.unbatch().batch(20)
 batch = iter(dataset )<predict_on_test>
del df_members
del df_userlogs
del df_transactions
images, labels = next(batch)
 probabilities = model.predict(tf.cast(images, tf.float32))
predictions = np.argmax(probabilities, axis=-1)
 display_batch_of_images(( images, labels), predictions )<set_options>
%matplotlib inline 
<load_from_csv>
import sklearn as sl
from sklearn import model_selection
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
import lightgbm as lgb
df_train=pd.read_csv('/kaggle/input/stumbleupon/train.tsv',sep='\t')
 df_test=pd.read_csv('/kaggle/input/stumbleupon/test.tsv',sep='\t',usecols=['urlid','boilerplate'] )<feature_engineering>
cols = [c for c in df_training.columns if c not in ['is_churn','msno']]
X = df_training[cols]
Y = df_training['is_churn']
validation_size = 0.20
seed = 7
scoring = 'roc_auc'
X_train, X_validation, Y_train, Y_validation = model_selection.train_test_split(X, Y, test_size = validation_size, random_state = seed )
<load_pretrained><EOS> 
lgb_params = { 'learning_rate': 0.02,
'application': 'binary',
'max_depth': 35,
'num_leaves': 3500,
'verbosity': -1,
'metric': 'binary_logloss'
}
d_trainl = lgb.Dataset(X_train, label = Y_train)
 d_validl = lgb.Dataset(X_validation, label = Y_validation)
 watchlistl = [d_trainl, d_validl]
lgb_model = lgb.train(lgb_params,
train_set = d_trainl,
num_boost_round = 1000,
valid_sets = watchlistl,
early_stopping_rounds = 50,
verbose_eval = 10)
 lgb_pred = lgb_model.predict(df_testing[cols])
 lgb_testing = df_testing.copy()
lgb_testing['is_churn'] = lgb_pred.clip(0.+1e-15, 1-1e-15)
 lgb_testing[['msno','is_churn']].to_csv('lgb_result.csv', index = False )