Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
submit = pd.DataFrame()
submit['Id'] = test_ID
submit['SalePrice'] = final_price
submit.to_csv('submission.csv',index=False )<load_from_csv>
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
pd.set_option('display.max_rows', 100)
 pd.set_option('display.max_columns', 100)
 filterwarnings('ignore')
 train_set=pd.read_csv(r'.. /input/train.csv')
 test_set=pd.read_csv(r'.. /input/test.csv')
 train_test = pd.concat([train_set,test_set],axis=0 ).reset_index(drop=True)
 train_test = train_test.drop('Id',axis=1 )<data_type_conversions>
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']
EasyVariables = ['MiscFeature', 'MasVnrType', 'GarageType']
for var in EasyVariables:
train_test[var] = train_test[var].fillna('DNE' )<correct_missing_values>
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 )
FillWith0 = ['BsmtFinType2', 'BsmtCond', 'BsmtQual', 'BsmtExposure', 'BsmtFinType1','BsmtFinSF1','BsmtFinSF2','TotalBsmtSF',
'BsmtUnfSF','BsmtHalfBath','BsmtFullBath', 'FireplaceQu','GarageYrBlt','MasVnrArea']
for var in FillWith0:
train_test[var] = train_test[var].fillna(0)
<data_type_conversions>
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 )
train_test.Utilities = train_test.Utilities.fillna(4)
 train_test.Functional = train_test.Functional.fillna('Typ')
 train_test.SaleType = train_test.SaleType.fillna('WD')
 train_test.KitchenQual = train_test.KitchenQual.fillna(3)
 train_test.Electrical = train_test.Electrical.fillna('SBrkr')
 train_test.MSZoning = train_test.MSZoning.fillna('RL')
 train_test[['Exterior1st','Exterior2nd']] = train_test[['Exterior1st','Exterior2nd']].fillna('VinylSd')
<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 )
train_test = train_test.replace({'MSSubClass':{
20:'1-STORY 1946 & NEWER ALL STYLES',
30:'1-STORY 1945 & OLDER',
40:'1-STORY W/FINISHED ATTIC ALL AGES',
45:'1-1/2 STORY - UNFINISHED ALL AGES',
50:'1-1/2 STORY FINISHED ALL AGES',
60:'2-STORY 1946 & NEWER',
70:'2-STORY 1945 & OLDER',
75:'2-1/2 STORY ALL AGES',
80:'SPLIT OR MULTI-LEVEL',
85:'SPLIT FOYER',
90:'DUPLEX - ALL STYLES AND AGES',
120:'1-STORY PUD(Planned Unit Development)- 1946 & NEWER',
150:'1-1/2 STORY PUD - ALL AGES',
160:'2-STORY PUD - 1946 & NEWER',
180:'PUD - MULTILEVEL - INCL SPLIT LEV/FOYER',
190:'2 FAMILY CONVERSION - ALL STYLES AND AGES'}})
<feature_engineering>
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
train_test.loc[[2420,2503,2599],'PoolQC'] = 3
train_test.loc[2576,'GarageYrBlt'] = 1923
train_test.loc[2126,'GarageYrBlt'] = 1910
train_test.loc[[2576,2126],'GarageFinish'] = 1
train_test.loc[[2576,2126],'GarageQual'] = 3
train_test.loc[[2576,2126],'GarageCond'] = 3
train_test.loc[2576, 'GarageCars'] = 1
train_test.loc[2576, 'GarageArea'] = 288
train_test.loc[2592,'GarageYrBlt'] = 2007
train_test.loc[[2040, 2524, 2185, 2040],'BsmtCond'] = 3
train_test.loc[[2348,1487,948],'BsmtExposure'] = 3
train_test.loc[[2217, 2218],'BsmtQual'] = 3
train_test.loc[2284,'BsmtFinSF1'] = 638
train_test.loc[2284,'BsmtFinSF2'] = 0
train_test = train_test.drop(332, axis=0)
 train_test = train_test.drop([1200, 873],axis=0 )<drop_column>
