Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
neural1 = Sequential()
neural1.add(Dense(16, input_dim=784 , activation='relu'))
neural1.add(Dense(10, activation='softmax'))
neural2 = Sequential()
neural2.add(Dense(32, input_dim=784 , activation='relu'))
neural2.add(Dense(10, activation='softmax'))
neural3 = Sequential()
neural3.add(Dense(64, input_dim=784 , activation='relu'))
neural3.add(Dense(10, activation='softmax'))
neural4 = Sequential()
neural4.add(Dense(128, input_dim=784 , activation='relu'))
neural4.add(Dense(10, activation='softmax'))
neural5 = Sequential()
neural5.add(Dense(256, input_dim=784 , activation='relu'))
neural5.add(Dense(10, activation='softmax'))
neural6 = Sequential()
neural6.add(Dense(512, input_dim=784 , activation='relu'))
neural6.add(Dense(10, activation='softmax'))
neural7 = Sequential()
neural7.add(Dense(1024, input_dim=784 , activation='relu'))
neural7.add(Dense(10, activation='softmax'))
neural8 = Sequential()
neural8.add(Dense(2048, input_dim=784 , activation='relu'))
neural8.add(Dense(10, activation='softmax'))
neural1.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 neural2.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 neural3.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 neural4.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 neural5.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 neural6.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 neural7.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 neural8.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 neural1.fit(X_train, Y_train_c, epochs=50, batch_size=200)
 neural2.fit(X_train, Y_train_c, epochs=50, batch_size=200)
 neural3.fit(X_train, Y_train_c, epochs=50, batch_size=200)
 neural4.fit(X_train, Y_train_c, epochs=50, batch_size=200)
 neural5.fit(X_train, Y_train_c, epochs=50, batch_size=200)
 neural6.fit(X_train, Y_train_c, epochs=50, batch_size=200)
 neural7.fit(X_train, Y_train_c, epochs=50, batch_size=200)
 neural8.fit(X_train, Y_train_c, epochs=50, batch_size=200)
 scores1 = neural1.evaluate(X_test, Y_test_c)
 scores2 = neural2.evaluate(X_test, Y_test_c)
 scores3 = neural3.evaluate(X_test, Y_test_c)
 scores4 = neural4.evaluate(X_test, Y_test_c)
 scores5 = neural5.evaluate(X_test, Y_test_c)
 scores6 = neural6.evaluate(X_test, Y_test_c)
 scores7 = neural7.evaluate(X_test, Y_test_c)
 scores8 = neural8.evaluate(X_test, Y_test_c)
 plt.plot([1,2,3,4,5,6,7,8],
[scores1[0],scores2[0],scores3[0],scores4[0],scores5[0],scores6[0],
scores7[0],scores8[0]],'-o' )<choose_model_class>
train = pd.read_csv(".. /input/train_V2.csv")
 test = pd.read_csv(".. /input/test_V2.csv" )
X_train_2d = X_train.reshape(X_train.shape[0], 28, 28,1)
 X_train_2d.shape
X_test_2d = X_test.reshape(X_test.shape[0], 28, 28,1)
 X_test_2d.shape
model_1= Sequential()
model_1.add(Conv2D(filters = 32, kernel_size =(5,5),activation ='relu', input_shape=(28,28,1)))
 model_1.add(MaxPooling2D(pool_size=(2, 2)))
 model_1.add(Flatten())
 model_1.add(Dense(512, activation='relu'))
model_1.add(Dense(10, activation='softmax'))
model_2= Sequential()
model_2.add(Conv2D(filters = 32, kernel_size =(5,5),activation ='relu', input_shape=(28,28,1)))
 model_2.add(MaxPooling2D(pool_size=(2, 2)))
 model_2.add(Conv2D(filters = 64, kernel_size =(5,5),activation ='relu'))
model_2.add(MaxPooling2D(pool_size=(2, 2)))
 model_2.add(Flatten())
 model_2.add(Dense(512, activation='relu'))
model_2.add(Dense(10, activation='softmax'))
model_3= Sequential()
model_3.add(Conv2D(filters = 32, kernel_size =(5,5),activation ='relu', input_shape=(28,28,1)))
 model_3.add(MaxPooling2D(pool_size=(2, 2)))
 model_3.add(Conv2D(filters = 64, kernel_size =(5,5),activation ='relu'))
