Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<concatenate>
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import warnings
import tensorflow as tf
import itertools
import sys
import gc
from IPython import get_ipython
train_X = pd.concat([train_X_0, train_X_1], axis=0)
 train_X = train_X.reset_index(drop=True)
 print(train_X.shape)
 print(train_X_0.shape)
 train_X.head() 
<concatenate>
%matplotlib inline
pd.options.display.max_columns = 50
sns.set_style('darkgrid')
 sns.set_palette('bone')
 warnings.filterwarnings('ignore' )
y = pd.concat([y_0, y_1], axis=0)
 y = y.reset_index(drop=True)
 y[0].shape<load_from_csv>
def toTupleList(list1, list2):
return list(itertools.product(list1, list2))
test_X = pd.read_csv(".. /input/feature2/test_X_features_10.csv")
 test_X.shape<normalization>
def fillInf(df, val):
numcols = df.select_dtypes(include='number' ).columns
cols = numcols[numcols != 'winPlacePerc']
df[df == np.Inf] = np.NaN
df[df == np.NINF] = np.NaN
for c in cols: df[c].fillna(val, inplace=True )
scaler = StandardScaler()
train_columns = train_X.columns
train_X[train_columns] = scaler.fit_transform(train_X[train_columns])
 test_X[train_columns] = scaler.transform(test_X[train_columns] )<define_variables>
%%time
train = pd.read_csv('.. /input/train_V2.csv')
 train = reduce_mem_usage(train)
 test = pd.read_csv('.. /input/test_V2.csv')
 test = reduce_mem_usage(test)
 print(train.shape, test.shape )
train_columns = train_X.columns
n_fold = 3<split>
nullValues = train[train.isnull().any(axis=1)]
nullValues
%%time
folds = KFold(n_splits=n_fold, shuffle=True, random_state=42)
 oof = np.zeros(len(train_X))
train_score = []
fold_idxs = []
predictions = np.zeros(len(test_X))
xgb_params = {'eta': 0.05,
'max_depth': 10,
'subsample': 0.8,
'objective': 'reg:linear',
'eval_metric': 'mae',
'nthread': 4}
for fold_,(trn_idx, val_idx)in enumerate(folds.split(train_X,train_y.values)) :
strLog = "fold {}".format(fold_)
 print(strLog)
 fold_idxs.append(val_idx)
 X_tr, X_val = train_X[train_columns].iloc[trn_idx], train_X[train_columns].iloc[val_idx]
y_tr, y_val = train_y.iloc[trn_idx], train_y.iloc[val_idx]
train_data = xgb.DMatrix(data=X_tr, label=y_tr, feature_names=X_tr.columns)
 valid_data = xgb.DMatrix(data=X_val, label=y_val, feature_names=X_tr.columns)
 watchlist = [(valid_data, 'valid_data')]
model = xgb.train(dtrain=train_data, num_boost_round=10000, evals=watchlist, early_stopping_rounds=40,params=xgb_params)
 oof[val_idx] = model.predict(xgb.DMatrix(X_val, feature_names=X_tr.columns), ntree_limit=model.best_ntree_limit)
 predictions += model.predict(xgb.DMatrix(test_X[train_columns], feature_names=X_tr.columns), ntree_limit=model.best_ntree_limit)/folds.n_splits
cv_score = mean_absolute_error(train_y, oof)
 print(cv_score )<save_to_csv>
print('null values:', len(nullValues))
train.dropna(inplace=True )
submission = pd.read_csv('.. /input/LANL-Earthquake-Prediction/sample_submission.csv')
 cat_saved=pd.DataFrame(oof,columns=['oof'])
 cat_saved.to_csv('xgb_oof.csv',index=False)
 submission["time_to_failure"] = predictions
submission.to_csv(f'xgb_submission_{cv_score:.3f}.csv', index=False)
 submission.head()<load_from_csv>
train['matchType'].value_counts()
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<load_from_csv>
corr_matrix = train.corr()
corr_matrix['winPlacePerc'].sort_values(ascending=False )
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<split>
for c in ['Id', 'groupId', 'matchId']:
print(f'{c} different values:', train[c].nunique() )
def clean_inputs(train, test, img_shape =(-1,28,28,1), num_classes = 10):
t_X = train.drop("label", axis=1)
 t_Y = train["label"]
