Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
X_train = X_train[:,:,:,None]
X_test = X_test[:,:,:,None]<define_variables>
warnings.filterwarnings(action='ignore')
 plt.style.use('fivethirtyeight')
 gc.enable()
batch_size = 32
num_samples = X_train.shape[0]
num_classes = np.unique(y_train ).shape[0]
num_epochs = 50
img_rows, img_cols = X_train[0,:,:,0].shape
img_channels = 1
classes = np.unique(y_train )<categorify>
%time
train_d=dd.read_csv('.. /input/pubg-finish-placement-prediction/train_V2.csv',blocksize=25e6 )
y_train = np_utils.to_categorical(y_train, num_classes)
<data_type_conversions>
train=pd.read_csv('.. /input/pubg-finish-placement-prediction/train_V2.csv',nrows=100000)
 test=pd.read_csv(".. /input/pubg-finish-placement-prediction/test_V2.csv",)
 test_id=test['Id']
X_train_norm = X_train.astype('float32')
 X_test_norm = X_test.astype('float32')
 X_train_norm /= 255
X_test_norm /= 255<choose_model_class>
train.isnull().sum()
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=(28, 28, 1)))
 model.add(Conv2D(64,(3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'] )<choose_model_class>
train.isnull().sum()
learning_rate_reduction = ReduceLROnPlateau(monitor='val_loss',
patience=5,
verbose=1,
factor=0.2)
 es = EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=10 )<train_model>
train[train['groupId']==2]['winPlacePerc']
history = model.fit(
 X_train_norm,
y_train,
batch_size=batch_size,
epochs=num_epochs,
validation_split=0.1,
shuffle=True,
callbacks=[learning_rate_reduction, es]
)<set_options>
train_corr=train.drop(columns=['Id',"matchId","groupId"] )
! mkdir newer<save_model>
train_dbno=pd.DataFrame(train['DBNOs'].value_counts() ,columns=['DBNOs'])
 dbno=train_dbno.iloc[:9,:]
dbno.iloc[8]['DBNOs']=train_dbno.iloc[8:,:].sum() ['DBNOs']
model.save('newer/simple.h5' )<choose_model_class>
set1=set(i for i in train[(train['kills']>40)&(train['heals']==0)].index.tolist() )
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=(28, 28, 1)))
 model.add(BatchNormalization())
 model.add(Conv2D(64,(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'] )<train_model>
set2=set(i for i in train[(train['move']==0)&(train['kills']>20)].index.tolist() )
history1 = model.fit(
 X_train_norm,
y_train,
batch_size=batch_size,
epochs=num_epochs,
validation_split=0.1,
shuffle=True,
callbacks=[learning_rate_reduction, es]
)<save_model>
set3=set(i for i in train[(train['damageDealt']>4000)&(train['heals']<2)].index.tolist() )
model.save('newer/simple_batch.h5' )<choose_model_class>
set4=set(i for i in train[(train['rideDistance']>25000)&(train['roadkillsrate']>.4)].index.tolist() )
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=(28, 28, 1)))
 model.add(BatchNormalization())
 model.add(Conv2D(64,(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(64, kernel_size=(3, 3),
activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(128,(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'] )<train_model>
set5=set(i for i in train[(train['killStreaks']>3)&(train['weaponsAcquired']> 30)].index.tolist() )
history2 = model.fit(
 X_train_norm,
y_train,
batch_size=batch_size,
epochs=num_epochs,
validation_split=0.1,
shuffle=True,
callbacks=[learning_rate_reduction, es]
)<save_model>
set6=set(i for i in train[(train['killStreaks']>3)&(train['weaponsAcquired']> 30)].index.tolist() )
model.save('newer/32x64_64x128.h5' )<choose_model_class>
sets=set1 | set2 | set3 | set4 | set5 | set6
train.drop(train.index[list(sets)],inplace=True )
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=(28, 28, 1)))
 model.add(BatchNormalization())
 model.add(Conv2D(64,(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64,(5, 5), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'] )<train_model>
train.drop(train.index[list(sets)],inplace=True )
history3 = model.fit(
 X_train_norm,
y_train,
batch_size=batch_size,
epochs=num_epochs,
validation_split=0.1,
shuffle=True,
callbacks=[learning_rate_reduction, es]
)<save_model>
train.drop(['roadkillsrate'],axis=1,inplace=True)
 train.columns.drop('winPlacePerc')==test.columns
model.save('newer/32x64x64.h5' )<choose_model_class>
def new_features(df):
df['items']=df['boosts']+df['heals']
df['headshotKills_over_kills'] = df['headshotKills'] / df['kills']
df['headshotKills_over_kills'].fillna(0, inplace=True)
 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)
 df['walkDistance_over_heals'] = df['walkDistance'] / df['heals']
df['walkDistance_over_heals'].fillna(0, inplace=True)
 df['walkDistance_over_heals'].replace(np.inf, 0, inplace=True)
 df['walkDistance_over_kills'] = df['walkDistance'] / df['kills']
df['walkDistance_over_kills'].fillna(0, inplace=True)
 df['walkDistance_over_kills'].replace(np.inf, 0, inplace=True)
 df['teamwork'] = df['assists'] + df['revives']
return df
model = Sequential()
model.add(Conv2D(64, kernel_size=(3, 3),
activation='relu',
input_shape=(28, 28, 1)))
 model.add(BatchNormalization())
 model.add(Conv2D(128,(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'] )<train_model>
train=new_features(train)
 test=new_features(test )
history4 = model.fit(
 X_train_norm,
y_train,
batch_size=batch_size,
epochs=num_epochs,
validation_split=0.1,
shuffle=True,
callbacks=[learning_rate_reduction, es]
)<save_model>
def feature_engineering(is_train=True):
if is_train:
print("processing train.csv")
 df = train
else:
print("processing test.csv")
 df = test
target = 'winPlacePerc'
features = list(df.columns)
 features.remove("Id")
 features.remove("matchId")
 features.remove("groupId")
 features.remove('matchType')
 y = None
if is_train:
y = np.array(df.groupby(['matchId','groupId'])[target].agg('mean'), dtype=np.float64)
 features.remove(target)
 print("get group mean feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('mean')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
if is_train: df_out = agg.reset_index() [['matchId','groupId']]
else: df_out = df[['matchId','groupId']]
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_mean", "_mean_rank"], how='left', on=['matchId', 'groupId'])
 print("get group max feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('max')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_max", "_max_rank"], how='left', on=['matchId', 'groupId'])
 print("get group min feature")
 agg = df.groupby(['matchId','groupId'])[features].agg('min')
 agg_rank = agg.groupby('matchId')[features].rank(pct=True ).reset_index()
df_out = df_out.merge(agg.reset_index() , suffixes=["", ""], how='left', on=['matchId', 'groupId'])
 df_out = df_out.merge(agg_rank, suffixes=["_min", "_min_rank"], how='left', on=['matchId', 'groupId'])
 print("get group size feature")
 agg = df.groupby(['matchId','groupId'] ).size().reset_index(name='group_size')
 df_out = df_out.merge(agg, how='left', on=['matchId', 'groupId'])
 print("get match mean feature")
 agg = df.groupby(['matchId'])[features].agg('mean' ).reset_index()
df_out = df_out.merge(agg, suffixes=["", "_match_mean"], how='left', on=['matchId'])
 print("get match size feature")
 agg = df.groupby(['matchId'] ).size().reset_index(name='match_size')
 df_out = df_out.merge(agg, how='left', on=['matchId'])
 df_out.drop(["matchId", "groupId"], axis=1, inplace=True)
 X = np.array(df_out, dtype=np.float64)
 del df, df_out, agg, agg_rank
gc.collect()
return X, y
x_train, y = feature_engineering(True )
model.save('newer/64x128.h5' )<choose_model_class>
test,z=feature_engineering(False )
model = Sequential()
model.add(Conv2D(64, kernel_size=(3, 3),
activation='relu',
input_shape=(28, 28, 1)))
 model.add(BatchNormalization())
 model.add(Conv2D(128,(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(256, kernel_size=(3, 3),
activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(512,(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'] )<train_model>
scaler=MinMaxScaler(feature_range=(-1,1),copy=False ).fit(x_train)
 x_train=scaler.transform(x_train )
history5 = model.fit(
 X_train_norm,
y_train,
batch_size=batch_size,
epochs=num_epochs,
validation_split=0.1,
shuffle=True,
callbacks=[learning_rate_reduction, es]
)<save_model>
x_test=scaler.transform(test )
model.save('newer/64x128_256x512.h5' )<choose_model_class>
from xgboost.sklearn import XGBRegressor
from sklearn.model_selection import cross_val_score,KFold
import numpy as np
from sklearn.pipeline import make_pipeline
from sklearn.linear_model import Lasso,LinearRegression
from sklearn import preprocessing
from sklearn.model_selection import RandomizedSearchCV,train_test_split
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.base import BaseEstimator, TransformerMixin, RegressorMixin, clone
model = Sequential()
model.add(Conv2D(64, kernel_size=(3, 3),
activation='relu',
input_shape=(28, 28, 1)))
 model.add(BatchNormalization())
 model.add(Conv2D(128,(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(256, kernel_size=(3, 3),
activation='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(512,(3, 3), activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(512))
model.add(BatchNormalization())
 model.add(Dense(256))
model.add(BatchNormalization())
 model.add(Dense(128))
model.add(BatchNormalization())
 model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'] )<train_model>
n_folds=2
def mbs(model):
kf=KFold(n_folds,shuffle=True,random_state=42 ).get_n_splits(x_train)
 score=-cross_val_score(model,x_train,y,scoring='neg_mean_absolute_error',cv=kf)
 return score
history6 = model.fit(
 X_train_norm,
y_train,
batch_size=batch_size,
epochs=num_epochs,
validation_split=0.1,
shuffle=True,
callbacks=[learning_rate_reduction, es]
)<save_model>
lasso=Lasso(alpha=.005,random_state=1 )
model.save('newer/64x128_256x512_diff_fcnn.h5' )<load_pretrained>
params={'learning_rate':[.02,.03,.1],'min_child_weight':[4,6,8],'max_depth':[8,10],"subsample":[.6,.4],"n_estimators":[300,500]}
model = load_model('newer/64x128_256x512_diff_fcnn.h5' )<predict_on_test>
xgb=XGBRegressor(learning_rate=.03,min_child_weight=4,max_depth=10,subsample=.4,n_estimators=500,n_jobs=-1)
pred = model.predict_classes(X_test_norm )<prepare_output>
GBoost = GradientBoostingRegressor(n_estimators=600, learning_rate=0.05,
max_depth=4, max_features='auto',
min_samples_leaf=15, min_samples_split=5,
loss='huber', random_state =5 )
sample_submission['Label'] = pred<save_to_csv>
class Average_models(BaseEstimator,RegressorMixin,TransformerMixin):
def __init__(self,models):
self.models=models
def fit(self,X,y):
self.models_=[clone(x)for x in self.models]
for model in self.models_:
model.fit(X,y)
 return self
def predict(self,X):
predictions= np.column_stack([model.predict(X)for model in self.models_])
 return np.mean(predictions,axis=1 )
sample_submission.to_csv("submission.csv", index=False )<set_options>
training,valid,ytraining,yvalid=train_test_split(x_train,y)
 xgb.fit(training,ytraining)
 lasso.fit(training,ytraining)
 pred1=xgb.predict(valid)
 pred2=lasso.predict(valid)
 test_pred1=xgb.predict(test)
 test_pred2=lasso.predict(test)
 stacked_predictions=np.column_stack(( pred1,pred2))
stacked_test_prediction=np.column_stack(( test_pred1,test_pred2))
meta_model=LinearRegression()
meta_model.fit(stacked_predictions,yvalid)
 final=meta_model.predict(stacked_test_prediction )
<load_from_csv><EOS> 
df=pd.DataFrame({'id':test_id,'winPlacePerc':final},columns=['id','winPlacePerc'])
 csv=df[['id','winPlacePerc']].to_csv('submission.csv',index=False )