model_3.add(MaxPooling2D(pool_size=(2, 2)))
 model_3.add(Conv2D(filters = 128, kernel_size =(3,3),activation ='relu'))
model_3.add(MaxPooling2D(pool_size=(2, 2)))
 model_3.add(Flatten())
 model_3.add(Dense(512, activation='relu'))
model_3.add(Dense(10, activation='softmax'))
model_1.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 model_1.fit(X_train_2d, Y_train_c, epochs=30, batch_size=200)
 model_2.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 model_2.fit(X_train_2d, Y_train_c, epochs=30, batch_size=200)
 model_3.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 model_3.fit(X_train_2d, Y_train_c, epochs=30, batch_size=200)
 scores1 = model_1.evaluate(X_test_2d, Y_test_c)
 scores2 = model_2.evaluate(X_test_2d, Y_test_c)
 scores3 = model_3.evaluate(X_test_2d, Y_test_c)
<train_on_grid>
train = train.dropna()
model_2= Sequential()
model_2.add(Conv2D(filters = 32, kernel_size =(5,5),activation ='relu', input_shape=(28,28,1)))
 model_2.add(MaxPooling2D(pool_size=(2, 2)))
 model_2.add(Dropout(0.4))
model_2.add(Conv2D(filters = 64, kernel_size =(5,5),activation ='relu'))
model_2.add(MaxPooling2D(pool_size=(2, 2)))
 model_2.add(Dropout(0.4))
model_2.add(Flatten())
 model_2.add(Dense(128, activation='relu'))
model_2.add(Dropout(0.4))
model_2.add(Dense(10, activation='softmax'))
model_2.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
 model_2.fit(X_train_2d, Y_train_c, epochs=30, batch_size=200 )<init_hyperparams>
train['rideDistance'] =(train['rideDistance']/10 ).round(0)
 train['swimDistance'] =(train['swimDistance']/10 ).round(0)
 train['walkDistance'] =(train['walkDistance']/10 ).round(0 )
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X_train_2d )<train_model>
test['rideDistance'] =(test['rideDistance']/10 ).round(0)
 test['swimDistance'] =(test['swimDistance']/10 ).round(0)
 test['walkDistance'] =(test['walkDistance']/10 ).round(0 )
history = model_2.fit_generator(datagen.flow(X_train_2d,Y_train_c, batch_size=200),
epochs = 25, validation_data =(X_test_2d,Y_test_c),
verbose = 2, steps_per_epoch=X_train.shape[0]/200)
<compute_test_metric>
test["winPlacePerc"] = -1
scores2 = model_2.evaluate(X_test_2d, Y_test_c)
 print(scores2)
<save_to_csv>
df = pd.concat([train, test] )
T = T_df.values
T = T/255
T = T.reshape(T.shape[0], 28, 28,1)
 results = model_2.predict(T)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("submission_kaggle_2D.csv",index=False )<set_options>
del train
del test
warnings.filterwarnings("ignore")
 sns.set(style='white', context='notebook', palette='deep')
 def binary_pred_stats(ytrue, ypred, threshold=0.5):
one_correct = np.sum(( ytrue==1)*(ypred > threshold))
zero_correct = np.sum(( ytrue==0)*(ypred <= threshold))
sensitivity = one_correct / np.sum(ytrue==1)
 specificity = zero_correct / np.sum(ytrue==0)
 accuracy =(one_correct + zero_correct)/ len(ytrue)
 return sensitivity, specificity, accuracy
def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):

 plt.imshow(cm, interpolation='nearest', cmap=cmap)
 plt.title(title)
 plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
 plt.yticks(tick_marks, classes)
 if normalize:
cm = cm.astype('float')/ cm.sum(axis=1)[:, np.newaxis]
thresh = cm.max() / 2.
 for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])) :
