Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<train_model>
gc.enable()
history = model.fit(
 train_input, train_labels,
validation_split=0.2,
epochs=5,
batch_size=16 
)
 predictions1 = model.predict(test_input)
 print(predictions1[0:30] )<categorify>
def state(message,start = True, time = 0):
if(start):
print('Working on {}...'.format(message))
else :
print('Working on {} took({})Sec
'.format(message,round(time , 3)) )
embedding = hub.load("https://tfhub.dev/google/universal-sentence-encoder/4")
 embedded_xtrain = embedding(train_df['clean_text'] ).numpy()
embedded_xtest = embedding(test_df['clean_text'] ).numpy()
target = train_df["target"].to_numpy()
model2 = svm.SVR(kernel='rbf',gamma='auto')
 model2.fit(embedded_xtrain,target)
 predictions2 = model2.predict(embedded_xtest)
 predictions2 = np.mat(predictions2)
 predictions2 = predictions2.T
print(predictions2[0:30])
<define_variables>
def feature_engineering(is_train=True):
if is_train:
print("processing train_V2.csv")
 df = pd.read_csv('.. /input/train_V2.csv')
 df = df[df['maxPlace'] > 1]
else:
print("processing test_V2.csv")
 df = pd.read_csv('.. /input/test_V2.csv')
 state('totalDistance')
 s = timer()
df['totalDistance'] = df['rideDistance'] + df["walkDistance"] + df["swimDistance"]
e = timer()
state('totalDistance', False, e - s)
 state('rankPoints')
 s = timer()
df['rankPoints'] = np.where(df['rankPoints'] <= 0 ,0 , df['rankPoints'])
 e = timer()
state('rankPoints', False, e-s)
 target = 'winPlacePerc'
features = list(df.columns)
 features.remove("Id")
 features.remove("matchId")
 features.remove("groupId")
 features.remove("matchDuration")
 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
sequence_lenght = 512<categorify>
df_test = pd.read_csv('.. /input/test_V2.csv' )
USElite2_embedding = hub.load("https://tfhub.dev/google/universal-sentence-encoder-large/5")
 USElite2_embedded_xtrain = USElite2_embedding(train_df['clean_text'] ).numpy()
USElite2_embedded_xtest = USElite2_embedding(test_df['clean_text'] ).numpy()
USElite2_target = train_df["target"].to_numpy()
USElite2_embedded_xtest.shape
USE_for_m4_xtrain = USElite2_embedded_xtrain
USE_for_m4_xtest = USElite2_embedded_xtest
USE_for_m4_target = USElite2_target<split>
%%time
x_train, y = feature_engineering(True)
 scaler = preprocessing.MinMaxScaler(feature_range=(-1, 1), copy=False ).fit(x_train )
USElite2_x_train, USElite2_x_test, USElite2_y_train, USElite2_y_test = train_test_split(
 USElite2_embedded_xtrain,
USElite2_target,
test_size=0.1,
random_state=0,
shuffle=True 
)
 print(USElite2_x_train.shape)
 print(USElite2_y_train.shape)
 print(USElite2_x_test.shape)
 print(USElite2_y_test.shape)
 print(USElite2_x_train )<choose_model_class>
print("x_train", x_train.shape, x_train.max() , x_train.min())
 scaler.transform(x_train)
 print("x_train", x_train.shape, x_train.max() , x_train.min() )
def make_my_model() :
input = keras.layers.Input(shape=(sequence_lenght,1), dtype='float32')
 next_layer = keras.layers.Conv1D(265,kernel_size = 10, activation = "relu",padding="valid",strides = 1 )(input)
 next_layer = keras.layers.MaxPooling1D(pool_size=2 )(next_layer)
 next_layer = keras.layers.Conv1D(64,kernel_size = 5, padding="valid", strides = 1 )(next_layer)
 next_layer = keras.layers.LeakyReLU(alpha=0.1 )(next_layer)
 next_layer = keras.layers.MaxPooling1D(pool_size=3, strides=1 )(next_layer)
 next_layer = keras.layers.Flatten()(next_layer)
 next_layer = keras.layers.Dense(64 )(next_layer)
 next_layer = keras.layers.LeakyReLU(alpha=0.1 )(next_layer)
 output = keras.layers.Dense(1, activation="sigmoid" )(next_layer)
 return keras.Model(inputs=input, outputs=output )<train_model>
epoch_train = 30
mlp = MLPRegressor(hidden_layer_sizes=(60, 60, 28), alpha=0.001, learning_rate='adaptive', max_iter=epoch_train)
 mlp.fit(x_train,y)
 del x_train, y
gc.collect()
USElite2_x_train = np.reshape(USElite2_x_train,(USElite2_x_train.shape[0], USElite2_x_train.shape[1],1))
USElite2_y_train = np.reshape(USElite2_y_train,(USElite2_y_train.shape[0],1))
USElite2_x_test = np.reshape(USElite2_x_test,(USElite2_x_test.shape[0], USElite2_x_test.shape[1],1))
USElite2_y_test = np.reshape(USElite2_y_test,(USElite2_y_test.shape[0],1))
model3 = make_my_model()
model3.compile("adam", loss = "binary_crossentropy", metrics = ["acc"])
 model3.summary()
model3.fit(
 USElite2_x_train,
USElite2_y_train,
batch_size = 128,
epochs = 15,
validation_data =(USElite2_x_test,USElite2_y_test)
)
 USElite2_embedded_xtest = np.reshape(USElite2_embedded_xtest,(USElite2_embedded_xtest.shape[0],USElite2_embedded_xtest.shape[1],1))
predictions3 = model3.predict(USElite2_embedded_xtest)
 predictions3
print(predictions3[0:30] )<predict_on_test>
x_test, _ = feature_engineering(False)
 scaler.transform(x_test)
 print("x_test", x_test.shape, x_test.max() , x_test.min())
 np.clip(x_test, out=x_test, a_min=-1, a_max=1)
 print("x_test", x_test.shape, x_test.max() , x_test.min() )
model4 = svm.SVR(kernel='rbf', gamma='auto')
 model4.fit(USE_for_m4_xtrain ,USE_for_m4_target)
 predictions4 = model4.predict(USE_for_m4_xtest)
 predictions4 = np.mat(predictions4)
 predictions4 = predictions4.T
print(predictions4[0:30] )<data_type_conversions>
%%time
pred = mlp.predict(x_test)
 del x_test
gc.collect()
((0.5*predictions1+0.5*predictions2+0.1*predictions3+0.3*predictions4)*0.8 ).round().astype(int )<save_to_csv>
pred = pred.reshape(-1)
 pred =(pred + 1)/ 2
submission = pd.read_csv("/kaggle/input/nlp-getting-started/sample_submission.csv")
 print(submission)
 submission['target'] =(( 0.5*predictions1+0.5*predictions2+0.1*predictions3+0.3*predictions4)*0.8 ).round().astype(int)
 print(submission)
 submission.to_csv('submission.csv', index=False )<set_options>
%%time
print("fix winPlacePerc")
 for i in range(len(df_test)) :
winPlacePerc = pred[i]
maxPlace = int(df_test.iloc[i]['maxPlace'])
 if maxPlace == 0:
winPlacePerc = 0.0
elif maxPlace == 1:
winPlacePerc = 1.0
else:
gap = 1.0 /(maxPlace - 1)
 winPlacePerc = round(winPlacePerc / gap)* gap
if winPlacePerc < 0: winPlacePerc = 0.0
if winPlacePerc > 1: winPlacePerc = 1.0
pred[i] = winPlacePerc
<load_from_csv><EOS> 
df_test['winPlacePerc'] = pred
submission = df_test[['Id', 'winPlacePerc']]
submission.to_csv("pubg-submission.csv", index=False )