Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
variables = ['Age', 'Sex_bin', 'Pclass', 'Fare','Family', 'Embarked_S','Embarked_C','Cabin_NaN',\
'CA', 'SOTON', 'PC', 'SC', 'Master', 'Mr', 'Miss', 'C', 'Mrs']
X = df[variables]
y = df['Survived']
reg = LogisticRegression(max_iter = 500)
 reg.fit(X,y)
 resp = reg.predict(df_test[variables])
 submit = pd.Series(resp, index=df_test['PassengerId'], name='Survived')
 submit.to_csv("model.csv", header=True )<set_options>
def take_part_of_data(df, part):
match_ids = df['matchId'].unique()
match_ids_part = np.random.choice(match_ids, int(part * len(match_ids)))
 df = df[df['matchId'].isin(match_ids_part)]
del match_ids
del match_ids_part
warnings.filterwarnings('ignore' )<load_from_csv>
def add_new_features_1(df):
df['totalDistance'] = df['rideDistance'] + df['walkDistance'] + df['swimDistance']
df['healsAndBoosts'] = df['heals'] + df['boosts']
df['headshotKillsOverKills'] = df['headshotKills'] / df['kills']
df['headshotKillsOverKills'].fillna(0, inplace=True)
 df['killStreaksOverKills'] = df['killStreaks'] / df['kills']
df['killStreaksOverKills'].fillna(0, inplace=True)
 df['killsAndAssists'] = df['kills'] + df['assists']
df['assistsAndRevives'] = df['assists'] + df['revives']
Xy_train = pd.read_csv(".. /input/titanic/train.csv", index_col="PassengerId")
 class CC:
def __init__(self, dataframe):
for col in dataframe.columns: setattr(self, col, col)
 cc = CC(Xy_train)
 X_train = Xy_train.drop(columns=[cc.Survived])
 y_train = Xy_train[cc.Survived]<categorify>
def add_new_features_2(df):
df['playersJoined'] = df.groupby('matchId')['matchId'].transform('count')
 df['killsAndAssistsOverPlayersJoined'] = df['killsAndAssists'] *(( 100 - df['playersJoined'])/ 100 + 1)
 df['matchDurationOverPlayersJoined'] = df['matchDuration'] *(( 100 - df['playersJoined'])/ 100 + 1)
 df['damageDealtOverPlayersJoined'] = df['damageDealt'] *(( 100 - df['playersJoined'])/ 100 + 1 )
class GenericTransformer(BaseEstimator, TransformerMixin):

 def __init__(self, transformer, fitter=None):
self.transformer = transformer
self.fitter = fitter
def fit(self, X, y=None):
self.fit_val = None if self.fitter is None else self.fitter(X)
 return self
def transform(self, X):
return self.transformer(X)if self.fit_val is None else self.transformer(X, self.fit_val )<categorify>
def add_new_features_3(df):
df['totalDistanceOverKillsAndAssists'] = df['totalDistance'] / df['killsAndAssists']
df['totalDistanceOverKillsAndAssists'].fillna(0, inplace=True)
 df['totalDistanceOverKillsAndAssists'].replace(np.inf, 0, inplace=True)
 df['totalDistanceOverHealsAndBoosts'] = df['totalDistance'] / df['healsAndBoosts']
df['totalDistanceOverHealsAndBoosts'].fillna(0, inplace=True)
 df['totalDistanceOverHealsAndBoosts'].replace(np.inf, 0, inplace=True )
default_imputer = GenericTransformer(
 fitter = lambda X: {
col: "missing" if X[col].dtype == "object" else X[col].median() for col in X.columns},
transformer = lambda X, default_values:(
 X.assign(**{col: X[col].fillna(default_values[col])for col in X.columns})) , 
)
 assert not default_imputer.fit_transform(X_train ).isna().any().any()<categorify>
def add_new_features_4(df):
df['headshotRate'] = df['kills'] / df['headshotKills']
df['killStreakRate'] = df['killStreaks'] / df['kills']
df['healsAndBoosts'] = df['heals'] + df['boosts']
df['totalDistance'] = df['rideDistance'] + df['walkDistance'] + df['swimDistance']
df['killPlaceOverMaxPlace'] = df['killPlace'] / df['maxPlace']
df['headshotKillsOverKills'] = df['headshotKills'] / df['kills']
df['distanceOverWeapons'] = df['totalDistance'] / df['weaponsAcquired']
df['walkDistanceOverHeals'] = df['walkDistance'] / df['heals']
df['walkDistanceOverKills'] = 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
df.fillna(0, inplace=True )
class DummiesTransformer(BaseEstimator, TransformerMixin):

 def __init__(self, drop_first=False):
