Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
<SOS> metric: MAE Kaggle data source: pubg-finish-placement-prediction-(kernels-only)<compute_test_metric>
warnings.filterwarnings(action='ignore')
 plt.style.use('fivethirtyeight')
 gc.enable()
for idx, feat in enumerate(['OpenPorchSF','EnclosedPorch','3SsnPorch','ScreenPorch']):
print(feat," : ",adaptative_correlation(data[feat],data.tmpPrice)) 
<define_variables>
%time
train_d=dd.read_csv('.. /input/train_V2.csv',blocksize=25e6)
floors_cols = ['1stFlrSF','2ndFlrSF','LowQualFinSF','GrLivArea','FullBath','HalfBath','BedroomAbvGr','KitchenAbvGr','KitchenQual','TotRmsAbvGrd']<compute_test_metric>
train=pd.read_csv('.. /input/train_V2.csv',nrows=100000)
 test=pd.read_csv(".. /input/test_V2.csv",)
 test_id=test['Id']
adaptative_correlation(data.LowQualFinSF,data.tmpPrice )<drop_column>
train.isnull().sum()
data = data.drop(['LowQualFinSF'],axis=1 )<categorify>
train.isnull().sum()
mean_encoder(data,['KitchenQual'])
<feature_engineering>
train_group=train.groupby('groupId',as_index=False)['winPlacePerc'].agg('mean' )
data['LivingArea'] = data['TotalBsmtSF'] + data['1stFlrSF']+\
data['2ndFlrSF']+\
data['OpenPorchSF']+\
data['3SsnPorch']
data['LivingArea'] = np.power(data['LivingArea'],0.35 )<feature_engineering>
train[train['groupId']==2]['winPlacePerc']
HalfBathfactor = 0.6
data['TotalBath'] = data.BsmtFullBath + HalfBathfactor * data.BsmtHalfBath +\
data.FullBath + HalfBathfactor * data.HalfBath<feature_engineering>
train_corr=train.drop(columns=['Id',"matchId","groupId"])
data['Rooms_Area'] =(data.TotRmsAbvGrd+data.TotalBath)/ data.GrLivArea<feature_engineering>
train_dbno=pd.DataFrame(train['DBNOs'].value_counts() ,columns=['DBNOs'])
 dbno=train_dbno.iloc[:9,:]
dbno.iloc[8]['DBNOs']=train_dbno.iloc[8:,:].sum() ['DBNOs']
for feature in ['GrLivArea','BsmtFinSF1','1stFlrSF','2ndFlrSF','BsmtUnfSF','TotalBsmtSF','OpenPorchSF','EnclosedPorch','3SsnPorch','ScreenPorch']:
data[feature] = np.power(data[feature],0.35 )<define_variables>
set1=set(i for i in train[(train['kills']>40)&(train['heals']==0)].index.tolist() )
comfort_cols=['Heating','HeatingQC','CentralAir','Electrical','Functional','MiscFeature','MiscVal']<categorify>
set2=set(i for i in train[(train['move']==0)&(train['kills']>20)].index.tolist() )
mean_encoder(data,['Heating','HeatingQC','CentralAir','Electrical','Functional','MiscFeature'])
<feature_engineering>
set3=set(i for i in train[(train['damageDealt']>4000)&(train['heals']<2)].index.tolist() )
data['MiscVal'] = np.log1p(data.MiscVal )<define_variables>
set4=set(i for i in train[(train['rideDistance']>25000)&(train['roadkillsrate']>.4)].index.tolist() )
sale_cols = ['MoSold','YrSold','SaleType','SaleCondition']<categorify>
set5=set(i for i in train[(train['killStreaks']>3)&(train['weaponsAcquired']> 30)].index.tolist() )
mean_encoder(data,['MoSold','YrSold','SaleType','SaleCondition'])
<predict_on_test>
set6=set(i for i in train[(train['killStreaks']>3)&(train['weaponsAcquired']> 30)].index.tolist() )
quick_predict(data.loc['train'].drop(['SalePrice','tmpPrice'],axis=1),data.loc['train'].tmpPrice )<define_variables>
sets=set1 | set2 | set3 | set4 | set5 | set6
train_lenght = len(data.loc['train'] )<prepare_x_and_y>
train.drop(train.index[list(sets)],inplace=True )
y = data.tmpPrice.loc['train']
y_target = data.tmpPrice.loc['test']<prepare_x_and_y>
train.drop(['roadkillsrate'],axis=1,inplace=True)
 train.columns.drop('winPlacePerc')==test.columns
