Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
all_data['PoolQC'].replace(np.nan, 'None', regex=True, inplace=True )<define_variables>
plt.style.use('fivethirtyeight' )
all_data['MiscFeature'].replace(np.nan, 'None', regex=True, inplace=True )<define_variables>
train_df = pd.read_csv("/kaggle/input/pubg-finish-placement-prediction/train_V2.csv" )
all_data['Alley'].replace(np.nan, 'None', regex=True, inplace=True )<define_variables>
target = 'winPlacePerc'
all_data['Fence'].replace(np.nan, 'None', regex=True, inplace=True )<define_variables>
train_df_wp = train_df.drop(train_df[train_df.winPoints == 0].index )
all_data['FireplaceQu'].replace(np.nan, 'None', regex=True, inplace=True )<categorify>
np.corrcoef(train_df_wp.winPoints, train_df_wp[target] )
garage_categorical = ['GarageType', 'GarageFinish', 'GarageQual', 'GarageCond']
for variable in garage_categorical:
all_data[variable].replace(np.nan, 'None', regex=True, inplace=True )<define_variables>
train_df.isna().sum()
garage_numeric = ['GarageYrBlt', 'GarageCars', 'GarageArea']
for variable in garage_numeric:
all_data[variable].replace(np.nan, 0, regex=True, inplace=True )<define_variables>
train_df.winPlacePerc.fillna(-1, inplace = True )
basement_categorical = ['BsmtQual','BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2']
for variable in basement_categorical:
all_data[variable].replace(np.nan, 'None', regex=True, inplace=True )<categorify>
train_df.matchType.value_counts()
basement_numeric = ['BsmtFinSF1', 'BsmtFinSF2', 'BsmtUnfSF', 'TotalBsmtSF', 'BsmtFullBath', 'BsmtHalfBath']
for variable in basement_numeric:
all_data[variable].replace(np.nan, 0, regex=True, inplace=True )<categorify>
def onehot(series, name):
series_new = pd.DataFrame()
for col in series.unique() :
series_new[name + "_{0}".format(col)] = series == col
return(series_new.astype(int), series_new.columns )
all_data['MasVnrType'].replace(np.nan, 'None', regex=True, inplace=True)
 all_data['MasVnrArea'].replace(np.nan, 0, regex=True, inplace=True )<define_variables>
train_df.matchType.value_counts()
all_data['Functional'].replace(np.nan, 'Typ', regex=True, inplace=True )<drop_column>
encoded_ = onehot(train_df.matchType, "matchType" )
mode_impute_and_plot('Utilities')
 all_data = all_data.drop('Utilities', axis=1 )<sort_values>
train_df_j = train_df.join(encoded_[0])
 train_df_j = train_df_j.drop('matchType', axis = 1)
 train_df_j
cols_with_missing_values = all_data.isnull().sum().sort_values(ascending=False)
 display(pd.DataFrame(cols_with_missing_values[cols_with_missing_values[cols_with_missing_values > 0].index], columns=["Number of Missing Values"])) 
<drop_column>
random_seed = 800
all_data['MixedConditions'] = all_data['Condition1'] + ' - ' + all_data['Condition2']
all_data.drop(labels=['Condition1', 'Condition2'], axis=1, inplace=True )<drop_column>
features = train_df_j.columns
features = features.drop(['Id', 'groupId', 'matchId', target])
 features
all_data['MixedExterior'] = all_data['Exterior1st'] + ' - ' + all_data['Exterior2nd']
all_data.drop(labels=['Exterior1st', 'Exterior2nd'], axis=1, inplace=True )<create_dataframe>
XTrain, XTest, YTrain, YTest = train_test_split(train_df_j[features], train_df_j[target], test_size = 0.33, random_state = random_seed )
SF_df = all_data[['TotalBsmtSF', '1stFlrSF', '2ndFlrSF', 'SalePrice']]
SF_df = SF_df[~SF_df['SalePrice'].isnull() ]
SF_vars = list(SF_df)
 del SF_vars[-1]
SF_summary = []
for SF_type in SF_vars:
corr_val = np.corrcoef(SF_df[SF_type], SF_df['SalePrice'])[1][0]
SF_summary.append(corr_val)
 pd.DataFrame([SF_summary], columns=SF_vars, index=['SalePrice Correlation'] )<create_dataframe>
model = CatBoostRegressor(
 random_state = random_seed,
n_estimators = 1000,
depth = 8,
cat_features = cat_features_encoded,
loss_function = "MAE" )
bath_df = all_data[['BsmtFullBath', 'BsmtHalfBath', 'FullBath', 'HalfBath', 'SalePrice']]
bath_df = bath_df[~bath_df['SalePrice'].isnull() ]
bath_vars = list(bath_df)
 del bath_vars[-1]
bath_summary = []
for bath_type in bath_vars:
corr_val = np.corrcoef(bath_df[bath_type], bath_df['SalePrice'])[1][0]
bath_summary.append(corr_val)
 pd.DataFrame([bath_summary], columns=bath_vars, index=['SalePrice Correlation'] )<categorify>
