Kaggle competition: COVID19 Global Forecasting (Week 4)
def feature_engineering(is_train=True,debug=True):
test_idx = None
if is_train:
print("processing train.csv")
 if debug == True:
df = pd.read_csv('.. /input/train_V2.csv', nrows=10000)
 else:
df = pd.read_csv('.. /input/train_V2.csv')
 df = df[df['maxPlace'] > 1]
else:
print("processing test.csv")
 df = pd.read_csv('.. /input/test_V2.csv')
 test_idx = df.Id
print("remove some columns")
 target = 'winPlacePerc'
print("Adding Features")
 df['headshotrate'] = df['kills']/df['headshotKills']
df['killStreakrate'] = df['killStreaks']/df['kills']
df['healthitems'] = df['heals'] + df['boosts']
df['totalDistance'] = df['rideDistance'] + df["walkDistance"] + df["swimDistance"]
df['killPlace_over_maxPlace'] = df['killPlace'] / df['maxPlace']
df['headshotKills_over_kills'] = df['headshotKills'] / df['kills']
df['distance_over_weapons'] = df['totalDistance'] / df['weaponsAcquired']
df['walkDistance_over_heals'] = df['walkDistance'] / df['heals']
df['walkDistance_over_kills'] = df['walkDistance'] / df['kills']
df['killsPerWalkDistance'] = df['kills'] / df['walkDistance']
df["skill"] = df["headshotKills"] + df["roadKills"]
df[df == np.Inf] = np.NaN
df[df == np.NINF] = np.NaN
print("Removing Na's From DF")
 df.fillna(0, inplace=True)
 features = list(df.columns)
 features.remove("Id")
 features.remove("matchId")
 features.remove("groupId")
 features.remove("matchType")
 y = None
if is_train:
print("get target")
 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 = df_out
feature_names = list(df_out.columns)
 del df, df_out, agg, agg_rank
gc.collect()
return X, y, feature_names, test_idx<prepare_x_and_y>
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import nltk
from sklearn.preprocessing import LabelBinarizer,LabelEncoder,StandardScaler,MinMaxScaler
from sklearn.linear_model import LogisticRegression,SGDClassifier,LinearRegression
from sklearn.naive_bayes import MultinomialNB
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import classification_report,confusion_matrix,accuracy_score
from sklearn.model_selection import train_test_split
import keras
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from keras.models import Sequential
from keras.layers import Dense,LSTM
import tensorflow as tf
x_train, y_train, train_columns, _ = feature_engineering(True,False )<feature_engineering>
train_df = pd.read_csv(".. /input/covid19-global-forecasting-week-4/train.csv")
 test_df = pd.read_csv(".. /input/covid19-global-forecasting-week-4/test.csv")
 submission = pd.read_csv(".. /input/covid19-global-forecasting-week-4/submission.csv" )
x_test, _, _ , test_idx = feature_engineering(False,True )<set_options>
train_df.isna().sum()
warnings.filterwarnings("ignore")
 color = sns.color_palette() 
<train_model>
test_df.isna().sum()
train_index = round(int(x_train.shape[0]*0.8))
dev_X = x_train[:train_index]
val_X = x_train[train_index:]
dev_y = y_train[:train_index]
val_y = y_train[train_index:]
gc.collect() ;
def run_lgb(train_X, train_y, val_X, val_y, x_test):
params = {"objective" : "regression", "metric" : "mae", 'n_estimators':20000, 'early_stopping_rounds':200,
"num_leaves" : 31, "learning_rate" : 0.05, "bagging_fraction" : 0.7,
"bagging_seed" : 0, "num_threads" : 4,"colsample_bytree" : 0.7
}
lgtrain = lgb.Dataset(train_X, label=train_y)
 lgval = lgb.Dataset(val_X, label=val_y)
 model = lgb.train(params, lgtrain, valid_sets=[lgtrain, lgval], early_stopping_rounds=200, verbose_eval=1000)
 pred_test_y = model.predict(x_test, num_iteration=model.best_iteration)
 return pred_test_y, model