plt.text(j, i, cm[i, j],
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
 plt.tight_layout()
plt.ylabel('True label')
 plt.xlabel('Predicted label' )<load_from_csv>
df["Id"] = df.index
df_train = pd.read_csv(".. /input/digit-recognizer/train.csv")
 df_test = pd.read_csv(".. /input/digit-recognizer/test.csv")
<prepare_x_and_y>
df_train.info()
df_train.head()
Y_train = df_train['label']
X_train = df_train.drop(labels = ["label"],axis = 1)
 X_test = df_test
Y_train.hist()
Y_train.value_counts()
X_train = X_train.values.reshape(-1,28,28,1)
 X_test = X_test.values.reshape(-1,28,28,1)
 input_shape =(28,28,1)
 X_train = X_train / 255.0
X_test = X_test / 255.0
Y_train = to_categorical(Y_train, num_classes = 10)
 X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.15, random_state = 3)
 plt.figure()
g = plt.imshow(X_train[10][:,:,0])
 print(Y_train[10])
<choose_model_class>
duo = df[(df['matchType']=='duo')|(df['matchType']=='normal-duo')|(df['matchType']=='duo-fpp')|(df['matchType']=='normal-duo-fpp')]
squad = df[(df['matchType']=='squad')|(df['matchType']=='normal-squad')|(df['matchType']=='squad-fpp')|(df['matchType']=='normal-squad-fpp')]
solo = df[(df['matchType']=='solo')|(df['matchType']=='normal-solo')|(df['matchType']=='solo-fpp')|(df['matchType']=='normal-solo-fpp')]
flare = df[(df['matchType']=='flaretpp')|(df['matchType']=='flarefpp')]
crash = df[(df['matchType']=='crashtpp')|(df['matchType']=='crashfpp')]
model = Sequential()
model.add(Conv2D(filters = 128, kernel_size=(5, 5), activation='relu', padding='same', input_shape = input_shape))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 64, kernel_size=(5, 5), activation='relu', padding='same', input_shape = input_shape))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(64, kernel_size=(3, 3), padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64, kernel_size=(3, 3), padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(64, kernel_size=(3, 3), padding='same', activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.25))
model.add(Dense(10, activation='softmax'))
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
 model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])
 model.summary()<choose_model_class>
dp_by_type = {'flare':[flare,62],
'crash':[crash,149],
'squad':[squad,53635],
'solo':[solo,16165],
'duo':[duo,29691]
}
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001)
 epochs = 50
batch_size = 128
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.10,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(X_train)
<train_model>
for name,ele in dp_by_type.items() :
print(name + " : " + str(len(ele[0])))
 ele[0].to_csv(name+'.csv', index=False)
 ele[0] = 0
%%time

 estimator = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=batch_size),
epochs = epochs, validation_data =(X_val,Y_val),
verbose = 2, steps_per_epoch=X_train.shape[0] // batch_size
, callbacks=[learning_rate_reduction])
<predict_on_test>
del duo,squad,solo,flare,crash
predtrain = model.predict(X_train)
 sensitivity, specificity, accuracy = binary_pred_stats(Y_train, predtrain)
 print("train set:", sensitivity, specificity, accuracy)
 predtest = model.predict(X_val)
 sensitivity, specificity, accuracy = binary_pred_stats(Y_val, predtest)
 print("test set: ", sensitivity, specificity, accuracy )<save_to_csv>
del df
gc.collect()
results = model.predict(X_test)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label")
 submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission.to_csv("cnn_mnist_with_datagen.csv",index=False )<load_from_url>
def featureEngineering(df):
return featureEngineeringSecond(reduce_mem_usage(featureEngineeringFirst(df)) )
!wget --quiet https://raw.githubusercontent.com/tensorflow/models/master/official/nlp/bert/tokenization.py<import_modules>
def items(df):
df['items'] = df['heals'] + df['boosts']
return df
def survival(df):
df["survival"] = df["revives"] + df["boosts"] + df["heals"]
return df
def players_in_team(df):
agg = df.groupby(['groupId'] ).size().to_frame('players_in_team')
 return df.merge(agg, how='left', on=['groupId'])
 def total_distance(df):
df['total_distance'] = df['rideDistance'] + df['swimDistance'] + df['walkDistance']
return df
def total_time_by_distance(df):
df["total_time_by_distance"] = df["rideDistance"]/5+df["walkDistance"]+df["swimDistance"]*5
return df
def headshotKills_over_kills(df):
df['headshotKills_over_kills'] = df['headshotKills'] / df['kills']
df['headshotKills_over_kills'].fillna(0, inplace=True)
 return df