class Estimator(object):
def fit(self, x_train, y_train, x_valid, y_valid):
raise NotImplementedException
def predict(self, x):
raise NotImplementedException
index = train_test[(train_test.OverallQual==10)&(train_test.SalePrice<200000)].index
train_test = train_test.drop(index,axis=0)
 index = train_test[(train_test.GarageArea>1200)&(train_test.SalePrice<300000)].index
train_test = train_test.drop(index, axis=0)
 index = train_test[(train_test.YearRemodAdd<1970)&(train_test.SalePrice>300000)].index
train_test = train_test.drop(index,axis=0)
 index = train_test[(train_test['TotRmsAbvGrd']==14)].index
train_test = train_test.drop(index,axis=0 )<train_model>
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 )
train_test1 = train_test.copy()
train_test1 = pd.get_dummies(train_test1)
 trainX = train_test1[train_test1.LotFrontage.isnull() ==False].drop(['SalePrice','LotFrontage'],axis=1)
 testX = train_test[train_test.LotFrontage.isnull() ==True].drop(['SalePrice','LotFrontage'],axis=1)
 trainY = train_test1.LotFrontage[train_test1.LotFrontage.isnull() ==False]
trainx, testx, trainy, testy = train_test_split(trainX, np.log(trainY), test_size=0.3, random_state=0)
 model = Lasso(alpha=0.0009)
 model.fit(trainx,trainy)
 Predictions = model.predict(testx)
 MSE = np.sqrt(mean_squared_error(np.exp(testy),np.exp(Predictions)))
 print('LotFrontage MSE:',MSE,'
MSE / LotFrontage mean ratio:', MSE/(np.exp(testy.mean())) )<prepare_x_and_y>
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
trainX = train_test1[train_test1.LotFrontage.isnull() ==False].drop(['SalePrice','LotFrontage'],axis=1)
 testX = train_test1[train_test1.LotFrontage.isnull() ==True].drop(['SalePrice','LotFrontage'],axis=1)
 trainY = train_test1.LotFrontage[train_test1.LotFrontage.isnull() ==False]
model = Lasso(alpha=0.0009)
 model.fit(trainx,trainy)
 Predictions = model.predict(testX)
 train_test.loc[train_test.LotFrontage.isnull() ==True, 'LotFrontage'] = Predictions<feature_engineering>
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
train_test['t'] = train_test.YrSold.map({2006:1, 2007:2, 2008:3, 2009:4, 2010:5})
 train_test.t = train_test.MoSold * train_test.t
train_test = train_test.replace({'MoSold':{1:'Jan',2:'Feb',3:'Mar',4:'Apr',5:'May',6:
'Jun',7:'Jul',8:'Aug',9:'Sep',10:'Oct',11:'Nov',12:'Dec'}})
 train_test['GarageQualityOverall'] = train_test.GarageQual + train_test.GarageCond
train_test['HasPool'] = train_test.PoolQC.map(lambda x: 1 if x>0 else 0)
 train_test['HasGarage'] = train_test.GarageFinish.map(lambda x: 1 if x>0 else 0)
 train_test['HasFireplace'] = train_test.Fireplaces.map(lambda x: 1 if x>0 else 0)
 train_test['HasBasement'] = train_test.TotalBsmtSF.map(lambda x: 1 if x>0 else 0)
 train_test['AllStoriesFinished'] = train_test.HouseStyle.map(lambda x: 0 if(x=='1.5Unf' or x=='2.5Unf')else 1)
 train_test['CanHold2Cars'] = train_test.GarageCars.map(lambda x: 1 if x>=2 else 0)
 train_test['AfterCrisis'] = train_test.YrSold.map(lambda x: 1 if x>=2008 else 0)
 closed = train_test['EnclosedPorch'] + train_test['3SsnPorch'] + train_test['ScreenPorch']
opened = train_test['WoodDeckSF'] + train_test['OpenPorchSF']
train_test['PorchArea'] = closed + opened
train_test['HasPorch'] = train_test['PorchArea'].map(lambda x: 1 if x>0 else 0)
 train_test['PorchClosed'] = closed.map(lambda x: 1 if x > 0 else 0)
 IsFresh = train_test[(train_test.YearRemodAdd == train_test.YrSold)].index
train_test['IsFresh'] = train_test.index.map(lambda x: 1 if x in IsFresh else 0)
 train_test['BsmtFinType'] = train_test.BsmtFinType2 + train_test.BsmtFinType1
train_test['BsmtUnfinPercent'] = train_test.BsmtUnfSF / train_test.TotalBsmtSF
train_test.loc[train_test['BsmtUnfinPercent'].isnull() ,'BsmtUnfinPercent'] = 0
train_test['TotalHouseSF'] = train_test.TotalBsmtSF + train_test.GrLivArea
train_test['AmtOfStories'] = 0
train_test.loc[train_test.HouseStyle=='1Story','AmtOfStories'] = 1
train_test.loc[(train_test.HouseStyle=='1.5Fin')|(train_test.HouseStyle=='1.5Unf'), 'AmtOfStories'] = 1.5
train_test.loc[(train_test.HouseStyle=='2.5Fin')|(train_test.HouseStyle=='2.5Unf'), 'AmtOfStories'] = 2.5
train_test.loc[(train_test.HouseStyle=='2Story'), 'AmtOfStories'] = 2
train_test['OverallQuality'] = train_test.OverallQual + train_test.OverallCond
train_test['BasementQuality'] = train_test.BsmtCond + train_test.BsmtExposure
train_test['Bathrooms'] = train_test.BsmtFullBath + train_test.BsmtHalfBath/2 + train_test.FullBath + train_test.HalfBath/2
train_test['ExteriorQuality'] = train_test.ExterQual + train_test.ExterCond
train_test['Fireplaces_times_FireplaceQu'] = train_test.Fireplaces * train_test.FireplaceQu
train_test['BsmtFinSF'] = train_test.BsmtFinSF1 + train_test.BsmtFinSF2
train_test['YearBltandYearRemodAdd'] = train_test.YearBuilt + train_test.YearRemodAdd
train_test['GarageTypeGrades'] = train_test.GarageType.replace({'2Types':6,'Attchd':5,'Basment':4,'BuiltIn':3,'CarPort':2,'Detchd':1,'No_garage':0})
 Season = ['Mar', 'Apr', 'May', 'Jun']
train_test['SeasonForBuing'] = train_test.MoSold.map(lambda x: 1 if x in Season else 0)
 train_test = train_test.drop('Utilities',axis=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
train_test['RichNeighborhood'] = np.NaN
ones = NeighborhoodsBinned[0:3].index
twos = NeighborhoodsBinned[3:12].index
threes = NeighborhoodsBinned[12:19].index
fours = NeighborhoodsBinned[19:22].index
fives = NeighborhoodsBinned[22:].index
i=0
for variable in [ones, twos, threes, fours, fives]:
i += 1
for neighborhood in variable:
train_test.loc[train_test.Neighborhood==neighborhood,'RichNeighborhood'] = i
train_test = train_test.drop('Neighborhood',axis=1 )<categorify>
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
train_test.Exterior1st = train_test.Exterior1st.map(lambda x: 'Other' if(x!='CemntBd' and x!='VinylSd')else x)
 train_test.Exterior2nd = train_test.Exterior2nd.map(lambda x: 'Other' if(x!='CemntBd' and x!='VinylSd')else x)
 train_test.Functional = train_test.Functional.map(lambda x: 'Other' if x!='Typ' else x)
 train_test.GarageYrBlt = train_test.GarageYrBlt.map(lambda x: 'DNE' if x==0 else 'Classic' if x<=1935 else 'Modern' if x>=1995 else 'Mid-age')
 train_test.SaleType = train_test.SaleType.map(lambda x: 'Other' if x!='WD' else x)
 train_test.SaleCondition = train_test.SaleCondition.map(lambda x: 'Other' if(x!='Normal' and x!='Partial')else x)
 train_test.Condition1 = train_test.Condition1.map(lambda x: x if x in ['Norm', 'Feedr', 'Artery'] else 'Other')
 train_test.RoofMatl = train_test.RoofMatl.map(lambda x: 'Other' if x!='CompShg' else x)
 train_test = train_test.replace({'BldgType':{'Duplex':'Other', 'Twnhs':'Other', '2fmCon':'Other'},
'Electrical':{'FuseF':'Other','FuseP':'Other','Mix':'Other'},
'Foundation':{'Slab':'Other','Stone':'Other','Wood':'Other'},
'Heating':{'Grav':'Other','Wall':'Other','OthW':'Other','Floor':'Other'},
'HouseStyle':{'2.5Unf':'Other','1.5Unf':'Other','2.5Fin':'Other'},
'MSSubClass':{'1-1/2 STORY PUD - ALL AGES':'Other','1-STORY W/FINISHED ATTIC ALL AGES':'Other'\
,'PUD - MULTILEVEL - INCL SPLIT LEV/FOYER':'Other','1-1/2 STORY - UNFINISHED ALL AGES':'Other'\
,'2-1/2 STORY ALL AGES':'Other'},
'MSZoning':{'RH':'Other','C(all)':'Other'},
'MiscFeature':{'Gar2':'Other','TenC':'Other'}} )<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
for var in ['1stFlrSF','BsmtFinSF2','MasVnrArea', 'BsmtFinSF', 'TotalBsmtSF', 'TotalHouseSF']:
train_test['LOG{}'.format(var)] = np.log(1+train_test[var])
 train_test['LOGYearBltandYearRemodAdd'] = train_test['YearBltandYearRemodAdd']**2
train_test = train_test.drop(['1stFlrSF','BsmtFinSF','TotalBsmtSF'],axis=1 )<drop_column>
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
oof, trained_estimators = fit(estimator, scaled_x_train, y_train.values)
 del x_train
del scaled_x_train
del y_train
gc.collect()
return oof, trained_estimators
train_test.SalePrice = np.log(train_test.SalePrice)
 train_test = train_test.drop(['GarageQual','GarageCond','EnclosedPorch','3SsnPorch','OpenPorchSF','BsmtFinType2','BsmtFinType1','BsmtUnfSF','GrLivArea',
'BsmtCond','BsmtExposure','BsmtFullBath','BsmtHalfBath','FullBath','ExterCond'],axis=1 )<train_on_grid>
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
y = predict(trained_estimators, scaled_x_test)
 del x_test
del scaled_x_test
gc.collect()
return y
y =(train_test.SalePrice[train_test.SalePrice.isnull() ==False])
 train_test = pd.get_dummies(train_test)
 X = train_test[train_test.SalePrice.isnull() ==False].drop('SalePrice',axis=1)
 testX = train_test[train_test.SalePrice.isnull() ==True].drop('SalePrice',axis=1)
 model = Lasso(alpha = 0.0008)
 model.fit(X,y)
 predictions = model.predict(testX)
 model = ElasticNet(alpha = 0.0008)
 model.fit(X,y)
 predictions2 = model.predict(testX)
 model = XGBRegressor(n_estimators=4500)
 model.fit(X,y)
 predictions3 = model.predict(testX)
 predictions =(predictions + predictions2 + predictions2 + predictions3)/4
Final = pd.DataFrame({'Id':range(1461,2920),'SalePrice': np.exp(predictions)} ).set_index('Id')
 Final.to_csv(r'Blended.csv' )<create_dataframe>
df_train = pd.read_csv('.. /input/train_V2.csv', index_col='Id')
 df_train.shape
def get_missing_data_table(dataframe):

 total = dataframe.isnull().sum()
percentage = dataframe.isnull().sum() / dataframe.isnull().count()
missing_data = pd.concat([total, percentage], axis='columns', keys=['TOTAL','PERCENTAGE'])
 return missing_data.sort_index(ascending=True)
 def get_null_observations(dataframe, column):

 return dataframe[pd.isnull(dataframe[column])]
def delete_null_observations(dataframe, column):

 fixed_df = dataframe.drop(get_null_observations(dataframe,column ).index)
 return fixed_df
def transform_dummy_variables(dataframe, columns):

 df = dataframe.copy()
for column in columns:
df[column] = pd.Categorical(df[column])
 df = pd.get_dummies(df, drop_first=False)
 return df
def imput_nan_values(dataframe, column, strateg):
imp = Imputer(strategy=strateg)
 df = dataframe.copy()
df[column] = imp.fit_transform(df[column].values.reshape(-1,1))
return df
print("Everything's ready!" )<load_from_csv>
df_train = reduce_mem_usage(df_train )
df = pd.read_csv('.. /input/train.csv')
 df.head()<count_missing_values>
gc.collect()
get_missing_data_table(df )<drop_column>
df_train.drop(df_train[df_train['winPlacePerc'].isnull() ].index, inplace=True )
df = df.drop('Cabin', axis='columns')
 df = delete_null_observations(df, column='Embarked')
 df = df.reset_index(drop=True)
 df['Age'] = df['Age'].fillna(value=1000)
 get_missing_data_table(df )<drop_column>
import torch
from torch.utils.data import DataLoader, TensorDataset
import torch.nn as nn
from torch.nn.utils.weight_norm import weight_norm
df['Family Size'] = df['SibSp'] + df['Parch']
df = df.drop('SibSp', axis='columns')
 df = df.drop('Parch', axis='columns')
 df.head(5 )<drop_column>
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 device
titles = name_row.tolist()
for i in range(len(titles)) :
title = titles[i]
if title != 'Master' and title != 'Miss' and title != 'Mr' and title !='Mrs':
titles[i] = 'Other'
name_row = pd.DataFrame(titles, columns=['Title'])
 df['Title'] = name_row.copy()
df = df.drop('Name', axis='columns')
 df.head(5 )<prepare_output>
class PyTorch(Estimator):
def fit(self, x_train, y_train, x_valid, y_valid):
train_tensor = TensorDataset(
 torch.from_numpy(x_train.astype('float32')) ,
torch.from_numpy(y_train.astype('float32')))
 train_loader = DataLoader(train_tensor, batch_size=256, shuffle=True)
 self.model = nn.Sequential(
 weight_norm(nn.Linear(x_train.shape[1], 128)) ,
nn.ReLU() ,
weight_norm(nn.Linear(128, 128)) ,
nn.ReLU() ,
weight_norm(nn.Linear(128, 128)) ,
nn.ReLU() ,
weight_norm(nn.Linear(128, 128)) ,
nn.ReLU() ,
weight_norm(nn.Linear(128, 1)) ).to(device)
 for m in self.model:
if isinstance(m, nn.Linear):
nn.init.kaiming_normal_(m.weight_v)
 nn.init.kaiming_normal_(m.weight_g)
 nn.init.constant_(m.bias, 0)
 criterion = nn.L1Loss()
optimizer = torch.optim.Adam(self.model.parameters() , betas=(0.9, 0.999), lr=1e-3)
 self.model.train()
n_epochs = 50
for epoch in range(n_epochs):
epoch_loss = 0.0
for train_part, y_part in train_loader:
optimizer.zero_grad()
y_pred_part = self.model(train_part.to(device))
loss = criterion(y_pred_part.reshape(-1), y_part.to(device))
loss.backward()
optimizer.step()
epoch_loss += y_pred_part.shape[0] * loss.item()
print('Epoch %3d / %3d.Loss = %.5f' %(epoch + 1, n_epochs, epoch_loss / x_train.shape[0]))
def predict(self, x):
self.model.eval()
x_tensor = torch.from_numpy(x.astype('float32'))
x_loader = DataLoader(x_tensor, batch_size=256, shuffle=False)
 y_pred = np.empty(0)
 with torch.no_grad() :
for x_part in x_loader:
y_pred_part = self.model(x_part.to(device)).data.cpu().numpy().reshape(-1)
 y_pred = np.append(y_pred, y_pred_part)
 return y_pred
test_df = df.copy()
test_df = pd.DataFrame([df['Age'].tolist() , df['Title'].tolist() ] ).transpose()
test_df.columns = ['Age','Title']
test_df_list = test_df.values
for i in range(len(test_df_list)) :
age = test_df_list[i][0]
title = test_df_list[i][1]
if age == 1000:
if title == 'Master':
test_df_list[i][0] = 5.19
elif title == 'Miss':
test_df_list[i][0] = 21.87
elif title == 'Mr':
test_df_list[i][0] = 32.18
elif title == 'Mrs':
test_df_list[i][0] = 35.48
else:
test_df_list[i][0] = 42.81
df['Age'] = test_df['Age'].copy()
df['Age'] = df['Age'].astype('float64')
 df['Age'].describe()<drop_column>
%%time
scaler = StandardScaler()
oof, trained_estimators = pipeline_fit(PyTorch() , df_train, scaler )
df = df.drop('Ticket', axis='columns')
 df = df.drop('PassengerId', axis='columns')
 df.head(5 )<categorify>
del df_train
gc.collect()
df = transform_dummy_variables(df,['Sex','Pclass','Embarked','Title'])
 df.head(5 )<prepare_x_and_y>
df_test = pd.read_csv('.. /input/test_V2.csv', index_col = 'Id')
 df_test.shape
X_train = df.iloc[:,1:].values
y = df.iloc[:,0].values
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
 print('X_train: {0}'.format(X_train[0:5]))
print('y: {0}'.format(y[0:5]))<train_model>
df_test = reduce_mem_usage(df_test )
classifier = XGBClassifier()
classifier.fit(X_train, y )<define_search_space>
df_test_id = pd.DataFrame(index=df_test.index )
params = {
'min_child_weight': [1, 5, 10],
'gamma': [0.5, 1, 1.5, 2, 5],
'subsample': [0.6, 0.8, 1.0],
'colsample_bytree': [0.6, 0.8, 1.0],
'max_depth': [3, 4, 5]
}
folds = 4
param_comb = 5
skf = StratifiedKFold(n_splits=folds, shuffle = True, random_state = 1001)
 random_search = RandomizedSearchCV(classifier, param_distributions=params, n_iter=param_comb, scoring='roc_auc', n_jobs=4, cv=skf.split(X_train,y), verbose=3, random_state=1001, iid=True)
 random_search.fit(X_train, y)
 xgboost_classifier = random_search.best_estimator_<train_model>
gc.collect()
classifier = RandomForestClassifier()
classifier.fit(X_train, y )<train_on_grid>
y = pipeline_predict(trained_estimators, df_test, scaler )
params = {
'n_estimators': [5, 10, 15],
'criterion': ['gini', 'entropy'],
'max_features': ['auto', 'sqrt', 'log2', None],
'max_depth': [None, 3, 4, 5]
}
folds = 4
param_comb = 5
skf = StratifiedKFold(n_splits=folds, shuffle = True, random_state = 1001)
 random_search = RandomizedSearchCV(classifier, param_distributions=params, n_iter=param_comb, scoring='roc_auc', n_jobs=4, cv=skf.split(X_train,y), verbose=3, random_state=1001, iid=True)
 random_search.fit(X_train, y)
 randomforest_classifier = random_search.best_estimator_<train_model>
del df_test
gc.collect()
classifier = SVC(probability=True)
 classifier.fit(X_train, y )<train_on_grid>
df_oof = pd.DataFrame()
df_oof['pytorch_oof'] = oof
df_oof.to_csv('pytorch_oof.csv', index_label='id' )
params = {
'C': [0.5, 1, 1.5],
'kernel': ['rbf', 'linear', 'poly', 'sigmoid'],
'gamma': [0.001, 0.0001],
'class_weight': [None, 'balanced']
}
folds = 4
param_comb = 5
skf = StratifiedKFold(n_splits=folds, shuffle = True, random_state = 1001)
 random_search = RandomizedSearchCV(classifier, param_distributions=params, n_iter=param_comb, scoring='roc_auc', n_jobs=4, cv=skf.split(X_train,y), verbose=3, random_state=1001, iid=True)
 random_search.fit(X_train, y)
 svc_classifier = random_search.best_estimator_<train_model>
df_submission = pd.DataFrame(index=df_test_id.index)
 df_submission['winPlacePerc'] = y
df_submission.to_csv('pytorch_raw.csv', index_label='Id' )
classifier = VotingClassifier(estimators=[('xgb', xgboost_classifier),('rf',randomforest_classifier),('svc',svc_classifier)], voting='soft')
 classifier.fit(X_train, y )<compute_train_metric>
df_test = pd.read_csv('.. /input/test_V2.csv')
 df_test.shape
accuracies = cross_val_score(estimator=classifier, X=X_train, y=y, cv=5)
 print('accuracy mean: {0}'.format(accuracies.mean()))
print('accuracy std: {0}'.format(accuracies.std()))<load_from_csv>
df_submission = df_submission.merge(df_test[['Id', 'matchId', 'groupId', 'maxPlace', 'numGroups']], on='Id', how='left')
 df_submission.head()
df_test = pd.read_csv('.. /input/test.csv')
 df_test.describe()<count_missing_values>
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()
get_missing_data_table(df_test )<feature_engineering>
df_submission.loc[df_submission.maxPlace == 0, 'winPlacePerc'] = 0
df_submission.loc[df_submission.maxPlace == 1, 'winPlacePerc'] = 1
df_test = imput_nan_values(df_test,'Fare','median')
 df_test['Age'] = df_test['Age'].fillna(value=1000)
 name_row = df_test['Name'].copy()
name_row = pd.DataFrame(name_row.str.split(', ',1 ).tolist() , columns = ['Last name', 'Name'])
 name_row = name_row['Name'].copy()
name_row = pd.DataFrame(name_row.str.split('.',1 ).tolist() ,columns=["Title","Name"])
 name_row = name_row['Title'].copy()
titles = name_row.tolist()
for i in range(len(titles)) :
title = titles[i]
if title != 'Master' and title != 'Miss' and title != 'Mr' and title !='Mrs':
titles[i] = 'Other'
name_row = pd.DataFrame(titles, columns=['Title'])
 df_test['Title'] = name_row.copy()
test_df = df_test.copy()
test_df = pd.DataFrame([df_test['Age'].tolist() , df_test['Title'].tolist() ] ).transpose()
test_df.columns = ['Age','Title']
test_df_list = test_df.values
for i in range(len(test_df_list)) :
age = test_df_list[i][0]
title = test_df_list[i][1]
if age == 1000:
if title == 'Master':
test_df_list[i][0] = 5.19
elif title == 'Miss':
test_df_list[i][0] = 21.87
elif title == 'Mr':
test_df_list[i][0] = 32.18
elif title == 'Mrs':
test_df_list[i][0] = 35.48
else:
test_df_list[i][0] = 42.81
df_test['Age'] = test_df['Age'].copy()
get_missing_data_table(df_test )<data_type_conversions>
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
df_test = df_test.drop('Cabin', axis='columns')
 df_test['Family Size'] = df_test['SibSp'] + df_test['Parch']
df_test = df_test.drop('SibSp', axis='columns')
 df_test = df_test.drop('Parch', axis='columns')
 df_test = df_test.drop('Name', axis='columns')
 df_test = df_test.drop('Ticket', axis='columns')
 df_test = df_test.drop('PassengerId', axis='columns')
 df_test['Age'] = df_test['Age'].astype('float64')
 df_test = transform_dummy_variables(df_test,['Sex','Pclass','Embarked','Title'])
 df_test.head(5 )<save_to_csv>
df_submission.loc[(df_submission.maxPlace > 1)&(df_submission.numGroups == 1), 'winPlacePerc'] = 0
assert df_submission['winPlacePerc'].isnull().sum() == 0
X_test = df_test.values
sc = StandardScaler()
X_test = sc.fit_transform(X_test)
 pred = classifier.predict(X_test)
 test_dataset = pd.read_csv('.. /input/test.csv')
 ps_id = test_dataset.iloc[:,0].values
d = {'PassengerId':ps_id, 'Survived':pred}
df = pd.DataFrame(data=d)
 df = df.set_index('PassengerId')
 df.to_csv('predictions.csv')
 df.head(15 )<import_modules>
df_submission[['Id', 'winPlacePerc']].to_csv('pytorch_adjusted.csv', index=False )