pred_test, model = run_lgb(dev_X, dev_y, val_X, val_y, x_test )<load_from_csv>
train_df['Country_Region'] = train_df['Country_Region'] + ' ' + train_df['Province_State']
test_df['Country_Region'] = test_df['Country_Region'] + ' ' + test_df['Province_State']
del train_df['Province_State']
del test_df['Province_State']
df_sub = pd.read_csv(".. /input/sample_submission_V2.csv")
 df_test = pd.read_csv(".. /input/test_V2.csv")
 df_sub['winPlacePerc'] = pred_test
df_sub = df_sub.merge(df_test[["Id", "matchId", "groupId", "maxPlace", "numGroups"]], on="Id", how="left")
 df_sub_group = df_sub.groupby(["matchId", "groupId"] ).first().reset_index()
df_sub_group["rank"] = df_sub_group.groupby(["matchId"])["winPlacePerc"].rank()
df_sub_group = df_sub_group.merge(
 df_sub_group.groupby("matchId")["rank"].max().to_frame("max_rank" ).reset_index() ,
on="matchId", how="left")
 df_sub_group["adjusted_perc"] =(df_sub_group["rank"] - 1)/(df_sub_group["numGroups"] - 1)
 df_sub = df_sub.merge(df_sub_group[["adjusted_perc", "matchId", "groupId"]], on=["matchId", "groupId"], how="left")
 df_sub["winPlacePerc"] = df_sub["adjusted_perc"]
df_sub.loc[df_sub.maxPlace == 0, "winPlacePerc"] = 0
df_sub.loc[df_sub.maxPlace == 1, "winPlacePerc"] = 1
subset = df_sub.loc[df_sub.maxPlace > 1]
gap = 1.0 /(subset.maxPlace.values - 1)
 new_perc = np.around(subset.winPlacePerc.values / gap)* gap
df_sub.loc[df_sub.maxPlace > 1, "winPlacePerc"] = new_perc
df_sub.loc[(df_sub.maxPlace > 1)&(df_sub.numGroups == 1), "winPlacePerc"] = 0
assert df_sub["winPlacePerc"].isnull().sum() == 0
df_sub[["Id", "winPlacePerc"]].to_csv("submission_adjusted.csv", index=False )<load_from_csv>
def split_date(date):
date = date.split('-')
 date[0] = int(date[0])
 if(date[1][0] == '0'):
date[1] = int(date[1][1])
 else:
date[1] = int(date[1])
 if(date[2][0] == '0'):
date[2] = int(date[2][1])
 else:
date[2] = int(date[2])
 return date
train_df.Date = train_df.Date.apply(split_date)
 test_df.Date = test_df.Date.apply(split_date )
%%time
def preProcess(is_train=True,debug=True):
test_idx = None
if is_train:
print("processing train.csv")
 if debug == True:
df = pd.read_csv('.. /input/train_V2.csv', nrows=10000)
 else:
df = pd.read_csv('.. /input/train_V2.csv')
 df = df[df['maxPlace'] > 1]
else:
print("processing test.csv")
 df = pd.read_csv('.. /input/test_V2.csv')
 test_idx = df.Id
df['totalDistance'] = df['rideDistance'] + df['walkDistance'] + df['swimDistance']
df['kills_assists'] =(df['kills'] + df['assists'])
 df['killsWithoutMoving'] =(( df['kills'] > 0)&(df['totalDistance'] == 0))
df['healthitems'] = df['heals'] + df['boosts']
df = reduce_mem_usage(df)
 print("remove some columns")
 target = 'winPlacePerc'
features = list(df.columns)
 features.remove("Id")
 features.remove("matchId")
 features.remove("groupId")
 features.remove("matchType")
 y = None
if is_train:
print("get target")
 y = np.array(df[target])
 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()
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= reduce_mem_usage(df_out)
 print("get match Type feature")

df_out.drop(["matchId", "groupId"], axis=1, inplace=True)
 X = df_out