def teamwork(df):
df['teamwork'] = df['assists'] + df['revives']
return df
def total_items_acquired(df):
df['total_items_acquired'] = df["boosts"] + df["heals"] + df["weaponsAcquired"]
return df
def killPlace_over_maxPlace(df):
df['killPlace_over_maxPlace'] = df['killPlace'] / df['maxPlace']
df['killPlace_over_maxPlace'].fillna(0, inplace=True)
 df['killPlace_over_maxPlace'].replace(np.inf, 0, inplace=True)
 return df
def distance_over_heals(df):
df['walkDistance_over_heals'] = df['total_distance'] / df['heals']
df['walkDistance_over_heals'].fillna(0, inplace=True)
 df['walkDistance_over_heals'].replace(np.inf, 0, inplace=True)
 return df
def distance_over_kills(df):
df['walkDistance_over_kills'] = df['total_distance'] / df['kills']
df['walkDistance_over_kills'].fillna(0, inplace=True)
 df['walkDistance_over_kills'].replace(np.inf, 0, inplace=True)
 return df
def headshot_kill_rate(df):
df['headshot_kill_rate'] =(df['headshotKills']+1)/(df['kills']+1)
 return df
import tensorflow as tf
import numpy as np
import pandas as pd
import tensorflow.keras.backend as K
from tensorflow.keras.layers import Dense, Input, Bidirectional, SpatialDropout1D, Embedding, add, concatenate
from tensorflow.keras.layers import GRU, GlobalAveragePooling1D, LSTM, GlobalMaxPooling1D
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import Model
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, LearningRateScheduler
import tensorflow_hub as hub
import tokenization<load_from_csv>
def featureEngineeringFirst(df):
print(" Feature Engineering First started...")
 df = items(df)
 gc.collect()
df = survival(df)
 gc.collect()
df = players_in_team(df)
 gc.collect()
df = total_distance(df)
 gc.collect()
df = total_time_by_distance(df)
 gc.collect()
gc.collect()
df = teamwork(df)
 gc.collect()
df = total_items_acquired(df)
 gc.collect()
gc.collect()
gc.collect()
gc.collect()
gc.collect()
df = reduce_mem_usage(df)
 print(" Feature Engineering First fished ")
 return df
train = pd.read_csv('.. /input/preprocesseddata/train.csv')
 test = pd.read_csv('.. /input/preprocesseddata/test.csv')
 submission = pd.read_csv('.. /input/nlp-getting-started/sample_submission.csv' )<choose_model_class>
def min_by_team(df):
features = list(df.columns)
 features.remove('Id')
 features.remove('groupId')
 features.remove('matchId')
 agg = df.groupby(['matchId','groupId'])[features].min()
agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
return agg, agg_rank
def max_by_team(df):
features = list(df.columns)
 features.remove('Id')
 features.remove('groupId')
 features.remove('matchId')
 agg = df.groupby(['matchId', 'groupId'])[features].max()
agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
return agg, agg_rank
def sum_by_team(df):
features = list(df.columns)
 features.remove('Id')
 features.remove('groupId')
 features.remove('matchId')
 agg = df.groupby(['matchId', 'groupId'])[features].sum()
agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
return agg, agg_rank
def median_by_team(df):
features = list(df.columns)
 features.remove('Id')
 features.remove('groupId')
 features.remove('matchId')
 agg = df.groupby(['matchId', 'groupId'])[features].median()
agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
return agg, agg_rank
def mean_by_team(df):
features = list(df.columns)
 features.remove('Id')
 features.remove('groupId')
 features.remove('matchId')
 agg = df.groupby(['matchId', 'groupId'])[features].agg('mean')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
return agg, agg_rank
%%time
module_url = "https://tfhub.dev/tensorflow/bert_en_uncased_L-24_H-1024_A-16/1"
bert_layer = hub.KerasLayer(module_url, trainable=True )<categorify>
def mergeWithAgg(df,agg,agg_rank,name):
print(" Merge "+name)
 df = df.merge(agg, suffixes=["", "_"+name], how='left', on=['matchId', 'groupId'])
 df = df.merge(agg_rank, suffixes=["", "_"+name+"_rank"], how='left', on=['matchId', 'groupId'])
 return reduce_mem_usage(df )