t_X = t_X / 255
test_x = test.values / 255
t_X = np.reshape(t_X.values, img_shape)
 test_x = np.reshape(test_x, img_shape)
 t_Y = keras.utils.to_categorical(t_Y, num_classes = num_classes)
 train_x, dev_x, train_y, dev_y = train_test_split(t_X, t_Y, test_size = 0.15, random_state = 0)
 return train_x, train_y, dev_x, dev_y, test_x<split>
for q in ['maxPlace == numGroups', 'maxPlace != numGroups']:
print(q, ':', len(train.query(q)) )
train_x, train_y, dev_x, dev_y, test_x = clean_inputs(train, test )<choose_model_class>
def model(inp_shape):
X = Input(inp_shape, name='input')
 A = Conv2D(6,(7, 7), strides=(1, 1), padding='Same', activation='relu', name='C1' )(X)
 A = MaxPooling2D(pool_size=2, padding='valid' )(A)
 A = Conv2D(16,(5, 5), strides=(1, 1), padding='Same', activation='relu', name='C2' )(A)
 A = MaxPooling2D(pool_size=2, padding='valid' )(A)
 A = Flatten()(A)
 A = BatchNormalization()(A)
 A = Dense(120, activation='relu', kernel_regularizer=regularizers.l2(0.01), name='FC1' )(A)
 A = Dense(84, activation='relu', kernel_regularizer=regularizers.l2(0.01), name='FC2' )(A)
 A = Dense(10, activation='softmax', name='Final' )(A)
 model = Model(inputs=X, outputs=A, name='LeNet')
 return model<train_model>
train['totalDistance'] = train['walkDistance'] + train['rideDistance'] + train['swimDistance']
train['killsWithoutMoving'] =(( train['kills'] > 0)&(train['totalDistance'] == 0))
train.query('killsWithoutMoving == True' ).shape
datagen = ImageDataGenerator(
 rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1)
 datagen.fit(train_x )<choose_model_class>
train.drop('killsWithoutMoving', axis=1, inplace=True )
train_x_pad = np.pad(train_x,(( 0,0),(2,2),(2,2),(0,0)) , mode='constant', constant_values=0 ).astype(float)
 dev_x_pad = np.pad(dev_x,(( 0,0),(2,2),(2,2),(0,0)) , mode='constant', constant_values=0 ).astype(float)
 test_x_pad = np.pad(test_x,(( 0,0),(2,2),(2,2),(0,0)) , mode='constant', constant_values=0 ).astype(float)
 learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc', patience=3, verbose=1,factor=0.5, min_lr=0.00001)
 model = model(train_x_pad.shape[1:])
 model.summary()
