Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
X_train1, X_val1, y_train1, y_val1 = train_test_split(test_x, test_y, test_size=0.1, random_state=1337 )<train_model>
df_train= pd.read_csv('.. /input/pubg-finish-placement-prediction/train_V2.csv')
 df_test= pd.read_csv('.. /input/pubg-finish-placement-prediction/test_V2.csv' )
clf_rfc = RandomForestClassifier(n_estimators = 100)
 clf_rfc.fit(X_train1,y_train1 )<compute_train_metric>
df_train['belongs']= 'train'
df_test['belongs']= 'test'
accuracy_score(clf_rfc.predict(X_val1), y_val1 )<train_model>
df= pd.concat([df_train.drop('winPlacePerc', axis= 1), df_test], axis= 0, ignore_index= True )
clf_knn = KNeighborsClassifier()
clf_knn.fit(X_train1,y_train1)
 accuracy_score(clf_knn.predict(X_val1), y_val1 )<train_model>
df['killRate']= df['kills']/ df['matchDuration']
imaginary_data_pca = pca.transform(imaginary_data)
 print("original shape: ", imaginary_data.shape)
 print("transformed shape:", imaginary_data_pca.shape )<categorify>
df['DBNORate']= df['DBNOs']/ df['matchDuration']
pca = PCA(n_components=2)
 pca.fit(imaginary_data)
 imaginary_data_pca = pca.transform(imaginary_data)
<categorify>
df['entryCount']= 1
imaginary_data_pca_new = pca.inverse_transform(imaginary_data_pca)
<create_dataframe>
df['total_players_match']= df.groupby(['matchId'])['entryCount'].transform(np.sum )
data = {'Labels':labels, 'diagonals':diags, 'widths':widths, 'heights':heights, 'Area':frequencies, 'PC1':principalComponents[:, 0],'PC2':principalComponents[:, 1]}
df = pd.DataFrame(data)
 df 
<find_best_model_class>
df['total_players_group']= df.groupby(['groupId'])['entryCount'].transform(np.sum )
test_y = df['Labels']
test_x = df
test_x.drop('Labels', axis =1, inplace = True)
 X_train1, X_val1, y_train1, y_val1 = train_test_split(test_x, test_y, test_size=0.1, random_state=1337)
 clf_knn = KNeighborsClassifier()
clf_knn.fit(X_train1,y_train1)
 accuracy_score(clf_knn.predict(X_val1), y_val1 )<train_model>
df['killPlacePerc']=(df['killPlace']/ df['total_players_match'] )
mbdl = MiniBatchDictionaryLearning(n_components = 2)
 mbdl.fit(X )<normalization>
df.loc[df['killPoints']== 0, 'killPoints']= 1
comps = mbdl.transform(X )<split>
df['maxKillPointsMatch']= df.groupby(['matchId'])['killPoints'].transform(np.max )
def test_model(d1,d1_lab, d2, d2_lab,data):
data = {'Labels':labels, 'diagonals':diags, 'widths':widths, 'heights':heights, 'Area':frequencies, 'PC1':principalComponents[:, 0],'PC2':principalComponents[:, 1]}
data[d1_lab] = d1
data[d2_lab] = d2
df = pd.DataFrame(data)
 print(df.columns)
 test_y = df['Labels']
test_x = df
test_x.drop('Labels', axis =1, inplace = True)
 X_train1, X_val1, y_train1, y_val1 = train_test_split(test_x, test_y, test_size=0.1, random_state=1337)
 clf_knn = KNeighborsClassifier()
clf_knn.fit(X_train1,y_train1)
 return accuracy_score(clf_knn.predict(X_val1), y_val1)
 test_model(comps[:, 0],'d1', comps[:, 1],'d2', data )<normalization>
df['maxKillPointsGroup']= df.groupby(['groupId'])['killPoints'].transform(np.max )
lda = LinearDiscriminantAnalysis(n_components = 2,)
 comps = lda.fit_transform(X,y_train.values )<train_model>
df['ratioMatchKillPoints']= df['killPoints']/ df['maxKillPointsMatch']
test_model(comps[:, 0],'lda1', comps[:, 1],'lda2', data )<feature_engineering>
df['ratioGroupKillPoints']= df['killPoints']/ df['maxKillPointsGroup']
X = X / 255.0
test = test / 255.0 
<train_model>
df['killPointsBuckets']= df['killPointsBuckets'].astype(np.int8 )
X = X.values.reshape(X.shape[0], 28, 28,1)
 test = test.values.reshape(test.shape[0], 28, 28,1)
<init_hyperparams>
df.loc[df['winPoints']== 0, 'winPoints']= 1
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)
 valid_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 )<categorify>