def bert_encoder(texts, tokenizer, max_len=512):
all_tokens = []
all_masks = []
all_segments = []
for text in texts:
text = tokenizer.tokenize(text)
 text = text[:max_len-2]
input_sequence = ["[CLS]"] + text + ["[SEP]"]
pad_len = max_len - len(input_sequence)
 tokens = tokenizer.convert_tokens_to_ids(input_sequence)
 tokens += [0] * pad_len
pad_masks = [1] * len(input_sequence)+ [0] * pad_len
segments_ids = [0] * max_len
all_tokens.append(tokens)
 all_masks.append(pad_masks)
 all_segments.append(segments_ids)
 return np.array(all_tokens), np.array(all_masks), np.array(all_segments )<choose_model_class>
def featureEngineeringSecond(df):
print(" Feature Engineering Second started...")
 print(" Min")
 min_, min_rank = min_by_team(df)
 gc.collect()
print(" Max")
 max_, max_rank = max_by_team(df)
 gc.collect()
print(" Sum")
 sum_, sum_rank = sum_by_team(df)
 gc.collect()
print(" Median")
 median_, median_rank = median_by_team(df)
 gc.collect()
print(" Mean")
 mean_, mean_rank = mean_by_team(df)
 gc.collect()
df = mergeWithAgg(df,min_, min_rank,"min")
 del min_, min_rank
df = mergeWithAgg(df,max_, max_rank,"max")
 del max_, max_rank
df = mergeWithAgg(df,sum_, sum_rank,"sum")
 del sum_, sum_rank
df = mergeWithAgg(df,median_, median_rank,"median")
 del median_, median_rank
df = mergeWithAgg(df,mean_, mean_rank,"mean")
 del mean_, mean_rank
print(" Feature Engineering Second finished")
 return df
def build_model(bert_layer, max_len=512):
input_word_ids = Input(shape=(max_len,), dtype=tf.int32, name="input_word_ids")
 input_mask = Input(shape=(max_len,), dtype=tf.int32, name="input_mask")
 segment_ids = Input(shape=(max_len,), dtype=tf.int32, name="segment_ids")
 _, sequence_output = bert_layer([input_word_ids, input_mask, segment_ids])
 x = SpatialDropout1D(0.3 )(sequence_output)
 x = Bidirectional(GRU(LSTM_UNITS, return_sequences=True))(x)
 x = Bidirectional(GRU(LSTM_UNITS, return_sequences=True))(x)
 hidden = concatenate([GlobalMaxPooling1D()(x),GlobalAveragePooling1D()(x),])
 hidden = add([hidden, Dense(DENSE_HIDDEN_UNITS, activation='relu' )(hidden)])
 hidden = add([hidden, Dense(DENSE_HIDDEN_UNITS, activation='relu' )(hidden)])
 result = Dense(1, activation='sigmoid' )(hidden)
 model = Model(inputs=[input_word_ids, input_mask, segment_ids], outputs=result)
 model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
 return model<feature_engineering>
def baseline_model(input_dim):
model = Sequential()
model.add(Dense(32, kernel_initializer='he_normal',input_dim=input_dim , activation='selu'))
model.add(Dense(64, kernel_initializer='he_normal', activation='selu'))
model.add(Dense(128, kernel_initializer='he_normal', activation='selu'))
model.add(Dropout(0.1))
model.add(Dense(128, kernel_initializer='he_normal', activation='selu'))
model.add(BatchNormalization())
 model.add(Dense(64, kernel_initializer='he_normal', activation='selu'))
model.add(BatchNormalization())
 model.add(Dense(32, kernel_initializer='he_normal', activation='selu'))
model.add(Dense(8, kernel_initializer='he_normal', activation='selu'))
model.add(BatchNormalization())
 model.add(Dense(1, kernel_initializer='normal', activation='sigmoid'))
model.compile(optimizer='adam', loss='mse', metrics=['mae'])
 return model