model.compile('adam', 'categorical_crossentropy', metrics=['accuracy'])
 history = model.fit_generator(datagen.flow(train_x_pad, train_y, batch_size=32), validation_data=(dev_x_pad, dev_y), steps_per_epoch=len(train_x_pad)//32, epochs=25, callbacks=[learning_rate_reduction] )<choose_model_class>
train.drop(train.query('Id == "c3e444f7d1289f"' ).index, inplace=True )
def model2(num_classes = 10):
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(3,3), padding = 'same', activation ='relu', input_shape =(28,28,1)))
 model.add(BatchNormalization())
 model.add(Conv2D(filters = 32, kernel_size =(3,3), padding = 'same', activation ='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3,3), padding = 'same', activation ='relu'))
model.add(BatchNormalization())
 model.add(Conv2D(filters = 64, kernel_size =(3,3), padding = 'same', activation ='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation = "softmax"))
return model;<train_model>
train.drop(train.query(query ).index, inplace=True )
start = time.time()
model2 = model2(10)
 learning_rate_reduction2 = ReduceLROnPlateau(monitor='val_acc', patience=3, verbose=1,factor=0.5, min_lr=0.00001)
 model2.summary()
model2.compile('adam', 'categorical_crossentropy', metrics=['accuracy'])
 history2 = model2.fit_generator(datagen.flow(train_x, train_y, batch_size=32), validation_data=(dev_x, dev_y), steps_per_epoch=len(train_x)//32, epochs=25, callbacks=[learning_rate_reduction2])
 timeRecord = time.time() - start
print("--- %s seconds ---" %(timeRecord))<choose_model_class>
train.drop(train.query(query ).index, inplace=True )
def model3(num_classes = 10):
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(3,3), padding = 'same', activation ='relu', input_shape =(28,28,1)))
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Conv2D(filters = 64, kernel_size =(3,3), padding = 'same', activation ='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Conv2D(filters = 64, kernel_size =(3,3), padding = 'same', activation ='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dense(num_classes, activation = "softmax"))
return model;<train_model>
train.drop(train.query('Id == "da68d2812229a8"' ).index, inplace=True )
start = time.time()
model3 = model3(10)
 learning_rate_reduction3 = ReduceLROnPlateau(monitor='val_acc', patience=3, verbose=1,factor=0.5, min_lr=0.00001)
 model3.summary()
model3.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics=['accuracy'])
 history3 = model3.fit_generator(datagen.flow(train_x, train_y, batch_size=64),
validation_data=(dev_x, dev_y), steps_per_epoch=len(train_x)//64, epochs=25,
callbacks=[learning_rate_reduction3])
 timeRecord = time.time() - start
print("--- %s seconds ---" %(timeRecord))<save_to_csv>
train.drop(train.query(query ).index, inplace=True )
prediction = model2.predict(test_x)
 prediction = np.argmax(prediction, axis=1)
 prediction = pd.Series(prediction, name="Label")
 submission = pd.concat([pd.Series(range(1,28001), name = "ImageId"), prediction],axis = 1)
 submission.to_csv('mnist-submission.csv', index = False)
 print(submission )<load_from_csv>
train.drop(train.query(query ).index, inplace=True )
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )<load_from_csv>
train.drop(train.query(query ).index, inplace=True )
train = pd.read_csv(".. /input/train.csv")
 test = pd.read_csv(".. /input/test.csv" )<prepare_x_and_y>
train.drop(train.query(query ).index, inplace=True )
Y_train = train["label"]
X_train = train.drop(labels = ["label"],axis = 1)
 del train
g = sb.countplot(Y_train)
 Y_train.value_counts()
print(X_train.shape)
 print(test.shape )<split>
train.drop(train.query(query ).index, inplace=True )
X_train = X_train / 255.0
test = test / 255.0
X_train = X_train.values.reshape(-1,28,28,1)
 test = test.values.reshape(-1,28,28,1)
 Y_train = to_categorical(Y_train, num_classes = 10)
 X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 1)
<choose_model_class>
train.drop(train.query(query ).index, inplace=True )
model = Sequential()
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 128, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(Conv2D(filters = 128, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))<choose_model_class>
train = train.drop('totalDistance', axis=1 )
optimizer = Adam()
model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"])
<train_model>
data = train.append(test, sort=False ).reset_index(drop=True)
 del train, test
gc.collect()
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)
 datagen.fit(X_train )<train_model>
match = data.groupby('matchId')
 data['_killsPerc'] = match['kills'].rank(pct=True ).values
data['_damageDealtPerc'] = match['damageDealt'].rank(pct=True ).values
data['_walkDistancePerc'] = match['walkDistance'].rank(pct=True ).values
data['_walkPerc_killsPerc'] = data['_walkDistancePerc'] / data['_killsPerc']
data['_totalDistance'] = data['walkDistance'] + data['rideDistance'] + data['swimDistance']
data['_items'] = data['heals'] + data['boosts']
data['_damageDealtAndWalkDistance'] = data['damageDealt'] + data['walkDistance']
data['_headshotRate'] = data['headshotKills'] / data['kills']
data['_killPlace_maxPlace'] = data['killPlace'] / data['maxPlace']
fillInf(data, 0 )
epochs = 30
batch_size = 86
history = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=batch_size),
epochs = epochs, validation_data =(X_val,Y_val),
verbose = 2, steps_per_epoch=X_train.shape[0] // batch_size)
<save_to_csv>
nullCnt = data.isnull().sum()
nullCnt
predictions = model.predict_classes(test, verbose=0)
 submissions=pd.DataFrame({"ImageId": list(range(1,len(predictions)+1)) ,
"Label": predictions})
 submissions.to_csv("vvcp2.csv", index=False, header=True )<set_options>
corr = data.corr()
corr['winPlacePerc'].sort_values(ascending=False)
 del corr
