Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
total['Fare'].fillna(value = total[total.Pclass==3]['Fare'].median() , inplace = True )<feature_engineering>
def missing_values_table(df):
mis_val = df.isnull().sum()
mis_val_percent = 100 * df.isnull().sum() / len(df)
 mis_val_table = pd.concat([mis_val, mis_val_percent], axis=1)
 mis_val_table_ren_columns = mis_val_table.rename(
 columns = {0 : 'Missing Values', 1 : '% of Total Values'})
 mis_val_table_ren_columns = mis_val_table_ren_columns[
mis_val_table_ren_columns.iloc[:,1] != 0].sort_values(
 '% of Total Values', ascending=False ).round(1)
 print("Your selected dataframe has " + str(df.shape[1])+ " columns.
 "
"There are " + str(mis_val_table_ren_columns.shape[0])+
" columns that have missing values.")
 return mis_val_table_ren_columns
titles = list(total.Title.unique())
 for title in titles:
age = total.groupby('Title')['Age'].median().loc[title]
total.loc[(total.Age.isnull())&(total.Title == title),'Age'] = age<feature_engineering>
missing_values = missing_values_table(traindata)
 missing_values
traindata[traindata['winPlacePerc'].isnull() ]
traindata.drop(2744604, inplace= True )
total['Family_Size'] = total['Parch'] + total['SibSp']<feature_engineering>
traindata = pd.get_dummies(traindata, columns = ['matchType'] )
total['Last_Name'] = total['Name'].apply(lambda x: str.split(x, ",")[0])
 total['Fare'].fillna(total['Fare'].mean() , inplace=True)
 default_survival_rate = 0.5
total['Family_Survival'] = default_survival_rate
for grp, grp_df in total[['Survived','Name', 'Last_Name', 'Fare', 'Ticket', 'PassengerId',
'SibSp', 'Parch', 'Age', 'Cabin']].groupby(['Last_Name', 'Fare']):
if(len(grp_df)!= 1):
for ind, row in grp_df.iterrows() :
smax = grp_df.drop(ind)['Survived'].max()
smin = grp_df.drop(ind)['Survived'].min()
passID = row['PassengerId']
if(smax == 1.0):
total.loc[total['PassengerId'] == passID, 'Family_Survival'] = 1
elif(smin==0.0):
total.loc[total['PassengerId'] == passID, 'Family_Survival'] = 0
print("Number of passengers with family survival information:",
total.loc[total['Family_Survival']!=0.5].shape[0] )<groupby>
matchType_encoding = traindata.filter(regex='matchType')
 matchType_encoding.head()
for _, grp_df in total.groupby('Ticket'):
if(len(grp_df)!= 1):
for ind, row in grp_df.iterrows() :
if(row['Family_Survival'] == 0)|(row['Family_Survival']== 0.5):
smax = grp_df.drop(ind)['Survived'].max()
smin = grp_df.drop(ind)['Survived'].min()
passID = row['PassengerId']
if(smax == 1.0):
total.loc[total['PassengerId'] == passID, 'Family_Survival'] = 1
elif(smin==0.0):
total.loc[total['PassengerId'] == passID, 'Family_Survival'] = 0
print("Number of passenger with family/group survival information: "
+str(total[total['Family_Survival']!=0.5].shape[0])) 
<categorify>
traindata['groupId'] = traindata['groupId'].astype('category')
 traindata['matchId'] = traindata['matchId'].astype('category')
 traindata['groupId_cat'] = traindata['groupId'].cat.codes
traindata['matchId_cat'] = traindata['matchId'].cat.codes
traindata.drop(columns=['groupId', 'matchId'], inplace=True)
 traindata[['groupId_cat', 'matchId_cat']].head()
total.Sex.replace({'male':0, 'female':1}, inplace = True)
 features = ['Survived','Pclass','Sex','Family_Size','Family_Survival','Fare_Bin','Age_Bin']
total = total[features]<prepare_x_and_y>
traindata.drop(columns = ['Id'], inplace=True )
train = total[:train_len]
x_train = train.drop(columns = ['Survived'])
 y_train = train['Survived'].astype(int)
 x_test = total[train_len:].drop(columns = ['Survived'] )<normalization>
traindata['totalDistance'] = traindata['walkDistance'] + traindata['rideDistance'] + traindata['swimDistance']
traindata['headshotRate'] = traindata['headshotKills']/traindata['kills']
traindata['headshotRate'] = traindata['headshotRate'].fillna(0)
 traindata['playersJoined'] = traindata.groupby('matchId_cat')['matchId_cat'].transform('count')
 traindata['killsNorm'] = traindata['kills']*(( 100-traindata['playersJoined'])/100 + 1)
 traindata['damageDealtNorm'] = traindata['damageDealt']*(( 100-traindata['playersJoined'])/100 + 1)
 traindata['maxPlaceNorm'] = traindata['maxPlace']*(( 100-traindata['playersJoined'])/100 + 1)
 traindata['matchDurationNorm'] = traindata['matchDuration']*(( 100-traindata['playersJoined'])/100 + 1)
 traindata['healsandboosts'] = traindata['heals'] + traindata['boosts']