df['maxWinPointsMatch']= df.groupby(['matchId'])['winPoints'].transform(np.max )
y_train = to_categorical(y_train,num_classes=10)
<choose_model_class>
df['maxWinPointsGroup']= df.groupby(['groupId'])['winPoints'].transform(np.max )
learning_rate_reduction = ReduceLROnPlateau(monitor='accuracy',
patience=3,
verbose=10,
factor=0.5,
min_lr=0.00001)
<choose_model_class>
df['ratioMatchWinPoints']= df['winPoints']/ df['maxKillPointsMatch']
def build_model(input_shape=(28, 28, 1), classes = 10):
activation = 'relu'
padding = 'same'
gamma_initializer = 'uniform'
input_layer = Input(shape=input_shape)
 hidden=Conv2D(32,(3,3), padding=padding,activation = activation, name="conv1" )(input_layer)
 hidden=BatchNormalization(name="batch1" )(hidden)
 hidden=Conv2D(32,(3,3), padding=padding,activation = activation, name="conv2" )(hidden)
 hidden=BatchNormalization(name="batch2" )(hidden)
 hidden=Conv2D(32,(5,5), padding=padding,activation = activation, name="conv3" )(hidden)
 hidden=BatchNormalization(name="batch3" )(hidden)
 hidden=MaxPool2D(pool_size=2, padding=padding, name="max1" )(hidden)
 hidden=Dropout(0.4 )(hidden)
 hidden=Conv2D(64,(3,3), padding =padding, activation = activation, name="conv4" )(hidden)
 hidden=BatchNormalization(name = 'batch4' )(hidden)
 hidden=Conv2D(64,(3,3), padding =padding, activation = activation, name="conv45" )(hidden)
 hidden=BatchNormalization(name = 'batch5' )(hidden)
 hidden=Conv2D(64,(5,5), padding =padding, activation = activation, name="conv6" )(hidden)
 hidden=BatchNormalization(name = 'batch6' )(hidden)
 hidden=MaxPool2D(pool_size=2, padding="same", name="max2" )(hidden)
 hidden=Dropout(0.4 )(hidden)
 hidden=Flatten()(hidden)
 hidden=Dense(264,activation = activation, name="Dense1" )(hidden)
 hidden=Dropout(0.3 )(hidden)
 output = Dense(classes, activation = "softmax" )(hidden)
 model = Model(inputs=input_layer, outputs=output)
 return model<define_variables>
df['ratioGroupWinPoints']= df['winPoints']/ df['maxKillPointsGroup']
epochs = 50
initial_learningrate=2e-3
batch_size = 264<choose_model_class>
df['winPointsBuckets']= df['winPointsBuckets'].astype(np.int8 )
optimizer = Adam(learning_rate=initial_learningrate)
 model = build_model(input_shape=(28, 28, 1), classes = 10)
 model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"])
<split>
df['killPointsSumMatch']= df.groupby(['matchId'])['killPoints'].transform(np.sum )
X_train, X_val, y_train, y_val = train_test_split(X, y_train, test_size=0.1, random_state=1337)
 datagen.fit(X_train)
 valid_datagen.fit(X_val)
<train_model>
df['killPointsSumGroup']= df.groupby(['groupId'])['killPoints'].transform(np.sum )
callbacks = [learning_rate_reduction]
history = model.fit_generator(datagen.flow(X_train,y_train),
epochs = epochs,
validation_data=valid_datagen.flow(X_val,y_val),
verbose = 1,
callbacks = callbacks)
<prepare_output>
df['ratioKillPointsGroupAndMatch']= df['killPointsSumGroup']/ df['killPointsSumMatch']
y_pre_test=model.predict(X_val)
 y_pre_test=np.argmax(y_pre_test,axis=1)
 y_test=np.argmax(y_val,axis=1)
 conf=confusion_matrix(y_test,y_pre_test)
 conf=pd.DataFrame(conf,index=range(0,10),columns=range(0,10)) 
<train_model>
df['avgKillPointsGroup']= df.groupby(['matchId'])['killPoints'].transform(np.mean )
print('out of {} samples, we got {} incorrect'.format(len(X_train), round(len(X_train)- history.history['accuracy'][-1] * len(X_train)))) 
<predict_on_test>
df['avgKillPointsMatch']= df.groupby(['groupId'])['killPoints'].transform(np.mean )
predictions = model.predict(test )<prepare_output>
df['ratioAvgKillPointsGroupAndMatch']= df['avgKillPointsGroup']/ df['avgKillPointsMatch']
predictions = predictions.argmax(axis = -1)
 predictions<prepare_output>
df['groupRevived']= df.groupby(['groupId'])['revives'].transform(np.sum )
submission['Label'] = predictions<save_to_csv>
df['groupTeamKills']= df.groupby(['groupId'])['teamKills'].transform(np.sum )
submission.to_csv('submission.csv',index=False )<load_from_csv>
df['avgSpeed']=(df['walkDistance']+ df['swimDistance']+ df['rideDistance'])/ df['matchDuration']
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>
le= LabelEncoder()
le.fit(df['matchType'] )
X = train.drop(['label'], axis = 1 ).values/255
Y = train['label'].values
X_valid = test.values/255
X = X.reshape(X.shape[0],28,28,1)
 X_valid = X_valid.reshape(X_valid.shape[0],28,28,1)