self.drop_first = drop_first
def fit_transform(self, x, y=None):
result = pd.get_dummies(x, drop_first=self.drop_first)
 self.output_cols = result.columns
return result
def fit(self, x, y = None):
self.fit_transform(x, y)
 return self
def transform(self, x):
x_dummies = pd.get_dummies(x, drop_first=self.drop_first)
 new_cols = set(x_dummies.columns)
 return pd.DataFrame({col: x_dummies[col] if col in new_cols else 0
for col in self.output_cols})
 add_dummies = DummiesTransformer()
assert(X_train.dtypes == "object" ).any()
assert not(add_dummies.fit_transform(X_train ).dtypes == "object" ).any()<categorify>
def feature_engineering(df, is_train=True):
df['rankPoints'] = np.where(df['rankPoints'] <= 0, 0, df['rankPoints'])
 features = list(df.columns)
 features.remove("matchId")
 features.remove("groupId")
 features.remove("matchDuration")
 features.remove("matchType")
 if 'winPlacePerc' in features:
features.remove('winPlacePerc')
 y = None
if is_train:
y = df.groupby(['matchId','groupId'])['winPlacePerc'].agg('mean')
 elif 'winPlacePerc' in df.columns:
y = df['winPlacePerc']
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'])
 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'])
 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'])
 agg = df.groupby(['matchId','groupId'] ).size().reset_index(name='group_size')
 df_out = df_out.merge(agg, how='left', on=['matchId', 'groupId'])
 agg = df.groupby(['matchId'])[features].agg('mean' ).reset_index()
df_out = df_out.merge(agg, suffixes=["", "_match_mean"], how='left', on=['matchId'])
 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)
 del agg, agg_rank
return df_out, y
drop_original = GenericTransformer(lambda X: X.drop(columns=X_train.columns))
assert len(drop_original.fit_transform(X_train ).columns)== 0<create_dataframe>
class Estimator(object):
def fit(self, x_train, y_train, x_valid, y_valid):
raise NotImplementedException
def predict(self, x):
raise NotImplementedException
feat_accumulator = pd.DataFrame()
model = RidgeClassifier(random_state=0)
 def test_features(label, isolated, combination, prev_label=None, extra_imputers=[]):
isolated_pipeline = make_pipeline(*extra_imputers, default_imputer, *isolated,
drop_original, add_dummies, model)
 iso_accuracy = cross_val_score(isolated_pipeline, X_train, y_train, cv=5 ).mean()
feat_accumulator.loc[label, "isolated"] = iso_accuracy
combo_pipeline = make_pipeline(*extra_imputers, default_imputer, *combination,
drop_original, add_dummies, model)
 combo_accuracy = cross_val_score(combo_pipeline, X_train, y_train, cv=5 ).mean()
feat_accumulator.loc[label, "combination"] = combo_accuracy
if prev_label is not None:
old_combo_accuracy = feat_accumulator.loc[prev_label, "combination"]
feat_accumulator.loc[label, "improvement"] = combo_accuracy - old_combo_accuracy
display(feat_accumulator)
 test_features("null", [add_const], [add_const] )<feature_engineering>
class ScikitLearnEstimator(Estimator):
def __init__(self, estimator):
self.estimator = estimator
def fit(self, x_train, y_train, x_valid, y_valid):
self.estimator.fit(x_train, y_train)
 def predict(self, x):
return self.estimator.predict(x )
add_age = GenericTransformer(
 lambda X: X.assign(AGE_BINNED = pd.cut(
 X[cc.Age], [0,7,14,35,60,1000],
labels=["young child","child", "young adult", "adult", "old"])))
 test_features("age", [add_age], [add_age], "null" )<data_type_conversions>
def fit_predict_step(estimator, x_train, y_train, train_idx, valid_idx, x_test, oof):
x_train_train = x_train[train_idx]
y_train_train = y_train[train_idx]
x_train_valid = x_train[valid_idx]
y_train_valid = y_train[valid_idx]
estimator.fit(x_train_train, y_train_train, x_train_valid, y_train_valid)
 oof_part = estimator.predict(x_train_valid)
 print('MAE:', mean_absolute_error(y_train_valid, oof_part))
oof[valid_idx] = oof_part
y_part = estimator.predict(x_test)
 return y_part