gc.collect()
depCols = ['rideDistance', 'swimDistance', 'heals', 'boosts', 'headshotKills', 'roadKills', 'vehicleDestroys', 'killStreaks', 'DBNOs', 'killPoints', 'rankPoints', 'winPoints', 'matchDuration']
data = data.drop(depCols, axis=1 )
%matplotlib inline<load_from_csv>
minKills = data.sort_values(['matchId', 'groupId', 'kills', 'killPlace'] ).groupby(['matchId', 'groupId', 'kills'] ).first().reset_index().copy()
for n in np.arange(4):
c = 'kills_' + str(n)+ '_Place'
nKills =(minKills['kills'] == n)
 minKills.loc[nKills, c] = minKills[nKills].groupby(['matchId'])['killPlace'].rank().values
matchData = pd.merge(matchData, minKills[nKills][['matchId','groupId',c]], how='left')
 matchData = reduce_mem_usage(matchData)
 del minKills, nKills
%time dfLabel = pd.read_csv('.. /input/digit-recognizer/train.csv' )<load_from_csv>

 data = pd.merge(data, matchData)
 del matchData
gc.collect()
%time dfPredict = pd.read_csv('.. /input/digit-recognizer/test.csv' )<categorify>
data['enemy.players'] = data['m.players'] - data['players']
for c in sum_col:
data['p.max_msum.' + c] = data['max.' + c] / data['m.sum.' + c]
data['p.max_mmax.' + c] = data['max.' + c] / data['m.max.' + c]
data.drop(['m.sum.' + c, 'm.max.' + c], axis=1, inplace=True)
 fillInf(data, 0 )
dfTmp = dfLabel.copy(deep=True)
 tmpLabel = dfTmp['label']
label = to_categorical(tmpLabel, num_classes = 10)
 del dfTmp['label']
dfTmp = dfTmp/255
labeledImage = dfTmp.values.reshape(-1,28,28,1)
 assert labeledImage.shape ==(dfTmp.shape[0],28,28,1), "The tensor shape {} is not equal to expected tensor size {}".format(labeledImage.shape ,(dfTmp.shape[0],28,28,1))
assert len(label)== dfTmp.shape[0], "The size of the labels {} is not equal to the labeld image size {}".format(len(label),dfTmp.shape[0] )<split>

 match = data.groupby('matchId')
 matchRank = match[numCols].rank(pct=True ).rename(columns=lambda s: 'rank.' + s)
 data = reduce_mem_usage(pd.concat([data, matchRank], axis=1))
rank_col = matchRank.columns
del matchRank
gc.collect()
random_state=42
X_train, X_valid, y_train, y_valid = train_test_split(labeledImage, label, test_size = 0.1, random_state = random_state, stratify = label )<choose_model_class>
match = data.groupby('matchId')
 matchRank = match[rank_col].max().rename(columns=lambda s: 'max.' + s ).reset_index()
data = pd.merge(data, matchRank)
 for c in numCols:
data['rank.' + c] = data['rank.' + c] / data['max.rank.' + c]
data.drop(['max.rank.' + c], axis=1, inplace=True)
 del matchRank