feature_names = list(df_out.columns)
 del df, df_out, agg, agg_rank
gc.collect()
return X, y, feature_names, test_idx
x_train, y_train, train_columns, _ = preProcess(True,False)
 x_test, _, _ , test_idx = preProcess(False,True)
<split>
year = []
month = []
day = []
for i in train_df.Date:
year.append(i[0])
 month.append(i[1])
 day.append(i[2] )
random_seed=1
x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size = 0.1, random_state=random_seed)
<choose_model_class>
train_df['Year'] = year
train_df['Month'] = month
train_df['Day'] = day
del train_df['Date']
model = RandomForestRegressor(n_estimators=20, min_samples_leaf=5, max_features=0.5,
n_jobs=-1,verbose=2)
<train_model>
year = []
month = []
day = []
for i in test_df.Date:
year.append(i[0])
 month.append(i[1])
 day.append(i[2] )
%%time
model.fit(x_train, y_train)
<compute_train_metric>
test_df['Year'] = year
test_df['Month'] = month
test_df['Day'] = day
del test_df['Date']
del train_df['Id']
del test_df['ForecastId']
y_train_pred = model.predict(x_train)
 y_val_pred = model.predict(x_val)
 print('mae train: ', mean_absolute_error(y_train_pred, y_train))
print('mae validation: ', mean_absolute_error(y_val_pred, y_val)) 
<save_to_csv>
del train_df['Year']
del test_df['Year']
df_test = pd.read_csv('.. /input/' + 'test_V2.csv')
 pred = model.predict(x_test)
 df_test['winPlacePerc'] = pred
submission = df_test[['Id', 'winPlacePerc']]
submission.to_csv('submission_rf_feat1.csv', index=False )<import_modules>
train_df['ConfirmedCases'] = train_df['ConfirmedCases'].apply(int)
 train_df['Fatalities'] = train_df['Fatalities'].apply(int )
print(os.listdir(".. /input")) 
<define_variables>
cases = train_df.ConfirmedCases
fatalities = train_df.Fatalities
del train_df['ConfirmedCases']
del train_df['Fatalities']
small_train = True
only_imp_cols = False<load_from_csv>
lb = LabelEncoder()
train_df['Country_Region'] = lb.fit_transform(train_df['Country_Region'])
 test_df['Country_Region'] = lb.transform(test_df['Country_Region'] )
try:
train = pd.read_csv('.. /input/train_V2.csv')
 except:
train = pd.read_csv('~/.kaggle/competitions/pubg-finish-placement-prediction/train_V2.csv' )<define_variables>
scaler = MinMaxScaler()
x_train = scaler.fit_transform(train_df.values)
 x_test = scaler.transform(test_df.values )
((train['maxPlace'] - 1)* train['winPlacePerc'])[:10]<count_unique_values>
from xgboost import XGBRegressor
if small_train:
train = train[train['matchId'].isin(train['matchId'].unique() [np.random.rand(len(train['matchId'].unique())) < 0.4])].reset_index(drop=True )<feature_engineering>
rf = XGBRegressor(n_estimators = 2500 , random_state = 0 , max_depth = 27)
 rf.fit(x_train,cases )
def feature_engineering(df, train=True):
drop_labels = ['Id', 'groupId', 'matchId', 'maxPlace', 'numGroups', 'matchDuration', 'matchType']
if train:
df['wppBin'] = np.uint16(np.round(df['winPlacePerc']*(df['maxPlace']-1)))
 df['wppBin2'] = df['wppBin'] +(100 - df['maxPlace'])
 df['wppBin3'] = df['wppBin'] + np.uint16(np.round(( 100 - df['maxPlace'])*.5))