traindata['killsWithoutMoving'] =(( traindata['kills'] > 0)&(traindata['totalDistance'] == 0))
scaler = StandardScaler()
scaler.fit(x_train)
 x_train = scaler.transform(x_train)
 x_test = scaler.transform(x_test )<train_on_grid>
traindata.drop(traindata[traindata['killsWithoutMoving'] == True].index, inplace=True )
clf = KNeighborsClassifier()
params = {'n_neighbors':[6,8,10,12,14,16,18,20],
'leaf_size':list(range(1,50,5)) }
gs = GridSearchCV(clf, param_grid= params, cv = 5,scoring = "roc_auc",verbose=1)
 gs.fit(x_train, y_train)
 print(gs.best_score_)
 print(gs.best_estimator_)
 print(gs.best_params_ )<save_to_csv>
traindata.drop(traindata[traindata['roadKills'] > 10].index, inplace=True )
preds = gs.predict(x_test)
 pd.DataFrame({'PassengerId': test_copy['PassengerId'], 'Survived': preds} ).to_csv('submission.csv', index = False)
<load_from_csv>
traindata.drop(traindata[traindata['kills'] > 30].index, inplace=True )
train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv');
test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv');<prepare_x_and_y>
traindata.drop(traindata[traindata['longestKill'] >= 1000].index, inplace=True)
 traindata.drop(traindata[traindata['walkDistance'] >= 10000].index, inplace=True)
 traindata.drop(traindata[traindata['rideDistance'] >= 20000].index, inplace=True)
 traindata.drop(traindata[traindata['swimDistance'] >= 2000].index, inplace=True)
 traindata.drop(traindata[traindata['weaponsAcquired'] >= 80].index, inplace=True)
 traindata.drop(traindata[traindata['heals'] >= 40].index, inplace=True )
rows = 28
cols = 28
tot_rows = train.shape[0]
X_train = train.values[:,1:]
y_train = keras.utils.to_categorical(train.label, 10)
 X_train = X_train.reshape(tot_rows, rows, cols, 1)/255.0
X_test = test.values[:]
test_num_img = test.shape[0]
X_test = X_test.reshape(test_num_img, rows, cols, 1)/255.0<choose_model_class>
df = df_sample.drop(columns = ['winPlacePerc'])
 y = df_sample['winPlacePerc']
classifier = Sequential()
classifier.add(Conv2D(32,(5,5),input_shape=(28,28,1),activation = 'relu',padding='same'))
classifier.add(BatchNormalization())
 classifier.add(Conv2D(32,(3,3),activation = 'relu',padding='same'))
classifier.add(BatchNormalization())
 classifier.add(MaxPooling2D(pool_size=(2,2), strides=None))
classifier.add(BatchNormalization())
 classifier.add(Dropout(0.25))
classifier.add(Conv2D(64,(5,5),activation = 'relu',padding='same'))
classifier.add(BatchNormalization())
 classifier.add(Conv2D(64,(3,3),activation = 'relu',padding='same'))
classifier.add(BatchNormalization())
 classifier.add(Conv2D(64,(3,3),strides=(2,2),activation = 'relu',padding='same'))
classifier.add(BatchNormalization())
 classifier.add(Dropout(0.25))
classifier.add(Flatten())
 classifier.add(Dense(units=128,activation='relu'))
classifier.add(Dropout(0.4))
classifier.add(Dense(units=10,activation='softmax'))<train_model>
X_train, X_test, y_train, y_test = train_test_split(df, y, test_size=0.12, random_state=42)
classifier.compile(optimizer='adam',loss = 'binary_crossentropy',metrics=['accuracy'])
 classifier.fit(X_train,y_train,epochs=100,batch_size=64,validation_split=0.1,shuffle=True )<predict_on_test>
def print_score(m : RandomForestRegressor):
res = ['mae train: ', mean_absolute_error(m.predict(X_train), y_train),
'mae val: ', mean_absolute_error(m.predict(X_test), y_test)]
if hasattr(m, 'oob_score_'): res.append(m.oob_score_)
 print(res )
result = classifier.predict_classes(X_test )<save_to_csv>
m1 = RandomForestRegressor(n_estimators=40, min_samples_leaf=3, max_features='sqrt',
n_jobs=-1)
 m1.fit(X_train, y_train)
 print_score(m1 )