vocab_file = bert_layer.resolved_object.vocab_file.asset_path.numpy()
do_lower_case = bert_layer.resolved_object.do_lower_case.numpy()
tokenizer = tokenization.FullTokenizer(vocab_file, do_lower_case )<categorify>
def normalize(df):
result = df.copy()
for feature_name in df.columns:
if df[feature_name].dtype != object:
max_value = df[feature_name].max()
min_value = df[feature_name].min()
result[feature_name] =(df[feature_name] - min_value)/(max_value - min_value)
 return result
train_input = bert_encoder(train.text.values, tokenizer, max_len=160)
 test_input = bert_encoder(test.text.values, tokenizer, max_len=160)
 train_labels = train.target.values<train_model>
def learningPart(df,batch_size):
print(" Data processing started...")
 df_winPlacePerc = df.winPlacePerc
df = df.drop('winPlacePerc', axis=1)
 df = df.drop('matchType', axis=1)
 df = featureEngineering(df)
 df = df.drop('matchId', axis=1)
 df = df.drop('groupId', axis=1)
 gc.collect()
df['winPlacePerc'] = df_winPlacePerc.values
del df_winPlacePerc
gc.collect()
print(" Data scaling started...")
 df = normalize(df)
 print(" Data scaling finised")
 train_df = df[df["winPlacePerc"] != -1]
train_df = train_df.dropna()
gc.collect()
test_df = df[df["winPlacePerc"] == -1]
test_df = test_df.drop('winPlacePerc', axis=1)
 del df
gc.collect()
train_df_Y = train_df.winPlacePerc
train_df_X = train_df.drop('winPlacePerc', axis=1)
 del train_df
print(" Data processing finished")
 print(" Data spliting started...")
 X_train_df, X_test_df, y_train_df, y_test_df = train_test_split(train_df_X, train_df_Y, test_size=0.2)
 del train_df_Y
del train_df_X
print(" Data spliting finished")
 print(" Model training started...")
 epochs = 80
callbacks = [ModelCheckpoint('best_model_df.h5', verbose=0, monitor='val_loss',save_best_only=True, mode='auto')]
model_df = baseline_model(X_train_df.shape[1])
 history_df = model_df.fit(X_train_df, y_train_df, batch_size=batch_size, epochs=epochs, verbose=0, validation_data=(X_test_df, y_test_df),callbacks=callbacks)
 gc.collect()
print(" Model training finished")
 print(" Prediction started...")
 best_model_df = load_model('best_model_df.h5')
 predict_y_df = best_model_df.predict(t_test_df)
 predict_y_df = pd.DataFrame({'Id':test_df['Id'].values,'winPlacePerc':predict_y_df.flatten() })
 print(" Prediction finished")
 return predict_y_df, history_df
NUM_MODELS = 1
BATCH_SIZE = 16
LSTM_UNITS = 64
EPOCHS = 10
DENSE_HIDDEN_UNITS = 256
checkpoint_predictions = []
checkpoint_val_pred = []
weights = []
for model_idx in range(NUM_MODELS):
model = build_model(bert_layer, max_len=160)
 for global_epoch in range(EPOCHS):
model.fit(
 train_input, train_labels,
batch_size=BATCH_SIZE,
validation_split=.25,
epochs=1,
verbose=1,
callbacks=[
LearningRateScheduler(lambda epoch: 2e-6 *(0.6** global_epoch))
] 
)
 checkpoint_predictions.append(model.predict(test_input, batch_size=64 ).flatten())
 weights.append(2 ** global_epoch)
 del model
gc.collect()<compute_test_metric>
def minmax(df):
df_minmax = pd.DataFrame()
for feature_name in df.columns:
v_min = df[feature_name].min()
v_max = df[feature_name].max()
df_minmax[feature_name] = pd.Series([v_min,v_max])
 return df_minmax
test_pred = np.average(checkpoint_predictions, weights=weights, axis=0 )<save_to_csv>
def learningPart(df,batch_size):
print(" Data processing started...")
 df = df.drop('matchType', axis=1)
 train_df = df[df["winPlacePerc"] != -1]
gc.collect()
test_df = df[df["winPlacePerc"] == -1]
test_df = test_df.drop('winPlacePerc', axis=1)
 del df