drop_labels += ['winPlacePerc', 'wppBin', 'wppBin2', 'wppBin3']
df['killrate'] = df['kills']/df['matchDuration']
df['totalDistance'] = df['walkDistance'] + df['swimDistance'] + df['rideDistance']
df['avgSpeed'] = df['totalDistance'] / df['matchDuration']
df['healsPlusBoosts'] = df['boosts'] + df['heals']
df['kar'] = df['kills'] + df['assists'] + df['revives']
le = LabelEncoder()
le.fit(['squad-fpp', 'duo', 'solo-fpp', 'squad', 'duo-fpp', 'solo',
'normal-squad-fpp', 'crashfpp', 'flaretpp', 'normal-solo-fpp',
'flarefpp', 'normal-duo-fpp', 'normal-duo', 'crashtpp',
'normal-squad', 'normal-solo'])
 df['matchType'] = le.transform(df['matchType'])
 agglabels = [ele for ele in df.columns if ele not in drop_labels]
groupByMatch = df.groupby(['matchId','groupId'])[agglabels]
groupByGroup = df.groupby(['groupId', 'Id'])[agglabels]
df = df.merge(groupByMatch.agg('min' ).reset_index() , suffixes=['','match_min'], on=['matchId','groupId'], how='left')
 print('Checkpoint 1/7')
 df = df.merge(groupByMatch.agg('max' ).reset_index() , suffixes=['','match_max'], on=['matchId','groupId'], how='left')
 print('Checkpoint 2/7')
 df = df.merge(groupByMatch.agg('mean' ).reset_index() , suffixes=['','match_mean'], on=['matchId','groupId'], how='left')
 print('Checkpoint 3/7')
 df = df.merge(groupByMatch.agg('mean' ).groupby('matchId' ).rank(pct=True ).reset_index() , suffixes=['','match_rank'], on=['matchId', 'groupId'], how='left')
 print('Checkpoint 4/7')
 df = df.merge(groupByGroup.agg('min' ).reset_index() , suffixes=['','group_min'], on=['Id', 'groupId'], how='left')
 print('Checkpoint 5/7')
 df = df.merge(groupByGroup.agg('max' ).reset_index() , suffixes=['','group_max'], on=['Id', 'groupId'], how='left')
 print('Checkpoint 6/7')
 df = df.merge(groupByGroup.agg('mean' ).reset_index() , suffixes=['','group_mean'], on=['Id', 'groupId'], how='left')
 print('Checkpoint 7/7')
 df = df.merge(groupByGroup.agg('mean' ).groupby('groupId' ).rank(pct=True ).reset_index() , suffixes=['','group_rank'], on=['Id', 'groupId'], how='left')
 df = df.merge(groupByGroup.size().reset_index(name='group_size'), how='left', on=['Id', 'groupId'])
 df = df.merge(groupByMatch.size().reset_index(name='match_size'), how='left', on=['matchId','groupId'])
 return df<feature_engineering>
cases_pred = rf.predict(x_test)
 cases_pred
train, _ = reduce_mem_usage(train )<feature_engineering>
cases_pred = np.around(cases_pred,decimals = 0)
 cases_pred
msk = train['matchId'].unique() [np.random.rand(len(train['matchId'].unique())) < 0.8]
train_train = train[train['matchId'].isin(msk)].reset_index(drop=True)
 train_val = train[~train['matchId'].isin(msk)].reset_index(drop=True)
 train = None
msk = None
gc.collect()
train_train = feature_engineering(train_train)
 train_train, _ = reduce_mem_usage(train_train)
 train_val = feature_engineering(train_val)
 train_val, _ = reduce_mem_usage(train_val )<define_variables>
x_train_cas = []
for i in range(len(x_train)) :
x = list(x_train[i])
 x.append(cases[i])
 x_train_cas.append(x)
 x_train_cas[0]
