Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<prepare_x_and_y>
train_data_df = pd.read_csv('.. /input/train.csv')
 test_data_df = pd.read_csv('.. /input/test.csv' )
X_train = x_train = train.drop(['label'],1)
 Y_train = train['label']
x_test = test<data_type_conversions>
train_data_df = reduce_mem_usage(train_data_df)
 test_data_df = reduce_mem_usage(test_data_df )
X_train = X_train.astype('float32')
 x_test = x_test.astype('float32')
 X_train = X_train/255
x_test - x_test/255<categorify>
mean_match_features = train_data_df.groupby(['matchId'])[train_data_df.columns[3:-1]].agg('mean' ).reset_index().loc[:, 'assists':'winPoints']
size_match_features = pd.DataFrame(train_data_df.groupby(['matchId'])[train_data_df.columns[3]].agg('size' ).reset_index() [train_data_df.columns[3]])
 size_match_features.columns = ['match_size']
Y_train= tf.keras.utils.to_categorical(Y_train, 10)
 Y_train.shape<split>
mean_group_features = train_data_df.groupby(['matchId','groupId'])[train_data_df.columns[3:-1]].agg('mean' ).reset_index().loc[:, 'assists':'winPoints']
max_group_features = train_data_df.groupby(['matchId','groupId'])[train_data_df.columns[3:-1]].agg('max' ).reset_index().loc[:, 'assists':'winPoints']
min_group_features = train_data_df.groupby(['matchId','groupId'])[train_data_df.columns[3:-1]].agg('min' ).reset_index().loc[:, 'assists':'winPoints']
size_group_features = pd.DataFrame(train_data_df.groupby(['matchId','groupId'])[train_data_df.columns[3]].agg('size' ).reset_index() [train_data_df.columns[3]])
 size_group_features.columns = ['group_size']
x_train, val_x, y_train, val_y = train_test_split(X_train, Y_train, test_size=0.20 )<choose_model_class>
features_three = mean_group_features.join(max_group_features, lsuffix='_group_mean', rsuffix='_group_max')
 features_four = min_group_features.join(size_group_features, lsuffix='_group_min', rsuffix='_group_size')
 features_2 = features_three.join(features_four)
 features_2['matchId'] = train_data_df.groupby(['matchId', 'groupId'])['matchId'].agg('mean' ).values
features_2 = features_2.fillna(0.0)
 features_2 = features_2.replace([np.inf, -np.inf], 0.0 )
es = EarlyStopping(monitor='loss', patience=12)
 filepath="/kaggle/working/bestmodel.h5"
md = ModelCheckpoint(filepath, monitor='loss', verbose=1, save_best_only=True, mode='min' )<choose_model_class>
features = pd.merge(features_2, features_1, on='matchId', how='right' )
datagen = ImageDataGenerator(zoom_range = 0.1,
height_shift_range = 0.1,
width_shift_range = 0.1,
rotation_range = 10 )<choose_model_class>
features = features.drop(['matchId'], axis=1)
 features
epochs = 30
num_classes = 10
batch_size = 30
input_shape =(28, 28, 1)
 adam = tf.keras.optimizers.Adam(learning_rate=0.0001, beta_1=0.9, beta_2=0.999, amsgrad=False )<choose_model_class>
targets = train_data_df.groupby(['matchId', 'groupId'])['winPlacePerc'].agg('mean' ).reset_index() ['winPlacePerc']
targets
model = Sequential()
model.add(Conv2D(32,(3, 3), padding='same', input_shape=input_shape, activation= tf.nn.relu))
model.add(Conv2D(32,(3, 3), padding='same', activation= tf.nn.relu))
model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))
model.add(Conv2D(64,(3, 3), padding='same', activation= tf.nn.relu))
model.add(MaxPool2D(pool_size=(2, 2)))
 model.add(Dropout(0.25))

 model.add(Flatten())
 model.add(Dense(1024, activation=tf.nn.relu))
model.add(Dropout(0.25))

 model.add(Dense(512, activation=tf.nn.relu))
model.add(Dropout(0.25))
model.add(Dense(256, activation=tf.nn.relu))
model.add(Dropout(0.5))
model.add(Dense(10, activation= tf.nn.softmax))
model.compile(optimizer= adam, loss= tf.keras.losses.categorical_crossentropy, metrics=["accuracy"])
 model.summary() 