gc.collect()
train_df_Y = train_df.winPlacePerc
train_df_X = train_df.drop('winPlacePerc', axis=1)
 del train_df
train_df_X = featureEngineering(train_df_X)
 train_df_X = train_df_X.drop('matchId', axis=1)
 train_df_X = train_df_X.drop('groupId', axis=1)
 print(" Data processing finished")
 print(" Data spliting started...")
 X_train_df, X_test_df, y_train_df, y_test_df = train_test_split(train_df_X, train_df_Y, test_size=0.2)
 del train_df_Y
del train_df_X
gc.collect()
print(" Data spliting finished")
 print(" Data test processing started...")
 test_df = featureEngineering(test_df)
 test_df = test_df.drop('matchId', axis=1)
 test_df = test_df.drop('groupId', axis=1)
 print(" Data test processing finished")
 print(" Data scaling started...")
 minmaxv = pd.concat([minmax(test_df),minmax(X_train_df),minmax(X_test_df)])
 scaler_df = preprocessing.MinMaxScaler(feature_range=(-1, 1),copy=False ).fit(minmaxv)
 del minmaxv
gc.collect()
X_train_df = scaler_df.transform(X_train_df)
 gc.collect()
X_test_df = scaler_df.transform(X_test_df)
 gc.collect()
t_test_df = scaler_df.transform(test_df)
 print(" Data scaling finised")
 print(" Model training started...")
 epochs = 80
callbacks = [ModelCheckpoint('best_model_df.h5', verbose=0, monitor='val_loss',save_best_only=True, mode='auto')]
model_df = baseline_model(X_train_df.shape[1])
 history_df = model_df.fit(X_train_df, y_train_df, batch_size=batch_size, epochs=epochs, verbose=0, validation_data=(X_test_df, y_test_df),callbacks=callbacks)
 gc.collect()
print(" Model training finished")
 print(" Prediction started...")
 best_model_df = load_model('best_model_df.h5')
 predict_y_df = best_model_df.predict(t_test_df)
 predict_y_df = pd.DataFrame({'Id':test_df['Id'].values,'winPlacePerc':predict_y_df.flatten() })
 print(" Prediction finished")
 return predict_y_df, history_df
submission['target'] = test_pred.round().astype(int)
 submission.to_csv('submission.csv', index=False )<import_modules>
result = {}
for name,typeGame in dp_by_type.items() :
print("Start "+ name)
 predict, history = learningPart(pd.read_csv(name+".csv"),typeGame[1])
 result[name] = [predict,history]
print("End "+ name)
 dp_by_type[name] = 0
import random
import keras
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.patches as mpatches
from skimage.filters import threshold_otsu
from skimage.segmentation import clear_border
from skimage.measure import label, regionprops
from skimage.morphology import closing, square
from skimage.color import label2rgb
from math import sqrt
from sklearn.decomposition import PCA
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
import itertools
from keras.utils.np_utils import to_categorical
from keras.models import Sequential, Model
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, Input, BatchNormalization
from keras.optimizers import RMSprop, Adam
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ReduceLROnPlateau
from keras.callbacks import LearningRateScheduler
from keras.callbacks import ModelCheckpoint<load_from_csv>
data = []
for key,ele in result.items() :
data.append(ele[0])
train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')
 submission = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')
<prepare_x_and_y>
df = pd.concat(data, ignore_index=True)
 df = df.sort_values(by='Id', ascending=[True])
 df = df.reset_index(drop=True )
y_train = train['label']
X = train.drop(['label'],axis=1)
<create_dataframe>
submission = pd.read_csv(".. /input/sample_submission_V2.csv" )
labels =[]
frequencies = []
for i in range(len(y_train)) :
lab, freq = str(y_train[i]), len([n for n in X.values[i] if n > 0])
 labels.append(lab)
 frequencies.append(freq)
 data = {'Labels':labels, 'Frequencies':frequencies}
df = pd.DataFrame(data )<groupby>
submission["winPlacePerc"] = df.winPlacePerc
df.groupby('Labels' ).mean()<prepare_x_and_y>
submission.to_csv('submission.csv', index=False )
test_y = df['Labels']
test_x = df
test_x.drop('Labels', axis =1, inplace = True)
<split>
submission.to_csv('submission.csv', index=False )