model.fit(XTrain, YTrain )
neighborhood_dict = dict(zip(neighborhood_prices.index, neighborhood_prices.Cluster))
all_data['Neighborhood_Class'] = all_data['Neighborhood']
all_data['Neighborhood_Class'].replace(neighborhood_dict, inplace = True )<define_variables>
prediction = model.predict(XTest)
 prediction[prediction < 0] = 0
prediction[prediction > 1] = 1
def outlier_detection(data):
Q1, Q3 = np.percentile(data, [25,75])
 IQR = Q3-Q1
lower_cutoff = Q1 -(IQR * 1.5)
 upper_cutoff = Q3 +(IQR * 1.5)
 outliers =(data > Q3+1.5*IQR)|(data < Q1-1.5*IQR)
 return outliers<rename_columns>
mean_absolute_error(prediction, YTest )
temp_train = train_LotFrontage_no_outliers.copy()
temp_train.rename(columns={'1stFlrSF':'X1stFlrSF', '2ndFlrSF': 'X2ndFlrSF', '3SsnPorch': 'X3SsnPorch'}, inplace=True)
 mod = ols('LotFrontage ~ X1stFlrSF + X2ndFlrSF + X3SsnPorch + Alley + BedroomAbvGr + BldgType + BsmtCond + BsmtExposure + BsmtFinSF1 + BsmtFinSF2 + BsmtFinType1 + BsmtFinType2 + BsmtFullBath + BsmtHalfBath + BsmtQual + BsmtUnfSF + CentralAir + Electrical + EnclosedPorch + ExterCond + ExterQual + Fence + FireplaceQu + Fireplaces + Foundation + FullBath + Functional + GarageArea + GarageCars + GarageCond + GarageFinish + GarageQual + GarageType + GarageYrBlt + GrLivArea + HalfBath + Heating + HeatingQC + HouseStyle + KitchenAbvGr + KitchenQual + LandContour + LandSlope + LotArea + LotConfig + LotShape + LowQualFinSF + MSSubClass + MSZoning + MasVnrArea + MasVnrType + MiscFeature + MiscVal + MoSold + Neighborhood + OpenPorchSF + OverallCond + OverallQual + PavedDrive + PoolArea + PoolQC + RoofMatl + RoofStyle + SaleCondition + SaleType + ScreenPorch + Street + TotRmsAbvGrd + TotalBsmtSF + WoodDeckSF + YearBuilt + YearRemodAdd + YrSold + MixedConditions + MixedExterior + TotalSF + TotalBath + Neighborhood_Class', data=temp_train ).fit()
aov_table = sm.stats.anova_lm(mod, typ=3)
 display(aov_table[aov_table['PR(>F)'] < 0.01] )<prepare_x_and_y>
test_df = pd.read_csv("/kaggle/input/pubg-finish-placement-prediction/test_V2.csv")
 test_df
X = train_LotFrontage_no_outliers[aov_table[aov_table['PR(>F)'] < 0.01].index]
y = train_LotFrontage_no_outliers['LotFrontage']
X = pd.get_dummies(X)
 transformer = StandardScaler().fit(X)
 X = transformer.transform(X )<split>
encoded_ = onehot(test_df.matchType, "matchType" )
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=1234)
 clf = svm.SVR(kernel='rbf', C=100, gamma=0.001)
 clf.fit(X_train, y_train)
 preds = clf.predict(X_test)
 print('Mean Absolute Error: %.3f' % mean_absolute_error(y_test, preds))<normalization>
test_df_j = test_df.copy()
train_df_j = test_df_j.drop('matchType', axis = 1)
 test_df_j = test_df_j.join(encoded_[0] )
model_data = train_LotFrontage_no_outliers.copy()
model_data= model_data.append(test_LotFrontage)
 y = model_data['LotFrontage']
model_data = model_data[['MSZoning', 'Alley', 'LotArea', 'LotShape', 'LotConfig', 'Neighborhood', 'MixedConditions', 'GarageType', 'GarageCars', 'GarageArea']]
model_data = pd.get_dummies(model_data)
 model_X_train = model_data[~y.isnull() ]
model_X_test = model_data[y.isnull() ]
model_y_train = y[~y.isnull() ]
transformer = StandardScaler().fit(model_X_train)
 model_X_train = transformer.transform(model_X_train)
 model_X_test = transformer.transform(model_X_test)
 clf = svm.SVR(kernel='rbf', C=100, gamma=0.001)
 clf.fit(model_X_train, model_y_train)
 LotFrontage_preds = clf.predict(model_X_test )<filter>
out_df = pd.DataFrame(np.array([test_df_j.Id.values, model.predict(test_df_j[features])] ).T, columns = ['Id', 'winPlacePerc'] )
all_data.LotFrontage[all_data.LotFrontage.isnull() ] = LotFrontage_preds<drop_column>
out_df.to_csv('submission.csv', index = False )