gc.collect()
model = models.Sequential()
model.add(layers.Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same', activation ='relu', input_shape =(28,28,1)))
 model.add(layers.Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same', activation ='relu'))
model.add(layers.MaxPool2D(pool_size=(2,2)))
 model.add(layers.Dropout(0.25))
model.add(layers.Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(layers.Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(layers.MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(layers.Dropout(0.25))
model.add(layers.Flatten())
 model.add(layers.Dense(256, activation = "relu"))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(10, activation = "softmax"))<choose_model_class>

 constant_columns = [col for col in data.columns if data[col].nunique() == 1]
print(constant_columns)
 data.drop(constant_columns, axis=1, inplace=True )
optimizer = keras.optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0 )<choose_model_class>
data['matchType'] = data['matchType'].apply(mapper)
 data = pd.concat([data, pd.get_dummies(data['matchType'])], axis=1)
 data.drop(['matchType'], axis=1, inplace=True)
 data['matchId'] = data['matchId'].apply(lambda x: int(x, 16))
data['groupId'] = data['groupId'].apply(lambda x: int(x, 16))
learning_rate_reduction = keras.callbacks.ReduceLROnPlateau(monitor='val_acc', patience=3, verbose=1, factor=0.5, min_lr=0.00001 )<define_variables>
nullCnt = data.isnull().sum().sort_values()
print(nullCnt[nullCnt > 0] )
epochs = 30
batch_size = 512<init_hyperparams>
cols = [col for col in data.columns if col not in ['Id', 'matchId', 'groupId']]
for i, t in data.loc[:, cols].dtypes.iteritems() :
if t == object:
data[i] = pd.factorize(data[i])[0]
data = reduce_mem_usage(data)
 data.head()
datagen = keras.preprocessing.image.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)
 datagen.fit(X_train )<train_model>
X_train = data[data['winPlacePerc'].notnull() ].reset_index(drop=True)
 X_test = data[data['winPlacePerc'].isnull() ].drop(['winPlacePerc'], axis=1 ).reset_index(drop=True)
 del data
gc.collect()
history = model.fit_generator(datagen.flow(X_train,y_train, batch_size=batch_size),
epochs = epochs, validation_data =(X_valid,y_valid),
verbose = 2, steps_per_epoch=X_train.shape[0] // batch_size
, callbacks=[learning_rate_reduction] )<compute_test_metric>
Y_train = X_train.pop('winPlacePerc')
 X_test_grp = X_test[['matchId', 'groupId']].copy()
train_matchId = X_train['matchId']
X_train.drop(['matchId', 'groupId'], axis=1, inplace=True)
 X_test.drop(['matchId', 'groupId'], axis=1, inplace=True)
 print(X_train.shape, X_test.shape )
test_loss, test_acc = model.evaluate(X_valid, y_valid)
 print("The test accuraccy is {}".format(test_acc))<predict_on_test>
print(pd.DataFrame([[val for val in dir() ], [sys.getsizeof(eval(val)) for val in dir() ]],
index=['name','size'] ).T.sort_values('size', ascending=False ).reset_index(drop=True)[:10] )
results = model.predict(testImage)
 results = np.argmax(results,axis = 1)
 results = pd.Series(results,name="Label" )<save_to_csv>
params={'learning_rate': 0.1,
'objective':'mae',
'metric':'mae',
'num_leaves': 31,
'verbose': 1,
'random_state':42,
'bagging_fraction': 0.7,
'feature_fraction': 0.7
}
reg = lgb.LGBMRegressor(**params, n_estimators=10000)
 reg.fit(X_train, Y_train)
 pred = reg.predict(X_test, num_iteration=reg.best_iteration_ )
submission_result = pd.concat([pd.Series(range(1,28001),name = "ImageId"),results],axis = 1)
 submission_result.to_csv("result.csv",index=False )<import_modules>
X_test_grp['_nofit.winPlacePerc'] = pred
group = X_test_grp.groupby(['matchId'])
 X_test_grp['winPlacePerc'] = pred
X_test_grp['_rank.winPlacePerc'] = group['winPlacePerc'].rank(method='min')
 X_test = pd.concat([X_test, X_test_grp], axis=1 )
GradientBoostingClassifier, ExtraTreesClassifier)
 INPUT_DIR = '.. /input'
N_FOLDS = 4
N_ITER = 50
SEED = 32<load_from_csv>
fullgroup =(X_test['numGroups'] == X_test['maxPlace'])
 subset = X_test.loc[fullgroup]
X_test.loc[fullgroup, 'winPlacePerc'] =(subset['_rank.winPlacePerc'].values - 1)/(subset['maxPlace'].values - 1)
 subset = X_test.loc[~fullgroup]
gap = 1.0 /(subset['maxPlace'].values - 1)
 new_perc = np.around(subset['winPlacePerc'].values / gap)* gap
X_test.loc[~fullgroup, 'winPlacePerc'] = new_perc
X_test['winPlacePerc'] = X_test['winPlacePerc'].clip(lower=0,upper=1 )
df_train_raw = pd.read_csv(os.path.join(INPUT_DIR, 'train.csv'))
df_test_raw = pd.read_csv(os.path.join(INPUT_DIR, 'test.csv'))<data_type_conversions>
X_test.loc[X_test['maxPlace'] == 0, 'winPlacePerc'] = 0
X_test.loc[X_test['maxPlace'] == 1, 'winPlacePerc'] = 1
X_test.loc[(X_test['maxPlace'] > 1)&(X_test['numGroups'] == 1), 'winPlacePerc'] = 0
X_test['winPlacePerc'].describe()
def get_title(name):

 title_search = re.search('([A-Za-z]+)\.', name)
 if title_search:
return title_search.group(1)
 return ""
df_full = [df_train_raw.copy() , df_test_raw.copy() ]
for dataset in df_full:
dataset['LastName'] = dataset['Name'].apply(lambda x: str.split(x, ",")[0])
 dataset['LastName'] = dataset['LastName'].astype('category' ).cat.codes
dataset['Namelength'] = dataset['Name'].apply(len)
 dataset['Title'] = dataset['Name'].apply(get_title)
 dataset['Title'] = dataset['Title'].replace(
 ['Lady', 'Countess', 'Capt', 'Col', 'Don', 'Dr', 'Major', 'Rev', 'Sir', 'Jonkheer', 'Dona'], 'Rare')
 dataset['Title'] = dataset['Title'].replace('Mlle', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Ms', 'Miss')
 dataset['Title'] = dataset['Title'].replace('Mme', 'Mrs')
 dataset['Title'] = dataset['Title'].map({"Mr": 1, "Miss": 2, "Mrs": 3, "Master": 4, "Rare": 5})
 dataset['Title'] = dataset['Title'].fillna(0)
 dataset['HasCabin'] = dataset["Cabin"].apply(lambda x: 0 if type(x)== float else 1)
 dataset['FamilySize'] = dataset['SibSp'] + dataset['Parch'] + 1
dataset['IsAlone'] = 0
dataset.loc[dataset['FamilySize'] == 1, 'IsAlone'] = 1
dataset['IsAlone'] = dataset['IsAlone'].astype('category')
 dataset['Embarked'].fillna(dataset['Embarked'].mode() [0], inplace = True)
 dataset['Embarked'] = dataset['Embarked'].map({'S': 0, 'C': 1, 'Q': 2})
 dataset['Fare'] = dataset['Fare'].fillna(dataset['Fare'].median())
 dataset['CatFare'] = pd.qcut(dataset['Fare'], q=5, labels=False)
 dataset['Age'].fillna(dataset['Age'].median() , inplace=True)
 dataset['CatAge'] = pd.qcut(dataset['Age'], q=4, labels=False)
 dataset['Sex'] = dataset['Sex'].map({'female': 0, 'male': 1})
 drop_columns = ['PassengerId', 'Name', 'Cabin', 'Ticket']
dataset.drop(drop_columns, axis=1, inplace=True)
 df_train, df_test = df_full
df_train.describe(include='all' )<normalization>
test = pd.read_csv('.. /input/test_V2.csv')
 test['matchId'] = test['matchId'].apply(lambda x: int(x,16))
test['groupId'] = test['groupId'].apply(lambda x: int(x,16))
submission = pd.merge(test, X_test[['matchId','groupId','winPlacePerc']])
 submission = submission[['Id','winPlacePerc']]
submission.to_csv("submission.csv", index=False )
def min_max_scale(train_data, test_data, numeric_cols):

 data = pd.concat([train_data, test_data])
 scaled_train_data, scaled_test_data = train_data.copy() , test_data.copy()
for feature_name in numeric_cols:
max_v = data[feature_name].max()
min_v = data[feature_name].min()
scaled_train_data[feature_name] =(train_data[feature_name] - min_v)/(max_v - min_v)
 scaled_test_data[feature_name] =(test_data[feature_name] - min_v)/(max_v - min_v)
 return scaled_train_data, scaled_test_data
def categorical_encode(train_data, test_data, categorical_cols):

 data = pd.concat([train_data, test_data])
 scaled_train_data, scaled_test_data = train_data.copy() , test_data.copy()
result = df.copy()
for feature_name in categorical_cols:
max_v = df[feature_name].max()
min_v = df[feature_name].min()
result[feature_name] =(df[feature_name] - min_v)/(max_v - min_v)
 return result
label_column = 'Survived'
cols = list(df_train.columns.values)
 num_cols = [e for e in df_train.select_dtypes(include=[np.number] ).columns.tolist() if e != label_column]
cat_cols = [e for e in cols if e not in num_cols and e != label_column]
print(num_cols, cat_cols)
 x_train, y_train = df_train.drop(label_column, axis=1), df_train[label_column].astype(int)
 x_test = df_test
x_train, x_test = min_max_scale(x_train, x_test, num_cols)
 x_train.describe(include = 'all' )<init_hyperparams>
MODELS = {
'lr': {
'model': LogisticRegression,
'params': {
'fit_intercept': [True, False],
'multi_class': ['ovr'],
'penalty': ['l2'],
'solver': ['newton-cg', 'lbfgs', 'liblinear', 'sag', 'saga'],
'tol': [0.01, 0.05, 0.1, 0.5, 1, 5],
'random_state': [SEED],
},
'best_params': {'tol': 0.05, 'solver': 'newton-cg', 'random_state': 32, 'penalty': 'l2', 'multi_class': 'ovr', 'fit_intercept': True},
'best score': 0.813692480359147,
},
'mlp': {
'model': MLPClassifier,
'params': {
'activation' : ['identity', 'logistic', 'tanh', 'relu'],
'solver' : ['lbfgs', 'adam'],
'learning_rate' : ['constant', 'invscaling', 'adaptive'],
'learning_rate_init': [.01,.05,.1,.2,.5, 1, 2],
'random_state': [SEED],
},
'best_params': {'solver': 'lbfgs', 'random_state': 32, 'learning_rate_init': 2, 'learning_rate': 'adaptive', 'activation': 'identity'},
'best_score': 0.8092031425364759,
},
'dt': {
'model': DecisionTreeClassifier,
'params': {
'criterion': ['gini', 'entropy'],
'max_depth': range(6, 10),
'max_features': ['auto', 'sqrt', 'log2', None],
'min_samples_split': [2, 5, 10],
'min_samples_leaf': [1, 2, 4],
},
'best_params': {'min_samples_split': 2, 'min_samples_leaf': 4, 'max_features': None, 'max_depth': 6, 'criterion': 'gini'},
'best_score': 0.8181818181818182,
},
'svc': {
'model': SVC,
'params': {
'C': [0.1, 0.5, 1., 2., 4.],
'kernel': ['linear', 'poly', 'rbf', 'sigmoid'],
'gamma': ['auto', 'scale'],
'degree': range(5),
'tol': [0.1, 0.5, 1, 5],
},
'best_params': {'tol': 1, 'shrinking': False, 'probability': False, 'kernel': 'rbf', 'gamma': 'scale', 'degree': 4, 'C': 2.0},
'best_score': 0.8428731762065096
},
'rf': {
'model': RandomForestClassifier,
'params': {
'n_estimators': range(10, 251, 20),
'max_features': ['auto', 'sqrt', 'log2', None],
'max_depth': range(5, 20),
'min_samples_split': range(2, 10),
'min_samples_leaf': range(1, 10),
'bootstrap': [True, False],
'random_state': [SEED],
},
'best_params': {'random_state': 32, 'n_jobs': -1, 'n_estimators': 70, 'min_samples_split': 10, 'min_samples_leaf': 2, 'max_features': None, 'max_depth': 17, 'bootstrap': True},
'best_score': 0.8417508417508418
},
'ada': {
'model': AdaBoostClassifier,
'params': {
'n_estimators': range(10, 251, 20),
'learning_rate': [.01,.05,.1,.2,.5, 1, 2],
'algorithm': ['SAMME', 'SAMME.R'],
'random_state': [SEED],
},
'best_params': {'random_state': 32, 'n_estimators': 170, 'learning_rate': 1, 'algorithm': 'SAMME.R'},
'best_score': 0.8237934904601572
},
'et': {
'model': ExtraTreesClassifier,
'params': {
'n_estimators': range(10, 251, 20),
'max_features': ['auto', 'sqrt', 'log2', None],
'max_depth': range(5, 20),
'min_samples_split': range(2, 10),
'min_samples_leaf': range(1, 10),
'bootstrap': [True, False],
'random_state': [SEED],
},
'best_params': {'random_state': 32, 'n_jobs': -1, 'n_estimators': 70, 'min_samples_split': 5, 'min_samples_leaf': 2, 'max_features': None, 'max_depth': 11, 'bootstrap': True},
'best_score': 0.8294051627384961
},
'gb': {
'model': GradientBoostingClassifier,
'params': {
'n_estimators': range(10, 251, 20),
'max_depth': range(5, 20),
'loss': ['deviance', 'exponential'],
'learning_rate': [.01,.05,.1,.2,.5, 1, 2],
'subsample': [.25,.5,.8, 1.],
'min_samples_split': range(2, 10),
'min_samples_leaf': range(1, 10),
'random_state': [SEED],
},
'best_params': {'subsample': 0.5, 'random_state': 32, 'n_estimators': 150, 'min_samples_split': 5, 'min_samples_leaf': 1, 'max_depth': 13, 'loss': 'exponential', 'learning_rate': 1},
'best_score': 0.8361391694725028
}
}<train_on_grid>
<prepare_output><EOS>