<train_model>
train_data_df.groupby(['matchId', 'groupId'])['winPlacePerc'].agg('max' )
History = model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size),
epochs = epochs,
validation_data =(val_x, val_y),
callbacks = [es,md],
shuffle= True 
)
<load_pretrained>
train_features = features.values[0:np.int32(0.8*len(features)) ]
train_targets = targets.values[0:np.int32(0.8*len(features)) ]
val_features = features.values[np.int32(0.8*len(features)) :len(features)]
val_targets = targets.values[np.int32(0.8*len(features)) :len(features)]
model1 = load_model("/kaggle/working/bestmodel.h5" )<predict_on_test>
import sklearn
pred = model1.predict(x_test)
 pred_class = model1.predict_classes(x_test )<save_to_csv>
scaler = MinMaxScaler(feature_range=(-1, 1)).fit(features.values)
 train_features = scaler.transform(train_features)
 val_features = scaler.transform(val_features )
submissions=pd.DataFrame({"ImageId": list(range(1,len(pred_class)+1)) ,
"Label": pred_class})
 submissions.to_csv("submissions.csv", index=False, header=True)
 submissions<set_options>
model = CatBoostRegressor(iterations=250, learning_rate=0.1, eval_metric='MAE', max_depth=8)
 model.fit(train_features, train_targets, eval_set=(val_features, val_targets))
pd.set_option('display.float_format', lambda x: '{:.3f}'.format(x))
%matplotlib inline
sns.set(style='darkgrid')
 def ignore_warn(*args, **kwargs):
pass
warnings.warn = ignore_warn
warnings.filterwarnings('ignore')
 print('All relevant packages imported successfully' )<load_from_csv>
mean_match_features = test_data_df.groupby(['matchId'])[train_data_df.columns[3:-1]].agg('mean' ).reset_index().loc[:, 'assists':'winPoints']
size_match_features = pd.DataFrame(test_data_df.groupby(['matchId'])[train_data_df.columns[3]].agg('size' ).reset_index() [train_data_df.columns[3]])
 size_match_features.columns = ['match_size']
df_train = pd.read_csv(".. /input/train.csv")
 df_test = pd.read_csv(".. /input/test.csv")
 print('Data imported')
 print('Checking shape of datasets')
 print("The are {} rows of data in train dataset and {} columns in training dataset".format(str(df_train.shape[0]), str(df_train.shape[1])))
 print("The are {} rows of data in train dataset and {} columns in training dataset".format(str(df_test.shape[0]), str(df_test.shape[1])))
 train_ID = df_train.iloc[:,0]
test_ID = df_test.iloc[:,0]<count_unique_values>
mean_group_features = test_data_df.groupby(['matchId','groupId'])[train_data_df.columns[3:-1]].agg('mean' ).reset_index().loc[:, 'assists':'winPoints']
max_group_features = test_data_df.groupby(['matchId','groupId'])[train_data_df.columns[3:-1]].agg('max' ).reset_index().loc[:, 'assists':'winPoints']
min_group_features = test_data_df.groupby(['matchId','groupId'])[train_data_df.columns[3:-1]].agg('min' ).reset_index().loc[:, 'assists':'winPoints']
size_group_features = pd.DataFrame(test_data_df.groupby(['matchId','groupId'])[train_data_df.columns[3]].agg('size' ).reset_index() [train_data_df.columns[3]])
 size_group_features.columns = ['group_size']
unique_ID = len(set(df_train.Id))
total_ID = df_train.shape[0]
duplicate_ID = total_ID - unique_ID
print("There are " + str(duplicate_ID)+ " duplicate IDs for " + str(total_ID)+ " total rows" )<feature_engineering>
features_three = mean_group_features.join(max_group_features, lsuffix='_group_mean', rsuffix='_group_max')
 features_four = min_group_features.join(size_group_features, lsuffix='_group_min', rsuffix='_group_size')
 features_2 = features_three.join(features_four)
 features_2['matchId'] = test_data_df.groupby(['matchId', 'groupId'])['matchId'].agg('mean' ).values
features_2 = features_2.fillna(0.0)
 features_2 = features_2.replace([np.inf, -np.inf], 0.0 )
df_train['Above Ground Standardised'] = np.abs(stats.zscore(df_train.GrLivArea))
df_train['Above Ground Standardised'].nlargest(10 ).to_frame()<feature_engineering>
features = pd.merge(features_2, features_1, on='matchId', how='right' )
df_train['Selling Price Standardised'] = np.abs(stats.zscore(df_train.SalePrice))
df_train[df_train['Above Ground Standardised']>4]<drop_column>
matches = test_data_df.groupby(['matchId', 'groupId'])['matchId'].agg('mean' ).values
df_train = df_train.drop(df_train[(df_train['Above Ground Standardised']> 4)].index)
 print('Outliers dropped successfully' )<drop_column>