X = data.drop(['SalePrice', 'tmpPrice'],axis=1)
 X = pd.get_dummies(X)
 X = RobustScaler().fit_transform(X)
 X_target = X[train_lenght:,:]
X = X[:train_lenght,:]<split>
def new_features(df):
df['items']=df['boosts']+df['heals']
df['headshotKills_over_kills'] = df['headshotKills'] / df['kills']
df['headshotKills_over_kills'].fillna(0, inplace=True)
 df['killPlace_over_maxPlace'] = df['killPlace'] / df['maxPlace']
df['killPlace_over_maxPlace'].fillna(0, inplace=True)
 df['killPlace_over_maxPlace'].replace(np.inf, 0, inplace=True)
 df['walkDistance_over_heals'] = df['walkDistance'] / df['heals']
df['walkDistance_over_heals'].fillna(0, inplace=True)
 df['walkDistance_over_heals'].replace(np.inf, 0, inplace=True)
 df['walkDistance_over_kills'] = df['walkDistance'] / df['kills']
df['walkDistance_over_kills'].fillna(0, inplace=True)
 df['walkDistance_over_kills'].replace(np.inf, 0, inplace=True)
 df['teamwork'] = df['assists'] + df['revives']
return df
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0 )<import_modules>
train=new_features(train)
 test=new_features(test )
from sklearn.linear_model import ElasticNet, Lasso, BayesianRidge, LassoLarsIC
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from sklearn.kernel_ridge import KernelRidge
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import RobustScaler
from sklearn.base import BaseEstimator, TransformerMixin, RegressorMixin, clone
from sklearn.model_selection import KFold, cross_val_score,cross_val_predict, train_test_split
from sklearn.metrics import mean_squared_error
import xgboost as xgb<compute_train_metric>
def feature_engineering(is_train=True):
if is_train:
print("processing train.csv")
 df = train
else:
print("processing test.csv")
 df = test
target = 'winPlacePerc'
features = list(df.columns)
 features.remove("Id")
 features.remove("matchId")
 features.remove("groupId")
 features.remove('matchType')
 y = None
if is_train:
y = np.array(df.groupby(['matchId','groupId'])[target].agg('mean'), dtype=np.float64)
 features.remove(target)
 print("get group mean feature")
 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'])
 print("get group max feature")
 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'])
 print("get group min feature")
 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'])
 print("get group size feature")
 agg = df.groupby(['matchId','groupId'] ).size().reset_index(name='group_size')
 df_out = df_out.merge(agg, how='left', on=['matchId', 'groupId'])
 print("get match mean feature")
 agg = df.groupby(['matchId'])[features].agg('mean' ).reset_index()
df_out = df_out.merge(agg, suffixes=["", "_match_mean"], how='left', on=['matchId'])
 print("get match size feature")
 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)
 X = np.array(df_out, dtype=np.float64)
 del df, df_out, agg, agg_rank
gc.collect()
return X, y
x_train, y = feature_engineering(True)
n_folds = 5
def rmsle_cv(model):
kf = KFold(n_folds, shuffle=True, random_state=42 ).get_n_splits(X)
 rmse= np.sqrt(-cross_val_score(model, X, y, scoring="neg_mean_squared_error", cv = kf))