<split>
df['matchTypeLabels']= le.fit_transform(df['matchType'] )
X_train, X_dev, Y_train,Y_dev = train_test_split(X,Y,test_size = 0.2 )<choose_model_class>
df.drop(['entryCount', 'groupId', 'matchId', 'matchType', 'DBNOs', 'winPoints', 'killPoints', 'killStreaks', 'maxKillPointsGroup', 'revives', 'headshotKills', 'teamKills', 'roadKills', 'vehicleDestroys'], axis= 1, inplace= True )
f = 2
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(f*16,kernel_size =(3,3), padding = 'same',activation='relu',
kernel_initializer='he_uniform',
input_shape =(28,28,1)) ,
tf.keras.layers.Conv2D(f*16,(3,3), activation = "relu", padding = 'same'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.MaxPooling2D() ,
tf.keras.layers.Conv2D(f*32, kernel_size =(3,3), padding = 'same',activation='relu'),
tf.keras.layers.Conv2D(f*32,(3,3), activation = "relu", padding = 'same'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.MaxPooling2D() ,
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Conv2D(f*64, kernel_size =(3,3), padding = 'same',activation='relu'),
tf.keras.layers.Conv2D(f*64,(3,3), activation = "relu", padding = 'same'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.MaxPooling2D() ,
tf.keras.layers.Dropout(0.125),
keras.layers.Flatten() ,
keras.layers.Dense(512, activation = 'relu'),
keras.layers.Dense(128, activation = 'relu'),
keras.layers.Dense(10, activation='softmax')
 ])
 model.summary()<set_options>
df.set_index('Id', inplace= True )
from keras.utils import plot_model 
<choose_model_class>
df_train= df[df['belongs']== 'train']
model.compile(optimizer=Adam(learning_rate=0.0003),
loss = 'sparse_categorical_crossentropy',
metrics = ['accuracy'] )<init_hyperparams>
df_test= df[df['belongs']== 'test']
train_datagen = ImageDataGenerator(rotation_range=10,
width_shift_range=0.15,
height_shift_range=0.15,
shear_range=0.05,
zoom_range=0.15,
horizontal_flip=False)
 valid_datagen = ImageDataGenerator(horizontal_flip=False, 
)
 callback = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=5)
 def lr_decay(epoch, initial_learningrate = 0.0003):
return initial_learningrate * 0.99 ** epoch
learning_rate_reduction = tf.keras.callbacks.ReduceLROnPlateau(monitor='val_acc',
patience=300,
verbose=1,
factor=0.5,
min_lr=0.00001)
 batchsize = 512*2
epoch = 45
history = model.fit_generator(train_datagen.flow(X, Y, batch_size = batchsize),
steps_per_epoch = 100,
epochs = epoch,
callbacks=[
LearningRateScheduler(lr_decay),
callback],
validation_data=valid_datagen.flow(X_dev, Y_dev),
validation_steps=50,
)<save_to_csv>
df_train.drop('belongs', axis= 1, inplace= True )
yhat = model.predict_classes(X_valid)
 submission['Label']=pd.Series(yhat)
 submission.to_csv('submission.csv',index=False )<set_options>
df_test.drop('belongs', axis= 1, inplace= True )
warnings.filterwarnings("ignore")
 %matplotlib inline<drop_column>
df_train['winPlacePerc']= pd.read_csv('.. /input/pubg-finish-placement-prediction/train_V2.csv', usecols= ['winPlacePerc'] )
train = train.drop(train[(train.GrLivArea > 4000)&(train.SalePrice < 300000)].index )<drop_column>
df_train.set_index('Id', inplace= True )
train_ID = train["Id"]
test_ID = test["Id"]
train.drop("Id", axis=1, inplace=True)
 test.drop("Id", axis=1, inplace=True )<feature_engineering>
df_train.dropna(inplace= True )
train.SalePrice = np.log1p(train.SalePrice )<prepare_x_and_y>
X_train, X_test, y_train, y_test= train_test_split(df_train.drop(['winPlacePerc'], axis= 1), df_train['winPlacePerc'], test_size= 0.3 )
ntrain = train.shape[0]
ntest = test.shape[0]
y_train = train.SalePrice.values
all_data = pd.concat(( train, test)).reset_index(drop=True)
 all_data.drop(['SalePrice'], axis=1, inplace=True)
 print("all_data size is : {}".format(all_data.shape))<create_dataframe>
model= XGBRegressor(n_estimators= 500, max_depth= 7, n_jobs= -1, min_child_weight= 7, subsample=0.84, colsample_bytree= 0.97, eta=0.3, seed=42 )
total_na = all_data.isnull().sum()
total_na = total_na.drop(total_na[total_na == 0].index ).sort_values(ascending=False)
 missing = pd.DataFrame({"Total Missing data": total_na})
 missing.head(20 )<feature_engineering>
model.fit(
 X_train,
y_train,
eval_metric="rmse",
eval_set=[(X_train, y_train),(X_test, y_test)],
verbose=True,
early_stopping_rounds = 10 )
<categorify><EOS> 
predictions= pd.DataFrame({'winPlacePerc': model.predict(df_test ).clip(0,1)}, index= df_test.index)
 predictions.to_csv('submission_1.csv' )