Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<feature_engineering>
train_data_df = pd.read_csv('.. /input/train.csv')
 test_data_df = pd.read_csv('.. /input/test.csv' )
df_total['HouseSFSq'] = df_total['HouseSF']**2
df_total['HouseSFCb'] = df_total['HouseSF']**3<feature_engineering>
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']
std_group_features = train_data_df.groupby(['matchId','groupId'])[train_data_df.columns[3:-1]].agg('std' ).reset_index().loc[:, 'assists':'winPoints']
df_total['OverQualSq'] = df_total['OverallQual']**2
df_total['OverCondSq'] = df_total['OverallCond']**2<feature_engineering>
features_one = mean_group_features.join(max_group_features, lsuffix='_mean', rsuffix='_max')
 features_two = min_group_features.join(std_group_features, lsuffix='_min', rsuffix='_std')
 features = features_one.join(features_two)
 features = features.fillna(0.0)
 features
df_total['HasPool'] = df_total['PoolArea'].apply(lambda x: 1 if x > 0 else 0)
 df_total['Has2ndFloor'] = df_total['2ndFlrSF'].apply(lambda x: 1 if x > 0 else 0)
 df_total['HasGarage'] = df_total['GarageCars'].apply(lambda x: 1 if x > 0 else 0)
 df_total['HasBsmt'] = df_total['TotalBsmtSF'].apply(lambda x: 1 if x > 0 else 0)
 df_total['HasFireplace'] = df_total['Fireplaces'].apply(lambda x: 1 if x > 0 else 0)
 df_total["HasShed"] =(df_total["MiscFeature"] == "Shed" ).apply(lambda x: 1 if x > 0 else 0 )<define_variables>
targets = train_data_df.groupby(['matchId', 'groupId'])['winPlacePerc'].agg('mean' ).reset_index() ['winPlacePerc']
targets
neighborhood_map = {"MeadowV" : 1, "IDOTRR" : 1, "BrDale" : 1, "OldTown" : 2, "Edwards" : 2, "BrkSide" : 2, "Sawyer" : 2,
"Blueste" : 2, "SWISU" : 2, "NAmes" : 2, "NPkVill" : 2, "Mitchel" : 2, "SawyerW" : 3, "Gilbert" : 3,
"NWAmes" : 3, "Blmngtn" : 3, "CollgCr" : 3, "ClearCr" : 3, "Crawfor" : 3, "Veenker" : 4,
"Somerst" : 4, "Timber" : 4, "StoneBr" : 5, "NoRidge" : 5, "NridgHt" : 5,}
df_total["NeighborhoodCluster"] = df_total["Neighborhood"].map(neighborhood_map )<categorify>
df_total["HighSeason"] = df_total["MoSold"].replace(
 {'1': 0, '2': 0, '3': 0, '4': 1, '5': 1, '6': 1, '7': 1, '8': 0, '9': 0, '10': 0, '11': 0, '12': 0} )<feature_engineering>
import sklearn
df_total["Remodeled"] =(df_total["YearRemodAdd"] != df_total["YearBuilt"] ).apply(lambda x: 1 if x > 0 else 0)
 df_total["RecentRemodel"] =(df_total["YearRemodAdd"] == df_total["YrSold"] ).apply(lambda x: 1 if x > 0 else 0 )<categorify>
scaler = MinMaxScaler(feature_range=(-1, 1)).fit(features.values)
df_total["Unusual_sale"] = df_total.SaleCondition.replace(
 {'Abnorml': 1, 'Alloca': 1, 'AdjLand': 1, 'Family': 1, 'Normal': 0, 'Partial': 0})
 df_total["BoughtOffPlan"] = df_total.SaleCondition.replace(
 {"Abnorml" : 0, "Alloca" : 0, "AdjLand" : 0, "Family" : 0, "Normal" : 0, "Partial" : 1} )<drop_column>
bayes_cv_tuner = BayesSearchCV(
 estimator = CatBoostRegressor(iterations = 1500, eval_metric='MAE')
 ,
search_spaces = {
'learning_rate': [0.05, 0.1, 0.15, 0.2, 0.25, 0.3],
'max_depth':(4, 6),
},
scoring = 'neg_mean_absolute_error',
cv = KFold(
 n_splits=5,
shuffle=True,
random_state=42 
),
n_jobs = 1,
n_iter = 6,
verbose = 0,
refit = True,
random_state = 42
)
df_total.drop(['GarageArea', 'GarageCond', 'TotRmsAbvGrd', 'AboveSF', '2ndFlrSF', 'GarageYrBlt', 'BsmtFinSF2'],
axis = 1, inplace = True)
 df_total = df_total.drop(['Utilities', 'Street', 'PoolQC', 'PoolArea'], axis=1 )<drop_column>