train_cols = [ele for ele in train_train.columns if ele not in ['Id', 'groupId', 'matchId', 'winPlacePerc', 'wppBin', 'wppBin2', 'wppBin3']]
target = 'winPlacePerc'<choose_model_class>
x_train_cas = np.array(x_train_cas )
m1inputs = Input(shape=(len(train_cols),))
m1l1 = BatchNormalization()(m1inputs)
 m1l1 = Dense(128, activation='relu' )(m1l1)
 m1l1 = BatchNormalization()(m1l1)
 m1l2 = Dense(128, activation='relu' )(m1l1)
 m1l2 = BatchNormalization()(m1l2)
 m1l2 = Dropout(0.75 )(m1l2)
 m1l3 = Dense(128, activation='relu' )(m1l2)
 m1l3 = BatchNormalization()(m1l3)
 m1l4 = Dense(128, activation='relu' )(m1l3)
 m1l4 = BatchNormalization()(m1l4)
 m1l4 = Dropout(0.75 )(m1l4)
 m1output = Dense(100, activation='softmax' )(m1l4 )<choose_model_class>
rf = XGBRegressor(n_estimators = 2500 , random_state = 0 , max_depth = 27)
 rf.fit(x_train_cas,fatalities )
model1 = Model(inputs=[m1inputs], outputs=[m1output])
 adam = Adam(lr=0.0005, beta_1=0.9, beta_2=0.999, epsilon=None, amsgrad=False)
 model1.compile(optimizer=adam, loss='binary_crossentropy', metrics=['acc'] )<categorify>
x_test_cas = []
for i in range(len(x_test)) :
x = list(x_test[i])
 x.append(cases_pred[i])
 x_test_cas.append(x)
 x_test_cas[0]
enc = OneHotEncoder()
enc.fit(np.arange(100 ).reshape(-1,1)) 
<train_model>
fatalities_pred = rf.predict(x_test_cas)
 fatalities_pred
model1.fit(train_train[train_cols], enc.transform(train_train['wppBin'].values.reshape(-1,1)) ,
epochs=20, batch_size=4096,
validation_data=(train_val[train_cols], enc.transform(train_val['wppBin'].values.reshape(-1,1))),
verbose=2 )<predict_on_test>
fatalities_pred = np.around(fatalities_pred,decimals = 0)
 fatalities_pred
val_preds = model1.predict(train_val[train_cols], verbose=True )<prepare_output>
submission['ConfirmedCases'] = cases_pred
submission['Fatalities'] = fatalities_pred
val_preds_float = np.argmax(val_preds, axis=1)/(train_val['maxPlace'] + 1 )<compute_test_metric>
submission.to_csv("submission.csv" , index = False )
print('estimated score from verify set:')
 np.mean(np.abs(val_preds_float - train_val[target]))<choose_model_class>
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import nltk
from sklearn.preprocessing import LabelBinarizer,LabelEncoder,StandardScaler,MinMaxScaler
from sklearn.linear_model import LogisticRegression,SGDClassifier,LinearRegression
from sklearn.naive_bayes import MultinomialNB
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import classification_report,confusion_matrix,accuracy_score
from sklearn.model_selection import train_test_split
import keras
from keras.wrappers.scikit_learn import KerasRegressor
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import KFold
from keras.models import Sequential
from keras.layers import Dense,LSTM
import tensorflow as tf
m2inputs = Input(shape=(len(train_cols),))
m2l1 = BatchNormalization()(m2inputs)
 m2l1 = Dense(128, activation='relu' )(m2l1)
 m2l1 = BatchNormalization()(m2l1)
 m2l2 = Dense(128, activation='relu' )(m2l1)
 m2l2 = BatchNormalization()(m2l2)
 m2l2 = Dropout(0.75 )(m2l2)
 m2l3 = Dense(128, activation='relu' )(m2l2)
 m2l3 = BatchNormalization()(m2l3)
 m2l4 = Dense(128, activation='relu' )(m2l3)
 m2l4 = BatchNormalization()(m2l4)
 m2l4 = Dropout(0.75 )(m2l4)
 m2output = Dense(100, activation='softmax' )(m2l4 )<train_model>
train_df = pd.read_csv(".. /input/covid19-global-forecasting-week-4/train.csv")
 test_df = pd.read_csv(".. /input/covid19-global-forecasting-week-4/test.csv")
 submission = pd.read_csv(".. /input/covid19-global-forecasting-week-4/submission.csv" )