out = pd.DataFrame({"ImageId": i+1 , "Label": result[i]} for i in range(0, test_num_img))
out.to_csv('submission.csv', index=False )<load_from_url>
to_keep = fi[fi.imp>0.005].cols
print('Significant features: ', len(to_keep))
to_keep
!wget --quiet https://raw.githubusercontent.com/tensorflow/models/master/official/nlp/bert/tokenization.py<import_modules>
df_keep = df[to_keep].copy()
X_train, X_test, y_train, y_test = train_test_split(df, y, test_size=0.12, random_state=42)
import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow.keras.layers import Dense, Input
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import Model
from tensorflow.keras.callbacks import ModelCheckpoint
import tensorflow_hub as hub
import tokenization
from sklearn.model_selection import train_test_split
from sklearn.model_selection import GridSearchCV
from sklearn.svm import SVC
from sklearn.metrics import confusion_matrix, accuracy_score, precision_score, recall_score<categorify>
m2 = RandomForestRegressor(n_estimators=80, min_samples_leaf=3, max_features='sqrt',
n_jobs=-1)
 m2.fit(X_train, y_train)
 print_score(m2 )
def bert_encode(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
segment_ids = [0] * max_len
all_tokens.append(tokens)
 all_masks.append(pad_masks)
 all_segments.append(segment_ids)
 return np.array(all_tokens), np.array(all_masks), np.array(all_segments )<choose_model_class>
corr = np.round(scipy.stats.spearmanr(df_keep ).correlation, 4)
 corr_condensed = hc.distance.squareform(1-corr)
 z = hc.linkage(corr_condensed, method='average')
 fig = plt.figure(figsize=(14,10))
dendrogram = hc.dendrogram(z, labels=df_keep.columns, orientation='left', leaf_font_size=16)
 plt.plot()
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])
 clf_output = sequence_output[:, 0, :]
out = Dense(1, activation='sigmoid' )(clf_output)
 model = Model(inputs=[input_word_ids, input_mask, segment_ids], outputs=out)
 model.compile(Adam(lr=1e-5), loss='binary_crossentropy', metrics=['accuracy'])
 return model<choose_model_class>
val_perc_full = 0.12
n_valid_full = int(val_perc_full * len(traindata))
n_trn_full = len(traindata)-n_valid_full
df_full = traindata.drop(columns = ['winPlacePerc'])
 y = traindata['winPlacePerc']
df_full = df_full[to_keep]
X_train, X_test, y_train, y_test = train_test_split(df_full, y, test_size=0.12, random_state=42)
 print('Sample train shape: ', X_train.shape,
'Sample target shape: ', y_train.shape,
'Sample validation shape: ', X_test.shape )
%%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 )<load_from_csv>
m3 = RandomForestRegressor(n_estimators=70, min_samples_leaf=3, max_features=0.5,
n_jobs=-1)
 m3.fit(X_train, y_train)
 print_score(m3 )
train = pd.read_csv("/kaggle/input/nlp-getting-started/train.csv")
 test = pd.read_csv("/kaggle/input/nlp-getting-started/test.csv")
 submission = pd.read_csv("/kaggle/input/nlp-getting-started/sample_submission.csv" )<feature_engineering>
testdata['totalDistance'] = testdata['walkDistance'] + testdata['rideDistance'] + testdata['swimDistance']
testdata['headshotRate'] = testdata['headshotKills']/testdata['kills']
testdata['headshotRate'] = testdata['headshotRate'].fillna(0)
 testdata['playersJoined'] = testdata.groupby('matchId')['matchId'].transform('count')
 testdata['killsNorm'] = testdata['kills']*(( 100-testdata['playersJoined'])/100 + 1)
 testdata['damageDealtNorm'] = testdata['damageDealt']*(( 100-testdata['playersJoined'])/100 + 1)
 testdata['maxPlaceNorm'] = testdata['maxPlace']*(( 100-testdata['playersJoined'])/100 + 1)
 testdata['matchDurationNorm'] = testdata['matchDuration']*(( 100-testdata['playersJoined'])/100 + 1)
 testdata['healsandboosts'] = testdata['heals'] + testdata['boosts']
testdata['killsWithoutMoving'] =(( testdata['kills'] > 0)&(testdata['totalDistance'] == 0))
testdata['groupId'] = testdata['groupId'].astype('category')
 testdata['matchId'] = testdata['matchId'].astype('category')
 testdata['groupId_cat'] = testdata['groupId'].cat.codes
testdata['matchId_cat'] = testdata['matchId'].cat.codes
test_pred = testdata[to_keep].copy()
test_pred.fillna(0, inplace=True)
 test_pred.head()
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>
predictions = np.clip(a = m3.predict(test_pred), a_min = 0.0, a_max = 1.0)
 pred_df = pd.DataFrame({'Id' : testdata['Id'], 'winPlacePerc' : predictions})
 pred_df.to_csv("submission.csv", index=False )