return(rmse )<compute_train_metric>
test,z=feature_engineering(False )
model1 = linear_model.Ridge(alpha=30)
 score = rmsle_cv(model1)
 print("model score: {:.4f} standard deviation({:.4f})
 ".format(score.mean() , score.std()))<compute_train_metric>
scaler=MinMaxScaler(feature_range=(-1,1),copy=False ).fit(x_train)
 x_train=scaler.transform(x_train )
model2 = linear_model.Lasso(alpha=.00075)
 score = rmsle_cv(model2)
 print("model score: {:.4f} standard deviation({:.4f})
 ".format(score.mean() , score.std()))<choose_model_class>
x_test=scaler.transform(test )
model3 = RandomForestRegressor(bootstrap=True, ccp_alpha=0.000001, criterion='mse',
max_depth=None, max_features=0.2, max_leaf_nodes=None,
max_samples=None, min_impurity_decrease=0.0,
min_impurity_split=None, min_samples_leaf=1,
min_samples_split=2, min_weight_fraction_leaf=0.0,
n_estimators=1000, n_jobs=-1, oob_score=False,
random_state=None, verbose=0, warm_start=False)
 score = rmsle_cv(model3)
 print("model score: {:.4f} standard deviation({:.4f})
 ".format(score.mean() , score.std()))<choose_model_class>
from xgboost.sklearn import XGBRegressor
from sklearn.model_selection import cross_val_score,KFold
import numpy as np
from sklearn.pipeline import make_pipeline
from sklearn.linear_model import Lasso,LinearRegression
from sklearn import preprocessing
from sklearn.model_selection import RandomizedSearchCV,train_test_split
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.base import BaseEstimator, TransformerMixin, RegressorMixin, clone
model4 = GradientBoostingRegressor(n_estimators=500,
learning_rate=0.027,
max_features=0.7,
criterion='mse',
min_samples_leaf=4,
min_samples_split=4,
loss='huber',
random_state =42)
 score = rmsle_cv(model4)
 print("gbr score: {:.4f} standard deviation({:.4f})
 ".format(score.mean() , score.std()))<choose_model_class>
n_folds=2
def mbs(model):
kf=KFold(n_folds,shuffle=True,random_state=42 ).get_n_splits(x_train)
 score=-cross_val_score(model,x_train,y,scoring='neg_mean_absolute_error',cv=kf)
 return score
model5 = xgb.XGBRegressor(learning_rate=0.04,n_estimators=1000,
max_depth=2, min_child_weight=0,
gamma=0, subsample=0.75,
colsample_bytree=0.6,
objective='reg:linear', nthread=-1,
scale_pos_weight=1.2, seed=27,
reg_alpha=0.0006 )<compute_test_metric>
lasso=Lasso(alpha=.005,random_state=1 )
score = rmsle_cv(model5)
 print("xgboost score: {:.4f}({:.4f})
 ".format(score.mean() , score.std()))<predict_on_test>
params={'learning_rate':[.02,.03,.1],'min_child_weight':[4,6,8],'max_depth':[8,10],"subsample":[.6,.4],"n_estimators":[300,500]}
model1.fit(X, y)
 model2.fit(X, y)
 model3.fit(X, y)
 model4.fit(X, y)
 model5.fit(X, y)
 predictions1 = model1.predict(X_target)
 predictions2 = model2.predict(X_target)
 predictions3 = model3.predict(X_target)
 predictions4 = model4.predict(X_target)
 predictions5 = model5.predict(X_target)
 predictions = 0.2*predictions1+0.3*predictions2+0.1*predictions3+0.2*predictions4+0.2*predictions5<prepare_output>
xgb=XGBRegressor(learning_rate=.03,min_child_weight=4,max_depth=10,subsample=.4,n_estimators=500,n_jobs=-1)
predictions = np.expm1(predictions )<create_dataframe>
GBoost = GradientBoostingRegressor(n_estimators=600, learning_rate=0.05,
max_depth=4, max_features='auto',
min_samples_leaf=15, min_samples_split=5,
loss='huber', random_state =5 )
submission = pd.DataFrame({'Id': data.loc['test'].index, 'SalePrice': predictions} )<save_to_csv>
class Average_models(BaseEstimator,RegressorMixin,TransformerMixin):
def __init__(self,models):
self.models=models
def fit(self,X,y):
self.models_=[clone(x)for x in self.models]
for model in self.models_:
model.fit(X,y)
 return self
def predict(self,X):
predictions= np.column_stack([model.predict(X)for model in self.models_])
 return np.mean(predictions,axis=1 )
submission.to_csv("submission.csv", index=False )<save_to_csv>
training,valid,ytraining,yvalid=train_test_split(x_train,y)
 xgb.fit(training,ytraining)
 lasso.fit(training,ytraining)
 pred1=xgb.predict(valid)
 pred2=lasso.predict(valid)
 test_pred1=xgb.predict(test)
 test_pred2=lasso.predict(test)
 stacked_predictions=np.column_stack(( pred1,pred2))
stacked_test_prediction=np.column_stack(( test_pred1,test_pred2))
meta_model=LinearRegression()
meta_model.fit(stacked_predictions,yvalid)
 final=meta_model.predict(stacked_test_prediction)
<set_options><EOS> 
df=pd.DataFrame({'id':test_id,'winPlacePerc':final},columns=['id','winPlacePerc'])
 csv=df[['id','winPlacePerc']].to_csv('submission_stack3.csv',index=False )