model2 = Model(inputs=[m2inputs], outputs=[m2output])
 adam = Adam(lr=0.0005, beta_1=0.9, beta_2=0.999, epsilon=None, amsgrad=False)
 model2.compile(optimizer=adam, loss='binary_crossentropy', metrics=['acc'])
 model2.fit(train_train[train_cols], enc.transform(train_train['wppBin2'].values.reshape(-1,1)) ,
epochs=20,
batch_size=4096,
validation_data=(train_val[train_cols], enc.transform(train_val['wppBin2'].values.reshape(-1,1))),
verbose=2 )<choose_model_class>
train_df.isna().sum()
m3inputs = Input(shape=(len(train_cols),))
m3l1 = BatchNormalization()(m3inputs)
 m3l1 = Dense(128, activation='relu' )(m3l1)
 m3l1 = BatchNormalization()(m3l1)
 m3l2 = Dense(128, activation='relu' )(m3l1)
 m3l2 = BatchNormalization()(m3l2)
 m3l2 = Dropout(0.75 )(m3l2)
 m3l3 = Dense(128, activation='relu' )(m3l2)
 m3l3 = BatchNormalization()(m3l3)
 m3l4 = Dense(128, activation='relu' )(m3l3)
 m3l4 = BatchNormalization()(m3l4)
 m3l4 = Dropout(0.75 )(m3l4)
 m3output = Dense(100, activation='softmax' )(m3l4 )<train_model>
test_df.isna().sum()
model3 = Model(inputs=[m3inputs], outputs=[m3output])
 adam = Adam(lr=0.0005, beta_1=0.9, beta_2=0.999, epsilon=None, amsgrad=False)
 model3.compile(optimizer=adam, loss='binary_crossentropy', metrics=['acc'])
 model3.fit(train_train[train_cols], enc.transform(train_train['wppBin3'].values.reshape(-1,1)) ,
epochs=20,
batch_size=4096,
validation_data=(train_val[train_cols], enc.transform(train_val['wppBin3'].values.reshape(-1,1))),
verbose=2 )<predict_on_test>
train_df['Country_Region'] = train_df['Country_Region'] + ' ' + train_df['Province_State']
test_df['Country_Region'] = test_df['Country_Region'] + ' ' + test_df['Province_State']
del train_df['Province_State']
del test_df['Province_State']
predsbin1 = model1.predict(train_val[train_cols], verbose=True)
 predsbin2 = model2.predict(train_val[train_cols], verbose=True)
 predsbin3 = model3.predict(train_val[train_cols], verbose=True )<predict_on_test>
def split_date(date):
date = date.split('-')
 date[0] = int(date[0])
 if(date[1][0] == '0'):
date[1] = int(date[1][1])
 else:
date[1] = int(date[1])
 if(date[2][0] == '0'):
date[2] = int(date[2][1])
 else:
date[2] = int(date[2])
 return date
train_df.Date = train_df.Date.apply(split_date)
 test_df.Date = test_df.Date.apply(split_date )
tpredsbin1 = model1.predict(train_train[train_cols], verbose=True)
 tpredsbin2 = model2.predict(train_train[train_cols], verbose=True)
 tpredsbin3 = model3.predict(train_train[train_cols], verbose=True )<define_variables>
year = []
month = []
day = []
for i in train_df.Date:
year.append(i[0])
 month.append(i[1])
 day.append(i[2] )
trainMaxPlace = train_train['maxPlace']
valMaxPlace = train_val['maxPlace']
trainTar = train_train[target]
valTar = train_val[target]
train_train = None
train_verify = None
gc.collect()<prepare_output>
train_df['Year'] = year
train_df['Month'] = month
train_df['Day'] = day
del train_df['Date']
preds1 = np.argmax(predsbin1,axis=1)/(valMaxPlace + 1)
 preds2 =(np.argmax(predsbin2,axis=1)-(100 - valMaxPlace)) /(valMaxPlace + 1)
 preds3 =(np.argmax(predsbin3,axis=1)- np.uint16(np.round(( 100 - valMaxPlace)*.5)))/(valMaxPlace + 1 )<prepare_output>