%env JOBLIB_TEMP_FOLDER=/tmp
numerical_features = df_total.select_dtypes(exclude = ["object"] ).columns
numerical_features = numerical_features.drop("SalePrice" )<define_variables>
bayes_cv_tuner.fit(scaler.transform(features.values), targets.values )
print("Pre - dummy number of features : " + str(df_total.shape[1]))<categorify>
model = bayes_cv_tuner.best_estimator_
df_total = pd.get_dummies(df_total)
 print("New number of features : " + str(df_total.shape[1]))<train_model>
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']
std_group_features = test_data_df.groupby(['matchId','groupId'])[train_data_df.columns[3:-1]].agg('std' ).reset_index().loc[:, 'assists':'winPoints']
df_total = df_total.drop(['SalePrice', "MSZoning_C(all)"], axis = 1)
 train = df_total.iloc[:df_train_ends,:]
test = df_total.iloc[df_train_ends:,:]
print(train.shape )<split>
features_one = mean_group_features.join(max_group_features, lsuffix='_mean', rsuffix='_max')
 features_two = min_group_features.join(std_group_features, lsuffix='_min', rsuffix='_std')
 features = features_one.join(features_two)
 features = features.fillna(0.0)
 features
X_train, X_test, y_train, y_test = train_test_split(train, y, test_size = 0.3, random_state = 0)
 print("X_train : " + str(X_train.shape))
print("X_test : " + str(X_test.shape))
print("y_train : " + str(y_train.shape))
print("y_test : " + str(y_test.shape))<normalization>
matches = test_data_df.groupby(['matchId', 'groupId'])['matchId'].agg('mean' ).values
rbstSc = RobustScaler()
X_train.loc[:, numerical_features] = rbstSc.fit_transform(X_train.loc[:, numerical_features])
 X_test.loc[:, numerical_features] = rbstSc.transform(X_test.loc[:, numerical_features] )<normalization>
groups = test_data_df.groupby(['matchId', 'groupId'])['groupId'].agg('mean' ).values
train.loc[:, numerical_features] = rbstSc.fit_transform(train.loc[:, numerical_features])
 test.loc[:, numerical_features] = rbstSc.transform(test.loc[:, numerical_features] )<compute_test_metric>
test_features = features.values
test_features = scaler.transform(test_features )
def rmsle(y, y_pred):
return np.sqrt(mean_squared_error(y, y_pred))<compute_train_metric>
predictions = model.predict(test_features )
y_rf_train = randomforest.predict(X_train)
 y_rf_test = randomforest.predict(X_test)
 print("Random Forest RMSE on Training set :", rmsle(y_train, y_rf_train ).mean())
 print("Random Forest RMSE on Validation set :", rmsle(y_test, y_rf_test ).mean() )<predict_on_test>
features['winPlacePercPred'] = predictions
features['matchId'] = matches
features['groupId'] = groups
group_preds = features.groupby(['matchId', 'groupId'])['winPlacePercPred'].agg('mean' ).groupby('matchId' ).rank(pct=True ).reset_index()
group_preds = group_preds['winPlacePercPred']
randomforest = RandomForestRegressor(n_estimators=1000, random_state=42)
 randomforest.fit(train, y)
 y_rf = randomforest.predict(train)
 print("Random Forest RMSE on Full Training set :", rmsle(y, y_rf ).mean())
 randforest_submission_price = randomforest.predict(test )<compute_train_metric>
test_data_df = test_data_df.sort_values(['matchId', 'groupId'] )
y_train_pred = reg.predict(X_train)
 y_test_pred = reg.predict(X_test)
 print("RMSE on Training set :", rmsle(y_train.values, y_train_pred ).mean())
 print("RMSE on Validation set :", rmsle(y_test.values, y_test_pred ).mean() )<compute_train_metric>
dictionary = dict(zip(features['groupId'].values, group_preds))
reg = LinearRegression()
reg.fit(train, y)
 y_train_OLS = reg.predict(train)
 OLS_predictions = reg.predict(test)
 print("RMSE on Full Training set :", rmsle(y, y_train_OLS ).mean() )<train_on_grid>
new_ranking_preds = []
for i in test_data_df['groupId'].values:
new_ranking_preds.append(dictionary[i])
 test_data_df['winPlacePercPred'] = new_ranking_preds
alphas = np.linspace(1,50,50)
 ridge = RidgeCV(alphas = alphas, cv=10)
 ridge.fit(X_train, y_train)
 alpha = ridge.alpha_
print("Best alpha :", alpha)
 print("Try again for more precision with alphas centered around " + str(alpha))
ridge = RidgeCV(alphas = [alpha *.6, alpha *.65, alpha *.7, alpha *.75, alpha *.8, alpha *.85,
alpha *.9, alpha *.95, alpha, alpha * 1.05, alpha * 1.1, alpha * 1.15,
alpha * 1.25, alpha * 1.3, alpha * 1.35, alpha * 1.4],
cv = 10)
 ridge.fit(X_train, y_train)
 alpha = ridge.alpha_
print("Best alpha :", alpha)
 print("Ridge picked " + str(sum(ridge.coef_ != 0)) + " features and eliminated the other " + \
str(sum(ridge.coef_ == 0)) + " features" )<compute_train_metric>
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(10 )
<train_on_grid><EOS> 
predictions.to_csv('PUBG_preds.csv', index=False )