groups = test_data_df.groupby(['matchId', 'groupId'])['groupId'].agg('mean' ).values
df_train = df_train.drop(['Above Ground Standardised','Selling Price Standardised', "TotalBathroom"], axis = 1 )<drop_column>
features = features.drop(['matchId'], axis=1 )
df_train_ends = df_train.shape[0]
df_train1 = df_train.copy()
df_test1 = df_test.copy()
df_total = pd.concat(( df_train1, df_test1))
df_total = df_total.drop('Id', axis = 1 )<count_values>
test_features = features.values
test_features = scaler.transform(test_features )
print("There are {} anomalies in the YearBuilt variable".format(df_total[(df_total['YearBuilt'] > 2010)|(df_total['YearBuilt'] < 1858)]['YearBuilt'].count()))
print("There are {} anomalies in the YearRemodAdd variable".format(df_total[(df_total['YearRemodAdd'] > 2010)|(df_total['YearRemodAdd'] < 1858)]['YearRemodAdd'].count()))
print("There are {} anomalies in the GarageYrBlt variable".format(df_total[(df_total['GarageYrBlt'] > 2010)|(df_total['GarageYrBlt'] < 1858)]['GarageYrBlt'].count()))
print("There are {} anomalies in the YrSold variable".format(df_total[(df_total['YrSold'] > 2010)|(df_total['YrSold'] < 2006)]['YrSold'].count()))<filter>
predictions = model.predict(test_features )
df_total[(df_total['GarageYrBlt'] > 2010)|(df_total['GarageYrBlt'] < 1858)][['YearBuilt', 'YearRemodAdd', 'GarageYrBlt']]<feature_engineering>
features['winPlacePercPred'] = predictions
features['matchId'] = matches
features['groupId'] = groups
group_preds = features.groupby(['matchId', 'groupId'])['winPlacePercPred'].agg('mean' ).groupby(['matchId'] ).rank(pct=True )
df_total.loc[1132,'GarageYrBlt'] = '2007'<create_dataframe>
test_data_df = test_data_df.sort_values(['matchId', 'groupId'] )
df_total_na =(df_total.isnull().sum() / len(df_total)) * 100
df_total_na = df_total_na.drop(df_total_na[df_total_na == 0].index ).sort_values(ascending=False)[:30]
missing_data = pd.DataFrame({'Percentage of data missing' :df_total_na})
 missing_data.head(20 )<groupby>
dictionary = dict(zip(features['groupId'].values, group_preds.values))
df_total['LotFrontage'] = df_total.groupby('Neighborhood')['LotFrontage'].transform(lambda x: x.fillna(x.median()))<data_type_conversions>
new_ranking_preds = []
for i in test_data_df['groupId'].values:
new_ranking_preds.append(dictionary[i])
 test_data_df['winPlacePercPred'] = new_ranking_preds
df_total['MSZoning'] = df_total['MSZoning'].fillna(df_total['MSZoning'].mode() [0] )<groupby>
predictions = pd.DataFrame(np.transpose(np.array([test_data_df.loc[:, 'Id'], test_data_df['winPlacePercPred']])))
 predictions.columns = ['Id', 'winPlacePerc']
predictions['Id'] = np.int32(predictions['Id'])
 predictions = predictions.sort_values(by=['Id'])
 predictions.head(20 )
df_total['MSSubClass'] = df_total.groupby('Neighborhood')['MSSubClass'].transform(lambda x: x.fillna(x.mode()))<data_type_conversions>
maxPlaces = test_data_df.sort_values(by=['Id'])['maxPlace'].values
numGroups = test_data_df.sort_values(by=['Id'])['numGroups'].values
new_predictions = predictions['winPlacePerc'].values
for i in range(0, len(test_data_df)) :
gap = 1.0 /(maxPlaces[i] - 1.0)
 new_predictions[i] = round(new_predictions[i]/gap)*gap
df_total['GarageYrBlt'] = df_total['GarageYrBlt'].fillna(df_total['YearBuilt'] )<categorify>
predictions['winPlacePerc'] = new_predictions
modes = ["Functional", 'Electrical', "Utilities",'KitchenQual', 'Exterior1st', 'Exterior2nd', 'SaleType']
for col in modes:
df_total[col] = df_total[col].fillna(df_total[col].mode() [0] )<categorify>
predictions.to_csv('PUBG_preds.csv', index=False )