year = []
month = []
day = []
for i in test_df.Date:
year.append(i[0])
 month.append(i[1])
 day.append(i[2] )
tpreds1 = np.argmax(tpredsbin1,axis=1)/(trainMaxPlace + 1)
 tpreds2 =(np.argmax(tpredsbin2,axis=1)-(100 - trainMaxPlace)) /(trainMaxPlace + 1)
 tpreds3 =(np.argmax(tpredsbin3,axis=1)- np.uint16(np.round(( 100 - trainMaxPlace)*.5)))/(trainMaxPlace + 1 )<compute_test_metric>
test_df['Year'] = year
test_df['Month'] = month
test_df['Day'] = day
del test_df['Date']
del train_df['Id']
del test_df['ForecastId']
print('Training scores:')
 print(np.mean(np.abs(tpreds1 - trainTar)))
 print(np.mean(np.abs(tpreds2 - trainTar)))
 print(np.mean(np.abs(tpreds3 - trainTar)))
 print('Validation scores')
 print(np.mean(np.abs(preds1 - valTar)))
 print(np.mean(np.abs(preds2 - valTar)))
 print(np.mean(np.abs(preds3 - valTar)) )<create_dataframe>
del train_df['Year']
del test_df['Year']
train_outs = pd.DataFrame({'p1': tpreds1,
'p2': tpreds2,
'p3': tpreds3,
'maxPlace': trainMaxPlace,
'winPlacePerc': trainTar})
 val_outs = pd.DataFrame({'p1': preds1,
'p2': preds2,
'p3': preds3,
'maxPlace': valMaxPlace,
'winPlacePerc': valTar})
 train_outs.to_csv('train_out.csv')
 val_outs.to_csv('validate_out.csv' )<train_model>
train_df['ConfirmedCases'] = train_df['ConfirmedCases'].apply(int)
 train_df['Fatalities'] = train_df['Fatalities'].apply(int )
clf = xgb.XGBRegressor(max_depth=4,
silent=False,
learning_rate=0.6,
n_estimators=100,
n_jobs=-1)
 boost_cols = ['p1', 'p2', 'p3', 'maxPlace']
target = 'winPlacePerc'
clf.fit(train_outs[boost_cols], train_outs[target],
eval_set=[(train_outs[boost_cols], train_outs[target]),(val_outs[boost_cols], val_outs[target])],
verbose=False, eval_metric='mae', early_stopping_rounds=5 )<predict_on_test>
cases = train_df.ConfirmedCases
fatalities = train_df.Fatalities
del train_df['ConfirmedCases']
del train_df['Fatalities']
xgb_val_preds = clf.predict(val_outs[boost_cols])
 print('validation set mae: {}'.format(np.mean(np.abs(xgb_val_preds - val_outs[target]))))<load_from_csv>
lb = LabelEncoder()
train_df['Country_Region'] = lb.fit_transform(train_df['Country_Region'])
 test_df['Country_Region'] = lb.transform(test_df['Country_Region'] )
try:
test = pd.read_csv('.. /input/test_V2.csv')
 except:
test = pd.read_csv('~/.kaggle/competitions/pubg-finish-placement-prediction/test_V2.csv')
 test, _ = reduce_mem_usage(test)
 test = feature_engineering(test, train=False)
 test_ids = test['Id']
test = test[train_cols]<feature_engineering>
scaler = MinMaxScaler()
x_train = scaler.fit_transform(train_df.values)
 x_test = scaler.transform(test_df.values )
test, _ = reduce_mem_usage(test )<predict_on_test>
from xgboost import XGBRegressor
preds1 = model1.predict(test, verbose=True)
 preds2 = model2.predict(test, verbose=True)
 preds3 = model3.predict(test, verbose=True )<set_options>
rf = XGBRegressor(n_estimators = 2500 , random_state = 0 , max_depth = 27)
 rf.fit(x_train,cases )
testMaxPlace = test['maxPlace']
test = None
gc.collect()<prepare_output>
cases_pred = rf.predict(x_test)
 cases_pred