add_class = GenericTransformer(lambda X: X.assign(CLASS_BINNED=X[cc.Pclass].astype(str)))
 test_features("class", [add_class], [add_age, add_class], "age" )<train_on_grid>
def fit_predict(estimator, x_train, y_train, x_test):
oof = np.zeros(x_train.shape[0])
 y = np.zeros(x_test.shape[0])
 kf = KFold(n_splits=5, random_state=42)
 for train_idx, valid_idx in kf.split(x_train):
y_part = fit_predict_step(estimator, x_train, y_train, train_idx, valid_idx, x_test, oof)
 y += y_part / kf.n_splits
print('Final MAE:', mean_absolute_error(y_train, oof))
return oof, y
class TicketSurvivalTransformer(BaseEstimator, TransformerMixin):

 def __init__(self, xy):
self.xy = xy
def fit(self, X, y=None):
X_with_survival = X.assign(Survived = self.xy.reindex(X.index)[cc.Survived])
 self.mean_survival = X_with_survival[cc.Survived].mean()
self.group_stats =(X_with_survival.groupby(cc.Ticket)[cc.Survived].agg(["count", "sum"]))
self.fit_X = X.copy()
return self
def transform(self, X):
X_with_survival = X.assign(Survived = self.xy.reindex(X.index)[cc.Survived])
 group_stats_by_passenger = self.group_stats.reindex(X[cc.Ticket].unique() , fill_value=0)
 X_counts = group_stats_by_passenger.loc[X[cc.Ticket]].set_index(X.index)
 is_overlap = np.array([x in self.fit_X.index for x in X.index])
 other_counts =(X_counts["count"]-1 ).where(is_overlap, X_counts["count"])
 other_survivor_count =(( X_counts["sum"] - self.xy[cc.Survived].reindex(X.index)) 
.where(is_overlap, X_counts["sum"]))
survival_fraction =(other_survivor_count + self.mean_survival)/(other_counts + 1)
 return X.assign(SurvivorFraction=survival_fraction)
 assert np.isclose(( TicketSurvivalTransformer(Xy_train ).fit(X_train.loc[:400])
.transform(X_train.loc[400:])["SurvivorFraction"].mean()),
Xy_train.loc[400:][cc.Survived].mean() , rtol=0.1 )<feature_engineering>
def fit_step(estimator, x_train, y_train, train_idx, valid_idx, oof):
x_train_train = x_train[train_idx]
y_train_train = y_train[train_idx]
x_train_valid = x_train[valid_idx]
y_train_valid = y_train[valid_idx]
estimator.fit(x_train_train, y_train_train, x_train_valid, y_train_valid)
 oof_part = estimator.predict(x_train_valid)
 mae = mean_absolute_error(y_train_valid, oof_part)
 print('MAE:', mae)
 oof[valid_idx] = oof_part
return estimator, mae
add_tkt = TicketSurvivalTransformer(Xy_train)
 test_features("tkt", [add_tkt], [add_age, add_class, add_tkt], "class" )<feature_engineering>
def fit(estimator, x_train, y_train):
oof = np.zeros(x_train.shape[0])
 kf = KFold(n_splits=5, random_state=42)
 trained_estimators = []
for train_idx, valid_idx in kf.split(x_train):
e, mae = fit_step(estimator, x_train, y_train, train_idx, valid_idx, oof)
 trained_estimators.append(deepcopy(e))
print('Final MAE:', mean_absolute_error(y_train, oof))
return oof, trained_estimators
test_features("sex", [add_sex], [add_age, add_class, add_tkt, add_sex], "tkt" )<feature_engineering>
def predict(trained_estimators, x_test):
y = np.zeros(x_test.shape[0])
 for estimator in trained_estimators:
y_part = estimator.predict(x_test)
 y += y_part / len(trained_estimators)
 return y
impute_age = TitleAgeImputer()
test_features("age_from_title", [add_age], [add_age, add_class, add_tkt, add_sex], "sex",
extra_imputers=[impute_age] )<save_to_csv>
def pipeline_fit(estimator, df_train, scaler=None):
add_new_features_4(df_train)
 x_train, y_train = feature_engineering(df_train, is_train=True)
 x_train = reduce_mem_usage(x_train)
 gc.collect()
if not(scaler is None):
scaler.fit(x_train)
 scaled_x_train = scaler.transform(x_train)
 else:
scaled_x_train = x_train.values
del x_train
gc.collect()
oof, trained_estimators = fit(estimator, scaled_x_train, y_train.values)
 del scaled_x_train
del y_train
gc.collect()
return oof, trained_estimators
final_model = RidgeClassifier(random_state=0)
 final_X_train = final_pipeline.fit_transform(X_train)
 cvscore = cross_val_score(final_model, final_X_train, y_train, cv=4)
 print(f"cv = {np.mean(cvscore)}:
{list(cvscore)}")
 final_model.fit(final_X_train, y_train)
 X_test = pd.read_csv(".. /input/titanic/test.csv", index_col="PassengerId")
 final_X_test = final_pipeline.transform(X_test)
 preds_test = final_model.predict(final_X_test)
 output = pd.DataFrame({'PassengerID': X_test.index,
'Survived': preds_test})
 file = 'submission.csv'
output.to_csv(file, index=False)
 print(f"Wrote predictions to '{file}'" )<import_modules>
def pipeline_predict(trained_estimators, df_test, scaler=None):
add_new_features_4(df_test)
 x_test, _ = feature_engineering(df_test, is_train=False)
 x_test = reduce_mem_usage(x_test)
 gc.collect()
if not(scaler is None):
scaled_x_test = scaler.transform(x_test)
 else:
scaled_x_test = x_test.values
del x_test
gc.collect()
y = predict(trained_estimators, scaled_x_test)
 del scaled_x_test
gc.collect()
return y
import csv
import pandas as pd<define_variables>
df_train = pd.read_csv('.. /input/train_V2.csv', index_col='Id')
 df_train.shape
sub_files = [
'.. /input/esemble-lb-0-842/submission_1.csv',
'.. /input/humpback-ensemble/0.858_85_submission_0.45_standard_0.55_boostrap.csv',
]
sub_weight = [
0.842**2,
0.858**2,
]<save_to_csv>
df_train = reduce_mem_usage(df_train )
Hlabel = 'Image'
Htarget = 'Id'
npt = 6
place_weights = {}
for i in range(npt):
place_weights[i] =(1 /(i + 1))
print(place_weights)
 lg = len(sub_files)
 sub = [None]*lg
for i, file in enumerate(sub_files):
print("Reading {}: w={} - {}".format(i, sub_weight[i], file))
reader = csv.DictReader(open(file,"r"))
sub[i] = sorted(reader, key=lambda d: str(d[Hlabel]))
out = open("sub_ens.csv", "w", newline='')
 writer = csv.writer(out)
 writer.writerow([Hlabel,Htarget])
 for p, row in enumerate(sub[0]):
target_weight = {}
for s in range(lg):
row1 = sub[s][p]
for ind, trgt in enumerate(row1[Htarget].split(' ')) :
target_weight[trgt] = target_weight.get(trgt,0)+(place_weights[ind]*sub_weight[s])
 tops_trgt = sorted(target_weight, key=target_weight.get, reverse=True)[:npt]
writer.writerow([row1[Hlabel], " ".join(tops_trgt)])
 out.close()<import_modules>
gc.collect()
print("scikit-learn version: {}".format(sklearn.__version__))
print("StackingRegressor imported" )<load_from_csv>
df_train.drop(df_train[df_train['winPlacePerc'].isnull() ].index, inplace=True )
train_data=pd.read_csv(".. /input/train.csv")
 test_data=pd.read_csv(".. /input/test.csv")
 print("training_data(shape): {}".format(train_data.shape))
print("testing_data(shape): {}".format(test_data.shape))<concatenate>
import lightgbm as lgb
all_data=pd.concat([train_data,test_data], ignore_index="true", axis=0)
 all_data.reset_index()
print("Shape of all data: {}".format(all_data.shape))<train_model>
class LightGBM(Estimator):
def __init__(self, params):
self.params = params
def fit(self, x_train, y_train, x_valid, y_valid):
lgb_train = lgb.Dataset(data=x_train.astype('float32'), label=y_train.astype('float32'))
lgb_valid = lgb.Dataset(data=x_valid.astype('float32'), label=y_valid.astype('float32'))
self.lgb_model = lgb.train(self.params, lgb_train, valid_sets=lgb_valid, verbose_eval=1000)
 def predict(self, x):
return self.lgb_model.predict(x.astype('float32'), num_iteration=self.lgb_model.best_iteration )
print("kurtosis: %f" % SalePrice.kurtosis())
 print("Skewness: %f" % SalePrice.skew())
(mu, sigma)= norm.fit(SalePrice)
 print('Normal dist.( mu = {:.2f} and sigma = {:.2f})'.format(mu, sigma))<count_values>
params = {'objective': 'regression',
'metric': 'mae',
'n_estimators': 10000,
'early_stopping_rounds': 100,
'num_leaves': 300,
'max_depth': 14,
'bagging_fraction': 0.9,
'learning_rate': 0.03,
'bagging_seed': 0,
'num_threads': 4,
'colsample_bytree': 0.7}
count_outliers(SalePrice,"before" )<categorify>
%%time
oof, trained_estimators = pipeline_fit(LightGBM(params), df_train )
def target_transform(target,transformation=None):
if(transformation is None):
print("{} transformation ".format("Log"))
count_zero=sum(val == 0 for val in target)
 if(count_zero > 0):
print("Transformation not applicable for zeroes values")
 else:
SalePrice=np.log1p(target)
 return SalePrice
if(transformation=="sqrt"):
print("{} transformation ".format(transformation))
SalePrice=np.sqrt(target)
 return SalePrice
if(transformation=="boxcox1p"):
print("{} transformation ".format(transformation))
SalePrice=boxcox1p(target , 0.2)
 return SalePrice<categorify>
del df_train
gc.collect()
SalePrice=target_transform(SalePrice )<train_model>
df_test = pd.read_csv('.. /input/test_V2.csv', index_col = 'Id')
 df_test.shape
print("kurtosis: %f" % SalePrice.kurtosis())
 print("Skewness: %f" % SalePrice.skew())
(mu, sigma)= norm.fit(SalePrice)
 print('Normal dist.( mu = {:.2f} and sigma = {:.2f})'.format(mu, sigma))<define_variables>
df_test = reduce_mem_usage(df_test )
categorical_cols=[col for col in all_data.columns if all_data[col].dtype =='O']
numerical_cols=[col for col in all_data.columns if all_data[col].dtype !='O']
print("categorical_columns :
")
 print(categorical_cols)
 print("
")
 print("numerical_columns :
")
 print(numerical_cols )<create_dataframe>
df_test_id = pd.DataFrame(index=df_test.index )
null_percentage =(all_data.isnull().sum() /len(all_data)) *100
null_ratio = null_percentage.sort_values(ascending=False)
 missing_data = pd.DataFrame({'Missing Values Ratio' :null_ratio})
 print(missing_data.shape)
 missing_data.head(10 )<count_values>
gc.collect()
print("Number of columns has a missing data")
 print(len(null_percentage.index)- len([x for x in null_percentage.index if null_percentage.loc[x]==0]))<data_type_conversions>
y = pipeline_predict(trained_estimators, df_test )
all_data["PoolQC"] = all_data["PoolQC"].fillna("None")
 all_data["MiscFeature"] = all_data["MiscFeature"].fillna("None")
 all_data["Alley"] = all_data["Alley"].fillna("None")
 all_data["Fence"] = all_data["Fence"].fillna("None")
 all_data["FireplaceQu"] = all_data["FireplaceQu"].fillna("None")
 all_data["MasVnrType"] = all_data["MasVnrType"].fillna("None")
 all_data["MasVnrArea"] = all_data["MasVnrArea"].fillna(0)
 all_data['MSZoning'] = all_data['MSZoning'].fillna(all_data['MSZoning'].mode() [0])
 all_data['MSSubClass'] = all_data['MSSubClass'].fillna("None")
 all_data["Functional"] = all_data["Functional"].fillna("Typ")
 all_data = all_data.drop(['Utilities'], axis=1)
 all_data["LotFrontage"] = all_data.groupby("Neighborhood")["LotFrontage"].transform(lambda x: x.fillna(x.median()))
for col in ['GarageType', 'GarageFinish', 'GarageQual', 'GarageCond']:
all_data[col] = all_data[col].fillna('None')
 for col in('GarageYrBlt', 'GarageArea', 'GarageCars'):
all_data[col] = all_data[col].fillna(0)
 for col in('BsmtFinSF1', 'BsmtFinSF2', 'BsmtUnfSF','TotalBsmtSF', 'BsmtFullBath', 'BsmtHalfBath'):
all_data[col] = all_data[col].fillna(0)
 for col in('BsmtQual', 'BsmtCond', 'BsmtExposure', 'BsmtFinType1', 'BsmtFinType2'):
all_data[col] = all_data[col].fillna('None')
 for col in ['Electrical','KitchenQual', 'Exterior1st', 'Exterior2nd', 'SaleType']:
all_data[col] = all_data[col].fillna(all_data[col].mode() [0])
<create_dataframe>
del df_test
gc.collect()
null_percentage =(all_data.isnull().sum() /len(all_data)) *100
null_ratio = null_percentage.sort_values(ascending=False)
 missing_data = pd.DataFrame({'Missing Values Ratio' :null_ratio})
 print("Number of columns has a missing data")
 print(len(null_percentage.index)- len([x for x in null_percentage.index if null_percentage.loc[x]==0]))<data_type_conversions>
df_oof = pd.DataFrame()
df_oof['lgb_oof'] = oof
df_oof.to_csv('light_gbm_oof.csv', index_label='id' )
all_data['MSSubClass'] = all_data['MSSubClass'].apply(str)
 all_data['OverallCond'] = all_data['OverallCond'].astype(str)
 all_data['YrSold'] = all_data['YrSold'].astype(str)
 all_data['MoSold'] = all_data['MoSold'].astype(str )<feature_engineering>
df_submission = pd.DataFrame(index=df_test_id.index)
 df_submission['winPlacePerc'] = y
df_submission.to_csv('light_gbm_raw.csv', index_label='Id' )
all_data['TotalSF'] = all_data['TotalBsmtSF'] + all_data['1stFlrSF'] + all_data['2ndFlrSF']<categorify>
df_test = pd.read_csv('.. /input/test_V2.csv')
 df_test.shape
cols_label = ['FireplaceQu', 'BsmtQual', 'BsmtCond', 'GarageQual', 'GarageCond',
'ExterQual', 'ExterCond','HeatingQC', 'PoolQC', 'KitchenQual', 'BsmtFinType1',
'BsmtFinType2', 'Functional', 'Fence', 'BsmtExposure', 'GarageFinish', 'LandSlope',
'LotShape', 'PavedDrive', 'Street', 'Alley', 'CentralAir', 'MSSubClass', 'OverallCond',
'YrSold', 'MoSold']
enc_label=LabelEncoder()
for columns in cols_label:
all_data[columns] = enc_label.fit_transform(all_data[columns] )<feature_engineering>
df_submission = df_submission.merge(df_test[['Id', 'matchId', 'groupId', 'maxPlace', 'numGroups']], on='Id', how='left')
 df_submission.head()
skew_feat=skewness.iloc[:5]
for columns in skew_feat.index:
all_data[columns] = boxcox1p(all_data[columns],0.2)
<normalization>
df_submission_group = df_submission.groupby(['matchId', 'groupId'] ).first().reset_index()
df_submission_group['rank'] = df_submission_group.groupby(['matchId'])['winPlacePerc'].rank()
df_submission_group = df_submission_group.merge(df_submission_group.groupby('matchId')['rank'].max().to_frame('max_rank' ).reset_index() , on='matchId', how='left')
 df_submission_group['adjusted_perc'] =(df_submission_group['rank'] - 1)/(df_submission_group['numGroups'] - 1)
 df_submission = df_submission.merge(df_submission_group[['adjusted_perc', 'matchId', 'groupId']], on=['matchId', 'groupId'], how='left')
 df_submission['winPlacePerc'] = df_submission['adjusted_perc']
df_submission.head()
scale=StandardScaler()
numerical_features=all_data.dtypes[all_data.dtypes != "object"].index
all_data[numerical_features]=scale.fit_transform(all_data[numerical_features].values )<categorify>
df_submission.loc[df_submission.maxPlace == 0, 'winPlacePerc'] = 0
df_submission.loc[df_submission.maxPlace == 1, 'winPlacePerc'] = 1
all_data=pd.get_dummies(all_data )<split>
t = df_submission.loc[df_submission.maxPlace > 1]
gap = 1.0 /(t.maxPlace.values - 1)
 fixed_perc = np.around(t.winPlacePerc.values / gap)* gap
df_submission.loc[df_submission.maxPlace > 1, 'winPlacePerc'] = fixed_perc
train_set=all_data[:len(train_ID)]
predict_set=all_data[len(test_ID)+1:]<split>
df_submission.loc[(df_submission.maxPlace > 1)&(df_submission.numGroups == 1), 'winPlacePerc'] = 0
assert df_submission['winPlacePerc'].isnull().sum() == 0
<compute_test_metric><EOS> 
df_submission[['Id', 'winPlacePerc']].to_csv('light_gbm_adjusted.csv', index=False )