preds1 = np.argmax(preds1,axis=1)/(testMaxPlace + 1)
 preds2 =(np.argmax(preds2,axis=1)-(100 - testMaxPlace)) /(testMaxPlace + 1)
 preds3 =(np.argmax(preds3,axis=1)- np.uint16(np.round(( 100 - testMaxPlace)*.5)))/(testMaxPlace + 1 )<save_to_csv>
cases_pred = np.around(cases_pred,decimals = 0)
 cases_pred
test_outs = pd.DataFrame({'p1': preds1,
'p2': preds2,
'p3': preds3,
'maxPlace': testMaxPlace})
 test_outs.to_csv('test_out.csv' )<predict_on_test>
x_train_cas = []
for i in range(len(x_train)) :
x = list(x_train[i])
 x.append(cases[i])
 x_train_cas.append(x)
 x_train_cas[0]
test_preds = clf.predict(test_outs[boost_cols] )<save_to_csv>
x_train_cas = np.array(x_train_cas )
submission = pd.DataFrame({'Id': test_ids, 'winPlacePerc': test_preds})
 submission.to_csv('submission.csv', index=False )<set_options>
rf = XGBRegressor(n_estimators = 2500 , random_state = 0 , max_depth = 27)
 rf.fit(x_train_cas,fatalities )
le = LabelEncoder()
pd.set_option('display.max_rows', 400)
 pd.set_option('display.max_columns', 160)
 pd.set_option('display.max_colwidth', 40)
 warnings.filterwarnings("ignore" )<load_from_csv>
x_test_cas = []
for i in range(len(x_test)) :
x = list(x_test[i])
 x.append(cases_pred[i])
 x_test_cas.append(x)
 x_test_cas[0]
test = pd.read_csv('.. /input/competitive-data-science-predict-future-sales/test.csv')
 test.head()<load_from_csv>
fatalities_pred = rf.predict(x_test_cas)
 fatalities_pred
categories = pd.read_csv('.. /input/predict-future-sales-eng-translation/categories.csv')
 pd.DataFrame(categories.category_name.values.reshape(-1, 4))<categorify>
fatalities_pred = np.around(fatalities_pred,decimals = 0)
 fatalities_pred
categories['group_name'] = categories['category_name'].str.extract(r'(^[\w\s]*)')
 categories['group_name'] = categories['group_name'].str.strip()
categories['group_id'] = le.fit_transform(categories.group_name.values)
 categories.sample(5 )<load_from_csv>
submission['ConfirmedCases'] = cases_pred
submission['Fatalities'] = fatalities_pred
items = pd.read_csv('.. /input/predict-future-sales-eng-translation/items.csv')
 items['item_name'] = items['item_name'].str.lower()
items['item_name'] = items['item_name'].str.replace('.', '')
 for i in [r'[^\w\d\s\.]', r'\bthe\b', r'\bin\b', r'\bis\b',
r'\bfor\b', r'\bof\b', r'\bon\b', r'\band\b',
r'\bto\b', r'\bwith\b' , r'\byo\b']:
items['item_name'] = items['item_name'].str.replace(i, ' ')
 items['item_name'] = items['item_name'].str.replace(r'\b.\b', ' ')
 items['item_name_no_space'] = items['item_name'].str.replace(' ', '')
 items['item_name_first4'] = [x[:4] for x in items['item_name_no_space']]
items['item_name_first6'] = [x[:6] for x in items['item_name_no_space']]
items['item_name_first11'] = [x[:11] for x in items['item_name_no_space']]
del items['item_name_no_space']
items.item_name_first4 = le.fit_transform(items.item_name_first4.values)
 items.item_name_first6 = le.fit_transform(items.item_name_first6.values)
 items.item_name_first11 = le.fit_transform(items.item_name_first11.values)
 items = items.join(categories.set_index('category_id'), on='category_id')
 items.sample(10 )<remove_duplicates>
submission.to_csv("submission.csv" , index = False )